Skip to content

feat: rand expression support #1199

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion native/core/src/execution/jni_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_executePlan(
// query plan, we need to defer stream initialization to first time execution.
if exec_context.root_op.is_none() {
let start = Instant::now();
let planner = PhysicalPlanner::new(Arc::clone(&exec_context.session_ctx))
let planner = PhysicalPlanner::new(Arc::clone(&exec_context.session_ctx), partition)
Copy link
Contributor

Choose a reason for hiding this comment

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

here is interesting. Is there any reason the partition is not used in Comet native physical planner? this is def used in DF physical plan during plan node execution https://github.com/apache/datafusion/blob/main/datafusion/physical-plan/src/execution_plan.rs#L371

Copy link
Author

Choose a reason for hiding this comment

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

The spark partition index is erased when a native DF plan is sent for the execution for some reason : https://github.com/apache/datafusion-comet/blob/main/native/core/src/execution/jni_api.rs#L496

Copy link
Member

Choose a reason for hiding this comment

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

This is something that I would like to see improved. We currently use partition 0 for each native plan rather than the real partition id.

Copy link
Author

Choose a reason for hiding this comment

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

@andygrove Can i do it as a part of this PR or it would be better to create a separate one?

.with_exec_id(exec_context_id);
let (scans, root_op) = planner.create_plan(
&exec_context.spark_plan,
Expand Down
16 changes: 12 additions & 4 deletions native/core/src/execution/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ use datafusion_comet_proto::{
use datafusion_comet_spark_expr::{
ArrayInsert, Avg, AvgDecimal, BitwiseNotExpr, Cast, CheckOverflow, Contains, Correlation,
Covariance, CreateNamedStruct, DateTruncExpr, EndsWith, GetArrayStructFields, GetStructField,
HourExpr, IfExpr, Like, ListExtract, MinuteExpr, NormalizeNaNAndZero, RLike, SecondExpr,
SparkCastOptions, StartsWith, Stddev, StringSpaceExpr, SubstringExpr, SumDecimal,
HourExpr, IfExpr, Like, ListExtract, MinuteExpr, NormalizeNaNAndZero, RLike, RandExpr,
SecondExpr, SparkCastOptions, StartsWith, Stddev, StringSpaceExpr, SubstringExpr, SumDecimal,
TimestampTruncExpr, ToJson, UnboundColumn, Variance,
};
use itertools::Itertools;
Expand Down Expand Up @@ -140,6 +140,7 @@ pub const TEST_EXEC_CONTEXT_ID: i64 = -1;
pub struct PhysicalPlanner {
// The execution context id of this planner.
exec_context_id: i64,
partition: i32,
session_ctx: Arc<SessionContext>,
}

Expand All @@ -148,22 +149,25 @@ impl Default for PhysicalPlanner {
let session_ctx = Arc::new(SessionContext::new());
Self {
exec_context_id: TEST_EXEC_CONTEXT_ID,
partition: 0,
session_ctx,
}
}
}

impl PhysicalPlanner {
pub fn new(session_ctx: Arc<SessionContext>) -> Self {
pub fn new(session_ctx: Arc<SessionContext>, partition: i32) -> Self {
Self {
exec_context_id: TEST_EXEC_CONTEXT_ID,
session_ctx,
partition,
}
}

pub fn with_exec_id(self, exec_context_id: i64) -> Self {
Self {
exec_context_id,
partition: self.partition,
session_ctx: Arc::clone(&self.session_ctx),
}
}
Expand Down Expand Up @@ -762,6 +766,10 @@ impl PhysicalPlanner {
expr.legacy_negative_index,
)))
}
ExprStruct::Rand(expr) => {
let child = self.create_expr(expr.child.as_ref().unwrap(), input_schema)?;
Ok(Arc::new(RandExpr::new(child, self.partition)))
}
expr => Err(GeneralError(format!("Not implemented: {:?}", expr))),
}
}
Expand Down Expand Up @@ -2835,7 +2843,7 @@ mod tests {
datafusion_functions_nested::make_array::MakeArray::new(),
));
let task_ctx = session_ctx.task_ctx();
let planner = PhysicalPlanner::new(Arc::from(session_ctx));
let planner = PhysicalPlanner::new(Arc::from(session_ctx), 0);

// Create a plan for
// ProjectionExec: expr=[make_array(col_0@0) as col_0]
Expand Down
2 changes: 1 addition & 1 deletion native/core/src/parquet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_initRecordBat
try_unwrap_or_throw(&e, |mut env| unsafe {
let session_config = SessionConfig::new().with_batch_size(batch_size as usize);
let planer =
PhysicalPlanner::new(Arc::new(SessionContext::new_with_config(session_config)));
PhysicalPlanner::new(Arc::new(SessionContext::new_with_config(session_config)), 0);
let session_ctx = planer.session_ctx();

let path: String = env
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 @@ -84,6 +84,7 @@ message Expr {
GetArrayStructFields get_array_struct_fields = 57;
ArrayInsert array_insert = 58;
MathExpr integral_divide = 59;
UnaryExpr rand = 66;
}
}

Expand Down
2 changes: 2 additions & 0 deletions native/spark-expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ pub use cast::{spark_cast, Cast, SparkCastOptions};
mod conditional_funcs;
mod conversion_funcs;
mod math_funcs;
mod nondetermenistic_funcs;

pub use array_funcs::*;
pub use bitwise_funcs::*;
pub use conditional_funcs::*;
pub use conversion_funcs::*;
pub use nondetermenistic_funcs::*;

pub use comet_scalar_funcs::create_comet_physical_fun;
pub use datetime_funcs::{
Expand Down
20 changes: 20 additions & 0 deletions native/spark-expr/src/nondetermenistic_funcs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

pub mod rand;

pub use rand::RandExpr;
Loading
Loading