You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by "soumyakanti3578 (via GitHub)" <gi...@apache.org> on 2026/06/18 19:30:11 UTC

[PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

soumyakanti3578 opened a new pull request, #6550:
URL: https://github.com/apache/hive/pull/6550

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://cwiki.apache.org/confluence/display/Hive/HowToContribute
     2. Ensure that you have created an issue on the Hive project JIRA: https://issues.apache.org/jira/projects/HIVE/summary
     3. Ensure you have added or run the appropriate tests for your PR: 
     4. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]HIVE-XXXXX:  Your PR title ...'.
     5. Be sure to keep the PR description updated to reflect all changes.
     6. Please write your PR title to summarize what this PR proposes.
     7. If possible, provide a concise example to reproduce the issue for a faster review.
   
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   Added a new interface `NonNullableReturnTypeUDF` to identify UDFs with a non-nullable return type.
   When a NonNullableReturnTypeUDF is identified in `HiveFunctionHelper.getReturnType`, we can create the return type with nullability set to `false`.
   
   Currently `GenericUDFOPNull` and `GenericUDFOPNotNull` implements the interface.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   Calcite has a Strong policy where it validates and asserts that the output of a function like `IS NULL` has a non-nullable return type. This is done in [RexSimplify.validateStrongPolicy](https://github.com/apache/calcite/blob/calcite-1.33.0/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L1059)
   
   Jira: [HIVE-27070](https://issues.apache.org/jira/browse/HIVE-27070)
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description, screenshot and/or a reproducable example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Hive versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   ```
   mvn test -pl itests/qtest -Pitests -Dtest=TestMiniLlapLocalCliDriver -Dtest.output.overwrite=true -Dqfile="is_null_is_not_null_nested.q"
   ```


-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "soumyakanti3578 (via GitHub)" <gi...@apache.org>.
soumyakanti3578 commented on code in PR #6550:
URL: https://github.com/apache/hive/pull/6550#discussion_r3469162696


##########
ql/src/test/queries/clientpositive/is_null_is_not_null_nested.q:
##########
@@ -0,0 +1,28 @@
+-- Tests for nested IS NULL / IS NOT NULL predicates.
+-- (name IS NULL) IS NULL should simplify to FALSE since IS NULL always returns a non-nullable BOOLEAN.
+-- (name IS NOT NULL) IS NULL should simplify to FALSE for the same reason.
+
+CREATE TABLE t_is_null_nested (id INT, name STRING);
+
+INSERT INTO t_is_null_nested VALUES
+  (1, 'alice'),
+  (2, NULL);
+
+-- always FALSE
+SELECT id, (name IS NULL) IS NULL FROM t_is_null_nested ORDER BY id;
+
+-- always FALSE
+SELECT id, (name IS NOT NULL) IS NULL FROM t_is_null_nested ORDER BY id;
+
+-- always TRUE
+SELECT id, (name IS NULL) IS NOT NULL FROM t_is_null_nested ORDER BY id;
+
+-- always TRUE
+SELECT id, (name IS NOT NULL) IS NOT NULL FROM t_is_null_nested ORDER BY id;
+
+-- no rows should be filtered out
+SELECT id FROM t_is_null_nested WHERE (name IS NULL) IS NOT NULL ORDER BY id;
+
+-- should filter out all rows
+SELECT id FROM t_is_null_nested WHERE (name IS NULL) IS NULL ORDER BY id;
+

Review Comment:
   Done 👍🏼 



-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #6550:
URL: https://github.com/apache/hive/pull/6550#discussion_r3465472761


##########
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/NonNullableReturnTypeUDF.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.hadoop.hive.ql.udf.generic;
+
+/**
+ * Interface for UDFs that always return a non-nullable datatype
+ * (e.g. {@code IS NULL}, {@code IS NOT NULL}).
+ */
+public interface NonNullableReturnTypeUDF {

Review Comment:
   The existing `UDFType` annotation interface may be a better place to document the nullability of the function.



##########
ql/src/java/org/apache/hadoop/hive/ql/parse/type/HiveFunctionHelper.java:
##########
@@ -135,12 +136,20 @@ public RelDataType getReturnType(FunctionInfo fi, List<RexNode> inputs)
       inputsOIs[i] = createObjectInspector(inputs.get(i));
     }
     // 2) Initialize and obtain return type
-    ObjectInspector oi = fi.getGenericUDF() != null ?
-        fi.getGenericUDF().initializeAndFoldConstants(inputsOIs) :
+    GenericUDF genericUDF = fi.getGenericUDF();
+    ObjectInspector oi = genericUDF != null ?
+        genericUDF.initializeAndFoldConstants(inputsOIs) :
         fi.getGenericUDTF().initialize(inputsOIs);
     // 3) Convert to RelDataType
-    return TypeConverter.convert(
+    RelDataType returnType = TypeConverter.convert(
         TypeInfoUtils.getTypeInfoFromObjectInspector(oi), rexBuilder.getTypeFactory());
+    // Hive has no concept of non-nullable types, but some UDFs (e.g. IS NULL) always return
+    // non-nullable BOOLEAN, which Calcite's RexSimplify asserts via validateStrongPolicy.
+    if (genericUDF instanceof NonNullableReturnTypeUDF) {
+      returnType = rexBuilder.getTypeFactory().createTypeWithNullability(returnType, false);
+    }

Review Comment:
   Instead of altering the type here we could pass the correct nullability via the overloaded `TypeConvereter.convert` method.



##########
ql/src/test/queries/clientpositive/is_null_is_not_null_nested.q:
##########
@@ -0,0 +1,28 @@
+-- Tests for nested IS NULL / IS NOT NULL predicates.
+-- (name IS NULL) IS NULL should simplify to FALSE since IS NULL always returns a non-nullable BOOLEAN.
+-- (name IS NOT NULL) IS NULL should simplify to FALSE for the same reason.
+
+CREATE TABLE t_is_null_nested (id INT, name STRING);
+
+INSERT INTO t_is_null_nested VALUES
+  (1, 'alice'),
+  (2, NULL);
+
+-- always FALSE
+SELECT id, (name IS NULL) IS NULL FROM t_is_null_nested ORDER BY id;
+
+-- always FALSE
+SELECT id, (name IS NOT NULL) IS NULL FROM t_is_null_nested ORDER BY id;
+
+-- always TRUE
+SELECT id, (name IS NULL) IS NOT NULL FROM t_is_null_nested ORDER BY id;
+
+-- always TRUE
+SELECT id, (name IS NOT NULL) IS NOT NULL FROM t_is_null_nested ORDER BY id;
+
+-- no rows should be filtered out
+SELECT id FROM t_is_null_nested WHERE (name IS NULL) IS NOT NULL ORDER BY id;
+
+-- should filter out all rows
+SELECT id FROM t_is_null_nested WHERE (name IS NULL) IS NULL ORDER BY id;
+

Review Comment:
   In all of the above queries the condition is either true or false and this should be visible in the plan. The `EXPLAIN CBO` command would be a better way to ensure that simplification works as expected thus we could drop the data and output. Without data we could also drop the `ORDER BY` clauses.



-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "soumyakanti3578 (via GitHub)" <gi...@apache.org>.
soumyakanti3578 commented on code in PR #6550:
URL: https://github.com/apache/hive/pull/6550#discussion_r3469168963


##########
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/NonNullableReturnTypeUDF.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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.hadoop.hive.ql.udf.generic;
+
+/**
+ * Interface for UDFs that always return a non-nullable datatype
+ * (e.g. {@code IS NULL}, {@code IS NOT NULL}).
+ */
+public interface NonNullableReturnTypeUDF {

Review Comment:
   That is indeed a better place. I have deleted `NonNullableReturnTypeUDF` in the latest commit.



-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #6550:
URL: https://github.com/apache/hive/pull/6550#issuecomment-4746032320

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=6550) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=6550&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=6550&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=6550&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=6550&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=6550&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=6550)
   
   


-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "soumyakanti3578 (via GitHub)" <gi...@apache.org>.
soumyakanti3578 merged PR #6550:
URL: https://github.com/apache/hive/pull/6550


-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "soumyakanti3578 (via GitHub)" <gi...@apache.org>.
soumyakanti3578 commented on code in PR #6550:
URL: https://github.com/apache/hive/pull/6550#discussion_r3469172229


##########
ql/src/java/org/apache/hadoop/hive/ql/parse/type/HiveFunctionHelper.java:
##########
@@ -135,12 +136,20 @@ public RelDataType getReturnType(FunctionInfo fi, List<RexNode> inputs)
       inputsOIs[i] = createObjectInspector(inputs.get(i));
     }
     // 2) Initialize and obtain return type
-    ObjectInspector oi = fi.getGenericUDF() != null ?
-        fi.getGenericUDF().initializeAndFoldConstants(inputsOIs) :
+    GenericUDF genericUDF = fi.getGenericUDF();
+    ObjectInspector oi = genericUDF != null ?
+        genericUDF.initializeAndFoldConstants(inputsOIs) :
         fi.getGenericUDTF().initialize(inputsOIs);
     // 3) Convert to RelDataType
-    return TypeConverter.convert(
+    RelDataType returnType = TypeConverter.convert(
         TypeInfoUtils.getTypeInfoFromObjectInspector(oi), rexBuilder.getTypeFactory());
+    // Hive has no concept of non-nullable types, but some UDFs (e.g. IS NULL) always return
+    // non-nullable BOOLEAN, which Calcite's RexSimplify asserts via validateStrongPolicy.
+    if (genericUDF instanceof NonNullableReturnTypeUDF) {
+      returnType = rexBuilder.getTypeFactory().createTypeWithNullability(returnType, false);
+    }

Review Comment:
   Done! 👍🏼 



-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


Re: [PR] HIVE-27070: Multiple is NULL check together fails in CalcitePlanner [hive]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #6550:
URL: https://github.com/apache/hive/pull/6550#issuecomment-4792647504

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=6550) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=6550&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=6550&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=6550&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=6550&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=6550&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=6550)
   
   


-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org