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

Merged
merged 21 commits into from
Jun 25, 2025
Merged
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
311 changes: 149 additions & 162 deletions native/Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions native/core/src/execution/jni_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,9 @@ 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))
.with_exec_id(exec_context_id);
let planner =
PhysicalPlanner::new(Arc::clone(&exec_context.session_ctx), partition)
.with_exec_id(exec_context_id);
let (scans, root_op) = planner.create_plan(
&exec_context.spark_plan,
&mut exec_context.input_sources.clone(),
Expand Down
17 changes: 12 additions & 5 deletions native/core/src/execution/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ use datafusion_comet_proto::{
use datafusion_comet_spark_expr::{
ArrayInsert, Avg, AvgDecimal, Cast, CheckOverflow, Contains, Correlation, Covariance,
CreateNamedStruct, EndsWith, GetArrayStructFields, GetStructField, IfExpr, Like, ListExtract,
NormalizeNaNAndZero, RLike, SparkCastOptions, StartsWith, Stddev, StringSpaceExpr,
NormalizeNaNAndZero, RLike, RandExpr, SparkCastOptions, StartsWith, Stddev, StringSpaceExpr,
SubstringExpr, SumDecimal, TimestampTruncExpr, ToJson, UnboundColumn, Variance,
};
use itertools::Itertools;
Expand Down Expand Up @@ -141,26 +141,29 @@ 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>,
}

impl Default for PhysicalPlanner {
fn default() -> Self {
Self::new(Arc::new(SessionContext::new()))
Self::new(Arc::new(SessionContext::new()), 0)
}
}

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 @@ -801,6 +804,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 @@ -2946,7 +2953,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 Expand Up @@ -3062,7 +3069,7 @@ mod tests {
fn test_array_repeat() {
let session_ctx = SessionContext::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);

// Mock scan operator with 3 INT32 columns
let op_scan = Operator {
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 @@ -687,7 +687,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 @@ -83,6 +83,7 @@ message Expr {
ArrayInsert array_insert = 58;
MathExpr integral_divide = 59;
ToPrettyString to_pretty_string = 60;
UnaryExpr rand = 61;
}
}

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, register_all_comet_functions};
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