@Test public void testTabStop() { check( "SELECT *\n\tFROM mytable", "SELECT *\n" + "FROM `MYTABLE`"); // make sure that the tab stops do not affect the placement of the // error tokens checkFails( "SELECT *\tFROM mytable\t\tWHERE x ^=^ = y AND b = 1", "(?s).*Encountered \"= =\" at line 1, column 32\\..*"); }
@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 one, 'x' as x, null as n from emp", "SELECT 1 AS `ONE`, '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.123'"); checkExp( "TIMESTAMP '2004-06-01 15:55:55.1236'", "TIMESTAMP '2004-06-01 15:55:55.124'"); checkExp( "TIMESTAMP '2004-06-01 15:55:55.9999'", "TIMESTAMP '2004-06-01 15:55:56.000'"); checkExpSame("NULL"); }
subTestIntervalYearPositive(); subTestIntervalYearToMonthPositive(); subTestIntervalMonthPositive(); subTestIntervalDayPositive(); subTestIntervalDayToHourPositive(); subTestIntervalDayToMinutePositive(); subTestIntervalDayToSecondPositive(); subTestIntervalHourPositive(); subTestIntervalHourToMinutePositive(); subTestIntervalHourToSecondPositive(); subTestIntervalMinutePositive(); subTestIntervalMinuteToSecondPositive(); subTestIntervalSecondPositive(); subTestIntervalYearFailsValidation(); subTestIntervalYearToMonthFailsValidation(); subTestIntervalMonthFailsValidation(); subTestIntervalDayFailsValidation(); subTestIntervalDayToHourFailsValidation(); subTestIntervalDayToMinuteFailsValidation(); subTestIntervalDayToSecondFailsValidation(); subTestIntervalHourFailsValidation(); subTestIntervalHourToMinuteFailsValidation(); subTestIntervalHourToSecondFailsValidation(); subTestIntervalMinuteFailsValidation(); subTestIntervalMinuteToSecondFailsValidation(); subTestIntervalSecondFailsValidation();
@Test public void testJoinCross() { check( "select * from a as a2 cross join b", "SELECT *\n" + "FROM `A` AS `A2`\n" + "CROSS JOIN `B`"); }
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'"); checkExp("TIME '12:01:01.001'", "TIME '12:01:01.001'");
@Test public void testBracketIdentifier() { quoting = Quoting.BRACKET; checkExp("ab", "`AB`"); checkExp(" [a \" b!c]", "`a \" b!c`"); checkExpFails(" ^`^a \" b!c`", "(?s).*Encountered.*"); checkExpFails(" ^\"^a \"\" b!c\"", "(?s).*Encountered.*"); checkExp("[x`y`z]", "`x``y``z`"); checkExpFails("^\"^x`y`z\"", "(?s).*Encountered.*"); checkExpFails("^`^x``y``z`", "(?s).*Encountered.*"); checkExp("[anything [even brackets]] is].[ok]", "`anything [even brackets] is`.`ok`"); // What would be a call to the 'item' function in DOUBLE_QUOTE and BACK_TICK // is a table alias. check("select * from myMap[field], myArray[1 + 2]", "SELECT *\n" + "FROM `MYMAP` AS `field`,\n" + "`MYARRAY` AS `1 + 2`"); check("select * from myMap [field], myArray [1 + 2]", "SELECT *\n" + "FROM `MYMAP` AS `field`,\n" + "`MYARRAY` AS `1 + 2`"); }
@Test public void testArthimeticOperators() { 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))"); }
@Test public void testIllegalCursors() { checkFails( "select ^cursor^(select * from emps) from emps", "CURSOR expression encountered in illegal context"); checkFails( "call p(^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 check( String sql, String expected) { getTester().check(sql, expected); }
protected SqlNode parseStmtAndHandleEx(String sql) { final SqlNode sqlNode; try { sqlNode = parseStmt(sql); } catch (SqlParseException e) { e.printStackTrace(); String message = "Received error while parsing SQL '" + sql + "'; error is:\n" + e.toString(); throw new AssertionError(message); } return sqlNode; }
protected SqlNode parseExpressionAndHandleEx(String sql) { final SqlNode sqlNode; try { sqlNode = parseExpression(sql); } catch (SqlParseException e) { String message = "Received error while parsing SQL '" + sql + "'; error is:\n" + e.toString(); throw new RuntimeException(message, e); } return sqlNode; }
/** * Tests that you can't quote the names of builtin functions. * * @see org.eigenbase.test.SqlValidatorTest#testQuotedFunction() */ @Test public void testQuotedFunction() { checkExpFails( "\"CAST\"(1 ^as^ double)", "(?s).*Encountered \"as\" at .*"); checkExpFails( "\"POSITION\"('b' ^in^ 'alphabet')", "(?s).*Encountered \"in \\\\'alphabet\\\\'\" at .*"); checkExpFails( "\"OVERLAY\"('a' ^PLAcing^ 'b' from 1)", "(?s).*Encountered \"PLAcing\" at.*"); checkExpFails( "\"SUBSTRING\"('a' ^from^ 1)", "(?s).*Encountered \"from\" at .*"); }
subTestIntervalYearPositive(); subTestIntervalYearToMonthPositive(); subTestIntervalMonthPositive(); subTestIntervalDayPositive(); subTestIntervalDayToHourPositive(); subTestIntervalDayToMinutePositive(); subTestIntervalDayToSecondPositive(); subTestIntervalHourPositive(); subTestIntervalHourToMinutePositive(); subTestIntervalHourToSecondPositive(); subTestIntervalMinutePositive(); subTestIntervalMinuteToSecondPositive(); subTestIntervalSecondPositive(); subTestIntervalYearFailsValidation(); subTestIntervalYearToMonthFailsValidation(); subTestIntervalMonthFailsValidation(); subTestIntervalDayFailsValidation(); subTestIntervalDayToHourFailsValidation(); subTestIntervalDayToMinuteFailsValidation(); subTestIntervalDayToSecondFailsValidation(); subTestIntervalHourFailsValidation(); subTestIntervalHourToMinuteFailsValidation(); subTestIntervalHourToSecondFailsValidation(); subTestIntervalMinuteFailsValidation(); subTestIntervalMinuteToSecondFailsValidation(); subTestIntervalSecondFailsValidation();
@Test public void testSelectDistinct() { check( "select distinct foo from bar", "SELECT DISTINCT `FOO`\n" + "FROM `BAR`"); }
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'"); checkExp("TIME '12:01:01.001'", "TIME '12:01:01.001'");
@Test public void testSubstring() { checkExp("substring('a' \n FROM \t 1)", "SUBSTRING('a' FROM 1)"); checkExp("substring('a' FROM 1 FOR 3)", "SUBSTRING('a' FROM 1 FOR 3)"); checkExp( "substring('a' FROM 'reg' FOR '\\')", "SUBSTRING('a' FROM 'reg' FOR '\\')"); checkExp( "substring('a', 'reg', '\\')", "SUBSTRING('a' FROM 'reg' FOR '\\')"); checkExp("substring('a', 1, 2)", "SUBSTRING('a' FROM 1 FOR 2)"); checkExp("substring('a' , 1)", "SUBSTRING('a' FROM 1)"); }
@Test public void testIllegalCursors() { checkFails( "select ^cursor^(select * from emps) from emps", "CURSOR expression encountered in illegal context"); checkFails( "call p(^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"); }