panbingkun commented on code in PR #40506:
URL: https://github.com/apache/spark/pull/40506#discussion_r1260533972
##########
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}
+ |${pathEval.code}
+ |$setJson
+ |$setPath
+ |$resultType $resultTerm = ($resultType) $refEvaluator.evaluate();
+ |boolean ${ev.isNull} = $resultTerm == null;
+ |${CodeGenerator.javaType(dataType)} ${ev.value} =
${CodeGenerator.defaultValue(dataType)};
+ |if (!${ev.isNull}) {
+ | ${ev.value} = $resultTerm;
+ |}
+ |""".stripMargin
+ )
+ }
+
+ override protected def withNewChildrenInternal(
+ newLeft: Expression, newRight: Expression): GetJsonObject =
+ copy(json = newLeft, path = newRight)
+}
+
+class GetJsonObjectEvaluator(path: Expression) extends Serializable {
+ import com.fasterxml.jackson.core.JsonToken._
+ import PathInstruction._
+ import SharedFactory._
+ import WriteStyle._
+
+ @transient
+ private lazy val parsedPath = parsePath(path.eval().asInstanceOf[UTF8String])
Review Comment:
Let's put this logic `path.eval().asInstanceOf[UTF8String]` outside.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]