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. - Description of a CTE sufficient for query planning.
- A bundle of unrelated things that we need for planning
Expr
s. - The state required when planning a
Query
. - Common information used for DELETE, UPDATE, and INSERT INTO … SELECT plans.
- Describes how to execute a SELECT query.
Enums
- Specifies how long a query will live.
Constants
Functions
- Cast a relation from one type to another using the specified type of cast.
- 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.
- Common part of the planning of windowed and non-windowed aggregation functions.
- plan_and 🔒
- Plans an
ARRAY
expression. - Plans an expression in the AS OF position of a
SELECT
orSUBSCRIBE
, orCREATE MATERIALIZED VIEW
statement. - 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.
- Creates plans for CTEs and introduces them to
qcx.ctes
. - Plans a slice of expressions.
- Plans the ORDER BY clause of a window function.
- Plans an expression in a
GROUP BY
clause. - plan_not 🔒
- plan_op 🔒
- plan_or 🔒
- Plans a slice of
ORDER BY
expressions. - Plans an expression that appears in an
ORDER BY
orDISTINCT ON
clause. - 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 🔒
- Plans a
ROWS 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
. - 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. - Plans a table function.
- Plans an expression in the
UP TO
position of aSUBSCRIBE
statement. - Plans a
VALUES
clause that appears in aSELECT
statement. - Plans a
VALUES
clause that appears at the top level of anINSERT
statement. - Generic function used to plan both array subqueries and list subqueries
- 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
. - 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.