This is an automated email from the ASF dual-hosted git repository.

LuciferYang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new 95dc804f3d [VL] Push literal-on-left comparisons into subfield filters 
(#13552)
95dc804f3d is described below

commit 95dc804f3d5652ea69f469a30c4902d24c4b62c4
Author: YangJie <[email protected]>
AuthorDate: Tue Nov 10 21:57:39 2026 -0500

    [VL] Push literal-on-left comparisons into subfield filters (#13552)
    
    ## What changes are proposed in this pull request?
    
    `SparkExprToSubfieldFilterParser::leafCallToSubfieldFilter` only recognized 
a column on the left side of a comparison, so a predicate such as `100 > id` 
was never turned into a subfield filter and could not be used for row group 
pruning. Spark does not rewrite it, and Gluten passes the Catalyst filter to 
the native scan as is.
    
    When the left operand is not a column and the right one is, the comparison 
is now rewritten with the operands swapped and the operator mirrored (`100 > 
id` becomes `id < 100`) and handled as before. This covers `=`, `<`, `<=`, `>` 
and `>=`. Results do not change, since the predicate was already evaluated as a 
remaining filter.
    
    Fixes #13546.
    
    ## How was this patch tested?
    
    Added `Literal-on-left comparison prunes Parquet row groups` to 
`VeloxMetricsSuite`. It writes a sorted Parquet file with 10 row groups and 
checks that five literal-on-left predicates skip and read the same row groups 
as their mirrored forms. Before this change, `100 > id` read all 10 row groups 
while `id < 100` skipped 9.
    
    ## Was this patch authored or co-authored using generative AI tooling?
    
    Yes. Generated-by: Claude Opus.
---
 .../gluten/execution/VeloxMetricsSuite.scala       | 44 ++++++++++++++++++++++
 .../functions/SparkExprToSubfieldFilterParser.cc   | 17 +++++++++
 2 files changed, 61 insertions(+)

diff --git 
a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxMetricsSuite.scala
 
b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxMetricsSuite.scala
index 8c85836240..1e8ce70d89 100644
--- 
a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxMetricsSuite.scala
+++ 
b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxMetricsSuite.scala
@@ -227,6 +227,50 @@ class VeloxMetricsSuite extends 
VeloxWholeStageTransformerSuite with AdaptiveSpa
     }
   }
 
+  test("Literal-on-left comparison prunes Parquet row groups") {
+    withTempPath {
+      dir =>
+        withTempView("sorted_ids") {
+          spark
+            .range(0, 10000, 1, 1)
+            .write
+            .format("parquet")
+            .option("parquet.block.rows", "1000")
+            .save(dir.getCanonicalPath)
+          
spark.read.parquet(dir.getCanonicalPath).createOrReplaceTempView("sorted_ids")
+          // The file has 10 row groups. Returns (skipped, processed) row 
groups of the scan.
+          def strides(query: String): (Long, Long) = {
+            var result = (0L, 0L)
+            runQueryAndCompare(query) {
+              df =>
+                val scan = find(df.queryExecution.executedPlan) {
+                  case _: FileSourceScanExecTransformer => true
+                  case _ => false
+                }
+                assert(scan.isDefined)
+                val metrics = scan.get.metrics
+                result = (metrics("skippedStrides").value, 
metrics("processedStrides").value)
+            }
+            result
+          }
+          // Each literal-on-left predicate must skip the same row groups as 
its mirrored form.
+          Seq(
+            "100 > id" -> "id < 100",
+            "100 >= id" -> "id <= 100",
+            "9900 < id" -> "id > 9900",
+            "9900 <= id" -> "id >= 9900",
+            "5000 = id" -> "id = 5000"
+          ).foreach {
+            case (left, right) =>
+              val expected = strides(s"SELECT id FROM sorted_ids WHERE $right")
+              val actual = strides(s"SELECT id FROM sorted_ids WHERE $left")
+              assert(expected._1 > 0, s"$right should prune row groups: 
$expected")
+              assert(actual == expected, s"$left vs $right")
+          }
+        }
+    }
+  }
+
   test("Metrics of noop filter's children") {
     runQueryAndCompare("SELECT c1, c2 FROM metrics_t1 where c1 < 50") {
       df =>
diff --git a/cpp/velox/operators/functions/SparkExprToSubfieldFilterParser.cc 
b/cpp/velox/operators/functions/SparkExprToSubfieldFilterParser.cc
index 31d44211cd..49e70c249b 100644
--- a/cpp/velox/operators/functions/SparkExprToSubfieldFilterParser.cc
+++ b/cpp/velox/operators/functions/SparkExprToSubfieldFilterParser.cc
@@ -16,6 +16,8 @@
  */
 #include "operators/functions/SparkExprToSubfieldFilterParser.h"
 
+#include <unordered_map>
+
 #include "utils/Exception.h"
 #include "velox/common/base/BloomFilter.h"
 #include "velox/expression/Expr.h"
@@ -117,6 +119,21 @@ SparkExprToSubfieldFilterParser::leafCallToSubfieldFilter(
   const auto* leftSide = call.inputs()[0].get();
 
   common::Subfield subfield;
+  // Spark keeps a literal-on-left comparison such as `5 < col` as written. 
Swap the operands
+  // and mirror the operator so it is handled like `col > 5`.
+  static const std::unordered_map<std::string, std::string> 
kMirroredComparisons = {
+      {"equalto", "equalto"},
+      {"lessthan", "greaterthan"},
+      {"lessthanorequal", "greaterthanorequal"},
+      {"greaterthan", "lessthan"},
+      {"greaterthanorequal", "lessthanorequal"}};
+  if (call.inputs().size() == 2 && !toSubfield(leftSide, subfield) && 
toSubfield(call.inputs()[1].get(), subfield)) {
+    auto it = kMirroredComparisons.find(call.name());
+    if (it != kMirroredComparisons.end()) {
+      const core::CallTypedExpr swapped(call.type(), {call.inputs()[1], 
call.inputs()[0]}, it->second);
+      return leafCallToSubfieldFilter(swapped, evaluator, negated);
+    }
+  }
   if (call.name() == "equalto") {
     if (toSubfield(leftSide, subfield)) {
       auto filter =


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to