Module query

Source
Expand description

SQL Querys are the declarative, computational part of SQL. This module turns Querys into HirRelationExprs - 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Β§

AggregateTableFuncVisitor πŸ”’
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.
CastRelationError πŸ”’
CteDesc
Description of a CTE sufficient for query planning.
ExprContext
A bundle of unrelated things that we need for planning Exprs.
MutRecBlockOptionExtracted
NameManager
Manages column names, doing lightweight string internment.
PlannedQuery πŸ”’
PlannedRootQuery
QueryContext
The state required when planning a Query.
ReadThenWritePlan
Common information used for DELETE, UPDATE, and INSERT INTO … SELECT plans.
SelectOptionExtracted
SelectPlan πŸ”’
Describes how to execute a SELECT query.
WindowFuncCollector πŸ”’

EnumsΒ§

ExpandedSelectItem πŸ”’
QueryLifetime
Specifies how long a query will live.

ConstantsΒ§

IGNORE_NULLS_ERROR_MSG

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 or SUBSCRIBE, or CREATE 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 or DISTINCT 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, the RelationDesc describing the shape of the result set, a RowSetFinishing 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 a SUBSCRIBE statement.
plan_update_query
plan_using_constraint πŸ”’
plan_values πŸ”’
Plans a VALUES clause that appears in a SELECT statement.
plan_values_insert πŸ”’
Plans a VALUES clause that appears at the top level of an INSERT 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 an OrderByExpr and column index. Column index is specified by the caller, but desc and nulls_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 πŸ”’