Skip to content

Commit 3f31e33

Browse files
Merge branch 'main' into docs/4420-docs-review-skill
2 parents 942339f + b82f5ea commit 3f31e33

28 files changed

Lines changed: 1436 additions & 157 deletions

File tree

‎.github/workflows/pr_build_linux.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ jobs:
666666
org.apache.spark.sql.comet.execution.arrow.CachedBatchRowIteratorSuite
667667
org.apache.spark.sql.comet.execution.arrow.CometArrowWriterSuite
668668
org.apache.spark.sql.comet.execution.arrow.CometStringWriterSuite
669+
org.apache.spark.sql.comet.execution.arrow.CometZstdCompressionCodecSuite
669670
org.apache.spark.sql.CometSparkInternalFunctionsSuite
670671
- name: "expressions"
671672
value: |

‎.github/workflows/pr_build_macos.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ jobs:
301301
org.apache.spark.sql.comet.execution.arrow.CachedBatchRowIteratorSuite
302302
org.apache.spark.sql.comet.execution.arrow.CometArrowWriterSuite
303303
org.apache.spark.sql.comet.execution.arrow.CometStringWriterSuite
304+
org.apache.spark.sql.comet.execution.arrow.CometZstdCompressionCodecSuite
304305
org.apache.spark.sql.CometSparkInternalFunctionsSuite
305306
- name: "expressions"
306307
value: |

‎docs/source/user-guide/latest/operators.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ The following operator families fall back to Spark and are not on the current ro
5353
omitted from the tables below and may be reconsidered based on demand:
5454

5555
- **Structured Streaming operators** (`StateStoreSaveExec`, `StateStoreRestoreExec`, `StreamingSymmetricHashJoinExec`, and similar): Comet targets batch execution.
56-
- **Cartesian / cross joins** (`CartesianProductExec`): rare and expensive, with little acceleration benefit.
5756
- **Pickled (non-Arrow) Python UDFs** (`BatchEvalPythonExec`): Comet accelerates Arrow-based Python UDFs only ([#4234](https://github.com/apache/datafusion-comet/pull/4234)).
5857
- **Typed Dataset operators** (`DeserializeToObjectExec`, `SerializeFromObjectExec`, `MapElementsExec`, `MapPartitionsExec`, `MapGroupsExec`, `CoGroupExec`, `AppendColumnsExec`, and similar): produced by `map`, `mapPartitions`, `groupByKey`, `cogroup`, and other typed `Dataset` transformations. They exist to run user JVM functions on JVM objects, which Comet cannot do natively. The operators above them can still run natively: set `spark.comet.convert.typedDataset.enabled=true` and Comet converts the output of a typed operation to Arrow. This is disabled by default because it can be slower than Spark when the operators above do little work, such as an aggregate over a few groups, which Spark compiles together with the typed operation into one loop.
5958

@@ -97,12 +96,13 @@ omitted from the tables below and may be reconsidered based on demand:
9796

9897
## Joins
9998

100-
| Operator | Status | Notes |
101-
| ----------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
102-
| `BroadcastHashJoinExec` | ✅ | Falls back to Spark for a `NOT IN` (null-aware anti join) whose streamed side reports an ordering. |
103-
| `ShuffledHashJoinExec` | ✅ | |
104-
| `SortMergeJoinExec` | ✅ | Supports `BINARY` join keys. Nested-type (struct, array, map) and collated-string join keys fall back to Spark. |
105-
| `BroadcastNestedLoopJoinExec` | ✅ | Falls back to Spark when the preserved side is broadcast (for example LEFT OUTER with BROADCAST on the left) ([#4429](https://github.com/apache/datafusion-comet/pull/4429)). |
99+
| Operator | Status | Notes |
100+
| ----------------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
101+
| `BroadcastHashJoinExec` | ✅ | Falls back to Spark for a `NOT IN` (null-aware anti join) whose streamed side reports an ordering. |
102+
| `ShuffledHashJoinExec` | ✅ | |
103+
| `SortMergeJoinExec` | ✅ | Supports `BINARY` join keys. Nested-type (struct, array, map) and collated-string join keys fall back to Spark. |
104+
| `BroadcastNestedLoopJoinExec` | ✅ | Falls back to Spark when the preserved side is broadcast (for example LEFT OUTER with BROADCAST on the left) ([#4429](https://github.com/apache/datafusion-comet/pull/4429)). |
105+
| `CartesianProductExec` | ⚠️ | Opt-in (`spark.comet.exec.cartesianProduct.enabled`). Runs natively only without a join condition and when each input reports exactly one partition. Row order can differ from Spark's. |
106106

107107
`ExistenceJoin` (produced when `EXISTS` / `IN` is combined with another predicate via `OR`) runs
108108
natively on `BroadcastHashJoinExec` and `ShuffledHashJoinExec`. Existence sort-merge joins, residual

‎native/core/src/execution/jni_api.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ fn op_name(op: &OpStruct) -> &'static str {
498498
OpStruct::CsvScan(_) => "CsvScan",
499499
OpStruct::ShuffleScan(_) => "ShuffleScan",
500500
OpStruct::BroadcastNestedLoopJoin(_) => "BroadcastNestedLoopJoin",
501+
OpStruct::CrossJoin(_) => "CrossJoin",
501502
OpStruct::Sample(_) => "Sample",
502503
OpStruct::MergeRows(_) => "MergeRows",
503504
OpStruct::RangeScan(_) => "RangeScan",

‎native/core/src/execution/planner/join.rs‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
//! Builders for the native join operators (hash join, sort-merge join, and broadcast nested loop
19-
//! join), and the join-key and join-filter planning they share.
18+
//! Builders for the native join operators (hash join, sort-merge join, broadcast nested loop
19+
//! join, and cross join), and the join-key and join-filter planning they share.
2020
2121
use std::{collections::HashMap, sync::Arc};
2222

@@ -37,7 +37,8 @@ use datafusion::{
3737
physical_plan::{
3838
filter::FilterExec,
3939
joins::{
40-
utils::JoinFilter, HashJoinExec, NestedLoopJoinExec, PartitionMode, SortMergeJoinExec,
40+
utils::JoinFilter, CrossJoinExec, HashJoinExec, NestedLoopJoinExec, PartitionMode,
41+
SortMergeJoinExec,
4142
},
4243
projection::ProjectionExec,
4344
sorts::sort::SortExec,
@@ -420,6 +421,48 @@ impl OperatorBuilder for BroadcastNestedLoopJoinBuilder {
420421
}
421422
}
422423

424+
/// Builder for `CrossJoin` (Spark's `CartesianProductExec`).
425+
pub struct CrossJoinBuilder;
426+
427+
impl OperatorBuilder for CrossJoinBuilder {
428+
fn build(
429+
&self,
430+
spark_plan: &Operator,
431+
inputs: &mut Vec<Arc<Global<JObject<'static>>>>,
432+
partition_count: usize,
433+
planner: &PhysicalPlanner,
434+
) -> PlanCreationResult {
435+
let children = &spark_plan.children;
436+
437+
// No keys or condition. The join type only satisfies the helper; CrossJoinExec emits left
438+
// then right columns, as Spark does.
439+
let (join_params, scans, shuffle_scans) = planner.parse_join_parameters(
440+
inputs,
441+
children,
442+
&[],
443+
&[],
444+
JoinType::Inner as i32,
445+
&None,
446+
partition_count,
447+
)?;
448+
449+
let left = Arc::clone(&join_params.left.native_plan);
450+
let right = Arc::clone(&join_params.right.native_plan);
451+
452+
let cross_join = Arc::new(CrossJoinExec::new(left, right));
453+
454+
Ok((
455+
scans,
456+
shuffle_scans,
457+
Arc::new(SparkPlan::new(
458+
spark_plan.plan_id,
459+
cross_join,
460+
vec![join_params.left, join_params.right],
461+
)),
462+
))
463+
}
464+
}
465+
423466
impl PhysicalPlanner {
424467
/// Keep Spark metric identities for the small set of nodes whose children
425468
/// runtime-filter placement can replace. Stop at native/Spark tree boundaries.

‎native/core/src/execution/planner/operator_registry.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub enum OperatorType {
6363
MergeRows,
6464
Explode,
6565
BroadcastNestedLoopJoin,
66+
CrossJoin,
6667
WindowGroupLimit,
6768
}
6869

@@ -151,7 +152,9 @@ impl OperatorRegistry {
151152

152153
/// Register join operators
153154
fn register_join_operators(&mut self) {
154-
use super::join::{BroadcastNestedLoopJoinBuilder, HashJoinBuilder, SortMergeJoinBuilder};
155+
use super::join::{
156+
BroadcastNestedLoopJoinBuilder, CrossJoinBuilder, HashJoinBuilder, SortMergeJoinBuilder,
157+
};
155158

156159
self.builders
157160
.insert(OperatorType::HashJoin, Box::new(HashJoinBuilder));
@@ -161,6 +164,8 @@ impl OperatorRegistry {
161164
OperatorType::BroadcastNestedLoopJoin,
162165
Box::new(BroadcastNestedLoopJoinBuilder),
163166
);
167+
self.builders
168+
.insert(OperatorType::CrossJoin, Box::new(CrossJoinBuilder));
164169
}
165170

166171
/// Register window operators
@@ -236,6 +241,7 @@ fn get_operator_type(spark_operator: &Operator) -> Option<OperatorType> {
236241
OpStruct::CsvScan(_) => Some(OperatorType::CsvScan),
237242
OpStruct::ShuffleScan(_) => Some(OperatorType::ShuffleScan),
238243
OpStruct::BroadcastNestedLoopJoin(_) => Some(OperatorType::BroadcastNestedLoopJoin),
244+
OpStruct::CrossJoin(_) => Some(OperatorType::CrossJoin),
239245
OpStruct::RangeScan(_) => None,
240246
OpStruct::Sample(_) => None, // Not yet in OperatorType enum
241247
OpStruct::MergeRows(_) => Some(OperatorType::MergeRows),

‎native/native-executors/Cargo.lock‎

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎native/proto/src/proto/operator.proto‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ message Operator {
7171
RangeScan range_scan = 121;
7272
MergeRows merge_rows = 122;
7373
ArrowPythonUdf arrow_python_udf = 123;
74+
CrossJoin cross_join = 124;
7475
// Extension point for optional, out-of-tree contrib scans (Delta, Lance, ...). The concrete
7576
// scan message (e.g. `DeltaScan`) is packed into this envelope on the JVM side and dispatched
7677
// by `type_url` on the native side. Using a single permanent field -- rather than a new oneof
@@ -1163,6 +1164,10 @@ message BroadcastNestedLoopJoin {
11631164
optional spark.spark_expression.Expr condition = 3;
11641165
}
11651166

1167+
// Spark's CartesianProductExec without a condition (one with a condition falls back to Spark).
1168+
message CrossJoin {
1169+
}
1170+
11661171
enum JoinType {
11671172
Inner = 0;
11681173
LeftOuter = 1;

‎native/spark-expr/benches/hll_agg.rs‎

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
//! Each runs as a grouped `AggregateMode::Partial` over 8192 rows x 10 batches, mirroring
2121
//! the other aggregate benches. The union input is a column of real sketch bytes minted
2222
//! up front with `SparkHllSketch`, since the kernel deserializes and merges each one.
23+
//! `hll_sketch_agg_final_dense` runs `hll_sketch_agg` as an `AggregateMode::Final` over
24+
//! partial states instead, with groups that turn dense at lgConfigK 18.
2325
2426
use arrow::array::{ArrayRef, BinaryBuilder, Int64Builder, RecordBatch, StringBuilder};
2527
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
@@ -43,19 +45,32 @@ use tokio::runtime::Runtime;
4345
const LG_CONFIG_K: u8 = 12;
4446
/// Number of distinct sketches minted for the union input; rows cycle through them.
4547
const SKETCH_POOL: usize = 256;
48+
/// `lgConfigK` for the Final-mode case, where a dense group holds 2^18 one-byte registers.
49+
const FINAL_LG_CONFIG_K: u8 = 18;
4650

51+
/// Runs the aggregate in `mode` over `partitions`. `input_schema` is the schema of the rows the
52+
/// Partial stage reads, which in Final mode is not the schema of `partitions`.
4753
async fn agg_test(
4854
partitions: &[Vec<RecordBatch>],
55+
mode: AggregateMode,
56+
input_schema: &SchemaRef,
4957
group_col: Arc<dyn PhysicalExpr>,
5058
input_exprs: Vec<Arc<dyn PhysicalExpr>>,
5159
aggregate_udf: Arc<AggregateUDF>,
5260
alias: &str,
5361
) {
54-
let schema = &partitions[0][0].schema();
5562
let scan: Arc<dyn ExecutionPlan> = Arc::new(DataSourceExec::new(Arc::new(
56-
MemorySourceConfig::try_new(partitions, Arc::clone(schema), None).unwrap(),
63+
MemorySourceConfig::try_new(partitions, partitions[0][0].schema(), None).unwrap(),
5764
)));
58-
let aggregate = create_aggregate(scan, group_col, input_exprs, schema, aggregate_udf, alias);
65+
let aggregate = create_aggregate(
66+
scan,
67+
mode,
68+
group_col,
69+
input_exprs,
70+
input_schema,
71+
aggregate_udf,
72+
alias,
73+
);
5974
let mut stream = aggregate
6075
.execute(0, Arc::new(TaskContext::default()))
6176
.unwrap();
@@ -66,14 +81,15 @@ async fn agg_test(
6681

6782
fn create_aggregate(
6883
scan: Arc<dyn ExecutionPlan>,
84+
mode: AggregateMode,
6985
group_col: Arc<dyn PhysicalExpr>,
7086
input_exprs: Vec<Arc<dyn PhysicalExpr>>,
71-
schema: &SchemaRef,
87+
input_schema: &SchemaRef,
7288
aggregate_udf: Arc<AggregateUDF>,
7389
alias: &str,
7490
) -> Arc<AggregateExec> {
7591
let aggr_expr = AggregateExprBuilder::new(aggregate_udf, input_exprs)
76-
.schema(schema.clone())
92+
.schema(input_schema.clone())
7793
.alias(alias)
7894
.with_ignore_nulls(false)
7995
.with_distinct(false)
@@ -82,12 +98,12 @@ fn create_aggregate(
8298

8399
Arc::new(
84100
AggregateExec::try_new(
85-
AggregateMode::Partial,
101+
mode,
86102
PhysicalGroupBy::new_single(vec![(group_col, "c0".to_string())]),
87103
vec![aggr_expr.into()],
88104
vec![None], // no filter expressions
89105
scan,
90-
Arc::clone(schema),
106+
Arc::clone(input_schema),
91107
)
92108
.unwrap(),
93109
)
@@ -134,10 +150,38 @@ fn create_record_batch(num_rows: usize, pool: &[Vec<u8>]) -> RecordBatch {
134150
RecordBatch::try_new(Arc::new(Schema::new(fields)), columns).unwrap()
135151
}
136152

153+
/// Partial `hll_sketch_agg` states for the Final-mode case: 20 groups, each merging 400 partial
154+
/// sketches of 200 distinct values. After 123 of them a group has more than 24,576 distinct
155+
/// values, the most a lgConfigK 18 sketch holds before it moves to the register array. `c0` is
156+
/// the Int64 group key and `c1` the state, one partial per group per round, the way a reducer
157+
/// reads map outputs.
158+
fn partial_states() -> RecordBatch {
159+
let (groups, partials, values) = (20i64, 400i64, 200i64);
160+
let mut keys = Int64Builder::with_capacity((groups * partials) as usize);
161+
let mut states = BinaryBuilder::new();
162+
for round in 0..partials {
163+
for group in 0..groups {
164+
let mut sketch = SparkHllSketch::new(FINAL_LG_CONFIG_K);
165+
let start = (round * groups + group) * values;
166+
for v in start..start + values {
167+
sketch.update_i64(v);
168+
}
169+
keys.append_value(group);
170+
states.append_value(sketch.to_sketch_bytes());
171+
}
172+
}
173+
RecordBatch::try_from_iter([
174+
("c0", Arc::new(keys.finish()) as ArrayRef),
175+
("c1", Arc::new(states.finish()) as ArrayRef),
176+
])
177+
.unwrap()
178+
}
179+
137180
fn criterion_benchmark(c: &mut Criterion) {
138181
let num_rows = 8192;
139182
let pool = sketch_pool();
140183
let batch = create_record_batch(num_rows, &pool);
184+
let schema = batch.schema();
141185
let batches = vec![batch; 10];
142186
let partitions = &[batches];
143187

@@ -155,6 +199,8 @@ fn criterion_benchmark(c: &mut Criterion) {
155199
b.to_async(&rt).iter(|| {
156200
black_box(agg_test(
157201
partitions,
202+
AggregateMode::Partial,
203+
&schema,
158204
c0.clone(),
159205
vec![c1.clone()],
160206
sketch_udf.clone(),
@@ -169,6 +215,8 @@ fn criterion_benchmark(c: &mut Criterion) {
169215
b.to_async(&rt).iter(|| {
170216
black_box(agg_test(
171217
partitions,
218+
AggregateMode::Partial,
219+
&schema,
172220
c0.clone(),
173221
vec![c2.clone()],
174222
union_udf.clone(),
@@ -177,6 +225,30 @@ fn criterion_benchmark(c: &mut Criterion) {
177225
})
178226
});
179227

228+
// The Final stage reads the states, but its aggregate is declared against the Int64 values
229+
// the Partial stage hashed.
230+
let final_partitions = &[vec![partial_states()]];
231+
let final_input_schema: SchemaRef = Arc::new(Schema::new(vec![
232+
Field::new("c0", DataType::Int64, false),
233+
Field::new("c1", DataType::Int64, false),
234+
]));
235+
let final_udf: Arc<AggregateUDF> = Arc::new(AggregateUDF::new_from_impl(HllSketchAgg::new(
236+
FINAL_LG_CONFIG_K as i32,
237+
)));
238+
group.bench_function("hll_sketch_agg_final_dense", |b| {
239+
b.to_async(&rt).iter(|| {
240+
black_box(agg_test(
241+
final_partitions,
242+
AggregateMode::Final,
243+
&final_input_schema,
244+
c0.clone(),
245+
vec![c1.clone()],
246+
final_udf.clone(),
247+
"hll_sketch_agg",
248+
))
249+
})
250+
});
251+
180252
group.finish();
181253
}
182254

0 commit comments

Comments
 (0)