Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PointerRef CTEs #7521

Merged
merged 10 commits into from
Jul 5, 2024
2 changes: 1 addition & 1 deletion edb/edgeql/compiler/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GlobalCompilerOptions:
#: Should type inheritance be expanded using CTEs.
#: When not explaining CTEs can be used to provide access to a type and its
#: descendents.
use_type_inheritance_ctes: bool = True
use_inheritance_ctes: bool = True

#: The name that can be used in a "DML is disallowed in ..."
#: error. When this is not None, any DML should cause an error.
Expand Down
2 changes: 1 addition & 1 deletion edb/edgeql/compiler/typegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def type_to_typeref(
or (
(
env.options.expand_inhviews or
env.options.use_type_inheritance_ctes
env.options.use_inheritance_ctes
)
and isinstance(t, s_objtypes.ObjectType)
)
Expand Down
24 changes: 22 additions & 2 deletions edb/ir/pathid.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ def from_typeref(
which case it is used instead.

Args:
schema:
A schema instance where the type *t* is defined.
typeref:
The descriptor of a type of the variable being defined.
namespace:
Expand All @@ -278,6 +276,28 @@ def from_typeref(
pid._namespace = frozenset(namespace)
return pid

@classmethod
def from_ptrref(
cls,
ptrref: irast.PointerRef,
*,
namespace: AbstractSet[Namespace] = frozenset(),
) -> PathId:
"""Return a ``PathId`` instance for a given :class:`ir.ast.PointerRef`

Args:
ptrref:
The descriptor of a ptr of the variable being defined.
namespace:
Optional namespace in which the variable is defined.

Returns:
A ``PathId`` instance of type described by *ptrref*.
"""
pid = cls.from_typeref(ptrref.out_source, namespace=namespace)
pid = pid.extend(ptrref=ptrref)
return pid

@classmethod
def new_dummy(cls, name: str) -> PathId:
name_hint = s_name.QualName(module='__derived__', name=name)
Expand Down
4 changes: 2 additions & 2 deletions edb/pgsql/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def compile_ir_to_sql_tree(
named_param_prefix: Optional[tuple[str, ...]] = None,
expected_cardinality_one: bool = False,
expand_inhviews: bool = False,
use_type_inheritance_ctes: bool = False,
use_inheritance_ctes: bool = False,
external_rvars: Optional[
Mapping[Tuple[irast.PathId, pgce.PathAspect], pgast.PathRangeVar]
] = None,
Expand Down Expand Up @@ -129,7 +129,7 @@ def compile_ir_to_sql_tree(
ignore_object_shapes=ignore_shapes,
explicit_top_cast=explicit_top_cast,
expand_inhviews=expand_inhviews,
use_type_inheritance_ctes=use_type_inheritance_ctes,
use_inheritance_ctes=use_inheritance_ctes,
singleton_mode=singleton_mode,
scope_tree_nodes=scope_tree_nodes,
external_rvars=external_rvars,
Expand Down
2 changes: 1 addition & 1 deletion edb/pgsql/compiler/clauses.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def fini_toplevel(
stmt.ctes[:0] = [
*ctx.param_ctes.values(),
*ctx.ptr_ctes.values(),
*ctx.ordered_type_ctes,
*ctx.ordered_inheritance_ctes,
]

if ctx.env.named_param_prefix is None:
Expand Down
15 changes: 10 additions & 5 deletions edb/pgsql/compiler/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ class CompilerContextLevel(compiler.ContextLevel):
#: materialized for performance reasons.
ptr_ctes: Dict[uuid.UUID, pgast.CommonTableExpr]

#: CTEs representing pointers and their inherited pointers
ptr_inheritance_ctes: Dict[uuid.UUID, pgast.CommonTableExpr]

#: CTEs representing types, when rewritten based on access policy
type_ctes: Dict[FullRewriteKey, pgast.CommonTableExpr]

Expand All @@ -257,7 +260,7 @@ class CompilerContextLevel(compiler.ContextLevel):

# Type and type inheriance CTEs in creation order. This ensures type CTEs
# referring to other CTEs are in the correct order.
ordered_type_ctes: list[pgast.CommonTableExpr]
ordered_inheritance_ctes: list[pgast.CommonTableExpr]

#: The logical parent of the current query in the
#: query hierarchy
Expand Down Expand Up @@ -359,10 +362,11 @@ def __init__(
self.rel_hierarchy = {}
self.param_ctes = {}
self.ptr_ctes = {}
self.ptr_inheritance_ctes = {}
self.type_ctes = {}
self.pending_type_ctes = set()
self.type_inheritance_ctes = {}
self.ordered_type_ctes = []
self.ordered_inheritance_ctes = []
self.dml_stmts = {}
self.parent_rel = None
self.pending_query = None
Expand Down Expand Up @@ -401,10 +405,11 @@ def __init__(
self.rel_hierarchy = prevlevel.rel_hierarchy
self.param_ctes = prevlevel.param_ctes
self.ptr_ctes = prevlevel.ptr_ctes
self.ptr_inheritance_ctes = prevlevel.ptr_inheritance_ctes
self.type_ctes = prevlevel.type_ctes
self.pending_type_ctes = prevlevel.pending_type_ctes
self.type_inheritance_ctes = prevlevel.type_inheritance_ctes
self.ordered_type_ctes = prevlevel.ordered_type_ctes
self.ordered_inheritance_ctes = prevlevel.ordered_inheritance_ctes
self.dml_stmts = prevlevel.dml_stmts
self.parent_rel = prevlevel.parent_rel
self.pending_query = prevlevel.pending_query
Expand Down Expand Up @@ -548,7 +553,7 @@ def __init__(
ignore_object_shapes: bool,
singleton_mode: bool,
expand_inhviews: bool,
use_type_inheritance_ctes: bool,
use_inheritance_ctes: bool,
explicit_top_cast: Optional[irast.TypeRef],
query_params: List[irast.Param],
type_rewrites: Dict[RewriteKey, irast.Set],
Expand All @@ -568,7 +573,7 @@ def __init__(
self.ignore_object_shapes = ignore_object_shapes
self.singleton_mode = singleton_mode
self.expand_inhviews = expand_inhviews
self.use_type_inheritance_ctes = use_type_inheritance_ctes
self.use_inheritance_ctes = use_inheritance_ctes
self.explicit_top_cast = explicit_top_cast
self.query_params = query_params
self.type_rewrites = type_rewrites
Expand Down
4 changes: 2 additions & 2 deletions edb/pgsql/compiler/dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3141,7 +3141,7 @@ def compile_triggers(
ictx.trigger_mode = True
ictx.type_ctes = {}
ictx.type_inheritance_ctes = {}
ictx.ordered_type_ctes = []
ictx.ordered_inheritance_ctes = []
ictx.toplevel_stmt = stmt

for stage in triggers:
Expand All @@ -3158,7 +3158,7 @@ def compile_triggers(

# Install any newly created type CTEs before the CTEs created from
# this trigger compilation but after anything compiled before.
stmt.ctes[start_ctes:start_ctes] = ictx.ordered_type_ctes
stmt.ctes[start_ctes:start_ctes] = ictx.ordered_inheritance_ctes


def compile_trigger(
Expand Down
Loading