-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-47670][SQL] Share repeated top-level JSON path parsing #56547
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
base: master
Are you sure you want to change the base?
Changes from all commits
08550ca
58a6c53
8fb127d
c330295
2d9d2d2
8b54e69
59e73dc
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 |
|---|---|---|
|
|
@@ -22,10 +22,13 @@ import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | |
| import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, CodegenFallback, ExprCode} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.Block.BlockHelper | ||
| import org.apache.spark.sql.catalyst.expressions.json.{GetJsonObjectEvaluator, JsonExpressionUtils, JsonToStructsEvaluator, JsonTupleEvaluator, SchemaOfJsonEvaluator, StructsToJsonEvaluator} | ||
| import org.apache.spark.sql.catalyst.expressions.json.{GetJsonObjectEvaluator, JsonExpressionUtils, | ||
| JsonPathParser, JsonToStructsEvaluator, JsonTupleEvaluator, MultiGetJsonObjectEvaluator, | ||
| PathInstruction, SchemaOfJsonEvaluator, StructsToJsonEvaluator} | ||
| import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke} | ||
| import org.apache.spark.sql.catalyst.json._ | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.{JSON_TO_STRUCT, RUNTIME_REPLACEABLE, TreePattern} | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.{GET_JSON_OBJECT, JSON_TO_STRUCT, | ||
| RUNTIME_REPLACEABLE, TreePattern} | ||
| import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryErrorsBase} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.types.StringTypeWithCollation | ||
|
|
@@ -63,6 +66,8 @@ case class GetJsonObject(json: Expression, path: Expression) | |
| override def nullable: Boolean = true | ||
| override def prettyName: String = "get_json_object" | ||
|
|
||
| final override val nodePatterns: Seq[TreePattern] = Seq(GET_JSON_OBJECT) | ||
|
|
||
| @transient | ||
| private lazy val evaluator = if (path.foldable) { | ||
| new GetJsonObjectEvaluator(path.eval().asInstanceOf[UTF8String]) | ||
|
|
@@ -136,6 +141,82 @@ case class GetJsonObject(json: Expression, path: Expression) | |
| copy(json = newLeft, path = newRight) | ||
| } | ||
|
|
||
| object GetJsonObject { | ||
| private[sql] def simpleTopLevelField(path: UTF8String): Option[String] = { | ||
| try { | ||
| Option(path).flatMap(value => JsonPathParser.parse(value.toString)).collect { | ||
| case List(PathInstruction.Key, PathInstruction.Named(fieldName)) => fieldName | ||
| } | ||
| } catch { | ||
| // Numeric subscripts are parsed as Long and can overflow before the parser returns None. | ||
| case _: NumberFormatException => None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts multiple simple top-level fields from a JSON string in one parse. This is an internal | ||
| * expression used to share sibling [[GetJsonObject]] expressions; unsupported JSON paths remain | ||
| * as independent GetJsonObject expressions. | ||
| */ | ||
| case class MultiGetJsonObject( | ||
|
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.
Best of all would be to make Behavior is preserved either way: Minor and independent: the inner
Member
Author
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. Thanks for the detailed suggestion. I removed GET_JSON_OBJECT from the inner pruning predicate in the latest commit. For the larger codegen change, I agree that RuntimeReplaceable would be cleaner once #56575 is available. Since that PR is still open/WIP, and emitting Invoke directly today would make the optimized plan less readable, I would prefer to retain the current internal expression in this PR and handle that refactor as a follow-up. The current doGenCode contains no JSON-processing logic; it only evaluates the child, handles nullability, and delegates to MultiGetJsonObjectEvaluator. |
||
| json: Expression, | ||
| fieldNames: Seq[String], | ||
| fallbackPaths: Seq[String]) | ||
| extends UnaryExpression | ||
| with ExpectsInputTypes { | ||
|
|
||
| require( | ||
| fieldNames.nonEmpty && | ||
| fieldNames.distinct.length == fieldNames.length && | ||
| fallbackPaths.length == fieldNames.length) | ||
|
|
||
| override def child: Expression = json | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = | ||
| Seq(StringTypeWithCollation(supportsTrimCollation = true)) | ||
|
|
||
| override lazy val dataType: DataType = StructType(fieldNames.indices.map { index => | ||
| StructField(s"_$index", StringType, nullable = true) | ||
| }) | ||
|
|
||
| override def nullable: Boolean = true | ||
|
|
||
| // This internal unary expression always returns null when its JSON child is null. | ||
| override def nullIntolerant: Boolean = true | ||
|
|
||
| override def prettyName: String = "multi_get_json_object" | ||
|
|
||
| final override val nodePatterns: Seq[TreePattern] = Seq(GET_JSON_OBJECT) | ||
|
|
||
| @transient | ||
| private lazy val evaluator = MultiGetJsonObjectEvaluator( | ||
| fieldNames, | ||
| fallbackPaths.map(UTF8String.fromString)) | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| evaluator.evaluate(json.eval(input).asInstanceOf[UTF8String]) | ||
| } | ||
|
|
||
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val refEvaluator = ctx.addReferenceObj("evaluator", evaluator) | ||
| val jsonEval = json.genCode(ctx) | ||
| val resultType = CodeGenerator.javaType(dataType) | ||
| ev.copy(code = code""" | ||
| |${jsonEval.code} | ||
| |boolean ${ev.isNull} = ${jsonEval.isNull}; | ||
| |$resultType ${ev.value} = ${CodeGenerator.defaultValue(dataType)}; | ||
| |if (!${ev.isNull}) { | ||
| | ${ev.value} = ($resultType) $refEvaluator.evaluate(${jsonEval.value}); | ||
| | ${ev.isNull} = ${ev.value} == null; | ||
| |} | ||
| |""".stripMargin) | ||
| } | ||
|
|
||
| override protected def withNewChildInternal(newChild: Expression): MultiGetJsonObject = | ||
| copy(json = newChild) | ||
| } | ||
|
|
||
| // scalastyle:off line.size.limit line.contains.tab | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(jsonStr, p1, p2, ..., pn) - Returns a tuple like the function get_json_object, but it takes multiple names. All the input parameters and output column types are string.", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Like other classes, shall we define
prettyNameto be more complete?multi_get_json_object?spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
Line 64 in a6e3fdd
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.
done