Search in sources :

Example 1 with Hoist

use of org.apache.calcite.tools.Hoist in project calcite by apache.

the class SqlParserTest method testHoist.

/**
 * Tests {@link Hoist}.
 */
@Test
protected void testHoist() {
    final String sql = "select 1 as x,\n" + "  'ab' || 'c' as y\n" + "from emp /* comment with 'quoted string'? */ as e\n" + "where deptno < 40\n" + "and hiredate > date '2010-05-06'";
    final Hoist.Hoisted hoisted = Hoist.create(Hoist.config()).hoist(sql);
    // Simple toString converts each variable to '?N'
    final String expected = "select ?0 as x,\n" + "  ?1 || ?2 as y\n" + "from emp /* comment with 'quoted string'? */ as e\n" + "where deptno < ?3\n" + "and hiredate > ?4";
    assertThat(hoisted.toString(), is(expected));
    // As above, using the function explicitly.
    assertThat(hoisted.substitute(Hoist::ordinalString), is(expected));
    // Simple toString converts each variable to '?N'
    final String expected1 = "select 1 as x,\n" + "  ?1 || ?2 as y\n" + "from emp /* comment with 'quoted string'? */ as e\n" + "where deptno < 40\n" + "and hiredate > date '2010-05-06'";
    assertThat(hoisted.substitute(Hoist::ordinalStringIfChar), is(expected1));
    // Custom function 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 emp /* comment with 'quoted string'? */ as e\n" + "where deptno < [3:DECIMAL:40]\n" + "and hiredate > [4:DATE:2010-05-06]";
    assertThat(hoisted.substitute(SqlParserTest::varToStr), is(expected2));
}
Also used : Hoist(org.apache.calcite.tools.Hoist) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test)

Example 2 with Hoist

use of org.apache.calcite.tools.Hoist 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

Hoist (org.apache.calcite.tools.Hoist)2 Test (org.junit.jupiter.api.Test)2 SqlDialect (org.apache.calcite.sql.SqlDialect)1 MysqlSqlDialect (org.apache.calcite.sql.dialect.MysqlSqlDialect)1 SqlParserTest (org.apache.calcite.sql.parser.SqlParserTest)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1