Java Code Examples for org.apache.calcite.sql.parser.SqlParser#create()
The following examples show how to use
org.apache.calcite.sql.parser.SqlParser#create() .
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: ParserFactory.java From quark with Apache License 2.0 | 6 votes |
public Parser getParser(String sql, Properties info) throws SQLException { SqlParser parser = SqlParser.create(sql, SqlParser.configBuilder() .setQuotedCasing(Casing.UNCHANGED) .setUnquotedCasing(Casing.UNCHANGED) .setQuoting(Quoting.DOUBLE_QUOTE) .setParserFactory(QuarkParserImpl.FACTORY) .build()); SqlNode sqlNode; try { sqlNode = parser.parseStmt(); } catch (SqlParseException e) { throw new RuntimeException( "parse failed: " + e.getMessage(), e); } if (sqlNode.getKind().equals(SqlKind.OTHER_DDL)) { return new DDLParser(); } else { return getSqlQueryParser(info); } }
Example 2
Source File: SqlParamsFinderTest.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
@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: SqlParseUtil.java From alchemy with Apache License 2.0 | 5 votes |
public static List<String> findQuerySql(List<String> sqls) throws SqlParseException { List<String> newSqls = new ArrayList<>(sqls.size()); for (String sql : sqls) { SqlParser sqlParser = SqlParser.create(sql, CONFIG); SqlNode sqlNode = sqlParser.parseStmt(); if (sqlNode.getKind() != SqlKind.INSERT) { throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql); } SqlInsert sqlInsert = (SqlInsert)sqlNode; newSqls.add(sqlInsert.getSource().toString()); } return newSqls; }
Example 4
Source File: SqlParamsFinderTest.java From kylin with Apache License 2.0 | 5 votes |
@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: ParserBenchmark.java From calcite with Apache License 2.0 | 5 votes |
@Setup public void setup() throws SqlParseException { StringBuilder sb = new StringBuilder((int) (length * 1.2)); sb.append("select 1"); Random rnd = new Random(); rnd.setSeed(424242); for (; sb.length() < length;) { for (int i = 0; i < 7 && sb.length() < length; i++) { sb.append(", "); switch (rnd.nextInt(3)) { case 0: sb.append("?"); break; case 1: sb.append(rnd.nextInt()); break; case 2: sb.append('\'').append(rnd.nextLong()).append(rnd.nextLong()) .append('\''); break; } } if (comments && sb.length() < length) { sb.append("// sb.append('\\'').append(rnd.nextLong()).append(rnd.nextLong()).append(rnd" + ".nextLong())"); } sb.append('\n'); } sb.append(" from dual"); parser = SqlParser.create("values(1)"); sql = sb.toString(); }
Example 6
Source File: PlannerImpl.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public SqlNode parse(final Reader reader) throws SqlParseException { switch (state) { case STATE_0_CLOSED: case STATE_1_RESET: ready(); } ensure(State.STATE_2_READY); SqlParser parser = SqlParser.create(reader, parserConfig); SqlNode sqlNode = parser.parseStmt(); state = State.STATE_3_PARSED; return sqlNode; }
Example 7
Source File: KSqlParser.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** Parser sql mapper kafka tree. */ public static TopicPartitionSchema parserTopic(String sql) { TopicPartitionSchema tps = new TopicPartitionSchema(); try { SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.JAVA).build(); SqlParser sqlParser = SqlParser.create(sql, config); SqlNode sqlNode = sqlParser.parseStmt(); parseNode(sqlNode, tps); } catch (Exception e) { ErrorUtils.print(KSqlParser.class).error("Parser kafka sql has error, msg is ", e); } return tps; }
Example 8
Source File: QueryMetricsUtil.java From nifi with Apache License 2.0 | 5 votes |
@Override public ValidationResult validate(final String subject, final String input, final ValidationContext context) { if (context.isExpressionLanguagePresent(input)) { return new ValidationResult.Builder() .input(input) .subject(subject) .valid(true) .explanation("Expression Language Present") .build(); } final String substituted = context.newPropertyValue(input).evaluateAttributeExpressions().getValue(); final SqlParser.Config config = SqlParser.configBuilder() .setLex(Lex.MYSQL_ANSI) .build(); final SqlParser parser = SqlParser.create(substituted, config); try { parser.parseStmt(); return new ValidationResult.Builder() .subject(subject) .input(input) .valid(true) .build(); } catch (final Exception e) { return new ValidationResult.Builder() .subject(subject) .input(input) .valid(false) .explanation("Not a valid SQL Statement: " + e.getMessage()) .build(); } }
Example 9
Source File: SqlParamsFinderTest.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
@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 10
Source File: CalciteParser.java From flink with Apache License 2.0 | 5 votes |
/** * Parses a SQL statement into a {@link SqlNode}. The {@link SqlNode} is not yet validated. * * @param sql a sql string to parse * @return a parsed sql node * @throws SqlParserException if an exception is thrown when parsing the statement */ public SqlNode parse(String sql) { try { SqlParser parser = SqlParser.create(sql, config); return parser.parseStmt(); } catch (SqlParseException e) { throw new SqlParserException("SQL parse failed. " + e.getMessage(), e); } }
Example 11
Source File: FlinkDDLDataTypeTest.java From flink with Apache License 2.0 | 5 votes |
private SqlParser getSqlParser(String sql) { return SqlParser.create(sql, SqlParser.configBuilder() .setParserFactory(FlinkSqlParserImpl.FACTORY) .setQuoting(Quoting.BACK_TICK) .setUnquotedCasing(Casing.UNCHANGED) .setQuotedCasing(Casing.UNCHANGED) .setConformance(conformance) .build()); }
Example 12
Source File: FlinkSqlParserImplTest.java From flink with Apache License 2.0 | 5 votes |
protected SqlParser getSqlParser(Reader source) { if (conformance0 == null) { return super.getSqlParser(source); } else { // overwrite the default sql conformance. return SqlParser.create(source, SqlParser.configBuilder() .setParserFactory(parserImplFactory()) .setQuoting(Quoting.DOUBLE_QUOTE) .setUnquotedCasing(Casing.TO_UPPER) .setQuotedCasing(Casing.UNCHANGED) .setConformance(conformance0) .build()); } }
Example 13
Source File: SqlQueryParser.java From quark with Apache License 2.0 | 5 votes |
public SqlParser getSqlParser(String sql) { try { final CalciteConnectionConfig config = context.getCfg(); return SqlParser.create(sql, SqlParser.configBuilder() .setQuotedCasing(config.quotedCasing()) .setUnquotedCasing(config.unquotedCasing()) .setQuoting(config.quoting()) .build()); } catch (Exception e) { return SqlParser.create(sql); } }
Example 14
Source File: CalciteParser.java From kylin with Apache License 2.0 | 4 votes |
public static SqlNode parse(String sql) throws SqlParseException { SqlParser.ConfigBuilder parserBuilder = SqlParser.configBuilder(); SqlParser sqlParser = SqlParser.create(sql, parserBuilder.build()); return sqlParser.parseQuery(); }
Example 15
Source File: BatsEngine.java From Bats with Apache License 2.0 | 4 votes |
public static SqlNode parse(String sql, SqlParser.Config config) throws SqlParseException { SqlParser sqlParser = SqlParser.create(sql, config); return sqlParser.parseQuery(); }
Example 16
Source File: CalcitePrepareImpl.java From calcite with Apache License 2.0 | 4 votes |
/** Factory method for SQL parser with a given configuration. */ protected SqlParser createParser(String sql, SqlParser.ConfigBuilder parserConfig) { return SqlParser.create(sql, parserConfig.build()); }
Example 17
Source File: SqlTestFactory.java From calcite with Apache License 2.0 | 4 votes |
public SqlParser createParser(String sql) { return SqlParser.create(new SourceStringReader(sql), parserConfig.get()); }
Example 18
Source File: BatsOptimizerTest.java From Bats with Apache License 2.0 | 4 votes |
static SqlParser createSqlParser(String sql) throws Exception { SqlParser.Config config = SqlParser.configBuilder().setUnquotedCasing(org.apache.calcite.util.Casing.TO_LOWER) .setParserFactory(org.apache.drill.exec.planner.sql.parser.impl.DrillParserImpl.FACTORY).build(); SqlParser sqlParser = SqlParser.create(sql, config); return sqlParser; }
Example 19
Source File: InitializerContext.java From calcite with Apache License 2.0 | 3 votes |
/** * 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 20
Source File: SqlAdvisor.java From Bats with Apache License 2.0 | 2 votes |
/** * Returns the underlying Parser metadata. * * <p>To use a different parser (recognizing a different dialect of SQL), * derived class should override. * * @return metadata */ protected SqlAbstractParserImpl.Metadata getParserMetadata() { SqlParser parser = SqlParser.create("", parserConfig); return parser.getMetadata(); }