Enum sql::plan::expr::HirScalarExpr [−][src]
pub enum HirScalarExpr {
Column(ColumnRef),
Parameter(usize),
Literal(Row, ColumnType),
CallNullary(NullaryFunc),
CallUnary {
func: UnaryFunc,
expr: Box<HirScalarExpr>,
},
CallBinary {
func: BinaryFunc,
expr1: Box<HirScalarExpr>,
expr2: Box<HirScalarExpr>,
},
CallVariadic {
func: VariadicFunc,
exprs: Vec<HirScalarExpr>,
},
If {
cond: Box<HirScalarExpr>,
then: Box<HirScalarExpr>,
els: Box<HirScalarExpr>,
},
Exists(Box<HirRelationExpr>),
Select(Box<HirRelationExpr>),
Windowing(WindowExpr),
}
Expand description
Just like expr::MirScalarExpr, except where otherwise noted below.
Variants
Column(ColumnRef)
Tuple Fields
0: ColumnRef
Unlike expr::MirScalarExpr, we can nest HirRelationExprs via eg Exists. This means that a variable could refer to a column of the current input, or to a column of an outer relation. We use ColumnRef to denote the difference.
Parameter(usize)
Tuple Fields
0: usize
Literal(Row, ColumnType)
CallNullary(NullaryFunc)
Tuple Fields
0: NullaryFunc
CallUnary
CallBinary
CallVariadic
If
Exists(Box<HirRelationExpr>)
Tuple Fields
0: Box<HirRelationExpr>
Returns true if expr
returns any rows
Select(Box<HirRelationExpr>)
Tuple Fields
0: Box<HirRelationExpr>
Given expr
with arity 1. If expr returns:
- 0 rows, return NULL
- 1 row, return the value of that row
-
1 rows, the sql spec says we should throw an error but we can’t (see https://github.com/MaterializeInc/materialize/issues/489) so instead we return all the rows. If there are multiple
Select
expressions in a single SQL query, the result is that we take the product of all of them. This is counter to the spec, but is consistent with eg postgres’ treatment of multiple set-returning-functions (see https://tapoueh.org/blog/2017/10/set-returning-functions-and-postgresql-10/).
Windowing(WindowExpr)
Tuple Fields
0: WindowExpr
Implementations
Replaces any parameter references in the expression with the
corresponding datum in params
.
Like HirScalarExpr::bind_parameters
, except that parameters are
replaced with the corresponding expression fragment from params
rather
than a datum.
Specifically, the parameter $1
will be replaced with params[0]
, the
parameter $2
will be replaced with params[1]
, and so on. Parameters
in self
that refer to invalid indices of params
will cause a panic.
Column references in parameters will be corrected to account for the depth at which they are spliced.
Constructs a column reference in the current scope.
pub fn literal_1d_array(
datums: Vec<Datum<'_>>,
element_scalar_type: ScalarType
) -> Result<HirScalarExpr, Error>
A generalization of visit
. The function pre
runs on a
HirScalarExpr
before it runs on any of the child HirScalarExpr
s.
The function post
runs on child HirScalarExpr
s first before the
parent. Optionally, pre
can return which child HirScalarExpr
s, if
any, should be visited (default is to visit all children).
Visits the column references in this scalar expression.
The depth
argument should indicate the subquery nesting depth of the expression,
which will be incremented with each subquery entered and presented to the supplied
function f
.
Like visit_columns
, but permits mutating the column references.
Like visit
but it enters the subqueries visiting the scalar expressions contained
in them. It takes the current depth of the expression and increases it when
entering a subquery.
Like visit_recursively
, but permits mutating the scalar expressions.
Attempts to simplify this expression to a literal 64-bit integer.
Returns None
if this expression cannot be simplified, e.g. because it
contains non-literal values.
Panics
Panics if this expression does not have type ScalarType::Int64
.
Attempts to simplify this expression to a literal string.
Returns None
if this expression cannot be simplified, e.g. because it
contains non-literal values.
Panics
Panics if this expression does not have type ScalarType::String
.
fn applied_to(
self,
id_gen: &mut IdGen,
col_map: &ColumnMap,
cte_map: &mut HashMap<LocalId, CteDesc>,
inner: &mut MirRelationExpr,
subquery_map: &Option<&HashMap<HirScalarExpr, usize>>
) -> MirScalarExpr
fn applied_to(
self,
id_gen: &mut IdGen,
col_map: &ColumnMap,
cte_map: &mut HashMap<LocalId, CteDesc>,
inner: &mut MirRelationExpr,
subquery_map: &Option<&HashMap<HirScalarExpr, usize>>
) -> MirScalarExpr
Rewrite self
into a expr::ScalarExpr
which can be applied to the modified inner
.
This method is responsible for decorrelating subqueries in self
by introducing further columns
to inner
, and rewriting self
to refer to its physical columns (specified by usize
positions).
The most complicated logic is for the scalar expressions that involve subqueries, each of which are
documented in more detail closer to their logic.
This process presumes that inner
is the result of decorrelation, meaning its first several columns
may be inherited from outer relations. The col_map
column map should provide specific offsets where
each of these references can be found.
fn lower_subqueries(
exprs: &[Self],
id_gen: &mut IdGen,
col_map: &ColumnMap,
cte_map: &mut HashMap<LocalId, CteDesc>,
inner: MirRelationExpr
) -> (MirRelationExpr, HashMap<HirScalarExpr, usize>)
fn lower_subqueries(
exprs: &[Self],
id_gen: &mut IdGen,
col_map: &ColumnMap,
cte_map: &mut HashMap<LocalId, CteDesc>,
inner: MirRelationExpr
) -> (MirRelationExpr, HashMap<HirScalarExpr, usize>)
Applies the subqueries in the given list of scalar expressions to every distinct value of the given relation and returns a join of the given relation with all the subqueries found, and the mapping of scalar expressions with columns projected by the returned join that will hold their results.
Rewrites self
into a expr::ScalarExpr
.
Trait Implementations
type Type = ColumnType
fn typ(
&self,
outers: &[RelationType],
inner: &RelationType,
params: &BTreeMap<usize, ScalarType>
) -> Self::Type
fn typ(
&self,
outers: &[RelationType],
inner: &RelationType,
params: &BTreeMap<usize, ScalarType>
) -> Self::Type
Computes the type of the expression.
Performs the conversion.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
Auto Trait Implementations
impl RefUnwindSafe for HirScalarExpr
impl Send for HirScalarExpr
impl Sync for HirScalarExpr
impl Unpin for HirScalarExpr
impl UnwindSafe for HirScalarExpr
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more