Skip to content

Commit

Permalink
[CALCITE-2783] Expressions like ( s or s is unknown ) = true causes E…
Browse files Browse the repository at this point in the history
…xceptions

Earlier, expressions like ( s or s is null ) = true have resulted in an exception.
Some other fixes to handle null translations to true/false in IS NULL/IS NOT NULL cases.

Close apache#1007
  • Loading branch information
kgyrtkirk committed Jan 23, 2019
1 parent fc10982 commit e804521
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,10 @@ public static CallImplementor createImplementor(
+ String.valueOf(call.getOperator());
final RexCall call2 = call2(false, translator, call);
switch (nullAs) {
case NOT_POSSIBLE: // Just foldAnd
case NOT_POSSIBLE:
// This doesn't mean that none of the arguments might be null, ex: (s and s is not null)
nullAs = NullAs.TRUE;
// fallthru
case TRUE:
// AND call should return false iff has FALSEs,
// thus if we convert nulls to true then no harm is made
Expand Down Expand Up @@ -641,7 +644,10 @@ public static CallImplementor createImplementor(
+ String.valueOf(call.getOperator());
final RexCall call2 = call2(harmonize, translator, call);
switch (nullAs) {
case NOT_POSSIBLE: // Just foldOr
case NOT_POSSIBLE:
// This doesn't mean that none of the arguments might be null, ex: (s or s is null)
nullAs = NullAs.FALSE;
// fallthru
case TRUE:
// This should return false iff all arguments are FALSE,
// thus we convert nulls to TRUE and foldOr
Expand Down Expand Up @@ -2535,6 +2541,12 @@ public Expression implement(
RexToLixTranslator translator, RexCall call, NullAs nullAs) {
List<RexNode> operands = call.getOperands();
assert operands.size() == 1;
switch (nullAs) {
case IS_NOT_NULL:
return BOXED_TRUE_EXPR;
case IS_NULL:
return BOXED_FALSE_EXPR;
}
if (seek == null) {
return translator.translate(operands.get(0),
negate ? NullAs.IS_NOT_NULL : NullAs.IS_NULL);
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/resources/sql/conditions.iq
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,21 @@ select case when s=0 then false else 100/s > 0 end from ax;
(2 rows)

!ok

# Test case for CALCITE-2783
with ax(s) as (values (true),(false),(cast(null as boolean)))
select s, (s or s is null), (s and s is not null) from ax;

+-------+--------+--------+
| S | EXPR$1 | EXPR$2 |
+-------+--------+--------+
| false | false | false |
| true | true | true |
| | true | false |
+-------+--------+--------+
(3 rows)

!ok


# End conditions.iq

0 comments on commit e804521

Please sign in to comment.