rich7420 commented on code in PR #7544:
URL: https://github.com/apache/datafusion-comet/pull/7544#discussion_r4705215069
##########
native/core/src/lib.rs:
##########
@@ -421,6 +422,10 @@ mod tests {
is_feature_enabled("hdfs-opendal"),
cfg!(feature = "hdfs-opendal")
);
+ assert_eq!(
+ is_feature_enabled("native-executors"),
Review Comment:
This passes for me without the match arm because both sides default to
false. Could the `client.rs` tests assert the feature is enabled, since CI runs
those with it on?
##########
spark/src/test/scala/org/apache/spark/sql/comet/CometNativeExecutorsPlanSuite.scala:
##########
@@ -0,0 +1,299 @@
+/*
+ * 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.comet
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.execution.{InputAdapter, ScalarSubquery,
SparkPlan, WholeStageCodegenExec}
+import org.apache.spark.sql.functions.{col, count, sum}
+import org.apache.spark.sql.internal.SQLConf
+
+import org.apache.comet.{CometConf, CometIcebergTestBase,
CometRuntimeException}
+import org.apache.comet.serde.NativeExecutors.{QueryRequest, Stage}
+import org.apache.comet.serde.OperatorOuterClass.{IcebergScan, Operator}
+
+/**
+ * Plans queries for Comet Native Executors (see
+ * https://github.com/apache/datafusion-comet/issues/7288) without a cluster.
The tests take the
+ * plan Comet would execute and turn it into the stage graph a cluster runs,
so they need neither
+ * a libcomet built with the `native-executors` feature nor the cluster
binaries.
+ * `CometNativeExecutorsSuite` runs the queries on a cluster.
+ */
+class CometNativeExecutorsPlanSuite
+ extends CometNativeExecutorsTestBase
+ with CometIcebergTestBase {
+
+ override protected def sparkConf: SparkConf = {
+ val conf = super.sparkConf
+ conf.set(CometConf.COMET_SHUFFLE_MODE.key, "native")
+ conf.set(SQLConf.SHUFFLE_PARTITIONS.key, "4")
+ // Without AQE, the plan is final once it is planned.
+ conf.set(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key, "false")
+ conf
+ }
+
+ /**
+ * The plan Comet would execute, as the planner sees it before Spark adds
whole-stage codegen.
+ */
+ private def cometPlan(df: DataFrame): SparkPlan =
df.queryExecution.executedPlan match {
+ case codegen: WholeStageCodegenExec =>
+ codegen.child.transform { case adapter: InputAdapter => adapter.child }
+ case plan => plan
+ }
+
+ private def stagesOf(df: DataFrame): Seq[Stage] =
+ CometNativeExecutors.buildQuery(cometPlan(df)).getStagesList.asScala.toSeq
+
+ private def refusal(df: DataFrame): String =
+
intercept[CometRuntimeException](CometNativeExecutors.validate(cometPlan(df))).getMessage
+
+ test("an aggregate is a scan stage and a stage that reads its hash
exchange") {
+ withTable { path =>
+ val stages =
stagesOf(spark.read.parquet(path).groupBy("k").agg(sum("v")))
+ assert(stages.size == 2)
+ val Seq(scan, aggregate) = stages
+ // The scan stage runs one copy of its plan for each partition, naming
that partition's files.
+ assert(scan.getInputsCount == 0)
+ assert(scan.getPartitionPlansCount > 1)
+ assert(scan.getNumPartitions == scan.getPartitionPlansCount)
+ assert(aggregate.getInputsCount == 1)
+ val input = aggregate.getInputs(0)
+ assert(input.getProducer == 0)
+ assert(input.getPartitioning.hasHashPartition)
+ assert(input.getPartitioning.getHashPartition.getNumPartitions == 4)
+ }
+ }
+
+ test("a global aggregate reads a single-partition exchange") {
+ withTable { path =>
+ val stages = stagesOf(spark.read.parquet(path).agg(sum("id")))
+ assert(stages.size == 2)
+ assert(stages.last.getInputs(0).getPartitioning.hasSinglePartition)
+ }
+ }
+
+ test("a broadcast join reads the build side whole") {
+ withTable { path =>
+ val left = spark.read.parquet(path).select("id", "k")
+ val right = spark.read.parquet(path).where("id <
10").select(col("k").as("k2"), col("v"))
+ val stages = stagesOf(left.join(right, left("k") ===
right("k2")).select("id", "v"))
+ assert(stages.flatMap(_.getInputsList.asScala).exists(_.getBroadcast),
stages)
+ }
+ }
+
+ test("a shuffled join reads both sides' hash exchanges") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "true") {
+ withTable { path =>
+ val left = spark.read.parquet(path).select("id", "k")
+ val right = spark.read.parquet(path).select(col("k").as("k2"),
col("v"))
+ val stages = stagesOf(left.join(right, left("k") ===
right("k2")).select("id", "v"))
+ val join = stages.last
+ assert(join.getInputsCount == 2)
+
assert(join.getInputsList.asScala.forall(_.getPartitioning.hasHashPartition))
+ }
+ }
+ }
+
+ test("a scan that lists no files gets one empty partition") {
+ withTempPath { dir =>
+ spark
+ .range(0, 100)
+ .selectExpr("id", "id % 7 AS k")
+ .write
+ .partitionBy("k")
+ .parquet(dir.getCanonicalPath)
+ val df = spark.read.parquet(dir.getCanonicalPath).where("k =
99").groupBy().count()
+ val scan = stagesOf(df).head
+ assert(scan.getPartitionPlansCount == 1)
+ assert(scan.getNumPartitions == 1)
+ }
+ }
+
+ test("an Iceberg scan that plans no tasks gets one empty partition") {
+ assume(icebergAvailable, "Iceberg not available in classpath")
+ withHadoopCatalog("cne_cat") {
+ withSQLConf(CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") {
+ spark.sql(
+ "CREATE TABLE cne_cat.db.t (id BIGINT, k BIGINT) USING iceberg
PARTITIONED BY (k)")
+ spark.sql("INSERT INTO cne_cat.db.t SELECT id, id % 7 FROM range(100)")
+ // No partition has k = 99, so Iceberg plans no tasks.
+ // A projection, since Iceberg answers a count from the table's
metadata.
+ val df = spark.table("cne_cat.db.t").where("k = 99").selectExpr("id +
1")
+
assert(cometPlan(df).exists(_.isInstanceOf[CometIcebergNativeScanExec]),
cometPlan(df))
+ val scan = stagesOf(df).head
+ assert(scan.getPartitionPlansCount == 1)
+ assert(scan.getNumPartitions == 1)
+ def icebergScans(op: Operator): Seq[IcebergScan] =
+ (if (op.hasIcebergScan) Seq(op.getIcebergScan) else Nil) ++
+ op.getChildrenList.asScala.flatMap(icebergScans)
+ val Seq(icebergScan) =
icebergScans(Operator.parseFrom(scan.getPartitionPlans(0)))
+ assert(icebergScan.getFileScanTasksCount == 0)
+ assert(icebergScan.getCommon.getRequiredSchemaCount > 0,
icebergScan.getCommon)
+ }
+ }
+ }
+
+ test("an empty range gets one partition") {
+ withSQLConf(CometConf.COMET_EXEC_RANGE_ENABLED.key -> "true") {
+ for (slices <- Seq(4, 0)) {
+ val range = spark.range(1, -2, 1, slices)
+ val Seq(project) = stagesOf(range.selectExpr("id + 1"))
+ assert(project.getInputsCount == 0)
+ assert(project.getNumPartitions == 1)
+ val Seq(producer, aggregate) = stagesOf(range.agg(count("id"),
sum("id")))
+ assert(producer.getNumPartitions == 1)
+ assert(aggregate.getInputs(0).getPartitioning.hasSinglePartition)
+ }
+ }
+ }
+
+ test("a scalar subquery's result replaces it in the plan") {
+ withTable { path =>
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ val df = sql("select id from t where v > (select avg(v) from t where k =
3)")
+ // Spark runs the subquery before it executes the plan.
+ df.collect()
+ val plan = cometPlan(df)
+ val subqueries = CometNativeExecutors.scalarSubqueries(plan).collect {
+ case sub: ScalarSubquery => sub
+ }
+ assert(subqueries.size == 1)
+ def plans(query: QueryRequest): String =
+ query.getStagesList.asScala.map(_.getPlan.toString).mkString
+ val query = CometNativeExecutors.buildQuery(plan)
+ assert(plans(query).contains("subquery"))
+ val rewritten = CometNativeExecutors.withSubqueryResults(query,
subqueries)
+ assert(!plans(rewritten).contains("subquery"))
+ }
+ }
+
+ test("a query Comet does not execute runs in Spark") {
+ withSQLConf(
+ CometConf.COMET_NATIVE_EXECUTORS_ENABLED.key -> "true",
+ CometConf.COMET_ENABLED.key -> "false") {
+ val df = spark.range(10).selectExpr("id + 1 AS i")
+ assert(df.collect().map(_.getLong(0)).toSeq == (1L to 10L))
+ assert(df.queryExecution.executedPlan.collect { case e:
CometNativeExecutorsExec =>
+ e
+ }.isEmpty)
+ }
+ }
+
+ test("a command's plan stays in Spark") {
+ withTable { path =>
+ withSQLConf(
+ CometConf.COMET_NATIVE_EXECUTORS_ENABLED.key -> "true",
+ CometConf.COMET_NATIVE_EXECUTORS_SCHEDULER_URL.key ->
"http://localhost:1") {
+ withTempView("t", "v") {
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ val df = sql("create or replace temp view v as select * from t")
+ // Explaining a command explains its result, which isn't a query to
run on the cluster.
+ assert(
+
df.queryExecution.explainString(org.apache.spark.sql.execution.SimpleMode).nonEmpty)
+ assert(df.queryExecution.executedPlan.collect { case e:
CometNativeExecutorsExec =>
Review Comment:
Could a CTAS test cover the write guard? This `collect` is always empty on a
childless `CommandResultExec`, and removing the write commands from the guard
still passes for me.
##########
spark/src/test/scala/org/apache/spark/sql/comet/CometNativeExecutorsPlanSuite.scala:
##########
@@ -0,0 +1,299 @@
+/*
+ * 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.comet
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.execution.{InputAdapter, ScalarSubquery,
SparkPlan, WholeStageCodegenExec}
+import org.apache.spark.sql.functions.{col, count, sum}
+import org.apache.spark.sql.internal.SQLConf
+
+import org.apache.comet.{CometConf, CometIcebergTestBase,
CometRuntimeException}
+import org.apache.comet.serde.NativeExecutors.{QueryRequest, Stage}
+import org.apache.comet.serde.OperatorOuterClass.{IcebergScan, Operator}
+
+/**
+ * Plans queries for Comet Native Executors (see
+ * https://github.com/apache/datafusion-comet/issues/7288) without a cluster.
The tests take the
+ * plan Comet would execute and turn it into the stage graph a cluster runs,
so they need neither
+ * a libcomet built with the `native-executors` feature nor the cluster
binaries.
+ * `CometNativeExecutorsSuite` runs the queries on a cluster.
+ */
+class CometNativeExecutorsPlanSuite
+ extends CometNativeExecutorsTestBase
+ with CometIcebergTestBase {
+
+ override protected def sparkConf: SparkConf = {
+ val conf = super.sparkConf
+ conf.set(CometConf.COMET_SHUFFLE_MODE.key, "native")
+ conf.set(SQLConf.SHUFFLE_PARTITIONS.key, "4")
+ // Without AQE, the plan is final once it is planned.
+ conf.set(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key, "false")
+ conf
+ }
+
+ /**
+ * The plan Comet would execute, as the planner sees it before Spark adds
whole-stage codegen.
+ */
+ private def cometPlan(df: DataFrame): SparkPlan =
df.queryExecution.executedPlan match {
+ case codegen: WholeStageCodegenExec =>
+ codegen.child.transform { case adapter: InputAdapter => adapter.child }
+ case plan => plan
+ }
+
+ private def stagesOf(df: DataFrame): Seq[Stage] =
+ CometNativeExecutors.buildQuery(cometPlan(df)).getStagesList.asScala.toSeq
+
+ private def refusal(df: DataFrame): String =
+
intercept[CometRuntimeException](CometNativeExecutors.validate(cometPlan(df))).getMessage
+
+ test("an aggregate is a scan stage and a stage that reads its hash
exchange") {
+ withTable { path =>
+ val stages =
stagesOf(spark.read.parquet(path).groupBy("k").agg(sum("v")))
+ assert(stages.size == 2)
+ val Seq(scan, aggregate) = stages
+ // The scan stage runs one copy of its plan for each partition, naming
that partition's files.
+ assert(scan.getInputsCount == 0)
+ assert(scan.getPartitionPlansCount > 1)
+ assert(scan.getNumPartitions == scan.getPartitionPlansCount)
+ assert(aggregate.getInputsCount == 1)
+ val input = aggregate.getInputs(0)
+ assert(input.getProducer == 0)
+ assert(input.getPartitioning.hasHashPartition)
+ assert(input.getPartitioning.getHashPartition.getNumPartitions == 4)
+ }
+ }
+
+ test("a global aggregate reads a single-partition exchange") {
+ withTable { path =>
+ val stages = stagesOf(spark.read.parquet(path).agg(sum("id")))
+ assert(stages.size == 2)
+ assert(stages.last.getInputs(0).getPartitioning.hasSinglePartition)
+ }
+ }
+
+ test("a broadcast join reads the build side whole") {
+ withTable { path =>
+ val left = spark.read.parquet(path).select("id", "k")
+ val right = spark.read.parquet(path).where("id <
10").select(col("k").as("k2"), col("v"))
+ val stages = stagesOf(left.join(right, left("k") ===
right("k2")).select("id", "v"))
+ assert(stages.flatMap(_.getInputsList.asScala).exists(_.getBroadcast),
stages)
+ }
+ }
+
+ test("a shuffled join reads both sides' hash exchanges") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "true") {
+ withTable { path =>
+ val left = spark.read.parquet(path).select("id", "k")
+ val right = spark.read.parquet(path).select(col("k").as("k2"),
col("v"))
+ val stages = stagesOf(left.join(right, left("k") ===
right("k2")).select("id", "v"))
+ val join = stages.last
+ assert(join.getInputsCount == 2)
+
assert(join.getInputsList.asScala.forall(_.getPartitioning.hasHashPartition))
+ }
+ }
+ }
+
+ test("a scan that lists no files gets one empty partition") {
+ withTempPath { dir =>
+ spark
+ .range(0, 100)
+ .selectExpr("id", "id % 7 AS k")
+ .write
+ .partitionBy("k")
+ .parquet(dir.getCanonicalPath)
+ val df = spark.read.parquet(dir.getCanonicalPath).where("k =
99").groupBy().count()
+ val scan = stagesOf(df).head
+ assert(scan.getPartitionPlansCount == 1)
+ assert(scan.getNumPartitions == 1)
+ }
+ }
+
+ test("an Iceberg scan that plans no tasks gets one empty partition") {
+ assume(icebergAvailable, "Iceberg not available in classpath")
+ withHadoopCatalog("cne_cat") {
+ withSQLConf(CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") {
+ spark.sql(
+ "CREATE TABLE cne_cat.db.t (id BIGINT, k BIGINT) USING iceberg
PARTITIONED BY (k)")
+ spark.sql("INSERT INTO cne_cat.db.t SELECT id, id % 7 FROM range(100)")
+ // No partition has k = 99, so Iceberg plans no tasks.
+ // A projection, since Iceberg answers a count from the table's
metadata.
+ val df = spark.table("cne_cat.db.t").where("k = 99").selectExpr("id +
1")
+
assert(cometPlan(df).exists(_.isInstanceOf[CometIcebergNativeScanExec]),
cometPlan(df))
+ val scan = stagesOf(df).head
+ assert(scan.getPartitionPlansCount == 1)
+ assert(scan.getNumPartitions == 1)
+ def icebergScans(op: Operator): Seq[IcebergScan] =
+ (if (op.hasIcebergScan) Seq(op.getIcebergScan) else Nil) ++
+ op.getChildrenList.asScala.flatMap(icebergScans)
+ val Seq(icebergScan) =
icebergScans(Operator.parseFrom(scan.getPartitionPlans(0)))
+ assert(icebergScan.getFileScanTasksCount == 0)
+ assert(icebergScan.getCommon.getRequiredSchemaCount > 0,
icebergScan.getCommon)
+ }
+ }
+ }
+
+ test("an empty range gets one partition") {
+ withSQLConf(CometConf.COMET_EXEC_RANGE_ENABLED.key -> "true") {
+ for (slices <- Seq(4, 0)) {
+ val range = spark.range(1, -2, 1, slices)
+ val Seq(project) = stagesOf(range.selectExpr("id + 1"))
+ assert(project.getInputsCount == 0)
+ assert(project.getNumPartitions == 1)
+ val Seq(producer, aggregate) = stagesOf(range.agg(count("id"),
sum("id")))
+ assert(producer.getNumPartitions == 1)
+ assert(aggregate.getInputs(0).getPartitioning.hasSinglePartition)
+ }
+ }
+ }
+
+ test("a scalar subquery's result replaces it in the plan") {
+ withTable { path =>
+ spark.read.parquet(path).createOrReplaceTempView("t")
+ val df = sql("select id from t where v > (select avg(v) from t where k =
3)")
+ // Spark runs the subquery before it executes the plan.
+ df.collect()
+ val plan = cometPlan(df)
+ val subqueries = CometNativeExecutors.scalarSubqueries(plan).collect {
+ case sub: ScalarSubquery => sub
+ }
+ assert(subqueries.size == 1)
+ def plans(query: QueryRequest): String =
+ query.getStagesList.asScala.map(_.getPlan.toString).mkString
Review Comment:
Could `plans()` include `getPartitionPlansList` too? A file-reading stage
runs those, and the test still passes for me with that rewrite loop removed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]