Skip to content
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
3 changes: 2 additions & 1 deletion docs/source/user-guide/latest/tuning/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ limit, or to a non-positive value to convert every eligible join regardless of s
A left-semi join returns each left row if any right row matches. For an unfiltered batch join
on a single integer equality key (`=`), set `spark.comet.exec.sortMergeJoin.rightKeyReduction.enabled=true`
to keep one copy of each repeated key per batch before the native sort. The option is off by
default. The right sort must read the key column directly from a Comet shuffle. With the
default. The right sort must read from a Comet shuffle. Its key can be a single shuffled integer column
or a `coalesce` of same-type integer columns and literals, such as `coalesce(r.primary_id, r.backup_id)`. With the
`fair_unified` memory pool, the native planner checks at first execution for memory held by another
Spark consumer in the task and skips reduction if it finds any, logging the reason at
debug level. This best-effort check does not cover consumers that allocate memory later.
Expand Down
39 changes: 26 additions & 13 deletions native/core/src/execution/planner/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use datafusion::{
config::ConfigOptions,
physical_expr::{
expressions::{CastExpr, Column},
LexOrdering, PhysicalExpr,
LexOrdering, PhysicalExpr, PhysicalSortExpr,
},
physical_plan::{
filter::FilterExec,
Expand Down Expand Up @@ -218,16 +218,19 @@ fn reduce_before_sort(
let Some(sort) = params.right.native_plan.downcast_ref::<SortExec>() else {
return Ok(None);
};
// Removing duplicates before a fetch can bring new keys into the first N rows.
if sort.fetch().is_some() {
return Ok(None);
}
let input = sort.input();
let ([(_, key)], [options]) = (params.join_on.as_slice(), sort_options) else {
let ([(_, key)], [options]) = (params.join_on.as_mut_slice(), sort_options) else {
return Ok(None);
};
let schema = input.schema();
let order = sort.expr().first();
if !(input.is::<ScanExec>() || input.is::<ShuffleScanExec>())
|| schema.fields().len() != 1
|| !schema.field(0).data_type().is_signed_integer()
|| !key.is::<Column>()
|| !key.data_type(&schema).is_ok_and(|t| t.is_signed_integer())
|| (key.is::<Column>() && schema.fields().len() != 1)
|| sort.expr().len() != 1
|| order.options != *options
|| !order.expr.eq(key)
Expand Down Expand Up @@ -260,12 +263,23 @@ fn reduce_before_sort(
return Ok(None);
}
}
let reducer: Arc<dyn ExecutionPlan> =
Arc::new(MembershipKeyReductionExec::new(Arc::clone(input)));
let sort = Arc::clone(&params.right.native_plan).replace_children(
vec![Arc::clone(&reducer)],
ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
)?;
let (input, ordering) = if key.is::<Column>() {
(Arc::clone(input), sort.expr().clone())
} else {
// Spark admitted an integer coalesce. Evaluate it once and use that column for
// reduction, sorting, and the join. A left-semi join needs no other right columns.
let name = "membership_key";
let projected =
ProjectionExec::try_new([(Arc::clone(key), name.to_owned())], Arc::clone(input))?;
*key = Arc::new(Column::new(name, 0));
let ordering = LexOrdering::from([PhysicalSortExpr::new(Arc::clone(key), *options)]);
(Arc::new(projected) as Arc<dyn ExecutionPlan>, ordering)
};
let reducer: Arc<dyn ExecutionPlan> = Arc::new(MembershipKeyReductionExec::new(input));
let sort = Arc::new(
SortExec::new(ordering, Arc::clone(&reducer))
Comment thread
sunchao marked this conversation as resolved.
Resolved
.with_preserve_partitioning(sort.preserve_partitioning()),
);
Arc::make_mut(&mut params.right).native_plan = sort;
// The Spark sort collects from its replacement. The join owns the reducer's named metrics.
Ok(Some(reducer))
Expand Down Expand Up @@ -307,8 +321,7 @@ impl OperatorBuilder for SortMergeJoinBuilder {
.collect();

let reducer = if join.right_key_reduction_enabled
&& matches!(&children[1].op_struct, Some(OpStruct::Sort(sort))
if sort.fetch.is_none() && sort.skip.is_none())
&& matches!(&children[1].op_struct, Some(OpStruct::Sort(sort)) if sort.skip.is_none())
{
reduce_before_sort(planner, &mut join_params, &sort_options)?
} else {
Expand Down
74 changes: 71 additions & 3 deletions native/core/src/execution/planner/tests/semi_join_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;

use super::{
create_bound_reference, create_proto_datatype, create_scan, create_sort_order, PhysicalPlanner,
};
use datafusion::{
physical_expr::expressions::Column,
physical_plan::{
joins::SortMergeJoinExec, projection::ProjectionExec, sorts::sort::SortExec, ExecutionPlan,
},
};
use datafusion_comet_operators::MembershipKeyReductionExec;
use datafusion_comet_proto::spark_expression::{self as expr, expr::ExprStruct, Expr};
use datafusion_comet_proto::spark_operator::{self as op, operator::OpStruct, JoinType, Operator};

fn reduced(input: Operator, sort: op::Sort) -> bool {
fn plan_join(input: Operator, sort: op::Sort, key: Expr) -> Arc<dyn ExecutionPlan> {
let sorted = Operator {
children: vec![input],
op_struct: Some(OpStruct::Sort(sort)),
..Default::default()
};
let key = create_bound_reference(0);
let join = Operator {
children: vec![sorted.clone(), sorted],
op_struct: Some(OpStruct::SortMergeJoin(op::SortMergeJoin {
Expand All @@ -48,11 +56,15 @@ fn reduced(input: Operator, sort: op::Sort) -> bool {
let (_, _, plan) = PhysicalPlanner::default()
.create_plan(&join, &mut vec![], 1)
.unwrap();
plan.children[1].native_plan.children()[0].is::<MembershipKeyReductionExec>()
Arc::clone(&plan.native_plan)
}

#[test]
fn reduces_only_a_matching_unlimited_sort_directly_over_the_shuffle() {
let reduced = |input, sort| {
plan_join(input, sort, create_bound_reference(0)).children()[1].children()[0]
.is::<MembershipKeyReductionExec>()
};
let field = create_proto_datatype();
let scan = create_scan();
let sort = op::Sort {
Expand Down Expand Up @@ -94,3 +106,59 @@ fn reduces_only_a_matching_unlimited_sort_directly_over_the_shuffle() {
};
assert!(!reduced(project, sort));
}

#[test]
fn projects_an_integer_coalesce_before_reducing_its_key() {
let wrap = |kind| Expr {
expr_struct: Some(kind),
..Default::default()
};
let plan_key = |key: Expr| {
let sort = op::Sort {
sort_orders: vec![wrap(ExprStruct::SortOrder(Box::new(expr::SortOrder {
child: Some(Box::new(key.clone())),
..Default::default()
})))],
..Default::default()
};
plan_join(create_scan(), sort, key)
};
let column = create_bound_reference(0);
let has_value = wrap(ExprStruct::IsNotNull(Box::new(expr::UnaryExpr {
child: Some(Box::new(column.clone())),
})));
// Spark lowers coalesce(k, 0) to CASE WHEN k IS NOT NULL THEN k ELSE 0 END.
let key = wrap(ExprStruct::CaseWhen(Box::new(expr::CaseWhen {
when: vec![has_value.clone()],
then: vec![column],
else_expr: Some(Box::new(wrap(ExprStruct::Literal(expr::Literal {
datatype: Some(create_proto_datatype()),
value: Some(expr::literal::Value::IntVal(0)),
..Default::default()
})))),
..Default::default()
})));
let plan = plan_key(key);
let join = plan.downcast_ref::<SortMergeJoinExec>().unwrap();
let sort = join.right().downcast_ref::<SortExec>().unwrap();
let reducer = sort
.input()
.downcast_ref::<MembershipKeyReductionExec>()
.unwrap();
let projection = reducer.children()[0]
.downcast_ref::<ProjectionExec>()
.unwrap();
let key = &join.on()[0].1;
assert_eq!(
key.downcast_ref::<Column>().unwrap(),
&Column::new("membership_key", 0)
);
assert!(sort.expr().first().expr.eq(key));
assert_eq!(projection.expr()[0].alias, "membership_key");

// A computed Boolean key still uses the original sort input.
let boolean_plan = plan_key(has_value);
let join = boolean_plan.downcast_ref::<SortMergeJoinExec>().unwrap();
let sort = join.right().downcast_ref::<SortExec>().unwrap();
assert!(sort.input().is::<super::ScanExec>());
}
5 changes: 2 additions & 3 deletions spark/src/main/scala/org/apache/comet/CometConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,8 @@ object CometConf extends ShimCometConf {
val COMET_EXEC_SORT_MERGE_JOIN_RIGHT_KEY_REDUCTION_ENABLED: ConfigEntry[Boolean] =
conf(s"$COMET_EXEC_CONFIG_PREFIX.sortMergeJoin.rightKeyReduction.enabled")
.category(CATEGORY_EXEC)
.doc(
"Reduce duplicate keys when an unfiltered left-semi sort-merge join reads its single " +
s"integer equality key directly from a Comet shuffle. $TUNING_GUIDE.")
.doc("Reduce repeated integer keys before the right sort of an unfiltered left-semi join " +
s"over a Comet shuffle. Supports a single column or coalesce expression. $TUNING_GUIDE.")
.booleanConf
.createWithDefault(false)
val COMET_EXEC_EXISTENCE_JOIN_ENABLED: ConfigEntry[Boolean] =
Expand Down
17 changes: 13 additions & 4 deletions spark/src/main/scala/org/apache/spark/sql/comet/operators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.broadcast.Broadcast
import org.apache.spark.internal.Logging
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSeq, AttributeSet, CodegenObjectFactoryMode, Expression, ExpressionSet, Generator, LeafExpression, Literal, NamedExpression, SortOrder, XXH64}
import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSeq, AttributeSet, Coalesce, CodegenObjectFactoryMode, Expression, ExpressionSet, Generator, LeafExpression, Literal, NamedExpression, SortOrder, XXH64}
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, AggregateMode, Average, CollectList, CollectSet, Final, ImperativeAggregate, Mode, Partial, PartialMerge, Percentile, Sum}
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, BuildSide}
Expand Down Expand Up @@ -3595,9 +3595,18 @@ object CometSortMergeJoinExec extends CometOperatorSerde[SortMergeJoinExec] {
// A sort on one integer column uses DataFusion's primitive merge cursor.
private def canReduceRightKey(join: SortMergeJoinExec): Boolean =
(join.right, join.rightKeys) match {
case (sort: CometSortExec, Seq(key: Attribute)) =>
key.dataType.isInstanceOf[IntegralType] && sort.output.size == 1 &&
key.semanticEquals(sort.output.head) && readsCometShuffle(sort.child)
case (sort: CometSortExec, Seq(key))
if key.dataType.isInstanceOf[IntegralType] && readsCometShuffle(sort.child) =>
key match {
case a: Attribute => sort.output.size == 1 && a.semanticEquals(sort.output.head)
// These operands have the key's type and are safe to evaluate before reduction.
case Coalesce(values) =>
values.forall {
case _: Attribute | _: Literal => true
case _ => false
}
case _ => false
}
case _ => false
}

Expand Down
48 changes: 46 additions & 2 deletions spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.apache.spark.memory.{MemoryConsumer, MemoryMode}
import org.apache.spark.sql.{Column, CometTestBase, DataFrame, QueryTest, Row}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation
import org.apache.spark.sql.catalyst.expressions.{And, Ascending, AttributeReference, BoundReference, Cast, Descending, DynamicPruningExpression, Expression, IsNotNull, Literal, SortOrder}
import org.apache.spark.sql.catalyst.expressions.{And, Ascending, AttributeReference, BoundReference, Cast, Coalesce, Descending, DynamicPruningExpression, Expression, IsNotNull, Literal, SortOrder}
import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, BuildSide}
import org.apache.spark.sql.catalyst.plans.{Inner, JoinType, LeftOuter, LeftSemi}
import org.apache.spark.sql.catalyst.plans.logical.Join
Expand Down Expand Up @@ -1710,7 +1710,9 @@ class CometJoinSuite extends CometTestBase {
assert(!eligible(CoalesceExec(1, shuffle), keys))
assert(!eligible(shuffle, keys, Inner))
assert(!eligible(shuffle, keys, condition = Some(Literal.TrueLiteral)))
assert(!eligible(shuffle, Seq(Cast(keys.head, IntegerType))))
val cast = Cast(keys.head, IntegerType)
assert(!eligible(shuffle, Seq(cast)))
assert(!eligible(shuffle, Seq(Coalesce(Seq(cast, Literal(0))))))
}
}

Expand Down Expand Up @@ -1791,6 +1793,13 @@ class CometJoinSuite extends CometTestBase {
config.key -> "true",
CometConf.COMET_SHUFFLE_MODE.key -> "native",
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false") {
val computed = run(s"l._2 = coalesce(r._1, ${wideKey - 1}L)", expected :+ 8)
val reduced = collectFirst(computed.queryExecution.executedPlan) {
case j: CometSortMergeJoinExec => j
}.get
val input = reduced.longMetric("right_key_reduction_input_rows").value
val output = reduced.longMetric("right_key_reduction_output_rows").value
assert(output > 0 && output < input, computed.queryExecution.executedPlan.treeString)
for ((condition, ids) <- Seq(
("l._2 <=> r._1", Seq(1, 1, 2, 3, 3, 4, 5, 6, 7)),
(s"$equality AND l._3 = r._2", Seq(3, 3, 5, 6, 7)))) {
Expand All @@ -1806,6 +1815,41 @@ class CometJoinSuite extends CometTestBase {
}
}

test("right key reduction coalesces nullable integer columns with and without a literal") {
val left = Seq[java.lang.Integer](null, -1, 0, 5, 7, 9).map(Tuple1(_))
val keys = Seq[(java.lang.Integer, java.lang.Integer)]((5, 9), (null, 7), (null, null))
val right = Seq.fill(8192)(keys).flatten
withSQLConf(
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
SQLConf.SHUFFLE_PARTITIONS.key -> "1",
CometConf.COMET_BATCH_SIZE.key -> "4096",
CometConf.COMET_SHUFFLE_MODE.key -> "native",
CometConf.COMET_EXEC_SORT_MERGE_JOIN_RIGHT_KEY_REDUCTION_ENABLED.key -> "true") {
withParquetTable(left, "reduction_left") {
withParquetTable(right, "reduction_right") {
for ((keyType, key, expected) <- Seq(
("INT", "coalesce(r.a, r.b)", Seq(5, 7)),
("TINYINT", "coalesce(r.a, r.b, -1Y)", Seq(-1, 5, 7)),
("SMALLINT", "coalesce(r.a, r.b, -1S)", Seq(-1, 5, 7)))) {
val df = sql(s"""SELECT /*+ MERGE(r) */ l._1 FROM reduction_left l
|LEFT SEMI JOIN (
| SELECT CAST(_1 AS $keyType) AS a, CAST(_2 AS $keyType) AS b
| FROM reduction_right
|) r ON CAST(l._1 AS $keyType) = $key""".stripMargin)
assert(df.collect().map(_.getInt(0)).sorted.toSeq == expected, keyType)
val plan = df.queryExecution.executedPlan
val join = collectFirst(plan) { case j: CometSortMergeJoinExec => j }
.getOrElse(fail(plan.treeString))
assert(join.rightKeys.head.dataType.sql == keyType)
val input = join.longMetric("right_key_reduction_input_rows").value
val output = join.longMetric("right_key_reduction_output_rows").value
assert(input == right.size && output > 0 && output < input, plan.treeString)
}
}
}
}
}

for (pool <- Seq("fair_unified", "greedy_unified")) {
test(s"right key reduction admission with resident JVM execution memory ($pool)") {
val rightRows = 32768
Expand Down
Loading