def _cast_to_ir(ir_set: irast.Set, cast: s_casts.Cast, orig_stype: s_types.Type, new_stype: s_types.Type, cardinality_mod: Optional[qlast.CardinalityModifier] = None, *, ctx: context.ContextLevel) -> irast.Set: orig_typeref = typegen.type_to_typeref(orig_stype, env=ctx.env) new_typeref = typegen.type_to_typeref(new_stype, env=ctx.env) cast_name = cast.get_name(ctx.env.schema) cast_ir = irast.TypeCast( expr=ir_set, from_type=orig_typeref, to_type=new_typeref, cardinality_mod=cardinality_mod, cast_name=cast_name, cast_module_id=ctx.env.schema.get_global(s_mod.Module, cast_name.module).id, sql_function=cast.get_from_function(ctx.env.schema), sql_cast=cast.get_from_cast(ctx.env.schema), sql_expr=bool(cast.get_code(ctx.env.schema)), ) return setgen.ensure_set(cast_ir, ctx=ctx)
def _cast_array_literal( ir_set: irast.Set, orig_stype: s_types.Type, new_stype: s_types.Type, *, srcctx: Optional[parsing.ParserContext], ctx: context.ContextLevel) -> irast.Set: assert isinstance(ir_set.expr, irast.Array) orig_typeref = typegen.type_to_typeref(orig_stype, env=ctx.env) new_typeref = typegen.type_to_typeref(new_stype, env=ctx.env) direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx) if direct_cast is None: if not new_stype.is_array(): raise errors.QueryError( f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} ' f'to {new_stype.get_displayname(ctx.env.schema)!r}', context=srcctx) from None assert isinstance(new_stype, s_types.Array) el_type = new_stype.get_subtypes(ctx.env.schema)[0] intermediate_stype = orig_stype else: el_type = new_stype ctx.env.schema, intermediate_stype = s_types.Array.from_subtypes( ctx.env.schema, [el_type]) intermediate_typeref = typegen.type_to_typeref( intermediate_stype, env=ctx.env) casted_els = [] for el in ir_set.expr.elements: el = compile_cast(el, el_type, cardinality_mod=qlast.CardinalityModifier.Required, ctx=ctx, srcctx=srcctx) casted_els.append(el) new_array = setgen.ensure_set( irast.Array(elements=casted_els, typeref=intermediate_typeref), ctx=ctx) if direct_cast is not None: return _cast_to_ir( new_array, direct_cast, intermediate_stype, new_stype, ctx=ctx) else: cast_ir = irast.TypeCast( expr=new_array, from_type=orig_typeref, to_type=new_typeref, sql_cast=True, sql_expr=False, ) return setgen.ensure_set(cast_ir, ctx=ctx)
def _inheritance_cast_to_ir(ir_set: irast.Set, orig_stype: s_types.Type, new_stype: s_types.Type, *, ctx: context.ContextLevel) -> irast.Set: orig_typeref = irtyputils.type_to_typeref(ctx.env.schema, orig_stype) new_typeref = irtyputils.type_to_typeref(ctx.env.schema, new_stype) cast_ir = irast.TypeCast( expr=ir_set, from_type=orig_typeref, to_type=new_typeref, cast_name=None, sql_function=None, sql_cast=True, sql_expr=False, ) return setgen.ensure_set(cast_ir, ctx=ctx)
def _cast_array_literal(ir_set: irast.Set, orig_stype: s_types.Type, new_stype: s_types.Type, *, srcctx: parsing.ParserContext, ctx: context.ContextLevel) -> irast.Base: orig_typeref = irtyputils.type_to_typeref(ctx.env.schema, orig_stype) new_typeref = irtyputils.type_to_typeref(ctx.env.schema, new_stype) direct_cast = _find_cast(orig_stype, new_stype, srcctx=srcctx, ctx=ctx) if direct_cast is None: if not new_stype.is_array(): raise errors.QueryError( f'cannot cast {orig_stype.get_displayname(ctx.env.schema)!r} ' f'to {new_stype.get_displayname(ctx.env.schema)!r}', context=srcctx) from None el_type = new_stype.get_subtypes(ctx.env.schema)[0] else: el_type = new_stype casted_els = [] for el in ir_set.expr.elements: el = compile_cast(el, el_type, ctx=ctx, srcctx=srcctx) casted_els.append(el) new_array = setgen.ensure_set(irast.Array(elements=casted_els, typeref=orig_typeref), ctx=ctx) if direct_cast is not None: return _cast_to_ir(new_array, direct_cast, orig_stype, new_stype, ctx=ctx) else: cast_ir = irast.TypeCast( expr=new_array, from_type=orig_typeref, to_type=new_typeref, sql_cast=True, ) return setgen.ensure_set(cast_ir, ctx=ctx)
def _inheritance_cast_to_ir(ir_set: irast.Set, orig_stype: s_types.Type, new_stype: s_types.Type, *, cardinality_mod: Optional[ qlast.CardinalityModifier], ctx: context.ContextLevel) -> irast.Set: orig_typeref = typegen.type_to_typeref(orig_stype, env=ctx.env) new_typeref = typegen.type_to_typeref(new_stype, env=ctx.env) cast_ir = irast.TypeCast( expr=ir_set, from_type=orig_typeref, to_type=new_typeref, cardinality_mod=cardinality_mod, cast_name=None, sql_function=None, sql_cast=True, sql_expr=False, ) return setgen.ensure_set(cast_ir, ctx=ctx)
def _cast_to_ir(ir_set: irast.Set, cast: s_casts.Cast, orig_stype: s_types.Type, new_stype: s_types.Type, *, ctx: context.ContextLevel) -> irast.Set: orig_typeref = irtyputils.type_to_typeref(ctx.env.schema, orig_stype) new_typeref = irtyputils.type_to_typeref(ctx.env.schema, new_stype) cast_name = cast.get_name(ctx.env.schema) cast_ir = irast.TypeCast( expr=ir_set, from_type=orig_typeref, to_type=new_typeref, cast_name=cast_name, cast_module_id=ctx.env.schema.get_global(s_mod.Module, cast_name.module).id, sql_function=cast.get_from_function(ctx.env.schema), sql_cast=cast.get_from_cast(ctx.env.schema), sql_expr=bool(cast.get_code(ctx.env.schema)), ) return setgen.ensure_set(cast_ir, ctx=ctx)