Skip to content

Feat: Add support for array_size #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions native/core/src/execution/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ use datafusion_expr::{
WindowFunctionDefinition,
};
use datafusion_functions_nested::array_has::ArrayHas;
use datafusion_functions_nested::length::ArrayLength;
use datafusion_physical_expr::expressions::{Literal, StatsType};
use datafusion_physical_expr::window::WindowExpr;
use datafusion_physical_expr::LexOrdering;
Expand Down Expand Up @@ -735,6 +736,24 @@ impl PhysicalPlanner {
));
Ok(array_has_expr)
}
ExprStruct::ArraySize(expr) => {
let src_array_expr =
self.create_expr(expr.child.as_ref().unwrap(), Arc::clone(&input_schema))?;
let array_size_expr = Arc::new(ScalarFunctionExpr::new(
"array_size",
Arc::new(ScalarUDF::new_from_impl(ArrayLength::new())),
vec![src_array_expr],
DataType::UInt64,
));
// Note: Return type of array_size if UInt32, casting to Int32 for Spark compatibility
let mut cast_opts = SparkCastOptions::new_without_timezone(EvalMode::Legacy, false);
cast_opts.allow_cast_unsigned_ints = true;
Ok(Arc::new(Cast::new(
array_size_expr,
DataType::Int32,
cast_opts,
)))
}
expr => Err(ExecutionError::GeneralError(format!(
"Not implemented: {:?}",
expr
Expand Down
1 change: 1 addition & 0 deletions native/proto/src/proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ message Expr {
BinaryExpr array_append = 58;
ArrayInsert array_insert = 59;
BinaryExpr array_contains = 60;
UnaryExpr array_size = 61;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,14 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
expr.children(1),
inputs,
(builder, binaryExpr) => builder.setArrayAppend(binaryExpr))
case _ if expr.prettyName == "array_size" || expr.prettyName == "size" =>
if (!expr.children.head.dataType.isInstanceOf[ArrayType]) {
return None
}
createUnaryExpr(
expr.children.head,
inputs,
(builder, unaryExpr) => builder.setArraySize(unaryExpr))
case _ =>
withInfo(expr, s"${expr.prettyName} is not supported", expr.children: _*)
None
Expand Down
11 changes: 11 additions & 0 deletions spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2529,4 +2529,15 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
spark.sql("SELECT array_contains((CASE WHEN _2 =_3 THEN array(_4) END), _4) FROM t1"));
}
}

test("array_size") {
withTempDir { dir =>
val path = new Path(dir.toURI.toString, "test.parquet")
makeParquetFileAllTypes(path, dictionaryEnabled = false, n = 10000)
spark.read.parquet(path.toString).createOrReplaceTempView("t1");
checkSparkAnswerAndOperator(spark.sql("SELECT array_size(array(_2, _3, _4)) FROM t1"))
checkSparkAnswerAndOperator(
spark.sql("SELECT array_size((CASE WHEN _2 =_3 THEN array(_4) END)) FROM t1"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ abstract class CometTestBase
df: => DataFrame,
includeClasses: Seq[Class[_]],
excludedClasses: Class[_]*): Unit = {
println(df.queryExecution.executedPlan)
checkCometOperators(stripAQEPlan(df.queryExecution.executedPlan), excludedClasses: _*)
checkPlanContains(stripAQEPlan(df.queryExecution.executedPlan), includeClasses: _*)
checkSparkAnswer(df)
Expand Down