未验证 提交 39d02caf 编写于 作者: P Plamen Hristov 提交者: GitHub

fix(sql): Fixed cast as column name (#1502)

上级 c5d9eb84
......@@ -333,7 +333,7 @@ class ExpressionParser {
CharSequence geohashTok = GenericLexer.immutableOf(tok);
tok = SqlUtil.fetchNext(lexer);
if (tok == null || tok.charAt(0) != '(') {
lexer.backTo(position + 7, geohashTok);
lexer.backTo(position + SqlKeywords.GEOHASH_KEYWORD_LENGTH, geohashTok);
tok = geohashTok;
processDefaultBranch = true;
break;
......@@ -512,6 +512,17 @@ class ExpressionParser {
case 'c':
case 'C':
if (SqlKeywords.isCastKeyword(tok)) {
CharSequence caseTok = GenericLexer.immutableOf(tok);
tok = SqlUtil.fetchNext(lexer);
if (tok == null || tok.charAt(0) != '(') {
lexer.backTo(position + SqlKeywords.CASE_KEYWORD_LENGTH, caseTok);
tok = caseTok;
processDefaultBranch = true;
break;
}
lexer.backTo(position + SqlKeywords.CASE_KEYWORD_LENGTH, caseTok);
tok = caseTok;
if (prevBranch != BRANCH_DOT_DEREFERENCE) {
castBraceCountStack.push(-1);
thisBranch = BRANCH_OPERATOR;
......
......@@ -26,6 +26,8 @@ package io.questdb.griffin;
public class SqlKeywords {
public static final String CONCAT_FUNC_NAME = "concat";
public static final int CASE_KEYWORD_LENGTH = 4;
public static int GEOHASH_KEYWORD_LENGTH = 7;
public static boolean isAddKeyword(CharSequence tok) {
if (tok.length() != 3) {
......
......@@ -100,7 +100,7 @@ public class ExpressionParserTest extends AbstractCairoTest {
Numbers.encodeLowHighShorts((short) 0, (short) (5 * (tok.length() - 1))),
ExpressionParser.extractGeoHashSuffix(0, tok));
}
for (String tok : new String[]{"#/x", "#/1x", "#/x1", "#/xx", "#/-1", }) {
for (String tok : new String[]{"#/x", "#/1x", "#/x1", "#/xx", "#/-1",}) {
Assert.assertThrows("[0] invalid bits size for GEOHASH constant",
SqlException.class, () -> ExpressionParser.extractGeoHashSuffix(0, tok));
}
......@@ -438,10 +438,19 @@ public class ExpressionParserTest extends AbstractCairoTest {
" end + 1)");
}
@Test
public void testCaseWithDanglingCast() {
assertFail(
"case (cast 1 as int)",
11,
"dangling expression"
);
}
@Test
public void testCaseWithCast() throws SqlException {
x("1castint1'th1'2'th2'0case5*1+",
"case (cast 1 as int)" +
x("1intcast1'th1'2'th2'0case5*1+",
"case (cast(1 as int))" +
" when 1" +
" then 'th1'" +
" when 2" +
......@@ -450,6 +459,16 @@ public class ExpressionParserTest extends AbstractCairoTest {
" end * 5 + 1");
}
@Test
public void testCastFunctionCallMultiSpace() throws SqlException {
x("1102030f+shortcast", "cast\t --- this is a comment\n\n(1+f(10,20,30) as short\n)");
}
@Test
public void testCastFunctionWithLambdaMultiSpaceNewlineAndComment() throws SqlException {
x("(select-choose a, b, c from (x))flongcast", "cast --- this is a comment\n\n(f(select a,b,c from x) as long\n)");
}
@Test
public void testCaseWithOuterBraces() throws SqlException {
x("10w11+10='th1'w23*1>'th2'0case1+*",
......@@ -1120,6 +1139,7 @@ public class ExpressionParserTest extends AbstractCairoTest {
x("i0.1e+3<90100case", "case when i < 0.1e+3 then 90 else 100 end");
}
private void assertFail(String content, int pos, String contains) {
try {
compiler.testParseExpression(content, rpnBuilder);
......
......@@ -814,6 +814,19 @@ public class SqlParserTest extends AbstractSqlParserTest {
"PARTITION BY YEAR");
}
@Test
public void testCreateTableCastMultiSpaceMultiNewlineAndComment() throws SqlException {
assertCreateTable(
"create table x as (select-choose a, b, c from (select [a, b, c] from tab)), cast(a as DOUBLE:38), cast(c as SYMBOL:83 capacity 16 cache)",
"create table x as (tab), cast (a as double ), cast\n--- this is a comment\n\n(c as symbol capacity 16\n)",
modelOf("tab")
.col("a", ColumnType.INT)
.col("b", ColumnType.LONG)
.col("c", ColumnType.STRING)
);
}
@Test
public void testCreateTableCastCapacityDef() throws SqlException {
assertCreateTable(
......
......@@ -44,6 +44,37 @@ import java.util.stream.Stream;
public class TimestampQueryTest extends AbstractGriffinTest {
@Test
public void testCast2AsValidColumnNameTouchFunction() throws Exception {
assertMemoryLeak(() -> {
//create table
String createStmt = "create table xyz(time timestamp, cast2 geohash(8c)) timestamp(time) partition by DAY;";
compiler.compile(createStmt, sqlExecutionContext);
//insert
executeInsert("INSERT INTO xyz VALUES(1609459199000000, #u33d8b12)");
String expected = "touch\n{\"data_pages\": 2, \"index_key_pages\":0, \"index_values_pages\": 0}\n";
String query = "select touch(select time, cast2 from xyz);";
assertSql(query, expected);
});
}
@Test
public void testCastAsValidColumnNameSelectTest() throws Exception {
assertMemoryLeak(() -> {
//create table
String createStmt = "create table xyz(time timestamp, cast geohash(8c)) timestamp(time) partition by DAY;";
compiler.compile(createStmt, sqlExecutionContext);
//insert
executeInsert("INSERT INTO xyz VALUES(1609459199000000, #u33d8b12)");
String expected = "time\tcast\n" +
"2020-12-31T23:59:59.000000Z\tu33d8b12\n";
String query = "select time, cast from xyz;";
assertSql(query, expected);
});
}
@Test
public void testEqualityTimestampFormatYearAndMonthNegativeTest() throws Exception {
assertMemoryLeak(() -> {
......@@ -1269,6 +1300,7 @@ public class TimestampQueryTest extends AbstractGriffinTest {
false
);
}
private void assertTimestampTtFailedQuery(String expectedError, String query) {
assertTimestampTtFailedQuery0(expectedError, query);
String dtsQuery = query.replace("nts", "dts");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册