Java Code Examples for org.apache.calcite.sql.parser.SqlParser#parseExpression()

The following examples show how to use org.apache.calcite.sql.parser.SqlParser#parseExpression() . 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: SqlParamsFinderTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testParamFinder() throws SqlParseException {
    SqlParser sqlParser1 = SqlParser.create("POWER($0, $1) + AVG(LN($3)) + EXP($5)");
    SqlNode sqlPattern = sqlParser1.parseExpression();
    SqlParser sqlParser2 = SqlParser.create("POWER(3, POWER(2, POWER(2, 3))) + AVG(LN(EXP(4))) + EXP(CAST('2018-03-22' AS DATE))");
    SqlNode sqlCall = sqlParser2.parseExpression();

    SqlParamsFinder sqlParamsFinder = new SqlParamsFinder((SqlCall)sqlPattern, (SqlCall)sqlCall);
    Map<Integer, SqlNode> paramNodes =  sqlParamsFinder.getParamNodes();

    Assert.assertEquals("3", paramNodes.get(0).toString());
    Assert.assertEquals("POWER(2, POWER(2, 3))", paramNodes.get(1).toString());
    Assert.assertEquals("EXP(4)", paramNodes.get(3).toString());
    Assert.assertEquals("CAST('2018-03-22' AS DATE)", paramNodes.get(5).toString());

}
 
Example 2
Source File: SqlParamsFinderTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testParamFinder() throws SqlParseException {
    SqlParser sqlParser1 = SqlParser.create("POWER($0, $1) + AVG(LN($3)) + EXP($5)");
    SqlNode sqlPattern = sqlParser1.parseExpression();
    SqlParser sqlParser2 = SqlParser.create("POWER(3, POWER(2, POWER(2, 3))) + AVG(LN(EXP(4))) + EXP(CAST('2018-03-22' AS DATE))");
    SqlNode sqlCall = sqlParser2.parseExpression();

    SqlParamsFinder sqlParamsFinder = new SqlParamsFinder((SqlCall)sqlPattern, (SqlCall)sqlCall);
    Map<Integer, SqlNode> paramNodes =  sqlParamsFinder.getParamNodes();

    Assert.assertEquals("3", paramNodes.get(0).toString());
    Assert.assertEquals("POWER(2, POWER(2, 3))", paramNodes.get(1).toString());
    Assert.assertEquals("EXP(4)", paramNodes.get(3).toString());
    Assert.assertEquals("CAST('2018-03-22' AS DATE)", paramNodes.get(5).toString());

}
 
Example 3
Source File: SqlParamsFinderTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWindowCallParams() throws SqlParseException {
    SqlParser sqlParser1 = SqlParser.create("STDDEV_POP($0) OVER($1)");
    SqlNode sqlPattern = sqlParser1.parseExpression();
    SqlParser sqlParser2 = SqlParser.create("STDDEV_POP(C1) OVER (ORDER BY C1)");
    SqlNode sqlCall = sqlParser2.parseExpression();
    SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance((SqlCall)sqlPattern, (SqlCall)sqlCall, true);
    Map<Integer, SqlNode> paramNodes =  sqlParamsFinder.getParamNodes();

    Assert.assertEquals("C1", paramNodes.get(0).toString());
    Assert.assertEquals("(ORDER BY `C1`)", paramNodes.get(1).toString());
    Assert.assertTrue(paramNodes.get(1) instanceof SqlWindow);
}
 
Example 4
Source File: SqlParamsFinderTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testWindowCallParams() throws SqlParseException {
    SqlParser sqlParser1 = SqlParser.create("STDDEV_POP($0) OVER($1)");
    SqlNode sqlPattern = sqlParser1.parseExpression();
    SqlParser sqlParser2 = SqlParser.create("STDDEV_POP(C1) OVER (ORDER BY C1)");
    SqlNode sqlCall = sqlParser2.parseExpression();
    SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance((SqlCall)sqlPattern, (SqlCall)sqlCall, true);
    Map<Integer, SqlNode> paramNodes =  sqlParamsFinder.getParamNodes();

    Assert.assertEquals("C1", paramNodes.get(0).toString());
    Assert.assertEquals("(ORDER BY `C1`)", paramNodes.get(1).toString());
    Assert.assertTrue(paramNodes.get(1) instanceof SqlWindow);
}
 
Example 5
Source File: InitializerContext.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Parse a column computation expression for a table. Usually this expression is declared
 * in the create table statement, i.e.
 * <pre>
 *   create table t(
 *     a int not null,
 *     b varchar(5) as (my_udf(a)) virtual,
 *     c int not null as (a + 1)
 *   );
 * </pre>
 *
 * <p>You can use the string format expression "my_udf(a)" and "a + 1"
 * as the initializer expression of column b and c.
 *
 * <p>Calcite doesn't really need this now because the DDL nodes
 * can be executed directly from {@code SqlNode}s, but we still provide the way
 * to initialize from a SQL-like string, because a string can be used to persist easily and
 * the column expressions are important part of the table metadata.
 *
 * @param config parse config
 * @param expr   the SQL-style column expression
 * @return a {@code SqlNode} instance
 */
default SqlNode parseExpression(SqlParser.Config config, String expr) {
  SqlParser parser = SqlParser.create(expr, config);
  try {
    return parser.parseExpression();
  } catch (SqlParseException e) {
    throw new RuntimeException("Failed to parse expression " + expr, e);
  }
}
 
Example 6
Source File: CalciteSqlParser.java    From incubator-pinot with Apache License 2.0 3 votes vote down vote up
/**
 * Compiles a String expression into {@link Expression}.
 *
 * @param expression String expression.
 * @return {@link Expression} equivalent of the string.
 *
 * @throws SqlParseException Throws parse exception if String is not a valid expression.
 */
public static Expression compileToExpression(String expression)
    throws SqlParseException {
  SqlParser sqlParser = getSqlParser(expression);
  SqlNode sqlNode = sqlParser.parseExpression();
  return toExpression(sqlNode);
}