[ 
https://issues.apache.org/jira/browse/IMPALA-15399?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150639#comment-18150639
 ] 

ASF subversion and git services commented on IMPALA-15399:
----------------------------------------------------------

Commit 1c10a1663cf6b7a16a2ffc4cfd10a3d0f43b05db in impala's branch 
refs/heads/master from Wellington Cabrera
[ https://gitbox.apache.org/repos/asf?p=impala.git;h=1c10a1663c ]

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]>


> Calcite planner: NULL * NULL datatype doesn't match
> ---------------------------------------------------
>
>                 Key: IMPALA-15399
>                 URL: https://issues.apache.org/jira/browse/IMPALA-15399
>             Project: IMPALA
>          Issue Type: Bug
>            Reporter: Steve Carlin
>            Priority: Major
>              Labels: hackathon2026
>             Fix For: Impala 5.1.0
>
>
> The exprs-test.cc file has some tests that are not in the e2e tests.
> One of them is testing the datatype returned by "NULL * NULL". 
> The original planner returns DOUBLE whereas the Calcite Planner returns null.
> The expr-test file isn't completely working yet, but this can be replicated 
> by creating a "select NULL * NULL" test within a ".test" file



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to