Daniel Becker has uploaded this change for review. ( 
http://gerrit.cloudera.org:8080/19124


Change subject: IMPALA-11462: Constant folding causes cast on literal to be lost
......................................................................

IMPALA-11462: Constant folding causes cast on literal to be lost

The following query returns -128 instead of 128 and the return type is
TINYINT instead of BIGINT:

  select shiftleft(cast(1 as bigint), z) c from (select 7 z ) x;

However, if we disable expression rewrites, the result is correct.

The expression rewrite rule 'FoldConstantsRule' folds the the cast to
bigint into the literal during expression rewrite. This modifies the
expression, so re-analysis is needed. Re-analysis resets the literal
expression (a NumericLiteral), which loses its type and becomes TINYINT
again.

'NumericLiteral's have three kinds of type:
 - natural type: the smallest type that can hold its value
 - explicit type: starts as the natural type and can be widened by
   explicit casts
 - implicit type: the result of casts the analyzer uses to
   adjust input types for functions and arithmetic operations
See NumericLiteral.java for more.

The problem is that when 'FoldConstantsRule' folds the cast into the
literal, it doesn't set its explicit type (only its implicit type
becomes BIGINT). When the expression is reset during re-analysis, the
type is reverted to the incorrect explicit type.

(In the case where expression rewrites are disabled, no re-analysis
takes place so the literal and its type are not reset.)

This patch fixes the error by setting the explicit type of
'NumericLiteral' when folding the cast into it.

Testing:
 - Added test
   
tests/query_test/test_queries.py::TestConstantFoldingNoTypeLoss::test_shiftleft
   that tests 'shiftleft' with all integer types.

Change-Id: Ie7f27b204792ef7c59dec5ead363d44ed0c3bc79
---
M fe/src/main/java/org/apache/impala/analysis/LiteralExpr.java
M tests/query_test/test_queries.py
2 files changed, 40 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.cloudera.org:29418/Impala-ASF refs/changes/24/19124/1
--
To view, visit http://gerrit.cloudera.org:8080/19124
To unsubscribe, visit http://gerrit.cloudera.org:8080/settings

Gerrit-Project: Impala-ASF
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie7f27b204792ef7c59dec5ead363d44ed0c3bc79
Gerrit-Change-Number: 19124
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Becker <[email protected]>
Gerrit-Reviewer: Csaba Ringhofer <[email protected]>
Gerrit-Reviewer: Noemi Pap-Takacs <[email protected]>
Gerrit-Reviewer: Peter Rozsa <[email protected]>

Reply via email to