You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "chenhao-db (via GitHub)" <gi...@apache.org> on 2024/12/18 20:48:01 UTC
[PR] [SPARK-50615] Push variant into scan. [spark]
chenhao-db opened a new pull request, #49235:
URL: https://github.com/apache/spark/pull/49235
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49235:
URL: https://github.com/apache/spark/pull/49235#discussion_r1891427713
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PushVariantIntoScan.scala:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.datasources
+
+import scala.collection.mutable.HashMap
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.variant.{VariantGet, VariantPathParser}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project, Subquery}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+// A metadata class of a struct field. All struct fields in a struct must either all have this
+// metadata, or all don't have it.
+// We define a "variant struct" as: a special struct with its fields annotated with this metadata.
+// It indicates that the struct should produce all requested fields of a variant type, and should be
+// treated specially by the scan.
+case class VariantMetadata(
+ // The `path` parameter of VariantGet. It has the same format as a JSON path, except that
+ // `[*]` is not supported.
+ path: String,
+ failOnError: Boolean,
+ timeZoneId: String) {
+ // Produce a metadata contain one key-value pair. The key is the special `METADATA_KEY`.
+ // The value contains three key-value pairs for `path`, `failOnError`, and `timeZoneId`.
+ def toMetadata: Metadata =
+ new MetadataBuilder().putMetadata(
+ VariantMetadata.METADATA_KEY,
+ new MetadataBuilder()
+ .putString(VariantMetadata.PATH_KEY, path)
+ .putBoolean(VariantMetadata.FAIL_ON_ERROR_KEY, failOnError)
+ .putString(VariantMetadata.TIME_ZONE_ID_KEY, timeZoneId)
+ .build()
+ ).build()
+
+ def parsedPath(): Array[VariantPathParser.PathSegment] = {
+ VariantPathParser.parse(path).getOrElse {
+ val name = if (failOnError) "variant_get" else "try_variant_get"
+ throw QueryExecutionErrors.invalidVariantGetPath(path, name)
+ }
+ }
+}
+
+object VariantMetadata {
+ val METADATA_KEY = "__VARIANT_METADATA_KEY"
+ val PATH_KEY = "path"
+ val FAIL_ON_ERROR_KEY = "failOnError"
+ val TIME_ZONE_ID_KEY = "timeZoneId"
+
+ def isVariantStruct(s: StructType): Boolean =
+ s.fields.length > 0 && s.fields.forall(_.metadata.contains(METADATA_KEY))
+
+ def isVariantStruct(t: DataType): Boolean = t match {
+ case s: StructType => isVariantStruct(s)
+ case _ => false
+ }
+
+ // Parse the `VariantMetadata` from a metadata produced by `toMetadata`.
+ def fromMetadata(metadata: Metadata): VariantMetadata = {
+ val value = metadata.getMetadata(METADATA_KEY)
+ VariantMetadata(
+ value.getString(PATH_KEY),
+ value.getBoolean(FAIL_ON_ERROR_KEY),
+ value.getString(TIME_ZONE_ID_KEY)
+ )
+ }
+}
+
+// Represent a requested field of a variant that the scan should produce.
+// Each `RequestedVariantField` is corresponded to a variant path extraction in the plan.
+case class RequestedVariantField(path: VariantMetadata, targetType: DataType)
+
+object RequestedVariantField {
+ def fullVariant: RequestedVariantField =
+ RequestedVariantField(VariantMetadata("$", failOnError = true, "UTC"), VariantType)
+
+ def apply(v: VariantGet): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata(v.path.eval().toString, v.failOnError, v.timeZoneId.get), v.dataType)
+
+ def apply(c: Cast): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata("$", c.evalMode != EvalMode.TRY, c.timeZoneId.get), c.dataType)
+}
+
+// Extract a nested struct access path. Return the (root attribute id, a sequence of ordinals to
+// access the field). For non-nested attribute access, the sequence is empty.
+object StructPath {
+ def unapply(expr: Expression): Option[(ExprId, Seq[Int])] = expr match {
+ case GetStructField(StructPath(root, path), ordinal, _) => Some((root, path :+ ordinal))
+ case a: Attribute => Some(a.exprId, Nil)
+ case _ => None
+ }
+}
+
+// A collection of all eligible variants in a relation, which are in the root of the relation output
+// schema, or only nested in struct types.
+// The user should:
+// 1. Call `addVariantFields` to add all eligible variants in a relation.
+// 2. Call `collectRequestedFields` on all expressions depending on the relation. This process will
+// add the requested fields of each variant and potentially remove non-eligible variants. See
+// `collectRequestedFields` for details.
+// 3. Call `rewriteType` to produce a new output schema for the relation.
+// 4. Call `rewriteExpr` to rewrite the previously visited expressions by replacing variant
+// extractions with struct accessed.
+class VariantInRelation {
+ // First level key: root attribute id.
+ // Second level key: struct access paths to the variant type.
+ // Third level key: requested fields of a variant type.
+ // Final value: the ordinal of a requested field in the final struct of requested fields.
+ val mapping = new HashMap[ExprId, HashMap[Seq[Int], HashMap[RequestedVariantField, Int]]]
+
+ // Extract the SQL-struct path where the leaf is a variant.
+ object StructPathToVariant {
+ def unapply(expr: Expression): Option[HashMap[RequestedVariantField, Int]] = expr match {
+ case StructPath(attrId, path) =>
+ mapping.get(attrId).flatMap(_.get(path))
+ case _ => None
+ }
+ }
+
+ // Find eligible variants recursively. `attrId` is the root attribute id.
+ // `path` is the current struct access path. `dataType` is the child data type after extracting
+ // `path` from the root attribute struct.
+ def addVariantFields(attrId: ExprId, dataType: DataType, defaultValue: Any,
+ path: Seq[Int]): Unit = {
+ dataType match {
+ // TODO(SHREDDING): non-null default value is not yet supported.
+ case _: VariantType if defaultValue == null =>
+ mapping.getOrElseUpdate(attrId, new HashMap).put(path, new HashMap)
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val row = defaultValue.asInstanceOf[InternalRow]
+ for ((field, idx) <- s.fields.zipWithIndex) {
+ val fieldDefault = if (row == null || row.isNullAt(idx)) {
+ null
+ } else {
+ row.get(idx, field.dataType)
+ }
+ addVariantFields(attrId, field.dataType, fieldDefault, path :+ idx)
+ }
+ case _ =>
+ }
+ }
+
+ def rewriteType(attrId: ExprId, dataType: DataType, path: Seq[Int]): DataType = {
+ dataType match {
+ case _: VariantType =>
+ mapping.get(attrId).flatMap(_.get(path)) match {
+ case Some(fields) =>
+ var requestedFields = fields.toArray.sortBy(_._2).map { case (field, ordinal) =>
+ StructField(ordinal.toString, field.targetType, metadata = field.path.toMetadata)
+ }
+ // Avoid producing an empty struct of requested fields. This is intended to simplify the
+ // scan implementation, which may not be able to handle empty struct type. This happens
+ // if the variant is not used, or only used in `IsNotNull/IsNull` expressions. The value
+ // of the placeholder field doesn't matter, even if the scan source accidentally
+ // contains such a field.
+ if (requestedFields.isEmpty) {
+ val placeholder = VariantMetadata("$.__placeholder_field__",
+ failOnError = false, timeZoneId = "UTC")
+ requestedFields = Array(StructField("0", BooleanType,
+ metadata = placeholder.toMetadata))
+ }
+ StructType(requestedFields)
+ case _ => dataType
+ }
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val newFields = s.fields.zipWithIndex.map { case (field, idx) =>
+ field.copy(dataType = rewriteType(attrId, field.dataType, path :+ idx))
+ }
+ StructType(newFields)
+ case _ => dataType
+ }
+ }
+
+ // Add a requested field to a variant column.
+ private def addField(map: HashMap[RequestedVariantField, Int],
+ field: RequestedVariantField): Unit = {
+ val idx = map.size
+ map.getOrElseUpdate(field, idx)
+ }
+
+ // Update `mapping` with any access to a variant. Add the requested fields of each variant and
+ // potentially remove non-eligible variants.
+ // If a struct containing a variant is directly used, this variant is not eligible for push down.
+ // This is because we need to replace the variant type with a struct producing all requested
+ // fields, which also changes the struct type containing it, and it is difficult to reconstruct
+ // the original struct value. This is not a big loss, because we need the full variant anyway.
+ def collectRequestedFields(expr: Expression): Unit = expr match {
+ case v@VariantGet(StructPathToVariant(fields), _, _, _, _) =>
+ addField(fields, RequestedVariantField(v))
+ case c@Cast(StructPathToVariant(fields), _, _, _) => addField(fields, RequestedVariantField(c))
+ case IsNotNull(StructPath(_, _)) | IsNull(StructPath(_, _)) =>
+ case StructPath(attrId, path) =>
+ mapping.get(attrId) match {
+ case Some(variants) =>
+ variants.get(path) match {
+ case Some(fields) =>
+ addField(fields, RequestedVariantField.fullVariant)
+ case _ =>
+ // Remove non-eligible variants.
+ variants.filterInPlace { case (key, _) => !key.startsWith(path) }
+ }
+ case _ =>
+ }
+ case _ => expr.children.foreach(collectRequestedFields)
+ }
+
+ def rewriteExpr(expr: Expression,
+ attributeMap: Map[ExprId, AttributeReference]): Expression = {
+ def rewriteAttribute(expr: Expression): Expression = expr.transformDown {
+ case a: Attribute => attributeMap.getOrElse(a.exprId, a)
+ }
+
+ // Rewrite patterns should be consistent with visit patterns in `collectRequestedFields`.
+ expr.transformDown {
+ case g@VariantGet(v@StructPathToVariant(fields), _, _, _, _) =>
+ // Rewrite the attribute in advance, rather than depending on the last branch to rewrite it.
+ // Ww need to avoid the `v@StructPathToVariant(fields)` branch to rewrite the child again.
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField(g)))
+ case c@Cast(v@StructPathToVariant(fields), _, _, _) =>
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField(c)))
+ case i@IsNotNull(StructPath(_, _)) => rewriteAttribute(i)
+ case i@IsNull(StructPath(_, _)) => rewriteAttribute(i)
+ case v@StructPathToVariant(fields) =>
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField.fullVariant))
+ case a: Attribute => attributeMap.getOrElse(a.exprId, a)
+ }
+ }
+}
+
+// Push variant into scan by rewriting the variant type with a struct type producing all requested
+// fields and rewriting the variant extraction expressions by struct accesses.
+// For example, for an input plan:
+// - Project [v:a::int, v:b::string, v]
+// - Filter [v:a::int = 1]
+// - Relation [v: variant]
+// Rewrite it as:
+// - Project [v.0, v.1, v.2]
+// - Filter [v.0 = 1]
+// - Relation [v: struct<0: int, 1: string, 2: variant>]
+// The struct fields are annotated with `VariantMetadata` to indicate the extraction path.
+object PushVariantIntoScan extends Rule[LogicalPlan] {
Review Comment:
This rule matches a similar plan pattern as `SchemaPruning`, shall we also put this rule in the `earlyScanPushDownRules` of `SparkOptimizer`?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49235:
URL: https://github.com/apache/spark/pull/49235#discussion_r1891418567
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PushVariantIntoScan.scala:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.datasources
+
+import scala.collection.mutable.HashMap
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.variant.{VariantGet, VariantPathParser}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project, Subquery}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+// A metadata class of a struct field. All struct fields in a struct must either all have this
+// metadata, or all don't have it.
+// We define a "variant struct" as: a special struct with its fields annotated with this metadata.
+// It indicates that the struct should produce all requested fields of a variant type, and should be
+// treated specially by the scan.
+case class VariantMetadata(
+ // The `path` parameter of VariantGet. It has the same format as a JSON path, except that
+ // `[*]` is not supported.
+ path: String,
+ failOnError: Boolean,
+ timeZoneId: String) {
+ // Produce a metadata contain one key-value pair. The key is the special `METADATA_KEY`.
+ // The value contains three key-value pairs for `path`, `failOnError`, and `timeZoneId`.
+ def toMetadata: Metadata =
+ new MetadataBuilder().putMetadata(
+ VariantMetadata.METADATA_KEY,
+ new MetadataBuilder()
+ .putString(VariantMetadata.PATH_KEY, path)
+ .putBoolean(VariantMetadata.FAIL_ON_ERROR_KEY, failOnError)
+ .putString(VariantMetadata.TIME_ZONE_ID_KEY, timeZoneId)
+ .build()
+ ).build()
+
+ def parsedPath(): Array[VariantPathParser.PathSegment] = {
+ VariantPathParser.parse(path).getOrElse {
+ val name = if (failOnError) "variant_get" else "try_variant_get"
+ throw QueryExecutionErrors.invalidVariantGetPath(path, name)
+ }
+ }
+}
+
+object VariantMetadata {
+ val METADATA_KEY = "__VARIANT_METADATA_KEY"
+ val PATH_KEY = "path"
+ val FAIL_ON_ERROR_KEY = "failOnError"
+ val TIME_ZONE_ID_KEY = "timeZoneId"
+
+ def isVariantStruct(s: StructType): Boolean =
+ s.fields.length > 0 && s.fields.forall(_.metadata.contains(METADATA_KEY))
+
+ def isVariantStruct(t: DataType): Boolean = t match {
+ case s: StructType => isVariantStruct(s)
+ case _ => false
+ }
+
+ // Parse the `VariantMetadata` from a metadata produced by `toMetadata`.
+ def fromMetadata(metadata: Metadata): VariantMetadata = {
+ val value = metadata.getMetadata(METADATA_KEY)
+ VariantMetadata(
+ value.getString(PATH_KEY),
+ value.getBoolean(FAIL_ON_ERROR_KEY),
+ value.getString(TIME_ZONE_ID_KEY)
+ )
+ }
+}
+
+// Represent a requested field of a variant that the scan should produce.
+// Each `RequestedVariantField` is corresponded to a variant path extraction in the plan.
+case class RequestedVariantField(path: VariantMetadata, targetType: DataType)
+
+object RequestedVariantField {
+ def fullVariant: RequestedVariantField =
+ RequestedVariantField(VariantMetadata("$", failOnError = true, "UTC"), VariantType)
+
+ def apply(v: VariantGet): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata(v.path.eval().toString, v.failOnError, v.timeZoneId.get), v.dataType)
+
+ def apply(c: Cast): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata("$", c.evalMode != EvalMode.TRY, c.timeZoneId.get), c.dataType)
+}
+
+// Extract a nested struct access path. Return the (root attribute id, a sequence of ordinals to
+// access the field). For non-nested attribute access, the sequence is empty.
+object StructPath {
+ def unapply(expr: Expression): Option[(ExprId, Seq[Int])] = expr match {
+ case GetStructField(StructPath(root, path), ordinal, _) => Some((root, path :+ ordinal))
+ case a: Attribute => Some(a.exprId, Nil)
+ case _ => None
+ }
+}
+
+// A collection of all eligible variants in a relation, which are in the root of the relation output
+// schema, or only nested in struct types.
+// The user should:
+// 1. Call `addVariantFields` to add all eligible variants in a relation.
+// 2. Call `collectRequestedFields` on all expressions depending on the relation. This process will
+// add the requested fields of each variant and potentially remove non-eligible variants. See
+// `collectRequestedFields` for details.
+// 3. Call `rewriteType` to produce a new output schema for the relation.
+// 4. Call `rewriteExpr` to rewrite the previously visited expressions by replacing variant
+// extractions with struct accessed.
+class VariantInRelation {
+ // First level key: root attribute id.
+ // Second level key: struct access paths to the variant type.
+ // Third level key: requested fields of a variant type.
+ // Final value: the ordinal of a requested field in the final struct of requested fields.
+ val mapping = new HashMap[ExprId, HashMap[Seq[Int], HashMap[RequestedVariantField, Int]]]
+
+ // Extract the SQL-struct path where the leaf is a variant.
+ object StructPathToVariant {
+ def unapply(expr: Expression): Option[HashMap[RequestedVariantField, Int]] = expr match {
+ case StructPath(attrId, path) =>
+ mapping.get(attrId).flatMap(_.get(path))
+ case _ => None
+ }
+ }
+
+ // Find eligible variants recursively. `attrId` is the root attribute id.
+ // `path` is the current struct access path. `dataType` is the child data type after extracting
+ // `path` from the root attribute struct.
+ def addVariantFields(attrId: ExprId, dataType: DataType, defaultValue: Any,
+ path: Seq[Int]): Unit = {
Review Comment:
```suggestion
def addVariantFields(
attrId: ExprId,
dataType: DataType,
defaultValue: Any,
path: Seq[Int]): Unit = {
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #49235: [SPARK-50615][SQL] Push variant into scan.
URL: https://github.com/apache/spark/pull/49235
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49235:
URL: https://github.com/apache/spark/pull/49235#discussion_r1891419666
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PushVariantIntoScan.scala:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.datasources
+
+import scala.collection.mutable.HashMap
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.variant.{VariantGet, VariantPathParser}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project, Subquery}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+// A metadata class of a struct field. All struct fields in a struct must either all have this
+// metadata, or all don't have it.
+// We define a "variant struct" as: a special struct with its fields annotated with this metadata.
+// It indicates that the struct should produce all requested fields of a variant type, and should be
+// treated specially by the scan.
+case class VariantMetadata(
+ // The `path` parameter of VariantGet. It has the same format as a JSON path, except that
+ // `[*]` is not supported.
+ path: String,
+ failOnError: Boolean,
+ timeZoneId: String) {
+ // Produce a metadata contain one key-value pair. The key is the special `METADATA_KEY`.
+ // The value contains three key-value pairs for `path`, `failOnError`, and `timeZoneId`.
+ def toMetadata: Metadata =
+ new MetadataBuilder().putMetadata(
+ VariantMetadata.METADATA_KEY,
+ new MetadataBuilder()
+ .putString(VariantMetadata.PATH_KEY, path)
+ .putBoolean(VariantMetadata.FAIL_ON_ERROR_KEY, failOnError)
+ .putString(VariantMetadata.TIME_ZONE_ID_KEY, timeZoneId)
+ .build()
+ ).build()
+
+ def parsedPath(): Array[VariantPathParser.PathSegment] = {
+ VariantPathParser.parse(path).getOrElse {
+ val name = if (failOnError) "variant_get" else "try_variant_get"
+ throw QueryExecutionErrors.invalidVariantGetPath(path, name)
+ }
+ }
+}
+
+object VariantMetadata {
+ val METADATA_KEY = "__VARIANT_METADATA_KEY"
+ val PATH_KEY = "path"
+ val FAIL_ON_ERROR_KEY = "failOnError"
+ val TIME_ZONE_ID_KEY = "timeZoneId"
+
+ def isVariantStruct(s: StructType): Boolean =
+ s.fields.length > 0 && s.fields.forall(_.metadata.contains(METADATA_KEY))
+
+ def isVariantStruct(t: DataType): Boolean = t match {
+ case s: StructType => isVariantStruct(s)
+ case _ => false
+ }
+
+ // Parse the `VariantMetadata` from a metadata produced by `toMetadata`.
+ def fromMetadata(metadata: Metadata): VariantMetadata = {
+ val value = metadata.getMetadata(METADATA_KEY)
+ VariantMetadata(
+ value.getString(PATH_KEY),
+ value.getBoolean(FAIL_ON_ERROR_KEY),
+ value.getString(TIME_ZONE_ID_KEY)
+ )
+ }
+}
+
+// Represent a requested field of a variant that the scan should produce.
+// Each `RequestedVariantField` is corresponded to a variant path extraction in the plan.
+case class RequestedVariantField(path: VariantMetadata, targetType: DataType)
+
+object RequestedVariantField {
+ def fullVariant: RequestedVariantField =
+ RequestedVariantField(VariantMetadata("$", failOnError = true, "UTC"), VariantType)
+
+ def apply(v: VariantGet): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata(v.path.eval().toString, v.failOnError, v.timeZoneId.get), v.dataType)
+
+ def apply(c: Cast): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata("$", c.evalMode != EvalMode.TRY, c.timeZoneId.get), c.dataType)
+}
+
+// Extract a nested struct access path. Return the (root attribute id, a sequence of ordinals to
+// access the field). For non-nested attribute access, the sequence is empty.
+object StructPath {
+ def unapply(expr: Expression): Option[(ExprId, Seq[Int])] = expr match {
+ case GetStructField(StructPath(root, path), ordinal, _) => Some((root, path :+ ordinal))
+ case a: Attribute => Some(a.exprId, Nil)
+ case _ => None
+ }
+}
+
+// A collection of all eligible variants in a relation, which are in the root of the relation output
+// schema, or only nested in struct types.
+// The user should:
+// 1. Call `addVariantFields` to add all eligible variants in a relation.
+// 2. Call `collectRequestedFields` on all expressions depending on the relation. This process will
+// add the requested fields of each variant and potentially remove non-eligible variants. See
+// `collectRequestedFields` for details.
+// 3. Call `rewriteType` to produce a new output schema for the relation.
+// 4. Call `rewriteExpr` to rewrite the previously visited expressions by replacing variant
+// extractions with struct accessed.
+class VariantInRelation {
+ // First level key: root attribute id.
+ // Second level key: struct access paths to the variant type.
+ // Third level key: requested fields of a variant type.
+ // Final value: the ordinal of a requested field in the final struct of requested fields.
+ val mapping = new HashMap[ExprId, HashMap[Seq[Int], HashMap[RequestedVariantField, Int]]]
+
+ // Extract the SQL-struct path where the leaf is a variant.
+ object StructPathToVariant {
+ def unapply(expr: Expression): Option[HashMap[RequestedVariantField, Int]] = expr match {
+ case StructPath(attrId, path) =>
+ mapping.get(attrId).flatMap(_.get(path))
+ case _ => None
+ }
+ }
+
+ // Find eligible variants recursively. `attrId` is the root attribute id.
+ // `path` is the current struct access path. `dataType` is the child data type after extracting
+ // `path` from the root attribute struct.
+ def addVariantFields(attrId: ExprId, dataType: DataType, defaultValue: Any,
+ path: Seq[Int]): Unit = {
+ dataType match {
+ // TODO(SHREDDING): non-null default value is not yet supported.
+ case _: VariantType if defaultValue == null =>
+ mapping.getOrElseUpdate(attrId, new HashMap).put(path, new HashMap)
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val row = defaultValue.asInstanceOf[InternalRow]
+ for ((field, idx) <- s.fields.zipWithIndex) {
+ val fieldDefault = if (row == null || row.isNullAt(idx)) {
+ null
+ } else {
+ row.get(idx, field.dataType)
+ }
+ addVariantFields(attrId, field.dataType, fieldDefault, path :+ idx)
+ }
+ case _ =>
+ }
+ }
+
+ def rewriteType(attrId: ExprId, dataType: DataType, path: Seq[Int]): DataType = {
+ dataType match {
+ case _: VariantType =>
+ mapping.get(attrId).flatMap(_.get(path)) match {
+ case Some(fields) =>
+ var requestedFields = fields.toArray.sortBy(_._2).map { case (field, ordinal) =>
+ StructField(ordinal.toString, field.targetType, metadata = field.path.toMetadata)
+ }
+ // Avoid producing an empty struct of requested fields. This is intended to simplify the
+ // scan implementation, which may not be able to handle empty struct type. This happens
+ // if the variant is not used, or only used in `IsNotNull/IsNull` expressions. The value
+ // of the placeholder field doesn't matter, even if the scan source accidentally
+ // contains such a field.
+ if (requestedFields.isEmpty) {
+ val placeholder = VariantMetadata("$.__placeholder_field__",
+ failOnError = false, timeZoneId = "UTC")
+ requestedFields = Array(StructField("0", BooleanType,
+ metadata = placeholder.toMetadata))
+ }
+ StructType(requestedFields)
+ case _ => dataType
+ }
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val newFields = s.fields.zipWithIndex.map { case (field, idx) =>
+ field.copy(dataType = rewriteType(attrId, field.dataType, path :+ idx))
+ }
+ StructType(newFields)
+ case _ => dataType
+ }
+ }
+
+ // Add a requested field to a variant column.
+ private def addField(map: HashMap[RequestedVariantField, Int],
+ field: RequestedVariantField): Unit = {
Review Comment:
```suggestion
private def addField(
map: HashMap[RequestedVariantField, Int],
field: RequestedVariantField): Unit = {
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615] Push variant into scan. [spark]
Posted by "chenhao-db (via GitHub)" <gi...@apache.org>.
chenhao-db commented on PR #49235:
URL: https://github.com/apache/spark/pull/49235#issuecomment-2552245041
@cloud-fan @gene-db @cashmand Please help review, thanks!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49235:
URL: https://github.com/apache/spark/pull/49235#discussion_r1891419987
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PushVariantIntoScan.scala:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.datasources
+
+import scala.collection.mutable.HashMap
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.variant.{VariantGet, VariantPathParser}
+import org.apache.spark.sql.catalyst.planning.PhysicalOperation
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project, Subquery}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.ResolveDefaultColumns
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+// A metadata class of a struct field. All struct fields in a struct must either all have this
+// metadata, or all don't have it.
+// We define a "variant struct" as: a special struct with its fields annotated with this metadata.
+// It indicates that the struct should produce all requested fields of a variant type, and should be
+// treated specially by the scan.
+case class VariantMetadata(
+ // The `path` parameter of VariantGet. It has the same format as a JSON path, except that
+ // `[*]` is not supported.
+ path: String,
+ failOnError: Boolean,
+ timeZoneId: String) {
+ // Produce a metadata contain one key-value pair. The key is the special `METADATA_KEY`.
+ // The value contains three key-value pairs for `path`, `failOnError`, and `timeZoneId`.
+ def toMetadata: Metadata =
+ new MetadataBuilder().putMetadata(
+ VariantMetadata.METADATA_KEY,
+ new MetadataBuilder()
+ .putString(VariantMetadata.PATH_KEY, path)
+ .putBoolean(VariantMetadata.FAIL_ON_ERROR_KEY, failOnError)
+ .putString(VariantMetadata.TIME_ZONE_ID_KEY, timeZoneId)
+ .build()
+ ).build()
+
+ def parsedPath(): Array[VariantPathParser.PathSegment] = {
+ VariantPathParser.parse(path).getOrElse {
+ val name = if (failOnError) "variant_get" else "try_variant_get"
+ throw QueryExecutionErrors.invalidVariantGetPath(path, name)
+ }
+ }
+}
+
+object VariantMetadata {
+ val METADATA_KEY = "__VARIANT_METADATA_KEY"
+ val PATH_KEY = "path"
+ val FAIL_ON_ERROR_KEY = "failOnError"
+ val TIME_ZONE_ID_KEY = "timeZoneId"
+
+ def isVariantStruct(s: StructType): Boolean =
+ s.fields.length > 0 && s.fields.forall(_.metadata.contains(METADATA_KEY))
+
+ def isVariantStruct(t: DataType): Boolean = t match {
+ case s: StructType => isVariantStruct(s)
+ case _ => false
+ }
+
+ // Parse the `VariantMetadata` from a metadata produced by `toMetadata`.
+ def fromMetadata(metadata: Metadata): VariantMetadata = {
+ val value = metadata.getMetadata(METADATA_KEY)
+ VariantMetadata(
+ value.getString(PATH_KEY),
+ value.getBoolean(FAIL_ON_ERROR_KEY),
+ value.getString(TIME_ZONE_ID_KEY)
+ )
+ }
+}
+
+// Represent a requested field of a variant that the scan should produce.
+// Each `RequestedVariantField` is corresponded to a variant path extraction in the plan.
+case class RequestedVariantField(path: VariantMetadata, targetType: DataType)
+
+object RequestedVariantField {
+ def fullVariant: RequestedVariantField =
+ RequestedVariantField(VariantMetadata("$", failOnError = true, "UTC"), VariantType)
+
+ def apply(v: VariantGet): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata(v.path.eval().toString, v.failOnError, v.timeZoneId.get), v.dataType)
+
+ def apply(c: Cast): RequestedVariantField =
+ RequestedVariantField(
+ VariantMetadata("$", c.evalMode != EvalMode.TRY, c.timeZoneId.get), c.dataType)
+}
+
+// Extract a nested struct access path. Return the (root attribute id, a sequence of ordinals to
+// access the field). For non-nested attribute access, the sequence is empty.
+object StructPath {
+ def unapply(expr: Expression): Option[(ExprId, Seq[Int])] = expr match {
+ case GetStructField(StructPath(root, path), ordinal, _) => Some((root, path :+ ordinal))
+ case a: Attribute => Some(a.exprId, Nil)
+ case _ => None
+ }
+}
+
+// A collection of all eligible variants in a relation, which are in the root of the relation output
+// schema, or only nested in struct types.
+// The user should:
+// 1. Call `addVariantFields` to add all eligible variants in a relation.
+// 2. Call `collectRequestedFields` on all expressions depending on the relation. This process will
+// add the requested fields of each variant and potentially remove non-eligible variants. See
+// `collectRequestedFields` for details.
+// 3. Call `rewriteType` to produce a new output schema for the relation.
+// 4. Call `rewriteExpr` to rewrite the previously visited expressions by replacing variant
+// extractions with struct accessed.
+class VariantInRelation {
+ // First level key: root attribute id.
+ // Second level key: struct access paths to the variant type.
+ // Third level key: requested fields of a variant type.
+ // Final value: the ordinal of a requested field in the final struct of requested fields.
+ val mapping = new HashMap[ExprId, HashMap[Seq[Int], HashMap[RequestedVariantField, Int]]]
+
+ // Extract the SQL-struct path where the leaf is a variant.
+ object StructPathToVariant {
+ def unapply(expr: Expression): Option[HashMap[RequestedVariantField, Int]] = expr match {
+ case StructPath(attrId, path) =>
+ mapping.get(attrId).flatMap(_.get(path))
+ case _ => None
+ }
+ }
+
+ // Find eligible variants recursively. `attrId` is the root attribute id.
+ // `path` is the current struct access path. `dataType` is the child data type after extracting
+ // `path` from the root attribute struct.
+ def addVariantFields(attrId: ExprId, dataType: DataType, defaultValue: Any,
+ path: Seq[Int]): Unit = {
+ dataType match {
+ // TODO(SHREDDING): non-null default value is not yet supported.
+ case _: VariantType if defaultValue == null =>
+ mapping.getOrElseUpdate(attrId, new HashMap).put(path, new HashMap)
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val row = defaultValue.asInstanceOf[InternalRow]
+ for ((field, idx) <- s.fields.zipWithIndex) {
+ val fieldDefault = if (row == null || row.isNullAt(idx)) {
+ null
+ } else {
+ row.get(idx, field.dataType)
+ }
+ addVariantFields(attrId, field.dataType, fieldDefault, path :+ idx)
+ }
+ case _ =>
+ }
+ }
+
+ def rewriteType(attrId: ExprId, dataType: DataType, path: Seq[Int]): DataType = {
+ dataType match {
+ case _: VariantType =>
+ mapping.get(attrId).flatMap(_.get(path)) match {
+ case Some(fields) =>
+ var requestedFields = fields.toArray.sortBy(_._2).map { case (field, ordinal) =>
+ StructField(ordinal.toString, field.targetType, metadata = field.path.toMetadata)
+ }
+ // Avoid producing an empty struct of requested fields. This is intended to simplify the
+ // scan implementation, which may not be able to handle empty struct type. This happens
+ // if the variant is not used, or only used in `IsNotNull/IsNull` expressions. The value
+ // of the placeholder field doesn't matter, even if the scan source accidentally
+ // contains such a field.
+ if (requestedFields.isEmpty) {
+ val placeholder = VariantMetadata("$.__placeholder_field__",
+ failOnError = false, timeZoneId = "UTC")
+ requestedFields = Array(StructField("0", BooleanType,
+ metadata = placeholder.toMetadata))
+ }
+ StructType(requestedFields)
+ case _ => dataType
+ }
+ case s: StructType if !VariantMetadata.isVariantStruct(s) =>
+ val newFields = s.fields.zipWithIndex.map { case (field, idx) =>
+ field.copy(dataType = rewriteType(attrId, field.dataType, path :+ idx))
+ }
+ StructType(newFields)
+ case _ => dataType
+ }
+ }
+
+ // Add a requested field to a variant column.
+ private def addField(map: HashMap[RequestedVariantField, Int],
+ field: RequestedVariantField): Unit = {
+ val idx = map.size
+ map.getOrElseUpdate(field, idx)
+ }
+
+ // Update `mapping` with any access to a variant. Add the requested fields of each variant and
+ // potentially remove non-eligible variants.
+ // If a struct containing a variant is directly used, this variant is not eligible for push down.
+ // This is because we need to replace the variant type with a struct producing all requested
+ // fields, which also changes the struct type containing it, and it is difficult to reconstruct
+ // the original struct value. This is not a big loss, because we need the full variant anyway.
+ def collectRequestedFields(expr: Expression): Unit = expr match {
+ case v@VariantGet(StructPathToVariant(fields), _, _, _, _) =>
+ addField(fields, RequestedVariantField(v))
+ case c@Cast(StructPathToVariant(fields), _, _, _) => addField(fields, RequestedVariantField(c))
+ case IsNotNull(StructPath(_, _)) | IsNull(StructPath(_, _)) =>
+ case StructPath(attrId, path) =>
+ mapping.get(attrId) match {
+ case Some(variants) =>
+ variants.get(path) match {
+ case Some(fields) =>
+ addField(fields, RequestedVariantField.fullVariant)
+ case _ =>
+ // Remove non-eligible variants.
+ variants.filterInPlace { case (key, _) => !key.startsWith(path) }
+ }
+ case _ =>
+ }
+ case _ => expr.children.foreach(collectRequestedFields)
+ }
+
+ def rewriteExpr(expr: Expression,
+ attributeMap: Map[ExprId, AttributeReference]): Expression = {
+ def rewriteAttribute(expr: Expression): Expression = expr.transformDown {
+ case a: Attribute => attributeMap.getOrElse(a.exprId, a)
+ }
+
+ // Rewrite patterns should be consistent with visit patterns in `collectRequestedFields`.
+ expr.transformDown {
+ case g@VariantGet(v@StructPathToVariant(fields), _, _, _, _) =>
+ // Rewrite the attribute in advance, rather than depending on the last branch to rewrite it.
+ // Ww need to avoid the `v@StructPathToVariant(fields)` branch to rewrite the child again.
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField(g)))
+ case c@Cast(v@StructPathToVariant(fields), _, _, _) =>
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField(c)))
+ case i@IsNotNull(StructPath(_, _)) => rewriteAttribute(i)
+ case i@IsNull(StructPath(_, _)) => rewriteAttribute(i)
+ case v@StructPathToVariant(fields) =>
+ GetStructField(rewriteAttribute(v), fields(RequestedVariantField.fullVariant))
+ case a: Attribute => attributeMap.getOrElse(a.exprId, a)
+ }
+ }
+}
+
+// Push variant into scan by rewriting the variant type with a struct type producing all requested
+// fields and rewriting the variant extraction expressions by struct accesses.
+// For example, for an input plan:
+// - Project [v:a::int, v:b::string, v]
+// - Filter [v:a::int = 1]
+// - Relation [v: variant]
+// Rewrite it as:
+// - Project [v.0, v.1, v.2]
+// - Filter [v.0 = 1]
+// - Relation [v: struct<0: int, 1: string, 2: variant>]
+// The struct fields are annotated with `VariantMetadata` to indicate the extraction path.
+object PushVariantIntoScan extends Rule[LogicalPlan] {
+ override def apply(plan: LogicalPlan): LogicalPlan = plan match {
+ // A correlated subquery will be rewritten into join later, and will go through this rule
+ // eventually.
+ case s: Subquery if s.correlated => plan
+ case _ if !SQLConf.get.getConf(SQLConf.PUSH_VARIANT_INTO_SCAN) => plan
+ case _ => plan.transformDown {
+ case p@PhysicalOperation(projectList, filters,
+ relation @ LogicalRelationWithTable(
+ hadoopFsRelation@HadoopFsRelation(_, _, _, _, _: ParquetFileFormat, _), _)) =>
+ rewritePlan(p, projectList, filters, relation, hadoopFsRelation)
+ }
+ }
+
+ private def rewritePlan(originalPlan: LogicalPlan,
Review Comment:
ditto, fix indentaiton please
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #49235:
URL: https://github.com/apache/spark/pull/49235#discussion_r1891417749
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala:
##########
@@ -95,7 +95,8 @@ class SparkOptimizer(
EliminateLimits,
ConstantFolding),
Batch("User Provided Optimizers", fixedPoint, experimentalMethods.extraOptimizations: _*),
- Batch("Replace CTE with Repartition", Once, ReplaceCTERefWithRepartition)))
+ Batch("Replace CTE with Repartition", Once, ReplaceCTERefWithRepartition),
+ Batch("Push Variant Into Scan", Once, PushVariantIntoScan)))
Review Comment:
why does this rule need to be in the end of optimizer?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "chenhao-db (via GitHub)" <gi...@apache.org>.
chenhao-db commented on PR #49235:
URL: https://github.com/apache/spark/pull/49235#issuecomment-2556170183
@cloud-fan Thanks! I have made the changes you recommended.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-50615][SQL] Push variant into scan. [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #49235:
URL: https://github.com/apache/spark/pull/49235#issuecomment-2556293160
thanks, merging to master!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org