cloud-fan commented on code in PR #45765:
URL: https://github.com/apache/spark/pull/45765#discussion_r1555471908


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala:
##########
@@ -501,55 +503,156 @@ case class JsonTuple(children: Seq[Expression])
       return nullRow
     }
 
+    val fieldNames = if (constantFields == fieldExpressions.length) {
+      // typically the user will provide the field names as foldable 
expressions
+      // so we can use the cached copy
+      foldableFieldNames.map(_.orNull)
+    } else if (constantFields == 0) {
+      // none are foldable so all field names need to be evaluated from the 
input row
+      fieldExpressions.zipWithIndex.map {
+        case (expr, index) =>
+          Option(expr.eval(input)).map {
+            path =>
+              JsonPathIndex(index, path.asInstanceOf[UTF8String].toString)
+          }.orNull
+      }
+    } else {
+      // if there is a mix of constant and non-constant expressions
+      // prefer the cached copy when available
+      foldableFieldNames.zip(fieldExpressions).zipWithIndex.map {
+        case ((null, expr), index) =>
+          Option(expr.eval(input)).map {
+            path =>
+              JsonPathIndex(index, path.asInstanceOf[UTF8String].toString)
+          }.orNull
+        case ((fieldName, _), _) => fieldName.orNull
+      }
+    }
+
+    val evaluator = new JsonTupleEvaluator(
+      json,
+      fieldNames
+        .filter{jsonPathIndex => jsonPathIndex != null && jsonPathIndex.path 
!= null }
+        .asJava,
+      fieldExpressions.length)
+    evaluator.evaluate()
+  }
+
+  override protected def withNewChildrenInternal(newChildren: 
IndexedSeq[Expression]): JsonTuple =
+    copy(children = newChildren)
+
+  override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): 
ExprCode = {

Review Comment:
   Haven't looked into it yet, but is it possible to make codegen simpler and 
write most of the code in Scala?



-- 
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-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to