Zoltán Borók-Nagy created IMPALA-15774:
------------------------------------------
Summary: Calcite planner: wrong results when simplifying string
literals with escape sequences
Key: IMPALA-15774
URL: https://issues.apache.org/jira/browse/IMPALA-15774
Project: IMPALA
Issue Type: Bug
Components: Frontend
Reporter: Zoltán Borók-Nagy
The Calcite planner keeps string literals in RexLiteral in their SQL-escaped
form and only unescapes them when RexLiteralConverter builds the Impala
StringLiteral (needsUnescaping = true). Calcite's own simplifications
(RexSimplify, predicate inference) compare the escaped text, so two spellings
of the same string compare as different and predicates are folded the wrong way.
Repro on master (83cdfdc9fb) with PLANNER=CALCITE; functional.alltypestiny has
string_col = '1' in 4 rows:
{code:sql}
-- Original planner: true, true, true, false, true
-- Calcite planner: false, false, false, true, false
select 'a\q' = 'aq', '\061' = '1', "it's" = 'it\'s', 'a\q' < 'aq', 'a\q' in
('aq', 'x');
-- Original planner: 4, Calcite planner: 0
select count(*) from functional.alltypestiny
where string_col = '1' and string_col = '\061';
-- Original planner: 0, Calcite planner: 4
select count(*) from functional.alltypestiny
where string_col = '1' and string_col != '\061';
-- Original planner: 16, Calcite planner: 0
select count(*) from functional.alltypestiny a join functional.alltypestiny b
on a.string_col = b.string_col
where a.string_col = '\061' and b.string_col = '1';
{code}
A single predicate like string_col = '\061' returns the right rows, and
expressions that are constant folded by Impala are correct too, e.g.
concat('a\q', 'b') = 'aqb' is true in both planners. Only Calcite's own
handling of the literal text is affected.
IMPALA-15753 (LIKE with an escaped wildcard) has the same cause: Calcite
simplifies the escaped pattern text, while it leaves the unescaped pattern
alone.
A possible fix is to unescape string literals when creating the RexLiteral and
convert them back with needsUnescaping = false, instead of working around
individual simplifications.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)