Skip to content

Commit

Permalink
length should return f64
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmmease committed Feb 24, 2024
1 parent 46b5066 commit a8536dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn length_transform(
.get_type(schema)
.with_context(|| format!("Failed to infer type of expression: {arg:?}"))?;

match dtype {
let len_expr = match dtype {
DataType::Utf8 | DataType::LargeUtf8 => Ok(Expr::Cast(expr::Cast {
expr: Box::new(Expr::ScalarFunction(expr::ScalarFunction {
func_def: ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::CharacterLength),
Expand All @@ -32,7 +32,9 @@ pub fn length_transform(
"length function support array and string arguments. Received argument with type {:?}",
dtype
))),
}
}?;

Ok(len_expr.cast_to(&DataType::Float64, schema)?)
} else {
Err(VegaFusionError::parse(format!(
"length requires a single argument. Received {} arguments",
Expand Down
23 changes: 3 additions & 20 deletions vegafusion-runtime/src/expression/compiler/member.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::expression::compiler::builtin_functions::array::length::length_transform;
use crate::expression::compiler::compile;
use crate::expression::compiler::config::CompilationConfig;
use crate::expression::compiler::utils::ExprHelpers;
Expand Down Expand Up @@ -84,26 +85,8 @@ pub fn compile_member(
}
}
_ => {
if property_string == "length" && matches!(dtype, DataType::Utf8 | DataType::LargeUtf8)
{
// Special case to treat foo.length as length(foo) on a string
Expr::ScalarFunction(expr::ScalarFunction {
func_def: ScalarFunctionDefinition::BuiltIn(
BuiltinScalarFunction::CharacterLength,
),
args: vec![compiled_object],
})
} else if property_string == "length"
&& matches!(
dtype,
DataType::List(_) | DataType::LargeList(_) | DataType::FixedSizeList(_, _)
)
{
// Special case to treat foo.length as length(foo) on an array
Expr::ScalarFunction(expr::ScalarFunction {
func_def: ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayLength),
args: vec![compiled_object],
})
if property_string == "length" {
length_transform(&[compiled_object], schema)?
} else if matches!(dtype, DataType::Utf8 | DataType::LargeUtf8) {
if let Some(index) = index {
// SQL substr function is 1-indexed so add one
Expand Down

0 comments on commit a8536dd

Please sign in to comment.