Skip to content

Commit ab995ab

Browse files
committed
sql: merge coerce_arg_to_types and coerce_args_to_type
No one calls the non-bulk version directly, so just merge the two functions together. This is pure code movement.
1 parent ed97b56 commit ab995ab

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

src/sql/src/plan/func.rs

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<R> Operation<R> {
219219
F: Fn(&ExprContext, Vec<ScalarExpr>) -> Result<R, anyhow::Error> + Send + Sync + 'static,
220220
{
221221
Self::new(move |ecx, spec, cexprs, params| {
222-
let exprs = coerce_args_to_type(ecx, spec, cexprs, params)?;
222+
let exprs = coerce_args_to_types(ecx, spec, cexprs, params)?;
223223
f(ecx, exprs)
224224
})
225225
}
@@ -894,59 +894,47 @@ fn find_match<'a, R: std::fmt::Debug>(
894894
)
895895
}
896896

897-
/// Generates `ScalarExpr` necessary to coerce `Expr` into the `ScalarType`
898-
/// corresponding to `ParameterType`; errors if not possible. This can only
899-
/// work within the `func` module because it relies on `ParameterType`.
900-
fn coerce_arg_to_type(
897+
fn coerce_args_to_types(
901898
ecx: &ExprContext,
902899
spec: FuncSpec,
903-
arg: CoercibleScalarExpr,
904-
param: &ParamType,
905-
) -> Result<ScalarExpr, anyhow::Error> {
900+
args: Vec<CoercibleScalarExpr>,
901+
params: ParamList,
902+
) -> Result<Vec<ScalarExpr>, anyhow::Error> {
906903
use ParamType::*;
907904
use ScalarType::*;
908905

909-
match param {
910-
// Concrete parameter type. Coerce then cast to that type.
911-
Plain(ty) => {
912-
let arg = typeconv::plan_coerce(ecx, arg, ty)?;
913-
if matches!(ty, Decimal(..)) && matches!(ecx.scalar_type(&arg), Decimal(..)) {
914-
// Suppress decimal -> decimal casts, to avoid casting to
915-
// the default decimal scale of 0.
916-
Ok(arg)
917-
} else {
918-
typeconv::plan_cast(spec, ecx, CastContext::Implicit, arg, ty)
906+
let mut exprs = Vec::new();
907+
for (i, arg) in args.into_iter().enumerate() {
908+
let expr = match &params[i] {
909+
// Concrete parameter type. Coerce then cast to that type.
910+
Plain(ty) => {
911+
let arg = typeconv::plan_coerce(ecx, arg, ty)?;
912+
if matches!(ty, Decimal(..)) && matches!(ecx.scalar_type(&arg), Decimal(..)) {
913+
// Suppress decimal -> decimal casts, to avoid casting to
914+
// the default decimal scale of 0.
915+
arg
916+
} else {
917+
typeconv::plan_cast(spec, ecx, CastContext::Implicit, arg, ty)?
918+
}
919919
}
920-
}
921920

922-
// Polymorphic pseudotypes. As in PostgreSQL, these bail on
923-
// uncoerced arguments.
924-
ArrayAny | ListAny | ListElementAny | NonVecAny | MapAny => match arg {
925-
CoercibleScalarExpr::Coerced(arg) => Ok(arg),
926-
_ => bail!("could not determine polymorphic type because input has type unknown"),
927-
},
928-
929-
// Special "any" psuedotype. Per PostgreSQL, uncoerced literals
930-
// are acceptable, but uncoerced parameters are not.
931-
Any => match arg {
932-
CoercibleScalarExpr::Parameter(n) => {
933-
bail!("could not determine data type of parameter ${}", n)
934-
}
935-
_ => arg.type_as_any(ecx),
936-
},
937-
}
938-
}
921+
// Polymorphic pseudotypes. As in PostgreSQL, these bail on
922+
// uncoerced arguments.
923+
ArrayAny | ListAny | ListElementAny | NonVecAny | MapAny => match arg {
924+
CoercibleScalarExpr::Coerced(arg) => arg,
925+
_ => bail!("could not determine polymorphic type because input has type unknown"),
926+
},
939927

940-
/// Batch version of `coerce_arg_to_type`.
941-
fn coerce_args_to_type(
942-
ecx: &ExprContext,
943-
spec: FuncSpec,
944-
cexprs: Vec<CoercibleScalarExpr>,
945-
params: ParamList,
946-
) -> Result<Vec<ScalarExpr>, anyhow::Error> {
947-
let mut exprs = Vec::new();
948-
for (i, cexpr) in cexprs.into_iter().enumerate() {
949-
exprs.push(coerce_arg_to_type(ecx, spec, cexpr, &params[i])?);
928+
// Special "any" psuedotype. Per PostgreSQL, uncoerced literals
929+
// are acceptable, but uncoerced parameters are not.
930+
Any => match arg {
931+
CoercibleScalarExpr::Parameter(n) => {
932+
bail!("could not determine data type of parameter ${}", n)
933+
}
934+
_ => arg.type_as_any(ecx)?,
935+
},
936+
};
937+
exprs.push(expr);
950938
}
951939
Ok(exprs)
952940
}
@@ -1180,7 +1168,7 @@ lazy_static! {
11801168
// For consistency with other functions, verify that
11811169
// coercion is possible, though we don't actually care about
11821170
// the coerced results.
1183-
coerce_args_to_type(ecx, spec, exprs, params)?;
1171+
coerce_args_to_types(ecx, spec, exprs, params)?;
11841172

11851173
// TODO(benesch): make this function have return type
11861174
// regtype, when we support that type. Document the function

0 commit comments

Comments
 (0)