MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / coerce_homogeneous_exprs

Function coerce_homogeneous_exprs

src/sql/src/plan/query.rs:5115–5151  ·  view source on GitHub ↗

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. If `force_type` is `Some`, the expressions are forced to the specified type via an explicit cast. Otherwise the best common type is guessed v

(
    ecx: &ExprContext,
    exprs: Vec<CoercibleScalarExpr>,
    force_type: Option<&SqlScalarType>,
)

Source from the content-addressed store, hash-verified

5113/// [union-type-conv]:
5114/// https://www.postgresql.org/docs/12/typeconv-union-case.html
5115pub fn coerce_homogeneous_exprs(
5116 ecx: &ExprContext,
5117 exprs: Vec<CoercibleScalarExpr>,
5118 force_type: Option<&SqlScalarType>,
5119) -> Result<Vec<HirScalarExpr>, PlanError> {
5120 assert!(!exprs.is_empty());
5121
5122 let target_holder;
5123 let target = match force_type {
5124 Some(t) => t,
5125 None => {
5126 let types: Vec<_> = exprs.iter().map(|e| ecx.scalar_type(e)).collect();
5127 target_holder = typeconv::guess_best_common_type(ecx, &types)?;
5128 &target_holder
5129 }
5130 };
5131
5132 // Try to cast all expressions to `target`.
5133 let mut out = Vec::new();
5134 for expr in exprs {
5135 let arg = typeconv::plan_coerce(ecx, expr, target)?;
5136 let ccx = match force_type {
5137 None => CastContext::Implicit,
5138 Some(_) => CastContext::Explicit,
5139 };
5140 match typeconv::plan_cast(ecx, ccx, arg.clone(), target) {
5141 Ok(expr) => out.push(expr),
5142 Err(_) => sql_bail!(
5143 "{} could not convert type {} to {}",
5144 ecx.name,
5145 ecx.humanize_sql_scalar_type(&ecx.scalar_type(&arg), false),
5146 ecx.humanize_sql_scalar_type(target, false),
5147 ),
5148 }
5149 }
5150 Ok(out)
5151}
5152
5153/// Creates a `ColumnOrder` from an `OrderByExpr` and column index.
5154/// Column index is specified by the caller, but `desc` and `nulls_last` is figured out here.

Callers 7

plan_valuesFunction · 0.85
plan_using_constraintFunction · 0.85
plan_arrayFunction · 0.85
plan_listFunction · 0.85
plan_mapFunction · 0.85
plan_caseFunction · 0.85

Calls 9

guess_best_common_typeFunction · 0.85
plan_coerceFunction · 0.85
plan_castFunction · 0.70
collectMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
scalar_typeMethod · 0.45
cloneMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected