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. Eg 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§
- 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 - Description of a CTE sufficient for query planning.
- A bundle of unrelated things that we need for planning
Expr
s. - Planned
Query π - The state required when planning a
Query
. - Common information used for DELETE, UPDATE, and INSERT INTO β¦ SELECT plans.
- Select
Plan πDescribes how to execute a SELECT query. - Window
Func πCollector
Enums§
- Expanded
Select πItem - Specifies how long a query will live.
Constants§
Functions§
- cast_
relation πCast a relation from one type to another using the specified type of cast. - check_
col_ πindex - 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.
- expand_
select_ πitem - invent_
column_ πname - Common part of the planning of windowed and non-windowed aggregation functions.
- plan_
and π - plan_
array πPlans anARRAY
expression. - plan_
array_ πsubquery - Plans an expression in the AS OF position of a
SELECT
orSUBSCRIBE
, orCREATE MATERIALIZED VIEW
statement. - plan_
case π - plan_
cast π - plan_
collate π - 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.
- TODO(ct2): Dedup this with plan_root_query.
- Creates plans for CTEs and introduces them to
qcx.ctes
. - plan_
exists π - plan_
expr_ πinner - plan_
exprs πPlans a slice of expressions. - plan_
field_ πaccess - plan_
function π - Plans the ORDER BY clause of a window function.
- plan_
group_ πby_ expr Plans an expression in aGROUP BY
clause. - plan_
identifier π - plan_
in_ πlist - plan_
index_ πlist - plan_
is_ πexpr - plan_
join π - plan_
join_ πidentity - plan_
like π - plan_
list π - plan_
list_ πsubquery - plan_
literal π - plan_
map π - plan_
map_ πsubquery - plan_
not π - plan_op π
- plan_or π
- plan_
order_ πby_ exprs Plans a slice ofORDER BY
expressions. - Plans an expression that appears in an
ORDER BY
orDISTINCT ON
clause. - plan_
parameter π - plan_
query π - plan_
query_ πinner - 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 aROWS FROM
expression. - 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.
- Plans an expression in the AS position of a
CREATE SECRET
. - plan_
set_ πexpr - plan_
slice_ πlist - 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 - Plans a table function.
- Plans an expression in the
UP TO
position of aSUBSCRIBE
statement. - plan_
values πPlans aVALUES
clause that appears in aSELECT
statement. - plan_
values_ πinsert Plans aVALUES
clause that appears at the top level of anINSERT
statement. - Generic function used to plan both array subqueries and list subqueries
- plan_
view_ πselect Plans a SELECT query. The SELECT query may contain an intrusive ORDER BY clause. - Plans an expression in the CHECK position of a
CREATE SOURCE ... FROM WEBHOOK
. - plan_
window_ πframe - The common part of the planning of all window functions.
- The common part of the planning of non-aggregate window functions, i.e., scalar window functions and value window functions.
- Creates a
ColumnOrder
from anOrderByExpr
and column index. Column index is specified by the caller, butdesc
andnulls_last
is figured out here. - Resolves the name to a set of function implementations.
- Attempts to push a projection through an order by.