You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "panbingkun (via GitHub)" <gi...@apache.org> on 2023/03/21 11:54:32 UTC
[GitHub] [spark] panbingkun commented on a diff in pull request #40506: [SPARK-42881][SQL] Codegen Support for get_json_object
panbingkun commented on code in PR #40506:
URL: https://github.com/apache/spark/pull/40506#discussion_r1143261154
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -140,18 +135,92 @@ case class GetJsonObject(json: Expression, path: Expression)
override def nullable: Boolean = true
override def prettyName: String = "get_json_object"
- @transient private lazy val parsedPath = parsePath(path.eval().asInstanceOf[UTF8String])
+ @transient
+ private lazy val evaluator = new GetJsonObjectEvaluator(right)
override def eval(input: InternalRow): Any = {
- val jsonStr = json.eval(input).asInstanceOf[UTF8String]
+ evaluator.setJson(json.eval(input).asInstanceOf[UTF8String])
+ evaluator.setPath(path.eval(input).asInstanceOf[UTF8String])
+ evaluator.evaluate()
+ }
+
+ protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+ val refEvaluator = ctx.addReferenceObj("evaluator", evaluator)
+ val jsonEval = json.genCode(ctx)
+ val pathEval = path.genCode(ctx)
+
+ val setJson =
+ s"""
+ |if (${jsonEval.isNull}) {
+ | $refEvaluator.setJson(null);
+ |} else {
+ | $refEvaluator.setJson(${jsonEval.value});
+ |}
+ |""".stripMargin
+ val setPath =
+ s"""
+ |if (${pathEval.isNull}) {
+ | $refEvaluator.setPath(null);
+ |} else {
+ | $refEvaluator.setPath(${pathEval.value});
+ |}
+ |""".stripMargin
+
+ val resultType = CodeGenerator.boxedType(dataType)
+ val resultTerm = ctx.freshName("result")
+ ev.copy(code =
+ code"""
+ |${jsonEval.code + "\n"}
Review Comment:
The generated code looks like this:
<img width="1057" alt="image" src="https://user-images.githubusercontent.com/15246973/226598452-fc784f17-ee57-450e-9aa7-9b4cea02e13c.png">
--
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