-
Notifications
You must be signed in to change notification settings - Fork 227
[AURON #2090] Convert CoalesceExec to native implementation #2294
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
base: master
Are you sure you want to change the base?
Changes from all commits
1afd617
3ab4631
6b85f78
3153c9a
bf1b856
60e119e
2833ad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // 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. | ||
|
|
||
| use std::{any::Any, fmt::Formatter, sync::Arc}; | ||
|
|
||
| use arrow::datatypes::SchemaRef; | ||
| use datafusion::{ | ||
| common::Result, | ||
| execution::context::TaskContext, | ||
| physical_expr::EquivalenceProperties, | ||
| physical_plan::{ | ||
| DisplayAs, DisplayFormatType, ExecutionPlan, ExecutionPlanProperties, PlanProperties, | ||
| SendableRecordBatchStream, Statistics, | ||
| execution_plan::{Boundedness, EmissionType}, | ||
| metrics::{ExecutionPlanMetricsSet, MetricsSet}, | ||
| }, | ||
| }; | ||
| use futures::StreamExt; | ||
| use once_cell::sync::OnceCell; | ||
|
|
||
| use crate::common::execution_context::ExecutionContext; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct CoalesceExec { | ||
| input: Arc<dyn ExecutionPlan>, | ||
| num_partitions: usize, | ||
| metrics: ExecutionPlanMetricsSet, | ||
| props: OnceCell<PlanProperties>, | ||
| } | ||
|
|
||
| impl CoalesceExec { | ||
| pub fn new(input: Arc<dyn ExecutionPlan>, num_partitions: usize) -> Self { | ||
| Self { | ||
| input, | ||
| num_partitions, | ||
| metrics: ExecutionPlanMetricsSet::new(), | ||
| props: OnceCell::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl DisplayAs for CoalesceExec { | ||
| fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result { | ||
| write!(f, "CoalesceExec[num_partitions={}]", self.num_partitions) | ||
| } | ||
| } | ||
|
|
||
| impl ExecutionPlan for CoalesceExec { | ||
| fn name(&self) -> &str { | ||
| "CoalesceExec" | ||
| } | ||
|
|
||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn schema(&self) -> SchemaRef { | ||
| self.input.schema() | ||
| } | ||
|
|
||
| fn properties(&self) -> &PlanProperties { | ||
| self.props.get_or_init(|| { | ||
| PlanProperties::new( | ||
| EquivalenceProperties::new(self.schema()), | ||
| self.input.output_partitioning().clone(), | ||
| EmissionType::Both, | ||
| Boundedness::Bounded, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> { | ||
| vec![&self.input] | ||
| } | ||
|
|
||
| fn with_new_children( | ||
| self: Arc<Self>, | ||
| children: Vec<Arc<dyn ExecutionPlan>>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| Ok(Arc::new(CoalesceExec::new( | ||
| children[0].clone(), | ||
| self.num_partitions, | ||
| ))) | ||
| } | ||
|
|
||
| fn execute( | ||
| &self, | ||
| partition: usize, | ||
| context: Arc<TaskContext>, | ||
| ) -> Result<SendableRecordBatchStream> { | ||
| let exec_ctx = ExecutionContext::new(context, partition, self.schema(), &self.metrics); | ||
| let mut input = exec_ctx.execute(&self.input)?; | ||
| Ok( | ||
| exec_ctx.output_with_sender("Coalesce", move |sender| async move { | ||
| while let Some(batch) = input.next().await.transpose()? { | ||
| sender.send(batch).await; | ||
| } | ||
| Ok(()) | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| fn metrics(&self) -> Option<MetricsSet> { | ||
| Some(self.metrics.clone_inner()) | ||
| } | ||
|
|
||
| fn statistics(&self) -> Result<Statistics> { | ||
| todo!() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.spark.sql.execution.auron.plan | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
||
| import org.apache.auron.sparkver | ||
|
|
||
| case class NativeCoalesceExec(numPartitions: Int, override val child: SparkPlan) | ||
| extends NativeCoalesceBase(numPartitions, child) { | ||
| @sparkver("3.2 / 3.3 / 3.4 / 3.5 / 4.0 / 4.1") | ||
| override protected def withNewChildInternal(newChild: SparkPlan): SparkPlan = | ||
| copy(child = newChild) | ||
|
|
||
| @sparkver("3.0 / 3.1") | ||
| override def withNewChildren(newChildren: Seq[SparkPlan]): SparkPlan = | ||
| copy(child = newChildren.head) | ||
|
|
||
| override def output: Seq[Attribute] = | ||
| child.output | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.auron | ||
|
|
||
| import org.apache.spark.sql.{AuronQueryTest, Row} | ||
| import org.apache.spark.sql.execution.auron.plan.NativeCoalesceExec | ||
|
|
||
| class AuronNativeCoalesceExecSuite extends AuronQueryTest with BaseAuronSQLSuite { | ||
| import testImplicits._ | ||
|
|
||
| test("test CoalesceExec to native") { | ||
| withSQLConf("spark.auron.enable.coalesce" -> "true") { | ||
| Seq((1, 2, "test test")) | ||
| .toDF("c1", "c2", "part") | ||
| .createOrReplaceTempView("coalesce_table1") | ||
| val df = { | ||
| spark.sql("select /*+ coalesce(2)*/ a.c1, a.c2 from coalesce_table1 a ") | ||
| } | ||
| df.show() | ||
|
|
||
| checkAnswer(df, Seq(Row(1, 2))) | ||
| val test = collectFirst(df.queryExecution.executedPlan) { | ||
| case coalesceExec: NativeCoalesceExec => | ||
| coalesceExec | ||
| } | ||
| println(test.get) | ||
|
|
||
| assert(collectFirst(df.queryExecution.executedPlan) { | ||
| case coalesceExec: NativeCoalesceExec => | ||
| coalesceExec | ||
| }.isDefined) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import org.apache.spark.Partitioner | |
| import org.apache.spark.SparkContext | ||
| import org.apache.spark.TaskContext | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.rdd.{CoalescedRDDPartition, RDD} | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
|
|
||
| import org.apache.auron.metric.SparkMetricNode | ||
|
|
@@ -80,6 +80,33 @@ class NativeRDD( | |
| } | ||
| } | ||
|
|
||
| class CoalesceNativeRDD( | ||
| @transient private val rddSparkContext: SparkContext, | ||
| rddDependencies: Seq[Dependency[_]], | ||
| partitions: Array[Partition], | ||
| @transient private val nativePlan: (Partition, TaskContext) => PhysicalPlanNode, | ||
| friendlyName: String) | ||
| extends NativeRDD( | ||
| rddSparkContext, | ||
| metrics = SparkMetricNode(Map.empty, Seq(), None), | ||
| rddPartitions = partitions, | ||
| rddPartitioner = None, | ||
| rddDependencies, | ||
| rddShuffleReadFull = false, | ||
| nativePlan, | ||
| friendlyName) | ||
| with Logging | ||
| with Serializable { | ||
|
|
||
| override protected def getPartitions: Array[Partition] = partitions | ||
|
|
||
| override def compute(split: Partition, context: TaskContext): Iterator[InternalRow] = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the coalesce meant to run natively, or as JVM iterator concatenation? The PR ships two implementations, and as written the native one never runs. This And even if the native node were reached, The two designs are mutually exclusive, and right now the PR carries both scaffolds and lands neither cleanly:
Which direction were you aiming for? The inline questions below are the correctness issues that hold regardless of which way it goes. |
||
| split.asInstanceOf[CoalescedRDDPartition].parents.iterator.flatMap { parentPartition => | ||
| firstParent[InternalRow].iterator(parentPartition, context) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class EmptyNativeRDD(@transient private val rddSparkContext: SparkContext) | ||
| extends NativeRDD( | ||
| rddSparkContext = rddSparkContext, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This asserts that a
NativeCoalesceExecnode appears in the plan, but never checks the one thing coalesce actually does — the output partition count. With a single-row, single-partition input,coalesce(2)collapses to a trivial case, so thefirstParentand index issues above (and any future coalescer bug) would still pass green here.Could the test cover the cases that exercise the grouping? Two that would catch the bugs above: