-
Notifications
You must be signed in to change notification settings - Fork 169
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
feat: Add specific configs for converting Spark Parquet and JSON data to Arrow #832
Changes from all commits
58fe17b
3502124
9537ac4
2eb2527
8f015dd
f78b87d
111586f
c998b06
961e381
6f47ec2
fa46035
6907482
2234277
a828d8f
dc31a02
95ce2b6
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,32 @@ | ||
<!--- | ||
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. | ||
--> | ||
|
||
# Supported Spark Data Sources | ||
|
||
## Parquet | ||
|
||
When `spark.comet.scan.enabled` is enabled, Parquet scans will be performed natively by Comet if all data types | ||
in the schema are supported. When this option is not enabled, the scan will fall back to Spark. In this case, | ||
enabling `spark.comet.convert.parquet.enabled` will immediately convert the data into Arrow format, allowing native | ||
execution to happen after that, but the process may not be efficient. | ||
|
||
## JSON | ||
|
||
Comet does not provide native JSON scan, but when `spark.comet.convert.json.enabled` is enabled, data is immediately | ||
converted into Arrow format, allowing native execution to happen after that. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,10 @@ import org.apache.spark.sql.execution._ | |
import org.apache.spark.sql.execution.adaptive.{AQEShuffleReadExec, BroadcastQueryStageExec, ShuffleQueryStageExec} | ||
import org.apache.spark.sql.execution.aggregate.HashAggregateExec | ||
import org.apache.spark.sql.execution.datasources._ | ||
import org.apache.spark.sql.execution.datasources.json.JsonFileFormat | ||
import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat | ||
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec | ||
import org.apache.spark.sql.execution.datasources.v2.json.JsonScan | ||
import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan | ||
import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} | ||
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, ShuffledHashJoinExec, SortMergeJoinExec} | ||
|
@@ -1095,7 +1097,7 @@ object CometSparkSessionExtensions extends Logging { | |
} | ||
|
||
private[comet] def isCometScanEnabled(conf: SQLConf): Boolean = { | ||
COMET_SCAN_ENABLED.get(conf) | ||
COMET_NATIVE_SCAN_ENABLED.get(conf) | ||
} | ||
|
||
private[comet] def isCometExecEnabled(conf: SQLConf): Boolean = { | ||
|
@@ -1131,12 +1133,39 @@ object CometSparkSessionExtensions extends Logging { | |
// operators can have a chance to be converted to columnar. Leaf operators that output | ||
// columnar batches, such as Spark's vectorized readers, will also be converted to native | ||
// comet batches. | ||
// TODO: consider converting other intermediate operators to columnar. | ||
op.isInstanceOf[LeafExecNode] && CometSparkToColumnarExec.isSchemaSupported(op.schema) && | ||
COMET_SPARK_TO_COLUMNAR_ENABLED.get(conf) && { | ||
if (CometSparkToColumnarExec.isSchemaSupported(op.schema)) { | ||
op match { | ||
// Convert Spark DS v1 scan to Arrow format | ||
case scan: FileSourceScanExec => | ||
scan.relation.fileFormat match { | ||
case _: JsonFileFormat => CometConf.COMET_CONVERT_FROM_JSON_ENABLED.get(conf) | ||
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. @parthchandra This is what we discussed. |
||
case _: ParquetFileFormat => CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.get(conf) | ||
case _ => isSparkToArrowEnabled(conf, op) | ||
} | ||
// Convert Spark DS v2 scan to Arrow format | ||
case scan: BatchScanExec => | ||
scan.scan match { | ||
case _: JsonScan => CometConf.COMET_CONVERT_FROM_JSON_ENABLED.get(conf) | ||
case _: ParquetScan => CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.get(conf) | ||
case _ => isSparkToArrowEnabled(conf, op) | ||
} | ||
Comment on lines
+1146
to
+1151
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 it intended the new options that precedence over the old operator list in Maybe it would be surprising if the logic was private def shouldApplySparkToColumnar(conf: SQLConf, op: SparkPlan): Boolean = {
// Only consider converting leaf nodes to columnar currently, so that all the following
// operators can have a chance to be converted to columnar. Leaf operators that output
// columnar batches, such as Spark's vectorized readers, will also be converted to native
// comet batches.
CometSparkToColumnarExec.isSchemaSupported(op.schema) && (
isSparkToArrowEnabledOp(conf, op) || isSparkToArrowEnabledDataSource(conf, op))
}
private def isSparkToArrowEnabledDataSource(conf: SQLConf, op: SparkPlan): Boolean = {
op match {
// Convert Spark DS v1 scan to Arrow format
case scan: FileSourceScanExec =>
scan.relation.fileFormat match {
case _: JsonFileFormat => CometConf.COMET_CONVERT_FROM_JSON_ENABLED.get(conf)
case _: ParquetFileFormat => CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.get(conf)
case _ => false
}
// Convert Spark DS v2 scan to Arrow format
case scan: BatchScanExec =>
scan.scan match {
case _: JsonScan => CometConf.COMET_CONVERT_FROM_JSON_ENABLED.get(conf)
case _: ParquetScan => CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.get(conf)
case _ => false
}
}
}
private def isSparkToArrowEnabledOp(conf: SQLConf, op: SparkPlan) = {
op.isInstanceOf[LeafExecNode] && COMET_SPARK_TO_ARROW_ENABLED.get(conf) && {
val simpleClassName = Utils.getSimpleName(op.getClass)
val nodeName = simpleClassName.replaceAll("Exec$", "")
COMET_SPARK_TO_ARROW_SUPPORTED_OPERATOR_LIST.get(conf).contains(nodeName)
}
} So that we do not change the behavior of existing configs. |
||
// other leaf nodes | ||
case _: LeafExecNode => | ||
isSparkToArrowEnabled(conf, op) | ||
case _ => | ||
// TODO: consider converting other intermediate operators to columnar. | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
private def isSparkToArrowEnabled(conf: SQLConf, op: SparkPlan) = { | ||
COMET_SPARK_TO_ARROW_ENABLED.get(conf) && { | ||
val simpleClassName = Utils.getSimpleName(op.getClass) | ||
val nodeName = simpleClassName.replaceAll("Exec$", "") | ||
COMET_SPARK_TO_COLUMNAR_SUPPORTED_OPERATOR_LIST.get(conf).contains(nodeName) | ||
COMET_SPARK_TO_ARROW_SUPPORTED_OPERATOR_LIST.get(conf).contains(nodeName) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ "a": [1], "b": { "c": "foo" }} | ||
{ "a": 2, "b": { "d": "bar" }} | ||
{ "b": { "d": "bar" }} | ||
{ "a": 3 } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1974,27 +1974,30 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper { | |
} | ||
|
||
test("get_struct_field") { | ||
withSQLConf( | ||
CometConf.COMET_SPARK_TO_COLUMNAR_ENABLED.key -> "true", | ||
CometConf.COMET_SPARK_TO_COLUMNAR_SUPPORTED_OPERATOR_LIST.key -> "FileSourceScan") { | ||
withTempPath { dir => | ||
var df = spark | ||
.range(5) | ||
// Add both a null struct and null inner value | ||
.select( | ||
when( | ||
col("id") > 1, | ||
struct( | ||
when(col("id") > 2, col("id")).alias("id"), | ||
when(col("id") > 2, struct(when(col("id") > 3, col("id")).alias("id"))) | ||
.as("nested2"))) | ||
.alias("nested1")) | ||
|
||
df.write.parquet(dir.toString()) | ||
|
||
df = spark.read.parquet(dir.toString()) | ||
checkSparkAnswerAndOperator(df.select("nested1.id")) | ||
checkSparkAnswerAndOperator(df.select("nested1.nested2.id")) | ||
Seq("", "parquet").foreach { v1List => | ||
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. I added this line so that we test with both v1 and v2 sources |
||
withSQLConf( | ||
SQLConf.USE_V1_SOURCE_LIST.key -> v1List, | ||
CometConf.COMET_NATIVE_SCAN_ENABLED.key -> "false", | ||
CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.key -> "true") { | ||
withTempPath { dir => | ||
var df = spark | ||
.range(5) | ||
// Add both a null struct and null inner value | ||
.select( | ||
when( | ||
col("id") > 1, | ||
struct( | ||
when(col("id") > 2, col("id")).alias("id"), | ||
when(col("id") > 2, struct(when(col("id") > 3, col("id")).alias("id"))) | ||
.as("nested2"))) | ||
.alias("nested1")) | ||
|
||
df.write.parquet(dir.toString()) | ||
|
||
df = spark.read.parquet(dir.toString()) | ||
checkSparkAnswerAndOperator(df.select("nested1.id")) | ||
checkSparkAnswerAndOperator(df.select("nested1.nested2.id")) | ||
} | ||
} | ||
} | ||
} | ||
|
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.
Do you prefer that name/should we update the config to sparkToArrow while we're making these updates?
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.
I don't have a strong feeling whether we should change the config key or not. I think the description sufficiently explains what this does and I didn't want to cause extra work for people who are already using these configs.
If others think we should change it then I am fine with that too.