Michael Smith has submitted this change and it was merged. ( 
https://gerrit.cloudera.org/25432 )

Change subject: IMPALA-15399: Fix NULL result type for NULL operands in Calcite
......................................................................

IMPALA-15399: Fix NULL result type for NULL operands in Calcite

With the Calcite planner, some expressions on NULL operands return
the NULL type instead of the type the original planner returns:
- NULL * NULL (also +, -, / and %) returns NULL instead of DOUBLE.
- NULL & NULL returns NULL instead of INT.
- FALSE || NULL and NULL || NULL return NULL instead of BOOLEAN.

For arithmetic, ImpalaTypeSystemImpl.deriveArithmeticType calls
TypesUtil.getArithmeticResultType, which returns NULL when both
operands are NULL. ArithmeticExpr in the original planner defaults
that case to DOUBLE (INT for DIV), so the backend never sees
NULL_TYPE. This patch adds the same defaults to
deriveArithmeticType. Function resolution then picks the DOUBLE
version of the operator and casts the NULL operands to DOUBLE.

For &, the Calcite parser used Calcite's BITAND_OPERATOR. Its return
type is the least restrictive type of the operands, which is NULL
when both operands are NULL, and it never calls the Impala type
system. ArithmeticExpr types the bitwise operators like DIV: it uses
the assignment compatible type of the operands and defaults to INT
when both are NULL. This patch adds a BITAND operator to
ImpalaCustomOperatorTable, like the existing custom PLUS, MINUS,
MULTIPLY, DIVIDE, INT_DIVIDE and PERCENT_REMAINDER operators. It has
the same name, kind, precedence and operand type checker as Calcite's
operator, but gets its return type from deriveArithmeticType, which
now handles BITAND, BITOR and BITXOR like DIV. The parser now returns
this operator for &. | and ^ are not supported by the Calcite parser
yet.

For ||, ImpalaConcatOrOperator correctly infers BOOLEAN when both
operands are BOOLEAN or NULL, and ImpalaConvertletTable converts the
call to OR(FALSE, null:NULL), typed BOOLEAN. However, unlike the
standard OR operator, || does not infer operand types, so the NULL
literal keeps the NULL type. CoerceNodes.processRexNodes then calls
RexUtil.pullFactors and RexUtil.toCnf. These drop FALSE and duplicate
operands from the OR, and when only one operand is left they return
it as is. The expression then becomes the NULL literal and its type
changes from BOOLEAN to NULL.

CoerceOperandShuttle now casts the NULL typed operands of AND and OR
calls to the type of the call (BOOLEAN). This runs before pullFactors
and toCnf, so a collapsed expression keeps the BOOLEAN type. Only AND
and OR need this, because they and CAST are the only operators that
skip operand coercion. The CONCAT path is unchanged.

Testing:
- Added tests to QueryTest/calcite.test that check the result types
  of NULL * NULL, NULL + NULL, NULL / NULL, NULL % NULL,
  NULL DIV NULL, FALSE || NULL, NULL || NULL, and of & with NULL and
  integer operands.
- Ran test_calcite_planner.py::test_calcite_frontend.
- Ran CalcitePlannerTest.
- Ran expr-test ArithmeticExprs, CompoundPredicates and
  CompoundVerticalBarExpr locally, with expr-test forced to use the
  Calcite planner. The NULL operand type failures are gone.

Change-Id: If8ac369da47fd63988569780c2315caebdab957c
Assisted-by: Claude Opus (Claude Code)
Reviewed-on: https://gerrit.cloudera.org/25432
Reviewed-by: Impala Public Jenkins <[email protected]>
Tested-by: Michael Smith <[email protected]>
---
M java/calcite-planner/src/main/codegen/templates/Parser.jj
M 
java/calcite-planner/src/main/java/org/apache/impala/calcite/coercenodes/CoerceOperandShuttle.java
M 
java/calcite-planner/src/main/java/org/apache/impala/calcite/operators/ImpalaCustomOperatorTable.java
M 
java/calcite-planner/src/main/java/org/apache/impala/calcite/type/ImpalaTypeSystemImpl.java
M testdata/workloads/functional-query/queries/QueryTest/calcite.test
5 files changed, 79 insertions(+), 2 deletions(-)

Approvals:
  Impala Public Jenkins: Looks good to me, approved
  Michael Smith: Verified

--
To view, visit https://gerrit.cloudera.org/25432
To unsubscribe, visit https://gerrit.cloudera.org/settings

Gerrit-Project: Impala-ASF
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: If8ac369da47fd63988569780c2315caebdab957c
Gerrit-Change-Number: 25432
Gerrit-PatchSet: 8
Gerrit-Owner: Wellington Cabrera <[email protected]>
Gerrit-Reviewer: Aman Sinha <[email protected]>
Gerrit-Reviewer: Impala Public Jenkins <[email protected]>
Gerrit-Reviewer: Joe McDonnell <[email protected]>
Gerrit-Reviewer: Michael Smith <[email protected]>
Gerrit-Reviewer: Steve Carlin <[email protected]>
Gerrit-Reviewer: Wellington Cabrera (968)

Reply via email to