ganeshashree commented on code in PR #58450:
URL: https://github.com/apache/spark/pull/58450#discussion_r3934080304


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -4160,23 +4160,46 @@ class AstBuilder extends DataTypeAstBuilder
       (JsonValueBehavior.Default, Some(expression(d.defaultExpr)))
   }
 
+  // A clause-free JSON_ARRAY / JSON_QUERY that is a top-level JSON_ARRAY 
element stays on the
+  // direct path. Those expressions emit JSON text implicitly, and the parent 
JSON_ARRAY must see
+  // that lexical fact before analyzer rewrites can wrap the child in a Cast.
+  private def isTopLevelJsonArrayElement(ctx: RuleContext): Boolean = {
+    @scala.annotation.tailrec
+    def loop(parent: RuleContext): Boolean = parent match {
+      case null => false
+      case _: JsonArrayValueContext => true
+      case _: ExpressionContext | _: ValueExpressionDefaultContext |
+          _: ParenthesizedExpressionContext | _: CollateContext =>
+        loop(parent.getParent)
+      case p: PredicatedContext if p.predicate() == null =>
+        loop(parent.getParent)
+      case _ => false
+    }
+    loop(ctx.getParent)
+  }
+
   /**
    * Create a [[JsonValue]] expression for the SQL:2016 `JSON_VALUE` scalar 
function. The `ON EMPTY`
    * / `ON ERROR` clauses default to `NULL` when absent, per the standard.
    */
   override def visitJsonValue(ctx: JsonValueContext): Expression = 
withOrigin(ctx) {
     val jsonExpr = expression(ctx.jsonExpr)
     val path = string(visitStringLit(ctx.path))
-    // Default RETURNING type is STRING. Normalize CHAR/VARCHAR to STRING for 
the cast, as the value
-    // is produced by a `Cast` to the declared type (a raw CHAR/VARCHAR target 
has no encoder).
-    val returning = Option(ctx.returning)
-      .map(dt => 
CharVarcharUtils.replaceCharVarcharWithStringForCast(typedVisit[DataType](dt)))
-      .getOrElse(StringType)
-    val (onEmpty, emptyDefault) = Option(ctx.emptyBehavior)
-      .map(buildJsonValueBehavior).getOrElse((JsonValueBehavior.Null, None))
-    val (onError, errorDefault) = Option(ctx.errorBehavior)
-      .map(buildJsonValueBehavior).getOrElse((JsonValueBehavior.Null, None))
-    JsonValue(jsonExpr, path, returning, onEmpty, onError, emptyDefault, 
errorDefault)
+    if (ctx.returning == null && ctx.emptyBehavior == null && 
ctx.errorBehavior == null) {

Review Comment:
   The scope of this PR is intentionally limited to shadowing: routing 
clause-free SQL/JSON constructor calls through routine resolution so a 
same-named routine can shadow the built-in. Making canonical SQL round-trip to 
the same builtin owner requires changing the existing rendering/clause behavior 
of the JSON functions (for example, emitting explicit default clauses, which 
alters every Expression.sql string for these constructors). That expands the 
existing behavior of the JSON functions beyond shadowing, so I am deferring it, 
along with the nested/qualified splicing, to the SPARK-59243 follow-up.
   



-- 
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]

Reply via email to