Skip to content
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

feat: support array_distinct #1306

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions native/core/src/execution/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use datafusion::{
};
use datafusion_comet_spark_expr::{create_comet_physical_fun, create_negate_expr};
use datafusion_functions_nested::concat::ArrayAppend;
use datafusion_functions_nested::set_ops::array_distinct_udf;
use datafusion_functions_nested::remove::array_remove_all_udf;
use datafusion_physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};

Expand Down Expand Up @@ -765,6 +766,19 @@ impl PhysicalPlanner {

Ok(Arc::new(case_expr))
}
ExprStruct::ArrayDistinct(expr) => {
let src_array_expr =
self.create_expr(expr.child.as_ref().unwrap(), Arc::clone(&input_schema))?;
let return_type = src_array_expr.data_type(&input_schema)?;
let args = vec![Arc::clone(&src_array_expr)];
let array_distinct_expr = Arc::new(ScalarFunctionExpr::new(
"array_distinct",
array_distinct_udf(),
args,
return_type,
));
Ok(array_distinct_expr)
}
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 @@ -86,6 +86,7 @@ message Expr {
ArrayInsert array_insert = 59;
BinaryExpr array_contains = 60;
BinaryExpr array_remove = 61;
UnaryExpr array_distinct = 62;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,11 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
expr.children(1),
inputs,
(builder, binaryExpr) => builder.setArrayContains(binaryExpr))
case expr if expr.prettyName == "array_distinct" =>
createUnaryExpr(
expr.children(0),
inputs,
(builder, unaryExpr) => builder.setArrayDistinct(unaryExpr))
case _ if expr.prettyName == "array_append" =>
createBinaryExpr(
expr.children(0),
Expand Down
12 changes: 12 additions & 0 deletions spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2551,4 +2551,16 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}
}
test("array_distinct") {
Seq(true, false).foreach { dictionaryEnabled =>
withTempDir { dir =>
val path = new Path(dir.toURI.toString, "test.parquet")
makeParquetFileAllTypes(path, dictionaryEnabled = dictionaryEnabled, 10)
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
checkSparkAnswerAndOperator(spark.sql("Select array_distinct(array(_2, _3,_4)) from t1"))
checkSparkAnswerAndOperator(
spark.sql("Select array_distinct(array(_2,_4, null)) from t1"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results for this check do not match on my machine but I am unsure why because in the spark shell (with and without comet) and also DataFusion cli It works as expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the different order is that datafusion sorts the data before it removes duplicates

}
}
}
}