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

soumyakantidas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new e876bb40c6d HIVE-29457: HiveSortExchangePullUpConstantsRule doesn't 
remove constant column from distribution keys (#6316)
e876bb40c6d is described below

commit e876bb40c6d0d245ca1fd7062d818230eef0d8fe
Author: Soumyakanti Das <[email protected]>
AuthorDate: Mon Feb 23 13:59:03 2026 -0800

    HIVE-29457: HiveSortExchangePullUpConstantsRule doesn't remove constant 
column from distribution keys (#6316)
---
 .../calcite/rules/HiveSortPullUpConstantsRule.java | 18 +++++++++++++-
 .../distribution_key_constant_value.q              |  6 +++++
 .../llap/distribution_key_constant_value.q.out     | 28 ++++++++++++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)

diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortPullUpConstantsRule.java
 
b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortPullUpConstantsRule.java
index 51f53cd0ead..99b094efa94 100644
--- 
a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortPullUpConstantsRule.java
+++ 
b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSortPullUpConstantsRule.java
@@ -42,6 +42,7 @@
 import org.apache.calcite.tools.RelBuilder;
 import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.mapping.Mappings;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelDistribution;
 import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
 import 
org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortExchange;
 import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit;
@@ -94,9 +95,24 @@ private HiveSortExchangePullUpConstantsRule() {
     @Override
     protected void buildSort(RelBuilder relBuilder, HiveSortExchange sortNode, 
Mappings.TargetMapping mapping) {
       List<RelFieldCollation> fieldCollations = 
applyToFieldCollations(sortNode.getCollation(), mapping);
-      RelDistribution distribution = sortNode.getDistribution().apply(mapping);
+      RelDistribution distribution = 
applyToDistribution(sortNode.getDistribution(), mapping);
       relBuilder.sortExchange(distribution, RelCollations.of(fieldCollations));
     }
+    
+    private RelDistribution applyToDistribution(
+        RelDistribution distribution, Mappings.TargetMapping mapping) {
+      List<Integer> newKeys = new ArrayList<>();
+      for (int key : distribution.getKeys()) {
+        final int target = mapping.getTargetOpt(key);
+        if (target < 0) {
+          // It is a constant, we can ignore it
+          continue;
+        }
+        newKeys.add(target);
+      }
+      
+      return new HiveRelDistribution(distribution.getType(), newKeys);
+    }
   }
 
 
diff --git 
a/ql/src/test/queries/clientpositive/distribution_key_constant_value.q 
b/ql/src/test/queries/clientpositive/distribution_key_constant_value.q
new file mode 100644
index 00000000000..5b8823bbf6c
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/distribution_key_constant_value.q
@@ -0,0 +1,6 @@
+CREATE TABLE test (col1 string, col2 string);
+
+EXPLAIN CBO
+SELECT col1, col2 FROM test
+WHERE col2 = 'a'
+DISTRIBUTE BY col1, col2; 
diff --git 
a/ql/src/test/results/clientpositive/llap/distribution_key_constant_value.q.out 
b/ql/src/test/results/clientpositive/llap/distribution_key_constant_value.q.out
new file mode 100644
index 00000000000..3c078bedea2
--- /dev/null
+++ 
b/ql/src/test/results/clientpositive/llap/distribution_key_constant_value.q.out
@@ -0,0 +1,28 @@
+PREHOOK: query: CREATE TABLE test (col1 string, col2 string)
+PREHOOK: type: CREATETABLE
+PREHOOK: Output: database:default
+PREHOOK: Output: default@test
+POSTHOOK: query: CREATE TABLE test (col1 string, col2 string)
+POSTHOOK: type: CREATETABLE
+POSTHOOK: Output: database:default
+POSTHOOK: Output: default@test
+PREHOOK: query: EXPLAIN CBO
+SELECT col1, col2 FROM test
+WHERE col2 = 'a'
+DISTRIBUTE BY col1, col2
+PREHOOK: type: QUERY
+PREHOOK: Input: default@test
+#### A masked pattern was here ####
+POSTHOOK: query: EXPLAIN CBO
+SELECT col1, col2 FROM test
+WHERE col2 = 'a'
+DISTRIBUTE BY col1, col2
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@test
+#### A masked pattern was here ####
+CBO PLAN:
+HiveSortExchange(distribution=[hash[0]], collation=[[]])
+  HiveProject(col1=[$0], col2=[CAST(_UTF-16LE'a':VARCHAR(2147483647) CHARACTER 
SET "UTF-16LE"):VARCHAR(2147483647) CHARACTER SET "UTF-16LE"])
+    HiveFilter(condition=[=($1, _UTF-16LE'a')])
+      HiveTableScan(table=[[default, test]], table:alias=[test])
+

Reply via email to