Search in sources :

Example 1 with SqlParserTest

use of org.apache.calcite.sql.parser.SqlParserTest in project calcite by apache.

the class DocumentationTest method testGenerateKeyWords.

/**
 * Generates a copy of {@code reference.md} with the current set of key
 * words. Fails if the copy is different from the original.
 */
@Test
void testGenerateKeyWords() throws IOException {
    final FileFixture f = new FileFixture();
    f.outFile.getParentFile().mkdirs();
    try (BufferedReader r = Util.reader(f.inFile);
        FileOutputStream fos = new FileOutputStream(f.outFile);
        PrintWriter w = Util.printWriter(f.outFile)) {
        String line;
        int stage = 0;
        while ((line = r.readLine()) != null) {
            if (line.equals("{% comment %} end {% endcomment %}")) {
                ++stage;
            }
            if (stage != 1) {
                w.println(line);
            }
            if (line.equals("{% comment %} start {% endcomment %}")) {
                ++stage;
                SqlAbstractParserImpl.Metadata metadata = new SqlParserTest().fixture().parser().getMetadata();
                int z = 0;
                for (String s : metadata.getTokens()) {
                    if (z++ > 0) {
                        w.println(",");
                    }
                    if (metadata.isKeyword(s)) {
                        w.print(metadata.isReservedWord(s) ? ("**" + s + "**") : s);
                    }
                }
                w.println(".");
            }
        }
        w.flush();
        fos.flush();
        fos.getFD().sync();
    }
    String diff = DiffTestCase.diff(f.outFile, f.inFile);
    if (!diff.isEmpty()) {
        throw new AssertionError("Mismatch between " + f.outFile + " and " + f.inFile + ":\n" + diff);
    }
}
Also used : SqlParserTest(org.apache.calcite.sql.parser.SqlParserTest) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) PrintWriter(java.io.PrintWriter) SqlAbstractParserImpl(org.apache.calcite.sql.parser.SqlAbstractParserImpl) SqlParserTest(org.apache.calcite.sql.parser.SqlParserTest) Test(org.junit.jupiter.api.Test)

Example 2 with SqlParserTest

use of org.apache.calcite.sql.parser.SqlParserTest in project calcite by apache.

the class BabelParserTest method testHoistMySql.

/**
 * Similar to {@link #testHoist()} but using custom parser.
 */
@Test
void testHoistMySql() {
    // SQL contains back-ticks, which require MySQL's quoting,
    // and DATEADD, which requires Babel.
    final String sql = "select 1 as x,\n" + "  'ab' || 'c' as y\n" + "from `my emp` /* comment with 'quoted string'? */ as e\n" + "where deptno < 40\n" + "and DATEADD(day, 1, hiredate) > date '2010-05-06'";
    final SqlDialect dialect = MysqlSqlDialect.DEFAULT;
    final Hoist.Hoisted hoisted = Hoist.create(Hoist.config().withParserConfig(dialect.configureParser(SqlParser.config()).withParserFactory(SqlBabelParserImpl::new))).hoist(sql);
    // Simple toString converts each variable to '?N'
    final String expected = "select ?0 as x,\n" + "  ?1 || ?2 as y\n" + "from `my emp` /* comment with 'quoted string'? */ as e\n" + "where deptno < ?3\n" + "and DATEADD(day, ?4, hiredate) > ?5";
    assertThat(hoisted.toString(), is(expected));
    // Custom string converts variables to '[N:TYPE:VALUE]'
    final String expected2 = "select [0:DECIMAL:1] as x,\n" + "  [1:CHAR:ab] || [2:CHAR:c] as y\n" + "from `my emp` /* comment with 'quoted string'? */ as e\n" + "where deptno < [3:DECIMAL:40]\n" + "and DATEADD(day, [4:DECIMAL:1], hiredate) > [5:DATE:2010-05-06]";
    assertThat(hoisted.substitute(SqlParserTest::varToStr), is(expected2));
}
Also used : SqlDialect(org.apache.calcite.sql.SqlDialect) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) Hoist(org.apache.calcite.tools.Hoist) SqlParserTest(org.apache.calcite.sql.parser.SqlParserTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqlParserTest (org.apache.calcite.sql.parser.SqlParserTest)2 Test (org.junit.jupiter.api.Test)2 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1 SqlDialect (org.apache.calcite.sql.SqlDialect)1 MysqlSqlDialect (org.apache.calcite.sql.dialect.MysqlSqlDialect)1 SqlAbstractParserImpl (org.apache.calcite.sql.parser.SqlAbstractParserImpl)1 Hoist (org.apache.calcite.tools.Hoist)1