PHILO-HE commented on code in PR #10300:
URL: 
https://github.com/apache/incubator-gluten/pull/10300#discussion_r2250538758


##########
backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala:
##########
@@ -1175,6 +1175,25 @@ abstract class ScalarFunctionsValidateSuite extends 
FunctionsValidateSuite {
     runQueryAndCompare("select cast(array(timestamp'2024-01-01 12:00:00') AS 
array<string>)") {
       checkGlutenOperatorMatch[ProjectExecTransformer]
     }
+    // Cast Array as String
+    runQueryAndCompare("select cast(array(1, 2) AS string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }
+    runQueryAndCompare("select cast(array(1L, null) AS string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }
+    runQueryAndCompare("select cast(array(1.1d, null) AS string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }
+    runQueryAndCompare("select cast(array(false, null) AS string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }
+    runQueryAndCompare("select cast(array(date'2024-01-01') AS string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }
+    runQueryAndCompare("select cast(array(timestamp'2024-01-01 12:00:00') AS 
string)") {
+      checkGlutenOperatorMatch[ProjectExecTransformer]
+    }

Review Comment:
   Please add a test for empty array.



##########
docs/Configuration.md:
##########
@@ -156,6 +156,7 @@ nav_order: 15
 | spark.gluten.sql.orc.charType.scan.fallback.enabled                | true    
          | Force fallback for orc char type scan.                              
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                       |
 | spark.gluten.sql.parquet.maxmin.index                              | false   
          | Enable row group max min index for parquet file scan                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                       |
 | spark.gluten.sql.removeNativeWriteFilesSortAndProject              | true    
          | When true, Gluten will remove the vanilla Spark V1Writes added sort 
and project for velox backend.                                                  
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                       |
+| spark.gluten.sql.rewrite.castArrayToString                         | true    
          | When true, rewrite `cast(array as String)` to `concat('[', 
array_join(array, ', ', null), ']')` to make velox support offload.             
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                |

Review Comment:
   ** to allow offloading to Velox.



##########
backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.gluten.extension
+
+import org.apache.gluten.config.GlutenConfig
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{ArrayJoin, Cast, Concat, 
Literal}
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.CAST
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{ArrayType, BooleanType, DateType, 
DoubleType, IntegerType, LongType, StringType, TimestampType}
+
+/**
+ * Velox does not support cast Array to String. Before velox support, 
temporarily add this rule to
+ * replace `cast(array as String)` with `concat('[', array_join(array, ', ', 
'null'), ']')` to
+ * support offload.
+ */
+case class RewriteCastFromArray(spark: SparkSession) extends Rule[LogicalPlan] 
{
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    if (
+      !GlutenConfig.get.enableRewriteCastArrayToString ||
+      SQLConf.get.getConf(SQLConf.LEGACY_COMPLEX_TYPES_TO_STRING)
+    ) {
+      return plan
+    }
+    plan.transformUpWithPruning(_.containsPattern(CAST)) {
+      case p =>
+        p.transformExpressionsUpWithPruning(_.containsPattern(CAST)) {
+          case cast @ Cast(child, StringType, timeZoneId, evalMode)
+              if child.dataType.isInstanceOf[ArrayType] =>
+            child.dataType match {

Review Comment:
   Using array's element to match for brevity.



##########
gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala:
##########
@@ -1418,6 +1421,14 @@ object GlutenConfig {
       .booleanConf
       .createWithDefault(true)
 
+  val ENABLE_REWRITE_CAST_ARRAY_TO_STRING =
+    buildConf("spark.gluten.sql.rewrite.castArrayToString")
+      .internal()
+      .doc("When true, rewrite `cast(array as String)` to" +
+        " `concat('[', array_join(array, ', ', null), ']')` to make velox 
support offload.")

Review Comment:
   ditto.



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