def _resolve_type_expr(texpr: qlast.TypeExpr, *, ctx: LayoutTraceContext): if isinstance(texpr, qlast.TypeName): if texpr.subtypes: return qltracer.Type(name=texpr.maintype.name) else: refname = ctx.get_ref_name(texpr.maintype) obj = ctx.objects.get(refname) if obj is None: obj = ctx.schema.get(refname, default=None) return obj elif isinstance(texpr, qlast.TypeOp): if texpr.op == '|': return qltracer.UnionType([ _resolve_type_expr(texpr.left, ctx=ctx), _resolve_type_expr(texpr.right, ctx=ctx), ]) else: raise NotImplementedError( f'unsupported type operation: {texpr.op}') else: raise NotImplementedError(f'unsupported type expression: {texpr!r}')
def _resolve_type_expr( texpr: qlast.TypeExpr, *, ctx: LayoutTraceContext, ) -> qltracer.TypeLike: if isinstance(texpr, qlast.TypeName): if texpr.subtypes: return qltracer.Type(name=s_name.QualName(module='__coll__', name=texpr.name or ''), ) else: return cast( qltracer.TypeLike, _resolve_type_name( texpr.maintype, tracer_type=qltracer.Type, real_type=s_types.Type, ctx=ctx, )) elif isinstance(texpr, qlast.TypeOp): if texpr.op == '|': return qltracer.UnionType([ _resolve_type_expr(texpr.left, ctx=ctx), _resolve_type_expr(texpr.right, ctx=ctx), ]) else: raise NotImplementedError( f'unsupported type operation: {texpr.op}') else: raise NotImplementedError(f'unsupported type expression: {texpr!r}')
def sdl_to_ddl(schema, documents): ddlgraph = {} mods = [] ctx = LayoutTraceContext( schema, local_modules=frozenset(mod for mod, schema_decl in documents.items()), ) for module_name, declarations in documents.items(): ctx.set_module(module_name) for decl_ast in declarations: if isinstance(decl_ast, qlast.CreateObject): _, fq_name = ctx.get_fq_name(decl_ast) if isinstance(decl_ast, (qlast.CreateObjectType, qlast.CreateAlias)): ctx.objects[fq_name] = qltracer.ObjectType(fq_name) elif isinstance(decl_ast, qlast.CreateScalarType): ctx.objects[fq_name] = qltracer.Type(fq_name) elif isinstance(decl_ast, (qlast.CreateLink, qlast.CreateProperty)): ctx.objects[fq_name] = qltracer.Pointer(fq_name, source=None, target=None) elif isinstance(decl_ast, qlast.CreateFunction): ctx.objects[fq_name] = qltracer.Function(fq_name) elif isinstance(decl_ast, qlast.CreateConstraint): ctx.objects[fq_name] = qltracer.Constraint(fq_name) elif isinstance(decl_ast, qlast.CreateAnnotation): ctx.objects[fq_name] = qltracer.Annotation(fq_name) else: raise AssertionError( f'unexpected SDL declaration: {decl_ast}') for module_name, declarations in documents.items(): ctx.set_module(module_name) for decl_ast in declarations: trace_layout(decl_ast, ctx=ctx) # compute the ancestors graph for fq_name in ctx.parents.keys(): ctx.ancestors[fq_name] = get_ancestors(fq_name, ctx.ancestors, ctx.parents) topological.normalize(ctx.inh_graph, _merge_items) ctx = DepTraceContext(schema, ddlgraph, ctx.objects, ctx.parents, ctx.ancestors, ctx.defdeps, ctx.constraints) for module_name, declarations in documents.items(): ctx.set_module(module_name) # module needs to be created regardless of whether its # contents are empty or not mods.append(qlast.CreateModule(name=qlast.ObjectRef(name=module_name))) for decl_ast in declarations: trace_dependencies(decl_ast, ctx=ctx) return mods + list(topological.sort(ddlgraph, allow_unresolved=False))
def sdl_to_ddl(schema, declarations): ddlgraph = {} mods = [] ctx = LayoutTraceContext( schema, local_modules=frozenset(mod for mod, schema_decl in declarations), ) for module_name, schema_ast in declarations: for decl_ast in schema_ast.declarations: if isinstance(decl_ast, qlast.CreateObject): fq_name = f'{module_name}::{decl_ast.name.name}' if isinstance(decl_ast, (qlast.CreateObjectType, qlast.CreateView)): ctx.objects[fq_name] = qltracer.ObjectType(fq_name) elif isinstance(decl_ast, qlast.CreateScalarType): ctx.objects[fq_name] = qltracer.Type(fq_name) elif isinstance(decl_ast, (qlast.CreateLink, qlast.CreateProperty)): ctx.objects[fq_name] = qltracer.Pointer(fq_name, source=None, target=None) for module_name, decl_ast in declarations: ctx.set_module(module_name) trace_layout(decl_ast, ctx=ctx) topological.normalize(ctx.inh_graph, _merge_items) ctx = DepTraceContext(schema, ddlgraph, ctx.objects) for module_name, decl_ast in declarations: ctx.set_module(module_name) trace_dependencies(decl_ast, ctx=ctx) mods.append(qlast.CreateModule(name=qlast.ObjectRef(name=module_name))) return mods + list(topological.sort(ddlgraph, allow_unresolved=False))
def sdl_to_ddl( schema: s_schema.Schema, documents: Mapping[str, List[qlast.DDL]], ) -> Tuple[qlast.DDLCommand, ...]: ddlgraph: DDLGraph = {} mods: List[qlast.DDLCommand] = [] ctx = LayoutTraceContext( schema, local_modules=frozenset(mod for mod in documents), ) for module_name, declarations in documents.items(): ctx.set_module(module_name) for decl_ast in declarations: if isinstance(decl_ast, qlast.CreateObject): _, fq_name = ctx.get_fq_name(decl_ast) if isinstance(decl_ast, (qlast.CreateObjectType, qlast.CreateAlias)): ctx.objects[fq_name] = qltracer.ObjectType(fq_name) elif isinstance(decl_ast, qlast.CreateScalarType): ctx.objects[fq_name] = qltracer.Type(fq_name) elif isinstance(decl_ast, (qlast.CreateLink, qlast.CreateProperty)): ctx.objects[fq_name] = qltracer.Pointer( fq_name, source=None, target=None) elif isinstance(decl_ast, qlast.CreateFunction): ctx.objects[fq_name] = qltracer.Function(fq_name) elif isinstance(decl_ast, qlast.CreateConstraint): ctx.objects[fq_name] = qltracer.Constraint(fq_name) elif isinstance(decl_ast, qlast.CreateAnnotation): ctx.objects[fq_name] = qltracer.Annotation(fq_name) else: raise AssertionError( f'unexpected SDL declaration: {decl_ast}') for module_name, declarations in documents.items(): ctx.set_module(module_name) for decl_ast in declarations: trace_layout(decl_ast, ctx=ctx) # compute the ancestors graph for obj_name in ctx.parents.keys(): ctx.ancestors[obj_name] = get_ancestors( obj_name, ctx.ancestors, ctx.parents) topological.normalize( ctx.inh_graph, merger=_graph_merge_cb, # type: ignore schema=schema, ) tracectx = DepTraceContext( schema, ddlgraph, ctx.objects, ctx.parents, ctx.ancestors, ctx.defdeps, ctx.constraints ) for module_name, declarations in documents.items(): tracectx.set_module(module_name) # module needs to be created regardless of whether its # contents are empty or not mods.append(qlast.CreateModule(name=qlast.ObjectRef(name=module_name))) for decl_ast in declarations: trace_dependencies(decl_ast, ctx=tracectx) ordered = topological.sort(ddlgraph, allow_unresolved=False) return tuple(mods) + tuple(ordered)