vamshikrishnakyatham commented on code in PR #13736:
URL: https://github.com/apache/hudi/pull/13736#discussion_r2292434892


##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedureFilterUtils.scala:
##########
@@ -0,0 +1,531 @@
+/*
+ * 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.spark.sql.hudi.command.procedures
+
+import org.apache.spark.sql.{Row, SparkSession}
+import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
+import org.apache.spark.sql.catalyst.expressions.{Expression, 
GenericInternalRow}
+import org.apache.spark.sql.catalyst.util.DateTimeUtils
+import org.apache.spark.sql.types.{DataType, StructType}
+import org.apache.spark.unsafe.types.UTF8String
+
+import scala.collection.JavaConverters._
+import scala.util.{Failure, Success, Try}
+
+/**
+ * Utility object for filtering procedure results using SQL expressions.
+ *
+ * Supports all Spark SQL data types including:
+ * - Primitive types: Boolean, Byte, Short, Int, Long, Float, Double, String, 
Binary
+ * - Date/Time types: Date, Timestamp, Instant, LocalDate, LocalDateTime
+ * - Decimal types: BigDecimal with precision/scale
+ * - Complex types: Array, Map, Struct (Row)
+ * - Nested combinations of all above types
+ */
+object HoodieProcedureFilterUtils {
+
+  /**
+   * Evaluates a SQL filter expression against a sequence of rows.
+   *
+   * @param rows             The rows to filter
+   * @param filterExpression SQL expression string
+   * @param schema           The schema of the rows
+   * @param sparkSession     Spark session for expression parsing
+   * @return Filtered rows that match the expression
+   */
+  def evaluateFilter(
+                      rows: Seq[Row],
+                      filterExpression: String,
+                      schema: StructType,
+                      sparkSession: SparkSession
+                    ): Seq[Row] = {
+
+    if (filterExpression == null || filterExpression.trim.isEmpty) {
+      rows
+    } else {
+      Try {
+        val parsedExpr = 
sparkSession.sessionState.sqlParser.parseExpression(filterExpression)
+
+        rows.filter { row =>
+          evaluateExpressionOnRow(parsedExpr, row, schema)
+        }
+      } match {
+        case Success(filteredRows) => filteredRows
+        case Failure(exception) =>
+          throw new IllegalArgumentException(
+            s"Failed to parse or evaluate filter expression 
'$filterExpression': ${exception.getMessage}",
+            exception
+          )
+      }
+    }
+  }
+
+  private def evaluateExpressionOnRow(
+                                       expression: Expression,
+                                       row: Row,
+                                       schema: StructType
+                                     ): Boolean = {
+
+    val internalRow = convertRowToInternalRow(row, schema)
+
+    Try {
+      // First pass: bind attributes
+      val attributeBound = expression.transform {
+        case attr: org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute 
=>
+          try {
+            val fieldIndex = schema.fieldIndex(attr.name)
+            val field = schema.fields(fieldIndex)
+            
org.apache.spark.sql.catalyst.expressions.BoundReference(fieldIndex, 
field.dataType, field.nullable)
+          } catch {
+            case _: IllegalArgumentException => attr
+          }
+      }
+
+      // Second pass: resolve functions
+      val functionResolved = attributeBound.transform {
+        case unresolvedFunc: 
org.apache.spark.sql.catalyst.analysis.UnresolvedFunction =>
+          unresolvedFunc.nameParts.head.toLowerCase match {
+            case "upper" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Upper(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "lower" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Lower(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "length" | "len" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Length(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "trim" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.StringTrim(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "ltrim" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.StringTrimLeft(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "rtrim" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.StringTrimRight(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "substring" | "substr" =>
+              if (unresolvedFunc.arguments.length == 3) {
+                org.apache.spark.sql.catalyst.expressions.Substring(
+                  unresolvedFunc.arguments(0),
+                  unresolvedFunc.arguments(1),
+                  unresolvedFunc.arguments(2)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "abs" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Abs(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "round" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Round(unresolvedFunc.arguments.head, 
org.apache.spark.sql.catalyst.expressions.Literal(0))
+              } else if (unresolvedFunc.arguments.length == 2) {
+                
org.apache.spark.sql.catalyst.expressions.Round(unresolvedFunc.arguments(0), 
unresolvedFunc.arguments(1))
+              } else {
+                unresolvedFunc
+              }
+            case "ceil" | "ceiling" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Ceil(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "floor" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Floor(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "year" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Year(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "month" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Month(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "day" | "dayofmonth" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.DayOfMonth(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "hour" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Hour(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "size" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Size(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "map_keys" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.MapKeys(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "map_values" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.MapValues(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "array_contains" =>
+              if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.ArrayContains(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "array_size" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.Size(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "sort_array" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                org.apache.spark.sql.catalyst.expressions.SortArray(
+                  unresolvedFunc.arguments.head,
+                  org.apache.spark.sql.catalyst.expressions.Literal(true)
+                )
+              } else if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.SortArray(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "like" =>
+              if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.Like(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1),
+                  '\\'
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "rlike" | "regexp_like" =>
+              if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.RLike(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "regexp_extract" =>
+              if (unresolvedFunc.arguments.length == 3) {
+                org.apache.spark.sql.catalyst.expressions.RegExpExtract(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1),
+                  unresolvedFunc.arguments(2)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "date_format" =>
+              if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.DateFormatClass(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "datediff" =>
+              if (unresolvedFunc.arguments.length == 2) {
+                org.apache.spark.sql.catalyst.expressions.DateDiff(
+                  unresolvedFunc.arguments.head,
+                  unresolvedFunc.arguments(1)
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "isnull" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.IsNull(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "isnotnull" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                
org.apache.spark.sql.catalyst.expressions.IsNotNull(unresolvedFunc.arguments.head)
+              } else {
+                unresolvedFunc
+              }
+            case "coalesce" =>
+              if (unresolvedFunc.arguments.nonEmpty) {
+                
org.apache.spark.sql.catalyst.expressions.Coalesce(unresolvedFunc.arguments)
+              } else {
+                unresolvedFunc
+              }
+            case "string" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                org.apache.spark.sql.catalyst.expressions.Cast(
+                  unresolvedFunc.arguments.head,
+                  org.apache.spark.sql.types.StringType
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "int" | "integer" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                org.apache.spark.sql.catalyst.expressions.Cast(
+                  unresolvedFunc.arguments.head,
+                  org.apache.spark.sql.types.IntegerType
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "long" | "bigint" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                org.apache.spark.sql.catalyst.expressions.Cast(
+                  unresolvedFunc.arguments.head,
+                  org.apache.spark.sql.types.LongType
+                )
+              } else {
+                unresolvedFunc
+              }
+            case "double" =>
+              if (unresolvedFunc.arguments.length == 1) {
+                org.apache.spark.sql.catalyst.expressions.Cast(
+                  unresolvedFunc.arguments.head,
+                  org.apache.spark.sql.types.DoubleType
+                )
+              } else {
+                unresolvedFunc
+              }
+            case _ => unresolvedFunc
+          }
+      }
+
+      // Third pass: handle type coercion for numeric comparisons
+      val boundExpr = functionResolved.transformUp {
+        case eq: org.apache.spark.sql.catalyst.expressions.EqualTo =>

Review Comment:
   fixed it



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

Reply via email to