Expand description
SQL Query
s are the declarative, computational part of SQL.
This module turns Query
s into HirRelationExpr
s - a more explicit, algebraic way of
describing computation.
Functions named plan_* are typically responsible for handling a single node of the SQL ast.
E.g. plan_query
is responsible for handling sqlparser::ast::Query
.
plan_* functions which correspond to operations on relations typically return a HirRelationExpr
.
plan_* functions which correspond to operations on scalars typically return a HirScalarExpr
and a ScalarType
. (The latter is because itβs not always possible to infer from a
HirScalarExpr
what the intended type is - notably in the case of decimals where the
scale/precision are encoded only in the type).
Aggregates are particularly twisty.
In SQL, a GROUP BY turns any columns not in the group key into vectors of
values. Then anywhere later in the scope, an aggregate function can be
applied to that group. Inside the arguments of an aggregate function, other
normal functions are applied element-wise over the vectors. Thus, SELECT sum(foo.x + foo.y) FROM foo GROUP BY x
means adding the scalar x
to the
vector y
and summing the results.
In HirRelationExpr
, aggregates can only be applied immediately at the time
of grouping.
To deal with this, whenever we see a SQL GROUP BY we look ahead for
aggregates and precompute them in the HirRelationExpr::Reduce
. When we
reach the same aggregates during normal planning later on, we look them up
in an ExprContext
to find the precomputed versions.
StructsΒ§
- Aggregate
Table πFunc Visitor - This is used to collect aggregates and table functions from within an
Expr
. See the explanation of aggregate handling at the top of the file for more details. - Cast
Relation πError - CteDesc
- Description of a CTE sufficient for query planning.
- Expr
Context - A bundle of unrelated things that we need for planning
Expr
s. - MutRec
Block Option Extracted - Name
Manager - Manages column names, doing lightweight string internment.
- Planned
Query π - Planned
Root Query - Query
Context - The state required when planning a
Query
. - Read
Then Write Plan - Common information used for DELETE, UPDATE, and INSERT INTO β¦ SELECT plans.
- Select
Option Extracted - Select
Plan π - Describes how to execute a SELECT query.
- Window
Func πCollector
EnumsΒ§
- Expanded
Select πItem - Query
Lifetime - Specifies how long a query will live.
ConstantsΒ§
StaticsΒ§
- EXECUTE_
CAST_ πCONTEXT - The CastContext used when matching up the types of parameters passed to EXECUTE.
- EXECUTE_
CONTEXT_ πREL_ TYPE - EXECUTE_
CONTEXT_ πSCOPE
FunctionsΒ§
- cast_
relation π - Cast a relation from one type to another using the specified type of cast.
- check_
col_ πindex - coerce_
homogeneous_ exprs - Coerces a list of expressions such that all input expressions will be cast to the same type. If successful, returns a new list of expressions in the same order as the input, where each expression has the appropriate casts to make them all of a uniform type.
- execute_
expr_ πcontext - Returns an
ExprContext
for the expressions in the parameters of an EXECUTE statement. - expand_
select_ πitem - extract_
scalar_ πsubscript_ from_ positions - handle_
mutation_ πusing_ clause - invent_
column_ πname - offset_
into_ value - Converts an OFFSET expression into a value.
- plan_
aggregate_ πcommon - Common part of the planning of windowed and non-windowed aggregation functions.
- plan_
and π - plan_
array π - Plans an
ARRAY
expression. - plan_
array_ πsubquery - plan_
as_ of - Plans an expression in the AS OF position of a
SELECT
orSUBSCRIBE
, orCREATE MATERIALIZED VIEW
statement. - plan_
case π - plan_
cast π - plan_
collate π - plan_
copy_ from - See the doc comment on
plan_copy_item
for the details of what this function returns. - plan_
copy_ from_ rows - Builds a plan that adds the default values for the missing columns and re-orders the datums in the given rows to match the order in the target table.
- plan_
copy_ item - Determines the mapping between some external data and a Materialize relation.
- plan_
ct_ query - TODO(ct2): Dedup this with plan_root_query.
- plan_
ctes - Creates plans for CTEs and introduces them to
qcx.ctes
. - plan_
default_ expr - plan_
delete_ query - plan_
exists π - plan_
expr - plan_
expr_ πinner - plan_
expr_ πor_ col_ index - plan_
exprs π - Plans a slice of expressions.
- plan_
field_ πaccess - plan_
function π - plan_
function_ πorder_ by - Plans the ORDER BY clause of a window function.
- plan_
group_ πby_ expr - Plans an expression in a
GROUP BY
clause. - plan_
homogenizing_ πfunction - plan_
identifier π - plan_
in_ πlist - plan_
index_ exprs - plan_
index_ πlist - plan_
insert_ query - plan_
is_ πexpr - plan_
join π - plan_
join_ πidentity - plan_
like π - plan_
list π - plan_
list_ πsubquery - plan_
literal π - plan_
map π - plan_
map_ πsubquery - plan_
mutation_ query_ inner - plan_
nested_ query - plan_
not π - plan_op π
- plan_or π
- plan_
order_ πby_ exprs - Plans a slice of
ORDER BY
expressions. - plan_
order_ πby_ or_ distinct_ expr - Plans an expression that appears in an
ORDER BY
orDISTINCT ON
clause. - plan_
parameter π - plan_
params - plan_
query π - plan_
query_ πinner - plan_
root_ query - Plans a top-level query, returning the
HirRelationExpr
describing the query plan, theRelationDesc
describing the shape of the result set, aRowSetFinishing
describing post-processing that must occur before results are sent to the client, and the types of the parameters in the query, if any were present. - plan_
row π - plan_
rows_ πfrom - Plans a
ROWS FROM
expression. - plan_
rows_ πfrom_ internal - Plans an expression coalescing multiple table functions. Each table function is followed by its row ordinality. The entire expression is followed by the coalesced row ordinality.
- plan_
scalar_ πtable_ funcs - plan_
secret_ as - Plans an expression in the AS position of a
CREATE SECRET
. - plan_
select_ πfrom_ where - Plans a SELECT query. The SELECT query may contain an intrusive ORDER BY clause.
- plan_
set_ πexpr - plan_
slice_ πlist - plan_
solitary_ πtable_ function - Plans a table function that appears alone, i.e., that is not part of a
ROWS FROM
clause that contains other table functions. Special aliasing rules apply. - plan_
subquery π - plan_
subscript π - plan_
subscript_ πarray - plan_
subscript_ πjsonb - plan_
subscript_ πlist - plan_
table_ πalias - plan_
table_ πfactor - plan_
table_ πfunction_ internal - Plans a table function.
- plan_
table_ πwith_ joins - plan_
up_ to - Plans an expression in the
UP TO
position of aSUBSCRIBE
statement. - plan_
update_ query - plan_
using_ πconstraint - plan_
values π - Plans a
VALUES
clause that appears in aSELECT
statement. - plan_
values_ πinsert - Plans a
VALUES
clause that appears at the top level of anINSERT
statement. - plan_
vector_ πlike_ subquery - Generic function used to plan both array subqueries and list subqueries
- plan_
webhook_ validate_ using - Plans an expression in the CHECK position of a
CREATE SOURCE ... FROM WEBHOOK
. - plan_
window_ πframe - plan_
window_ πfunction_ common - The common part of the planning of all window functions.
- plan_
window_ πfunction_ non_ aggr - The common part of the planning of non-aggregate window functions, i.e., scalar window functions and value window functions.
- resolve_
desc_ πand_ nulls_ last - Creates a
ColumnOrder
from anOrderByExpr
and column index. Column index is specified by the caller, butdesc
andnulls_last
is figured out here. - resolve_
func - Resolves the name to a set of function implementations.
- scalar_
type_ from_ catalog - scalar_
type_ from_ sql - try_
push_ πprojection_ order_ by - Attempts to push a projection through an order by.
- window_
frame_ πbound_ ast_ to_ expr - window_
frame_ πunit_ ast_ to_ expr