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

krisztiankasa 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 08067725bc6 HIVE-28917: NPE in merge statement when checking 
nullability of joini… (#5781)
08067725bc6 is described below

commit 08067725bc6e8810579324736a0aac453c06bf7b
Author: Soumyakanti Das <[email protected]>
AuthorDate: Thu May 15 01:43:35 2025 -0700

    HIVE-28917: NPE in merge statement when checking nullability of joini… 
(#5781)
---
 .../merge_with_null_check_on_joining_col.q         |  39 +++
 .../merge_with_null_check_on_joining_col.q.out     | 306 +++++++++++++++++++++
 ql/src/java/org/apache/hadoop/hive/ql/Context.java |  12 +-
 3 files changed, 355 insertions(+), 2 deletions(-)

diff --git 
a/iceberg/iceberg-handler/src/test/queries/positive/merge_with_null_check_on_joining_col.q
 
b/iceberg/iceberg-handler/src/test/queries/positive/merge_with_null_check_on_joining_col.q
new file mode 100644
index 00000000000..b32670608fe
--- /dev/null
+++ 
b/iceberg/iceberg-handler/src/test/queries/positive/merge_with_null_check_on_joining_col.q
@@ -0,0 +1,39 @@
+
+create table target(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write');
+create table source(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write');
+
+-- empty plan as joining column cannot be null for matched clause
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete;
+
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then update set b = t.b + 10;
+
+-- non empty plans as condition for all branches are not false
+-- matched and <joining col> is null is always false
+-- not matched and <joining col> is [not] null is not necessarily always false
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete
+when matched and t.a > 10 then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c);
+
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a is null then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c);
+
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is null then insert values (s.a, s.b, s.c);
+
+explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is not null then insert values (s.a, s.b, s.c);
diff --git 
a/iceberg/iceberg-handler/src/test/results/positive/merge_with_null_check_on_joining_col.q.out
 
b/iceberg/iceberg-handler/src/test/results/positive/merge_with_null_check_on_joining_col.q.out
new file mode 100644
index 00000000000..521eb4e26f2
--- /dev/null
+++ 
b/iceberg/iceberg-handler/src/test/results/positive/merge_with_null_check_on_joining_col.q.out
@@ -0,0 +1,306 @@
+PREHOOK: query: create table target(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write')
+PREHOOK: type: CREATETABLE
+PREHOOK: Output: database:default
+PREHOOK: Output: default@target
+POSTHOOK: query: create table target(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write')
+POSTHOOK: type: CREATETABLE
+POSTHOOK: Output: database:default
+POSTHOOK: Output: default@target
+PREHOOK: query: create table source(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write')
+PREHOOK: type: CREATETABLE
+PREHOOK: Output: database:default
+PREHOOK: Output: default@source
+POSTHOOK: query: create table source(a int, b int, c int) stored by iceberg 
tblproperties('format-version'='2', 'write.merge.mode'='copy-on-write')
+POSTHOOK: type: CREATETABLE
+POSTHOOK: Output: database:default
+POSTHOOK: Output: default@source
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveValues(tuples=[[]])
+
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then update set b = t.b + 10
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then update set b = t.b + 10
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveValues(tuples=[[]])
+
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete
+when matched and t.a > 10 then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c)
+PREHOOK: type: QUERY
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a is null then delete
+when matched and t.a > 10 then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveUnion(all=[true])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[+($6, 100)], t__c=[$7])
+    HiveJoin(condition=[AND(=($5, $8), =($6, $9))], joinType=[inner], 
algorithm=[none], cost=[not available])
+      HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1], t__c=[$2])
+        HiveFilter(condition=[AND(>($0, 10), IS NOT NULL($1))])
+          HiveTableScan(table=[[default, target]], table:alias=[target])
+      HiveProject(a=[$0], b=[$1])
+        HiveFilter(condition=[AND(>($0, 10), IS NOT NULL($1))])
+          HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
a=[$7], b=[$8], c=[$9])
+    HiveFilter(condition=[AND(IS NULL($5), IS NULL($6))])
+      HiveJoin(condition=[AND(=($5, $7), =($6, $8))], joinType=[right], 
algorithm=[none], cost=[not available])
+        HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1])
+          HiveFilter(condition=[AND(>($0, 20), IS NOT NULL($1))])
+            HiveTableScan(table=[[default, target]], table:alias=[target])
+        HiveProject(a=[$0], b=[$1], c=[$2])
+          HiveFilter(condition=[>($0, 20)])
+            HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7])
+    HiveSemiJoin(condition=[=($2, $10)], joinType=[semi])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7], a=[$8], b=[$9])
+        HiveFilter(condition=[OR(IS NULL(OR(AND(=($5, $8), =($6, $9), IS 
NULL($5)), >($5, 10), AND(IS NULL($5), IS NULL($6)))), AND(OR(<>($5, $8), 
<>($6, $9), IS NOT NULL($5)), <=($5, 10), OR(IS NOT NULL($5), IS NOT 
NULL($6))))])
+          HiveJoin(condition=[AND(=($5, $8), =($6, $9))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1], t__c=[$2])
+              HiveFilter(condition=[IS NOT NULL($9)])
+                HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+      HiveProject(t__file__path=[$0])
+        HiveFilter(condition=[=($1, 1)])
+          HiveProject(t__file__path=[$0], row_number_window_0=[row_number() 
OVER (PARTITION BY $0 ORDER BY $0 NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING 
AND UNBOUNDED FOLLOWING)])
+            HiveJoin(condition=[AND(=($1, $3), =($2, $4))], joinType=[left], 
algorithm=[none], cost=[not available])
+              HiveProject(t__file__path=[$9], t__a=[$0], t__b=[$1])
+                HiveFilter(condition=[AND(>($0, 10), IS NOT NULL($9))])
+                  HiveTableScan(table=[[default, target]], 
table:alias=[target])
+              HiveProject(a=[$0], b=[$1])
+                HiveFilter(condition=[AND(>($0, 10), IS NOT NULL($1))])
+                  HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], _o__c3=[-1:BIGINT], t__partition__projection=[$3], 
t__a=[$4], t__b=[$5], t__c=[$6])
+    HiveFilter(condition=[=($7, 1)])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__partition__projection=[$3], t__a=[$4], t__b=[$5], 
t__c=[$6], row_number_window_0=[row_number() OVER (PARTITION BY $2 ORDER BY $2 
NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])
+        HiveJoin(condition=[AND(=($4, $7), =($5, $8))], joinType=[left], 
algorithm=[none], cost=[not available])
+          HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__partition__projection=[$11], t__a=[$0], t__b=[$1], 
t__c=[$2])
+            HiveFilter(condition=[>($0, 10)])
+              HiveTableScan(table=[[default, target]], table:alias=[target])
+          HiveProject(a=[$0], b=[$1])
+            HiveFilter(condition=[AND(>($0, 10), IS NOT NULL($1))])
+              HiveTableScan(table=[[default, source]], table:alias=[s])
+
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a is null then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c)
+PREHOOK: type: QUERY
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a is null then update set b = t.b + 100
+when not matched and s.a > 20 then insert values (s.a, s.b, s.c)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveUnion(all=[true])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
a=[$7], b=[$8], c=[$9])
+    HiveFilter(condition=[AND(IS NULL($5), IS NULL($6))])
+      HiveJoin(condition=[AND(=($5, $7), =($6, $8))], joinType=[right], 
algorithm=[none], cost=[not available])
+        HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1])
+          HiveFilter(condition=[AND(>($0, 20), IS NOT NULL($1))])
+            HiveTableScan(table=[[default, target]], table:alias=[target])
+        HiveProject(a=[$0], b=[$1], c=[$2])
+          HiveFilter(condition=[>($0, 20)])
+            HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7])
+    HiveSemiJoin(condition=[=($2, $10)], joinType=[semi])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7], a=[$8], b=[$9])
+        HiveFilter(condition=[OR(IS NULL(OR(IS NULL($5), AND(=($5, $8), =($6, 
$9), >($5, 10)), AND(IS NULL($5), IS NULL($6)))), AND(OR(<>($5, $8), <>($6, 
$9), <=($5, 10)), IS NOT NULL($5)))])
+          HiveJoin(condition=[AND(=($5, $8), =($6, $9))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1], t__c=[$2])
+              HiveFilter(condition=[IS NOT NULL($9)])
+                HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+      HiveProject(t__file__path=[$0])
+        HiveFilter(condition=[=($1, 1)])
+          HiveProject(t__file__path=[$0], row_number_window_0=[row_number() 
OVER (PARTITION BY $0 ORDER BY $0 NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING 
AND UNBOUNDED FOLLOWING)])
+            HiveFilter(condition=[OR(IS NULL($1), AND(=($1, $3), =($2, $4), 
>($1, 10)))])
+              HiveJoin(condition=[AND(=($1, $3), =($2, $4))], joinType=[left], 
algorithm=[none], cost=[not available])
+                HiveProject(t__file__path=[$9], t__a=[$0], t__b=[$1])
+                  HiveFilter(condition=[IS NOT NULL($9)])
+                    HiveTableScan(table=[[default, target]], 
table:alias=[target])
+                HiveProject(a=[$0], b=[$1])
+                  HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                    HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], _o__c3=[-1:BIGINT], t__partition__projection=[$3], 
t__a=[$4], t__b=[$5], t__c=[$6])
+    HiveFilter(condition=[=($7, 1)])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__partition__projection=[$3], t__a=[$4], t__b=[$5], 
t__c=[$6], row_number_window_0=[row_number() OVER (PARTITION BY $2 ORDER BY $2 
NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])
+        HiveFilter(condition=[OR(IS NULL($4), AND(=($4, $7), =($5, $8), >($4, 
10)))])
+          HiveJoin(condition=[AND(=($4, $7), =($5, $8))], joinType=[full], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__partition__projection=[$11], t__a=[$0], t__b=[$1], 
t__c=[$2])
+              HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveTableScan(table=[[default, source]], table:alias=[s])
+
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is null then insert values (s.a, s.b, s.c)
+PREHOOK: type: QUERY
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is null then insert values (s.a, s.b, s.c)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveUnion(all=[true])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
a=[null:INTEGER], b=[$8], c=[$9])
+    HiveFilter(condition=[AND(IS NULL($5), IS NULL($6), IS NULL($7))])
+      HiveJoin(condition=[AND(=($5, $7), =($6, $8))], joinType=[full], 
algorithm=[none], cost=[not available])
+        HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1])
+          HiveTableScan(table=[[default, target]], table:alias=[target])
+        HiveProject(a=[$0], b=[$1], c=[$2])
+          HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7])
+    HiveSemiJoin(condition=[=($2, $10)], joinType=[semi])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7], a=[$8], b=[$9])
+        HiveFilter(condition=[OR(IS NULL(OR(AND(=($5, $8), =($6, $9), >($5, 
10)), >($5, 20), AND(IS NULL($5), IS NULL($6)))), AND(OR(<>($5, $8), <>($6, 
$9), <=($5, 10)), <=($5, 20), OR(IS NOT NULL($5), IS NOT NULL($6))))])
+          HiveJoin(condition=[AND(=($5, $8), =($6, $9))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1], t__c=[$2])
+              HiveFilter(condition=[IS NOT NULL($9)])
+                HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+      HiveProject(t__file__path=[$0])
+        HiveFilter(condition=[=($1, 1)])
+          HiveProject(t__file__path=[$0], row_number_window_0=[row_number() 
OVER (PARTITION BY $0 ORDER BY $0 NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING 
AND UNBOUNDED FOLLOWING)])
+            HiveFilter(condition=[OR(>($1, 20), AND(=($1, $3), =($2, $4), 
>($1, 10)))])
+              HiveJoin(condition=[AND(=($1, $3), =($2, $4))], joinType=[left], 
algorithm=[none], cost=[not available])
+                HiveProject(t__file__path=[$9], t__a=[$0], t__b=[$1])
+                  HiveFilter(condition=[IS NOT NULL($9)])
+                    HiveTableScan(table=[[default, target]], 
table:alias=[target])
+                HiveProject(a=[$0], b=[$1])
+                  HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                    HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], _o__c3=[-1:BIGINT], t__partition__projection=[$3], 
t__a=[$4], t__b=[$5], t__c=[$6])
+    HiveFilter(condition=[=($7, 1)])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__partition__projection=[$3], t__a=[$4], t__b=[$5], 
t__c=[$6], row_number_window_0=[row_number() OVER (PARTITION BY $2 ORDER BY $2 
NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])
+        HiveFilter(condition=[OR(>($4, 20), AND(=($4, $7), =($5, $8), >($4, 
10)))])
+          HiveJoin(condition=[AND(=($4, $7), =($5, $8))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__partition__projection=[$11], t__a=[$0], t__b=[$1], 
t__c=[$2])
+              HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+
+PREHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is not null then insert values (s.a, s.b, s.c)
+PREHOOK: type: QUERY
+PREHOOK: Input: default@source
+PREHOOK: Input: default@target
+PREHOOK: Output: default@target
+POSTHOOK: query: explain cbo
+merge into target as t using source as s on t.a = s.a and t.b = s.b
+when matched and t.a > 10 then delete
+when matched and t.a > 20 then update set b = t.b + 100
+when not matched and s.a is not null then insert values (s.a, s.b, s.c)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@source
+POSTHOOK: Input: default@target
+POSTHOOK: Output: default@target
+CBO PLAN:
+HiveUnion(all=[true])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
a=[$7], b=[$8], c=[$9])
+    HiveFilter(condition=[AND(IS NULL($5), IS NULL($6))])
+      HiveJoin(condition=[AND(=($5, $7), =($6, $8))], joinType=[right], 
algorithm=[none], cost=[not available])
+        HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1])
+          HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+            HiveTableScan(table=[[default, target]], table:alias=[target])
+        HiveProject(a=[$0], b=[$1], c=[$2])
+          HiveFilter(condition=[IS NOT NULL($0)])
+            HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7])
+    HiveSemiJoin(condition=[=($2, $10)], joinType=[semi])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__row__position=[$3], t__partition__projection=[$4], 
t__a=[$5], t__b=[$6], t__c=[$7], a=[$8], b=[$9])
+        HiveFilter(condition=[OR(IS NULL(OR(AND(=($5, $8), =($6, $9), >($5, 
10)), >($5, 20), AND(IS NULL($5), IS NULL($6)))), AND(OR(<>($5, $8), <>($6, 
$9), <=($5, 10)), <=($5, 20), OR(IS NOT NULL($5), IS NOT NULL($6))))])
+          HiveJoin(condition=[AND(=($5, $8), =($6, $9))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__row__position=[$10], t__partition__projection=[$11], 
t__a=[$0], t__b=[$1], t__c=[$2])
+              HiveFilter(condition=[IS NOT NULL($9)])
+                HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+      HiveProject(t__file__path=[$0])
+        HiveFilter(condition=[=($1, 1)])
+          HiveProject(t__file__path=[$0], row_number_window_0=[row_number() 
OVER (PARTITION BY $0 ORDER BY $0 NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING 
AND UNBOUNDED FOLLOWING)])
+            HiveFilter(condition=[OR(>($1, 20), AND(=($1, $3), =($2, $4), 
>($1, 10)))])
+              HiveJoin(condition=[AND(=($1, $3), =($2, $4))], joinType=[left], 
algorithm=[none], cost=[not available])
+                HiveProject(t__file__path=[$9], t__a=[$0], t__b=[$1])
+                  HiveFilter(condition=[IS NOT NULL($9)])
+                    HiveTableScan(table=[[default, target]], 
table:alias=[target])
+                HiveProject(a=[$0], b=[$1])
+                  HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                    HiveTableScan(table=[[default, source]], table:alias=[s])
+  HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], _o__c3=[-1:BIGINT], t__partition__projection=[$3], 
t__a=[$4], t__b=[$5], t__c=[$6])
+    HiveFilter(condition=[=($7, 1)])
+      HiveProject(t__partition__spec__id=[$0], t__partition__hash=[$1], 
t__file__path=[$2], t__partition__projection=[$3], t__a=[$4], t__b=[$5], 
t__c=[$6], row_number_window_0=[row_number() OVER (PARTITION BY $2 ORDER BY $2 
NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)])
+        HiveFilter(condition=[OR(>($4, 20), AND(=($4, $7), =($5, $8), >($4, 
10)))])
+          HiveJoin(condition=[AND(=($4, $7), =($5, $8))], joinType=[left], 
algorithm=[none], cost=[not available])
+            HiveProject(t__partition__spec__id=[$7], t__partition__hash=[$8], 
t__file__path=[$9], t__partition__projection=[$11], t__a=[$0], t__b=[$1], 
t__c=[$2])
+              HiveTableScan(table=[[default, target]], table:alias=[target])
+            HiveProject(a=[$0], b=[$1])
+              HiveFilter(condition=[AND(IS NOT NULL($0), IS NOT NULL($1))])
+                HiveTableScan(table=[[default, source]], table:alias=[s])
+
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/Context.java 
b/ql/src/java/org/apache/hadoop/hive/ql/Context.java
index 2e6df97c152..138787a733e 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/Context.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/Context.java
@@ -361,8 +361,16 @@ private DestClausePrefix getMergeDestClausePrefix(ASTNode 
curNode) {
     assert insert != null && insert.getType() == HiveParser.TOK_INSERT;
     ASTNode query = (ASTNode) insert.getParent();
     assert query != null && query.getType() == HiveParser.TOK_QUERY;
-
-    int tokFromIdx = 
query.getFirstChildWithType(HiveParser.TOK_FROM).getChildIndex();
+    ASTNode from = (ASTNode) query.getFirstChildWithType(HiveParser.TOK_FROM);
+    
+    if (from == null) {
+      // We are here when TOK_FROM is missing from the AST.
+      // This can happen for merge queries with a predicate like 
`<joining_column> is null`
+      // in the matched clause.
+      return DestClausePrefix.MERGE;
+    }
+    
+    int tokFromIdx = from.getChildIndex();
     for (int childIdx = tokFromIdx + 1; childIdx < query.getChildCount(); 
childIdx++) {
       assert query.getChild(childIdx).getType() == HiveParser.TOK_INSERT;
       if (insert == query.getChild(childIdx)) {

Reply via email to