|
31 | 31 | public class CastTypeAliasRewriterTest { |
32 | 32 |
|
33 | 33 | private final CastTypeAliasRewriter _rewriter = new CastTypeAliasRewriter(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Calcite / SQL standard uses BIGINT; rewriter maps it to Pinot LONG for server compatibility. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + public void testTopLevelCastBigintRewritesToLong() { |
| 40 | + Expression cast = RequestUtils.getFunctionExpression("cast", |
| 41 | + RequestUtils.getIdentifierExpression("event_timestamp"), |
| 42 | + RequestUtils.getLiteralExpression("BIGINT")); |
| 43 | + PinotQuery pinotQuery = new PinotQuery(); |
| 44 | + pinotQuery.addToSelectList(cast); |
| 45 | + |
| 46 | + PinotQuery rewritten = _rewriter.rewrite(pinotQuery); |
| 47 | + Assert.assertEquals( |
| 48 | + rewritten.getSelectList().get(0).getFunctionCall().getOperands().get(1).getLiteral().getStringValue(), |
| 49 | + "LONG"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Type literal matching is case-insensitive (switch uses {@code toUpperCase()}). |
| 54 | + */ |
| 55 | + @Test |
| 56 | + public void testBigintTypeLiteralCaseInsensitiveRewritesToLong() { |
| 57 | + Expression cast = RequestUtils.getFunctionExpression("cast", |
| 58 | + RequestUtils.getIdentifierExpression("x"), |
| 59 | + RequestUtils.getLiteralExpression("bigint")); |
| 60 | + PinotQuery pinotQuery = new PinotQuery(); |
| 61 | + pinotQuery.addToSelectList(cast); |
| 62 | + |
| 63 | + PinotQuery rewritten = _rewriter.rewrite(pinotQuery); |
| 64 | + Assert.assertEquals( |
| 65 | + rewritten.getSelectList().get(0).getFunctionCall().getOperands().get(1).getLiteral().getStringValue(), |
| 66 | + "LONG"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Compiled SQL with explicit {@code AS BIGINT} must rewrite the cast target to LONG. |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void testCompiledSimpleCastAsBigintRewritesToLong() { |
| 74 | + PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQueryWithoutRewrites( |
| 75 | + "SELECT CAST(event_timestamp AS BIGINT) FROM t"); |
| 76 | + PinotQuery rewritten = _rewriter.rewrite(pinotQuery); |
| 77 | + Assert.assertEquals( |
| 78 | + rewritten.getSelectList().get(0).getFunctionCall().getOperands().get(1).getLiteral().getStringValue(), |
| 79 | + "LONG"); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Nested {@code CAST(CAST(x AS BIGINT) AS BIGINT)} — both CAST type operands must become LONG. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + public void testCompiledNestedBothBigintRewritesBothToLong() { |
| 87 | + PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQueryWithoutRewrites( |
| 88 | + "SELECT CAST(CAST(event_timestamp AS BIGINT) AS BIGINT) FROM t"); |
| 89 | + PinotQuery rewritten = _rewriter.rewrite(pinotQuery); |
| 90 | + Expression outer = rewritten.getSelectList().get(0); |
| 91 | + Assert.assertEquals(outer.getFunctionCall().getOperands().get(1).getLiteral().getStringValue(), "LONG"); |
| 92 | + Expression inner = outer.getFunctionCall().getOperands().get(0); |
| 93 | + Assert.assertEquals(inner.getFunctionCall().getOperands().get(1).getLiteral().getStringValue(), "LONG"); |
| 94 | + } |
34 | 95 |
|
35 | 96 | /** |
36 | 97 | * Nested CAST(... AS DOUBLE) around CAST(col AS BIGINT) must rewrite the inner BIGINT alias so |
|
0 commit comments