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

Use inheritance CTEs instead of views in SQL resolver #7494

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions edb/pgsql/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,6 @@ def visit_Relation(self, node: pgast.Relation) -> None:
else:
self.write(common.qname(node.schemaname, node.name))

def _visit_values_expr(self, node: pgast.SelectStmt) -> None:
assert node.values
self.new_lines = 1
self.write('(')
self.write('VALUES')
self.new_lines = 1
self.indentation += 1
self.visit_list(node.values)
self.indentation -= 1
self.new_lines = 1
self.write(')')

def visit_NullRelation(self, node: pgast.NullRelation) -> None:
self.write('(SELECT ')
if node.target_list:
Expand All @@ -271,10 +259,6 @@ def visit_NullRelation(self, node: pgast.NullRelation) -> None:
self.write(')')

def visit_SelectStmt(self, node: pgast.SelectStmt) -> None:
if node.values:
self._visit_values_expr(node)
return

# This is a very crude detection of whether this SELECT is
# a top level statement.
parenthesize = bool(self.result)
Expand All @@ -292,6 +276,17 @@ def visit_SelectStmt(self, node: pgast.SelectStmt) -> None:
self.write('WITH ')
self.gen_ctes(node.ctes)

if node.values:
self.write('VALUES')
self.new_lines = 1
self.visit_list(node.values)
if parenthesize:
self.new_lines = 1
if self.reordered and not node.op:
self.indentation -= 1
self.write(')')
return

# If reordered is True, we try to put the FROM clause *before* SELECT,
# like it *ought* to be. We do various hokey things to try to make
# that look good.
Expand Down
199 changes: 199 additions & 0 deletions edb/pgsql/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
from __future__ import annotations
from typing import Optional, Tuple, AbstractSet, Dict, List, Iterator

from edb.schema import links as s_links
from edb.schema import name as sn
from edb.schema import pointers as s_pointers
from edb.schema import sources as s_sources
from edb.schema import schema as s_schema

from . import ast as pgast
from . import types
from . import common


def get_inheritance_view(
schema: s_schema.Schema,
obj: s_sources.Source | s_pointers.Pointer,
exclude_children: AbstractSet[
s_sources.Source | s_pointers.Pointer
] = frozenset(),
exclude_ptrs: AbstractSet[s_pointers.Pointer] = frozenset(),
) -> pgast.SelectStmt:
ptrs: Dict[sn.UnqualName, Tuple[str, Tuple[str, ...]]] = {}

if isinstance(obj, s_sources.Source):
pointers = list(obj.get_pointers(schema).items(schema))
# Sort by UUID timestamp for stable VIEW column order.
pointers.sort(key=lambda p: p[1].id.time)

for ptrname, ptr in pointers:
if ptr in exclude_ptrs:
continue
if ptr.is_pure_computable(schema):
continue
ptr_stor_info = types.get_pointer_storage_info(
ptr,
link_bias=isinstance(obj, s_links.Link),
schema=schema,
)
if (
isinstance(obj, s_links.Link)
or ptr_stor_info.table_type == 'ObjectType'
):
ptrs[ptrname] = (
ptr_stor_info.column_name,
ptr_stor_info.column_type,
)

for name, alias, type in obj.get_addon_columns(schema):
ptrs[sn.UnqualName(name)] = (alias, type)

else:
# MULTI PROPERTY
ptrs[sn.UnqualName('source')] = ('source', ('uuid',))
lp_info = types.get_pointer_storage_info(
obj,
link_bias=True,
schema=schema,
)
ptrs[sn.UnqualName('target')] = ('target', lp_info.column_type)

descendants = [
child
for child in obj.descendants(schema)
if types.has_table(child, schema) and child not in exclude_children
# XXX: Exclude sys/cfg tables from non sys/cfg views. This
# probably isn't *really* what we want to do, but until we
# figure that out, do *something* so that DDL isn't
# excruciatingly slow because of the cost of explicit id
# checks. See #5168.
and (
not types.is_cfg_view(child, schema)
or types.is_cfg_view(obj, schema)
)
]

# Hackily force 'source' to appear in abstract links. We need
# source present in the code we generate to enforce newly
# created exclusive constraints across types.
if (
ptrs
and isinstance(obj, s_links.Link)
and sn.UnqualName('source') not in ptrs
and obj.is_non_concrete(schema)
):
ptrs[sn.UnqualName('source')] = ('source', ('uuid',))

components = []
components.append(_get_select_from(schema, obj, ptrs))
components.extend(
_get_select_from(schema, child, ptrs) for child in descendants
)

return _union_all(filter(None, components))


def _union_all(components: Iterator[pgast.SelectStmt]) -> pgast.SelectStmt:
query = next(components)
for component in components:
query = pgast.SelectStmt(
larg=query,
op='UNION',
all=True,
rarg=component,
)
return query


def _get_select_from(
schema: s_schema.Schema,
obj: s_sources.Source | s_pointers.Pointer,
ptr_names: Dict[sn.UnqualName, Tuple[str, Tuple[str, ...]]],
) -> Optional[pgast.SelectStmt]:
schema_name, table_name = common.get_backend_name(
schema,
obj,
catenate=False,
aspect='table',
)
# the name of the rel var of the object table within the select query
table_rvar_name = table_name

target_list: List[pgast.ResTarget] = []

system_cols = ['tableoid', 'xmin', 'cmin', 'xmax', 'cmax', 'ctid']
for sys_col_name in system_cols:
val: pgast.BaseExpr
if not types.is_cfg_view(obj, schema):
val = pgast.ColumnRef(name=(table_rvar_name, sys_col_name))
else:
val = pgast.NullConstant()
target_list.append(pgast.ResTarget(name=sys_col_name, val=val))

if isinstance(obj, s_sources.Source):
ptrs = dict(obj.get_pointers(schema).items(schema))

for ptr_name, (alias, pg_type) in ptr_names.items():
ptr = ptrs.get(ptr_name)

if ptr_name == sn.UnqualName('__type__'):
# __type__ is special cased: since it is uniquely
# determined by the type, we directly insert it
# into the views instead of storing it (to save space)
val = pgast.TypeCast(
arg=pgast.StringConstant(val=str(obj.id)),
type_name=pgast.TypeName(name=('uuid',)),
)

elif ptr is not None:
ptr_stor_info = types.get_pointer_storage_info(
ptr,
link_bias=isinstance(obj, s_links.Link),
schema=schema,
)
if ptr_stor_info.column_type != pg_type:
return None
val = pgast.ColumnRef(
name=(table_rvar_name, ptr_stor_info.column_name)
)

elif ptr_name == sn.UnqualName('source'):
val = pgast.TypeCast(
arg=pgast.NullConstant(),
type_name=pgast.TypeName(name=('uuid',)),
)

elif ptr_name == sn.UnqualName('__fts_document__') or (
ptr_name.name.startswith('__ext_ai_')
and ptr_name.name.endswith('__')
):
# an addon column
val = pgast.ColumnRef(name=(table_rvar_name, ptr_name.name))

else:
return None

target_list.append(pgast.ResTarget(name=alias, val=val))

else:
for ptr_name, (alias, _) in ptr_names.items():
target_list.append(
pgast.ResTarget(
name=alias,
val=pgast.ColumnRef(name=(table_rvar_name, str(ptr_name))),
)
)

return pgast.SelectStmt(
from_clause=[
pgast.RelRangeVar(
alias=pgast.Alias(aliasname=table_rvar_name),
relation=pgast.Relation(
schemaname=schema_name,
name=table_name,
),
)
],
target_list=target_list,
)
4 changes: 4 additions & 0 deletions edb/pgsql/resolver/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def resolve_CopyStmt(stmt: pgast.CopyStmt, *, ctx: Context) -> pgast.CopyStmt:
# WHERE
where = dispatch.resolve_opt(stmt.where_clause, ctx=ctx)

# COPY will always be top-level, so we must extract CTEs
query.ctes = list(ctx.ctes_buffer)
ctx.ctes_buffer = []

return pgast.CopyStmt(
relation=None,
colnames=None,
Expand Down
10 changes: 9 additions & 1 deletion edb/pgsql/resolver/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
from __future__ import annotations

from copy import deepcopy
from typing import Optional, Sequence, List
from typing import Optional, Sequence, List, Dict
from dataclasses import dataclass, field
import enum
import uuid

from edb.pgsql import ast as pgast

from edb.common import compiler

from edb.schema import schema as s_schema
from edb.schema import objects as s_objects
from edb.schema import pointers as s_pointers


Expand Down Expand Up @@ -161,6 +163,10 @@ class ResolverContextLevel(compiler.ContextLevel):
# in the top-level WITH binding.
ctes_buffer: List[pgast.CommonTableExpr]

# A mapping of from objects to CTEs that provide an "inheritance view",
# which is basically a union of all of their descendant's tables.
inheritance_ctes: Dict[s_objects.InheritingObject, str]

options: Options

def __init__(
Expand All @@ -181,6 +187,7 @@ def __init__(
self.names = compiler.AliasGenerator()
self.subquery_depth = 0
self.ctes_buffer = []
self.inheritance_ctes = dict()

else:
self.schema = prevlevel.schema
Expand All @@ -189,6 +196,7 @@ def __init__(

self.subquery_depth = prevlevel.subquery_depth + 1
self.ctes_buffer = prevlevel.ctes_buffer
self.inheritance_ctes = prevlevel.inheritance_ctes

if mode == ContextSwitchMode.EMPTY:
self.scope = Scope(ctes=prevlevel.scope.ctes)
Expand Down
Loading