You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by ta...@apache.org on 2026/07/01 11:30:46 UTC
(gluten) 15/38: [GLUTEN][CORE] Share array-to-string cast rewrite rule
This is an automated email from the ASF dual-hosted git repository.
taiyang-li pushed a commit to branch fake_add_bolt_backend
in repository https://gitbox.apache.org/repos/asf/gluten.git
commit 50bd8f717f0a393830140a32b66d59d102eb6461
Author: liyang.127 <li...@bytedance.com>
AuthorDate: Mon Jun 29 21:07:21 2026 +0800
[GLUTEN][CORE] Share array-to-string cast rewrite rule
Move the reusable cast(array as string) rewrite logic into backends-core and keep the Velox rule as a thin config-gated wrapper so later backends can reuse the same optimizer rule body.
---
.../extension/SharedRewriteCastFromArrayRule.scala | 15 ++--------
.../gluten/extension/RewriteCastFromArray.scala | 33 ++--------------------
2 files changed, 4 insertions(+), 44 deletions(-)
diff --git a/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala b/backends-core/src/main/scala/org/apache/gluten/extension/SharedRewriteCastFromArrayRule.scala
similarity index 78%
copy from backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala
copy to backends-core/src/main/scala/org/apache/gluten/extension/SharedRewriteCastFromArrayRule.scala
index 4821418852..e3cd00770a 100644
--- a/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala
+++ b/backends-core/src/main/scala/org/apache/gluten/extension/SharedRewriteCastFromArrayRule.scala
@@ -16,9 +16,6 @@
*/
package org.apache.gluten.extension
-import org.apache.gluten.config.VeloxConfig
-
-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
@@ -26,17 +23,9 @@ import org.apache.spark.sql.catalyst.trees.TreePattern.CAST
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{ArrayType, StringType}
-/**
- * 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] {
+class SharedRewriteCastFromArrayRule(enabled: Boolean) extends Rule[LogicalPlan] {
override def apply(plan: LogicalPlan): LogicalPlan = {
- if (
- !VeloxConfig.get.enableRewriteCastArrayToString ||
- SQLConf.get.getConf(SQLConf.LEGACY_COMPLEX_TYPES_TO_STRING)
- ) {
+ if (!enabled || SQLConf.get.getConf(SQLConf.LEGACY_COMPLEX_TYPES_TO_STRING)) {
return plan
}
plan.transformUpWithPruning(_.containsPattern(CAST)) {
diff --git a/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala b/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala
index 4821418852..f0251abf02 100644
--- a/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala
+++ b/backends-velox/src/main/scala/org/apache/gluten/extension/RewriteCastFromArray.scala
@@ -19,40 +19,11 @@ package org.apache.gluten.extension
import org.apache.gluten.config.VeloxConfig
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, StringType}
/**
* 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 (
- !VeloxConfig.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(child, StringType, timeZoneId, evalMode)
- if child.dataType.isInstanceOf[ArrayType] =>
- val joinChild = child.dataType.asInstanceOf[ArrayType].elementType match {
- case StringType =>
- child
- case _ =>
- Cast(child, ArrayType(StringType), timeZoneId, evalMode)
- }
- val arrayJoin = ArrayJoin(joinChild, Literal(", "), Some(Literal("null")))
- Concat(Seq(Literal("["), arrayJoin, Literal("]")))
- }
- }
- }
-}
+case class RewriteCastFromArray(spark: SparkSession)
+ extends SharedRewriteCastFromArrayRule(VeloxConfig.get.enableRewriteCastArrayToString)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org