yaooqinn commented on code in PR #13796:
URL: https://github.com/apache/gluten/pull/13796#discussion_r4735400039


##########
backends-velox/src/main/scala/org/apache/gluten/expression/ExpressionTransformer.scala:
##########
@@ -41,6 +41,30 @@ case class VeloxAliasTransformer(
   }
 }
 
+case class VeloxToJsonInputTransformer(child: ExpressionTransformer)
+  extends UnaryExpressionTransformer {
+  override def substraitExprName: String = "cast"
+  override def original: Expression = child.original
+
+  override def doTransform(context: SubstraitContext): ExpressionNode = {
+    // Spark's struct field names are JSON keys, not case-insensitive column 
references.
+    // Restore them by position without changing the field types or the rest 
of the plan.
+    val inputType = ConverterUtils.getTypeNode(dataType, nullable, 
preserveStructFieldNames = true)
+    ExpressionBuilder.makeCast(inputType, child.doTransform(context), 
CastNode.CastMode.LEGACY)

Review Comment:
   Since this is a row-to-row cast, native validation runs `isAllowedCast` on 
each child pair 
(`cpp/velox/substrait/SubstraitToVeloxPlanValidator.cc:402-417`), and for 
same-type children it rejects DATE to DATE (`:324`), TIMESTAMP to TIMESTAMP 
(`:329-345`), TIMESTAMP_NTZ to TIMESTAMP_NTZ (`:372-377`) and VARBINARY to 
VARBINARY (`:425`). So when a field needs renaming, something like 
`to_json(struct(eventId, eventTime))` with a timestamp field would still fall 
back. Would it be worth letting equal-type children pass in the row branch of 
`isAllowedCast` (Velox's `CastExpr::applyRow` reuses a child that already has 
the target type), or at least adding a test that pins the fallback?
   



##########
backends-velox/src/test/scala/org/apache/gluten/functions/JsonFunctionsValidateSuite.scala:
##########
@@ -425,12 +425,35 @@ class JsonFunctionsValidateSuite extends 
FunctionsValidateSuite {
       }
 
       runQueryAndCompare("select to_json(e) from t") {
+        checkGlutenPlan[ProjectExecTransformer]
+      }
+
+      runQueryAndCompare("select to_json(array(named_struct('aA', a))) from 
t") {
+        checkGlutenPlan[ProjectExecTransformer]
+      }
+
+      runQueryAndCompare(
+        "select to_json(named_struct('outer', named_struct('UserId', a, 
'userid', b))) from t") {
+        checkGlutenPlan[ProjectExecTransformer]
+      }
+
+      runQueryAndCompare("select to_json(map('key', named_struct('UserId', 
a))) from t") {
+        checkGlutenPlan[ProjectExecTransformer]
+      }
+
+      runQueryAndCompare("select to_json(e, map('ignoreNullFields', 'false')) 
from t") {
         checkSparkPlan[ProjectExec]
       }
 
-      runQueryAndCompare("select to_json(Array(named_struct('aA', a))) from 
t") {
+      runQueryAndCompare("""select to_json(named_struct('A"B', a)) from t""") {

Review Comment:
   Nit: `'A"B'` already falls back on main through the uppercase rule, so this 
case does not show the bug that the guard also fixes on main. A lowercase name 
like `'a"b'` takes the no-rename path: main offloads it and Velox writes the 
field name unescaped (`ToJson.h`), giving `{"a"b":1}` where Spark gives 
`{"a\"b":1}`. Could you add it next to this one?
   



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