-
Notifications
You must be signed in to change notification settings - Fork 225
[AURON #2090] Convert CoalesceExec to native implementation #2124
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
native-engine/datafusion-ext-plans/src/coalesce_exec.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // 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!() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...s-spark/src/main/scala/org/apache/spark/sql/execution/auron/plan/NativeCoalesceExec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
59 changes: 59 additions & 0 deletions
59
...-extension-shims-spark/src/test/scala/org/apache/auron/AuronNativeCoalesceExecSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * 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") | ||
| // .coalesce(2) | ||
| .createOrReplaceTempView("coalesce_table1") | ||
| val df = { | ||
| // spark.sql("select count(a.c1), count(a.c2) from coalesce_table1 a ") | ||
| spark.sql("select /*+ coalesce(2)*/ a.c1, a.c2 from coalesce_table1 a ") | ||
| } | ||
| df.show() | ||
|
|
||
| checkAnswer(df, Seq(Row(1, 2))) | ||
| assert(collectFirst(df.queryExecution.executedPlan) { | ||
| case coalesceExec: NativeCoalesceExec => | ||
| coalesceExec | ||
| }.isDefined) | ||
|
|
||
| } | ||
| } | ||
|
|
||
| test("123") { | ||
| val random = new java.util.Random() | ||
| val data = (0 until 1000).map { _ => | ||
| (random.nextInt(10), random.nextInt(100)) | ||
| } | ||
| data.toDF("key", "value").coalesce(2).createOrReplaceTempView("coalesce_table1") | ||
|
|
||
| val df = | ||
| spark.sql("select count(key), count(value) from coalesce_table1 a ") | ||
|
|
||
| checkAnswer(df, Seq(Row(1, 2))) | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -411,6 +411,11 @@ public class SparkAuronConfiguration extends AuronConfiguration { | |
| .withDescription("Enable AggregateExec operation conversion to native Auron implementations.") | ||
| .withDefaultValue(true); | ||
|
|
||
| public static final ConfigOption<Boolean> ENABLE_COALESEC = new SQLConfOption<>(Boolean.class) | ||
| .withKey("auron.enable.coalesce") | ||
| .withCategory("Operator Supports") | ||
| .withDescription("Enable CoalesceExec operation conversion to native Auron implementations.") | ||
| .withDefaultValue(true); | ||
|
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. nit: needs a new line here |
||
| public static final ConfigOption<Boolean> ENABLE_EXPAND = new SQLConfOption<>(Boolean.class) | ||
| .withKey("auron.enable.expand") | ||
| .withCategory("Operator Supports") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should improve the testing here, some cases I can think about