org.apache.calcite.sql.parser.SqlParserTest Java Examples

The following examples show how to use org.apache.calcite.sql.parser.SqlParserTest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BabelParserTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** 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.configBuilder())
                  .setParserFactory(SqlBabelParserImpl::new)
                  .build()))
          .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));
}
 
Example #2
Source File: DocumentationTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** 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().getSqlParser("").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);
  }
}