|
6 | 6 | package org.elasticsearch.xpack.sql.expression.predicate.conditional;
|
7 | 7 |
|
8 | 8 | import org.elasticsearch.xpack.sql.expression.Expression;
|
| 9 | +import org.elasticsearch.xpack.sql.expression.Literal; |
9 | 10 | import org.elasticsearch.xpack.sql.expression.function.scalar.FunctionTestUtils;
|
10 | 11 | import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.Equals;
|
11 | 12 | import org.elasticsearch.xpack.sql.tree.AbstractNodeTestCase;
|
12 | 13 | import org.elasticsearch.xpack.sql.tree.NodeSubclassTests;
|
13 | 14 | import org.elasticsearch.xpack.sql.tree.Source;
|
14 | 15 | import org.elasticsearch.xpack.sql.tree.SourceTests;
|
| 16 | +import org.elasticsearch.xpack.sql.type.DataType; |
15 | 17 |
|
16 | 18 | import java.util.ArrayList;
|
| 19 | +import java.util.Arrays; |
17 | 20 | import java.util.List;
|
18 | 21 | import java.util.Objects;
|
19 | 22 |
|
20 | 23 | import static org.elasticsearch.xpack.sql.expression.function.scalar.FunctionTestUtils.randomIntLiteral;
|
21 | 24 | import static org.elasticsearch.xpack.sql.expression.function.scalar.FunctionTestUtils.randomStringLiteral;
|
| 25 | +import static org.elasticsearch.xpack.sql.tree.Source.EMPTY; |
22 | 26 | import static org.elasticsearch.xpack.sql.tree.SourceTests.randomSource;
|
23 | 27 |
|
24 | 28 | /**
|
@@ -77,6 +81,42 @@ public void testReplaceChildren() {
|
77 | 81 | assertEquals(new Case(c.source(), newChildren), c.replaceChildren(newChildren));
|
78 | 82 | }
|
79 | 83 |
|
| 84 | + public void testDataTypes() { |
| 85 | + // CASE WHEN 1 = 1 THEN NULL |
| 86 | + // ELSE 'default' |
| 87 | + // END |
| 88 | + Case c = new Case(EMPTY, Arrays.asList( |
| 89 | + new IfConditional(EMPTY, new Equals(EMPTY, Literal.of(EMPTY, 1), Literal.of(EMPTY, 1)), Literal.NULL), |
| 90 | + Literal.of(EMPTY, "default"))); |
| 91 | + assertEquals(DataType.KEYWORD, c.dataType()); |
| 92 | + |
| 93 | + // CASE WHEN 1 = 1 THEN 'foo' |
| 94 | + // ELSE NULL |
| 95 | + // END |
| 96 | + c = new Case(EMPTY, Arrays.asList( |
| 97 | + new IfConditional(EMPTY, new Equals(EMPTY, Literal.of(EMPTY, 1), Literal.of(EMPTY, 1)), Literal.of(EMPTY, "foo")), |
| 98 | + Literal.NULL)); |
| 99 | + assertEquals(DataType.KEYWORD, c.dataType()); |
| 100 | + |
| 101 | + // CASE WHEN 1 = 1 THEN NULL |
| 102 | + // ELSE NULL |
| 103 | + // END |
| 104 | + c = new Case(EMPTY, Arrays.asList( |
| 105 | + new IfConditional(EMPTY, new Equals(EMPTY, Literal.of(EMPTY, 1), Literal.of(EMPTY, 1)), Literal.NULL), |
| 106 | + Literal.NULL)); |
| 107 | + assertEquals(DataType.NULL, c.dataType()); |
| 108 | + |
| 109 | + // CASE WHEN 1 = 1 THEN NULL |
| 110 | + // WHEN 2 = 2 THEN 'foo' |
| 111 | + // ELSE NULL |
| 112 | + // END |
| 113 | + c = new Case(EMPTY, Arrays.asList( |
| 114 | + new IfConditional(EMPTY, new Equals(EMPTY, Literal.of(EMPTY, 1), Literal.of(EMPTY, 1)), Literal.NULL), |
| 115 | + new IfConditional(EMPTY, new Equals(EMPTY, Literal.of(EMPTY, 2), Literal.of(EMPTY, 2)), Literal.of(EMPTY, "foo")), |
| 116 | + Literal.NULL)); |
| 117 | + assertEquals(DataType.KEYWORD, c.dataType()); |
| 118 | + } |
| 119 | + |
80 | 120 | private List<Expression> mutateChildren(Case c) {
|
81 | 121 | boolean removeConditional = randomBoolean();
|
82 | 122 | List<Expression> expressions = new ArrayList<>(c.children().size());
|
|
0 commit comments