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>,
)
| 5113 | /// [union-type-conv]: |
| 5114 | /// https://www.postgresql.org/docs/12/typeconv-union-case.html |
| 5115 | pub 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. |
no test coverage detected