@Test public void testExplicitTable() { check("table emp", "(TABLE `EMP`)"); // FIXME should fail at "123" checkFails( "^table^ 123", "(?s)Encountered \"table 123\" at line 1, column 1\\.\n.*"); }
@Test public void testIdentifier() { checkExp("ab", "`AB`"); checkExp(" \"a \"\" b!c\"", "`a \" b!c`"); checkExpFails(" ^`^a \" b!c`", "(?s).*Encountered.*"); checkExp("\"x`y`z\"", "`x``y``z`"); checkExpFails("^`^x`y`z`", "(?s).*Encountered.*"); checkExp("myMap[field] + myArray[1 + 2]", "(`MYMAP`[`FIELD`] + `MYARRAY`[(1 + 2)])"); }
@Test public void testLiteral() { checkExpSame("'foo'"); checkExpSame("100"); check( "select 1 as uno, 'x' as x, null as n from emp", "SELECT 1 AS `UNO`, 'x' AS `X`, NULL AS `N`\n" + "FROM `EMP`"); // Even though it looks like a date, it's just a string. checkExp("'2004-06-01'", "'2004-06-01'"); checkExp("-.25", "-0.25"); checkExpSame("TIMESTAMP '2004-06-01 15:55:55'"); checkExpSame("TIMESTAMP '2004-06-01 15:55:55.900'"); checkExp( "TIMESTAMP '2004-06-01 15:55:55.1234'", "TIMESTAMP '2004-06-01 15:55:55.1234'"); checkExp( "TIMESTAMP '2004-06-01 15:55:55.1236'", "TIMESTAMP '2004-06-01 15:55:55.1236'"); checkExp( "TIMESTAMP '2004-06-01 15:55:55.9999'", "TIMESTAMP '2004-06-01 15:55:55.9999'"); checkExpSame("NULL"); }
@Test public void testNullIf() { checkExp( "nullif(v1,v2)", "(NULLIF(`V1`, `V2`))"); if (isReserved("NULLIF")) { checkExpFails( "1 + ^nullif^ + 3", "(?s)Encountered \"nullif \\+\" at line 1, column 5.*"); } }
assumeTrue("don't run this test for sub-classes", isNotSubclass()); final SqlAbstractParserImpl.Metadata metadata = getSqlParser("").getMetadata(); final SortedSet<String> keywords92 = keywords("92"); for (String s : metadata.getTokens()) { if (metadata.isKeyword(s) && metadata.isReservedWord(s)) { + DiffTestCase.diffLines(ImmutableList.copyOf(getReservedKeywords()), ImmutableList.copyOf(reservedKeywords)); assertThat(reason, reservedKeywords, is(getReservedKeywords()));
checkExp("CURRENT_TIME(3)", "CURRENT_TIME(3)"); checkExp("CURRENT_TIME", "`CURRENT_TIME`"); checkExp("CURRENT_TIME(x+y)", "CURRENT_TIME((`X` + `Y`))"); checkExp("LOCALTIME(3)", "LOCALTIME(3)"); checkExp("LOCALTIME", "`LOCALTIME`"); checkExp("LOCALTIME(x+y)", "LOCALTIME((`X` + `Y`))"); checkExp("LOCALTIMESTAMP(3)", "LOCALTIMESTAMP(3)"); checkExp("LOCALTIMESTAMP", "`LOCALTIMESTAMP`"); checkExp("LOCALTIMESTAMP(x+y)", "LOCALTIMESTAMP((`X` + `Y`))"); checkExp("CURRENT_DATE(3)", "CURRENT_DATE(3)"); checkExp("CURRENT_DATE", "`CURRENT_DATE`"); checkExp("CURRENT_TIMESTAMP(3)", "CURRENT_TIMESTAMP(3)"); checkExp("CURRENT_TIMESTAMP", "`CURRENT_TIMESTAMP`"); checkExp("CURRENT_TIMESTAMP(x+y)", "CURRENT_TIMESTAMP((`X` + `Y`))"); checkExp("DATE '2004-12-01'", "DATE '2004-12-01'"); checkExp("TIME '12:01:01'", "TIME '12:01:01'"); checkExp("TIME '12:01:01.'", "TIME '12:01:01'"); checkExp("TIME '12:01:01.000'", "TIME '12:01:01.000'");
protected SqlNode parseStmtAndHandleEx(String sql) { final SqlNode sqlNode; try { sqlNode = getSqlParser(sql).parseStmt(); } catch (SqlParseException e) { throw new RuntimeException("Error while parsing SQL: " + sql, e); } return sqlNode; }
@Test public void testUpsertValues() { final String expected = "UPSERT INTO `EMPS`\n" + "VALUES (ROW(1, 'Fredkin'))"; final String sql = "upsert into emps values (1,'Fredkin')"; if (isReserved("UPSERT")) { sql(sql) .ok(expected) .node(not(isDdl())); } }
@Test public void testIllegalCursors() { checkFails( "select ^cursor^(select * from emps) from emps", "CURSOR expression encountered in illegal context"); checkFails( "call list(^cursor^(select * from emps))", "CURSOR expression encountered in illegal context"); checkFails( "select f(^cursor^(select * from emps)) from emps", "CURSOR expression encountered in illegal context"); }
protected void checkExp( String sql, String expected) { getTester().checkExp(sql, expected); }
@Test public void testFunction() { check("select substring('Eggs and ham', 1, 3 + 2) || ' benedict' from emp", "SELECT (SUBSTRING('Eggs and ham' FROM 1 FOR (3 + 2)) || ' benedict')\n" + "FROM `EMP`"); checkExp( "log10(1)\r\n+power(2, mod(\r\n3\n\t\t\f\n,ln(4))*log10(5)-6*log10(7/abs(8)+9))*power(10,11)", "(LOG10(1) + (POWER(2, (((MOD(3, LN(4))) * LOG10(5)) - (6 * LOG10(((7 / ABS(8)) + 9))))) * POWER(10, 11)))"); }
@Test public void testArithmeticOperators() { checkExp("1-2+3*4/5/6-7", "(((1 - 2) + (((3 * 4) / 5) / 6)) - 7)"); checkExp("power(2,3)", "POWER(2, 3)"); checkExp("aBs(-2.3e-2)", "ABS(-2.3E-2)"); checkExp("MOD(5 ,\t\f\r\n2)", "(MOD(5, 2))"); checkExp("ln(5.43 )", "LN(5.43)"); checkExp("log10(- -.2 )", "LOG10(0.2)"); }
public void checkExpFails(String sql, String expected) { SqlParserTest.this.checkExpFails( sql.replace("$op", op).replace("$p", period), expected.replace("$op", op)); } }
@Test public void testOverlaps() { final String[] ops = { "overlaps", "equals", "precedes", "succeeds", "immediately precedes", "immediately succeeds" }; final String[] periods = {"period ", ""}; for (String period : periods) { for (String op : ops) { checkPeriodPredicate(new Checker(op, period)); } } }
assumeTrue("don't run this test for sub-classes", isNotSubclass()); final SqlAbstractParserImpl.Metadata metadata = getSqlParser("").getMetadata(); final SortedSet<String> keywords92 = keywords("92"); for (String s : metadata.getTokens()) { if (metadata.isKeyword(s) && metadata.isReservedWord(s)) { + DiffTestCase.diffLines(ImmutableList.copyOf(getReservedKeywords()), ImmutableList.copyOf(reservedKeywords)); assertThat(reason, reservedKeywords, is(getReservedKeywords()));
checkExp("CURRENT_TIME(3)", "CURRENT_TIME(3)"); checkExp("CURRENT_TIME", "`CURRENT_TIME`"); checkExp("CURRENT_TIME(x+y)", "CURRENT_TIME((`X` + `Y`))"); checkExp("LOCALTIME(3)", "LOCALTIME(3)"); checkExp("LOCALTIME", "`LOCALTIME`"); checkExp("LOCALTIME(x+y)", "LOCALTIME((`X` + `Y`))"); checkExp("LOCALTIMESTAMP(3)", "LOCALTIMESTAMP(3)"); checkExp("LOCALTIMESTAMP", "`LOCALTIMESTAMP`"); checkExp("LOCALTIMESTAMP(x+y)", "LOCALTIMESTAMP((`X` + `Y`))"); checkExp("CURRENT_DATE(3)", "CURRENT_DATE(3)"); checkExp("CURRENT_DATE", "`CURRENT_DATE`"); checkExp("CURRENT_TIMESTAMP(3)", "CURRENT_TIMESTAMP(3)"); checkExp("CURRENT_TIMESTAMP", "`CURRENT_TIMESTAMP`"); checkExp("CURRENT_TIMESTAMP(x+y)", "CURRENT_TIMESTAMP((`X` + `Y`))"); checkExp("DATE '2004-12-01'", "DATE '2004-12-01'"); checkExp("TIME '12:01:01'", "TIME '12:01:01'"); checkExp("TIME '12:01:01.'", "TIME '12:01:01'"); checkExp("TIME '12:01:01.000'", "TIME '12:01:01.000'");
@Test public void testColumnAliasWithAs() { check( "select 1 as foo from emp", "SELECT 1 AS `FOO`\n" + "FROM `EMP`"); }