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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
