-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-50964][SQL] Use StaticInvoke to implement VariantGet
codegen
#49627
Draft
panbingkun
wants to merge
3
commits into
apache:master
Choose a base branch
from
panbingkun:optimize_VariantGet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−93
Draft
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
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...n/scala/org/apache/spark/sql/catalyst/expressions/variant/ToVariantPathSegmentArray.scala
This file contains 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,81 @@ | ||
/* | ||
* 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.catalyst.expressions.variant | ||
|
||
import scala.util.parsing.combinator.RegexParsers | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, RuntimeReplaceable, UnaryExpression} | ||
import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke | ||
import org.apache.spark.sql.errors.QueryExecutionErrors | ||
import org.apache.spark.sql.types.{DataType, ObjectType, StringType} | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
case class ToVariantPathSegmentArray(path: Expression, funcName: String) | ||
extends UnaryExpression | ||
with RuntimeReplaceable { | ||
|
||
override def foldable: Boolean = path.foldable | ||
override def nullIntolerant: Boolean = true | ||
override def child: Expression = path | ||
override def prettyName: String = "to_variant_path_segment_array" | ||
|
||
override def dataType: DataType = ObjectType(classOf[Array[VariantPathSegment]]) | ||
|
||
override def replacement: Expression = | ||
StaticInvoke( | ||
VariantPathParser.getClass, | ||
dataType, | ||
"parse", | ||
Seq(path, Literal(UTF8String.fromString(funcName), StringType)), | ||
Seq(path.dataType, StringType) | ||
) | ||
|
||
override protected def withNewChildInternal(newChild: Expression): Expression = | ||
copy(path = newChild) | ||
} | ||
|
||
object VariantPathParser extends RegexParsers { | ||
private def root: Parser[Char] = '$' | ||
|
||
// Parse index segment like `[123]`. | ||
private def index: Parser[VariantPathSegment] = | ||
for { | ||
index <- '[' ~> "\\d+".r <~ ']' | ||
} yield { | ||
ArrayExtraction(index.toInt) | ||
} | ||
|
||
// Parse key segment like `.name`, `['name']`, or `["name"]`. | ||
private def key: Parser[VariantPathSegment] = | ||
for { | ||
key <- '.' ~> "[^\\.\\[]+".r | "['" ~> "[^\\'\\?]+".r <~ "']" | | ||
"[\"" ~> "[^\\\"\\?]+".r <~ "\"]" | ||
} yield { | ||
ObjectExtraction(key) | ||
} | ||
|
||
private val parser: Parser[List[VariantPathSegment]] = phrase(root ~> rep(key | index)) | ||
|
||
def parse(str: UTF8String, funcName: UTF8String): Array[VariantPathSegment] = { | ||
if (str == null) return null | ||
parseAll(parser, str.toString) match { | ||
case Success(result, _) => result.toArray | ||
case _ => throw QueryExecutionErrors.invalidVariantGetPath(str.toString, funcName.toString) | ||
} | ||
} | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ct/common/src/test/resources/query-tests/explain-results/function_try_variant_get.explain
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [try_variant_get(static_invoke(VariantExpressionEvalUtils.parseJson(g#0, false, true)), $, IntegerType, false, Some(America/Los_Angeles)) AS try_variant_get(parse_json(g), $)#0] | ||
Project [static_invoke(VariantGet.variantGet(static_invoke(VariantExpressionEvalUtils.parseJson(g#0, false, true)), static_invoke(VariantPathParser.parse($, try_variant_get)), IntegerType, VariantCastArgs(false,Some(America/Los_Angeles),America/Los_Angeles))) AS try_variant_get(parse_json(g), $)#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
2 changes: 1 addition & 1 deletion
2
...onnect/common/src/test/resources/query-tests/explain-results/function_variant_get.explain
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [variant_get(static_invoke(VariantExpressionEvalUtils.parseJson(g#0, false, true)), $, IntegerType, true, Some(America/Los_Angeles)) AS variant_get(parse_json(g), $)#0] | ||
Project [static_invoke(VariantGet.variantGet(static_invoke(VariantExpressionEvalUtils.parseJson(g#0, false, true)), static_invoke(VariantPathParser.parse($, variant_get)), IntegerType, VariantCastArgs(true,Some(America/Los_Angeles),America/Los_Angeles))) AS variant_get(parse_json(g), $)#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
This file contains 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 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.
This is likely the most annoying part of
RuntimeReplaceable
: pattern match becomes less intuitive.Now I think it's probably better not to use
RuntimeReplaceable
for expressions that need be matched post analysis, until we improveRuntimeReplaceable
to be replaced truly at runtime.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.
Let's hold it first, and I'll try to investigate how to make a truly
RuntimeReplaceable
(as we discussed privately, I think its idea is to only execute thereplaced expression
atruntime
, while still maintaining it during the analysis and optimization stages, right?)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.
Yes, that will be the best.