Skip to content

Commit

Permalink
[feature] (expr) support expression IS [NOT] TURE/FALSE (apache#38623)
Browse files Browse the repository at this point in the history
support expression IS [NOT] TURE/FALSE

```
mysql> select 1 is true, 1 is not true, 0 is true, 0 is not true;
+--------------------+---------------------------+--------------------+---------------------------+
| cast(1 as BOOLEAN) | ( not cast(1 as BOOLEAN)) | cast(0 as BOOLEAN) | ( not cast(0 as BOOLEAN)) |
+--------------------+---------------------------+--------------------+---------------------------+
|                  1 |                         0 |                  0 |                         1 |
+--------------------+---------------------------+--------------------+---------------------------+
```
  • Loading branch information
justfortaste authored and dataroaring committed Aug 26, 2024
1 parent 9288c8f commit 74e831d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ predicate
| NOT? kind=IN LEFT_PAREN query RIGHT_PAREN
| NOT? kind=IN LEFT_PAREN expression (COMMA expression)* RIGHT_PAREN
| IS NOT? kind=NULL
| IS NOT? kind=(TRUE | FALSE)
;

valueExpression
Expand Down
14 changes: 14 additions & 0 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -7300,6 +7300,20 @@ non_pred_expr ::=
{: RESULT = new CastExpr(targetType, e); :}
| KW_KEY encryptkey_name:name
{: RESULT = new EncryptKeyRef(name); :}
| expr:e KW_IS KW_TRUE
{: RESULT = new CastExpr(TypeDef.create(PrimitiveType.BOOLEAN), e); :}
| expr:e KW_IS KW_NOT KW_TRUE
{:
e = new CastExpr(TypeDef.create(PrimitiveType.BOOLEAN), e);
RESULT = new CompoundPredicate(CompoundPredicate.Operator.NOT, e, null);
:}
| expr:e KW_IS KW_FALSE
{:
e = new CastExpr(TypeDef.create(PrimitiveType.BOOLEAN), e);
RESULT = new CompoundPredicate(CompoundPredicate.Operator.NOT, e, null);
:}
| expr:e KW_IS KW_NOT KW_FALSE
{: RESULT = new CastExpr(TypeDef.create(PrimitiveType.BOOLEAN), e); :}
| KW_CONVERT LPAREN expr:e KW_USING ident:character RPAREN
{:
ArrayList<Expr> exprs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
import org.apache.doris.nereids.types.AggStateType;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.BooleanType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.LargeIntType;
import org.apache.doris.nereids.types.MapType;
Expand Down Expand Up @@ -3334,6 +3335,14 @@ private Expression withPredicate(Expression valueExpression, PredicateContext ct
case DorisParser.NULL:
outExpression = new IsNull(valueExpression);
break;
case DorisParser.TRUE:
outExpression = new Cast(valueExpression,
BooleanType.INSTANCE, true);
break;
case DorisParser.FALSE:
outExpression = new Not(new Cast(valueExpression,
BooleanType.INSTANCE, true));
break;
case DorisParser.MATCH:
case DorisParser.MATCH_ANY:
outExpression = new MatchAny(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@ public void testIsNull() {
assertExpr(e2);
}

@Test
public void testIsTrue() {
String e1 = "a is true";
assertExpr(e1);

String e2 = "a is not true";
assertExpr(e2);
}

@Test
public void testIsFalse() {
String e1 = "a is false";
assertExpr(e1);

String e2 = "a is not false";
assertExpr(e2);
}

@Test
public void testMatch() {
String sql = "select * from test "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ true
-- !test_compare_expression_12 --
2008-08-08 00:00:00

-- !test_compare_expression_13 --
true false true false

-- !test_compare_expression_14 --
true false true true

-- !test_compare_expression_15 --
true false false true

-- !test_compare_expression_16 --
\N \N \N \N

-- !test_compare_expression_17 --
true false false true

-- !test_compare_expression_18 --
true false false true

Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ select 1 not in (null, 2);
select timestamp '2008-08-08 00:00:00' in ('2008-08-08');
select case when true then timestamp '2008-08-08 00:00:00' else '2008-08-08' end;

select 1 is true, 1 is not true, 0 is false, 0 is not false;
select -1 is true, -1 is not true, -0 is false, -1 is not false;
select 1.1 is true, 1.1 is not true, 1.1 is false, 1.1 is not false;
select 'ab' is true, 'ab' is not true, 'ab' is false, 'ab' is not false;
select cast('2028-01-01' as date) is true, cast('2028-01-01' as date) is not true, cast('2028-01-01' as date) is false, cast('2028-01-01' as date) is not false;
select cast('2028-01-01 01:00:00' as datetime) is true, cast('2028-01-01 01:00:00' as datetime) is not true, cast('2028-01-01 01:00:00' as datetime) is false, cast('2028-01-01 01:00:00' as datetime) is not false;

0 comments on commit 74e831d

Please sign in to comment.