Java Code Examples for org.apache.calcite.sql.parser.SqlParser#parseStmt()
The following examples show how to use
org.apache.calcite.sql.parser.SqlParser#parseStmt() .
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: SqlParseUtil.java From alchemy with Apache License 2.0 | 6 votes |
public static void parse(List<String> sqls, List<String> sources, List<String> udfs, List<String> sinks) throws SqlParseException { for (String sql : sqls) { SqlParser sqlParser = SqlParser.create(sql, CONFIG); SqlNode sqlNode = sqlParser.parseStmt(); SqlKind kind = sqlNode.getKind(); switch (kind){ case INSERT: SqlInsert sqlInsert = (SqlInsert)sqlNode; addSink(sinks, findSinkName(sqlInsert)); SqlSelect source = (SqlSelect) sqlInsert.getSource(); parseSource(source, sources, udfs); break; case SELECT: parseSource((SqlSelect) sqlNode, sources, udfs); break; default: throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql); } } }
Example 2
Source File: AbstractMaterializedViewTest.java From calcite with Apache License 2.0 | 6 votes |
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema, SchemaPlus defaultSchema, String sql) throws SqlParseException { final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT); final SqlNode parsed = parser.parseStmt(); final CalciteCatalogReader catalogReader = new CalciteCatalogReader( CalciteSchema.from(rootSchema), CalciteSchema.from(defaultSchema).path(null), new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties())); final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(), catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT); final SqlNode validated = validator.validate(parsed); final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder() .withTrimUnusedFields(true) .withExpand(true) .withDecorrelationEnabled(true) .build(); final SqlToRelConverter converter = new SqlToRelConverter( (rowType, queryString, schemaPath, viewPath) -> { throw new UnsupportedOperationException("cannot expand view"); }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config); return converter.convertQuery(validated, false, true).rel; }
Example 3
Source File: CalcitePrepareImpl.java From calcite with Apache License 2.0 | 6 votes |
/** Shared implementation for {@link #parse}, {@link #convert} and * {@link #analyzeView}. */ private ParseResult parse_(Context context, String sql, boolean convert, boolean analyze, boolean fail) { final JavaTypeFactory typeFactory = context.getTypeFactory(); CalciteCatalogReader catalogReader = new CalciteCatalogReader( context.getRootSchema(), context.getDefaultSchemaPath(), typeFactory, context.config()); SqlParser parser = createParser(sql); SqlNode sqlNode; try { sqlNode = parser.parseStmt(); } catch (SqlParseException e) { throw new RuntimeException("parse failed", e); } final SqlValidator validator = createSqlValidator(context, catalogReader); SqlNode sqlNode1 = validator.validate(sqlNode); if (convert) { return convert_( context, sql, analyze, fail, catalogReader, validator, sqlNode1); } return new ParseResult(this, validator, sql, sqlNode1, validator.getValidatedNodeType(sqlNode1)); }
Example 4
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 5
Source File: QueryRecord.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 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 6
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 7
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 8
Source File: PlannerImpl.java From calcite with Apache License 2.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 9
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 10
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 11
Source File: CalciteSqlParser.java From sylph with Apache License 2.0 | 5 votes |
public List<Object> getPlan(String joinSql, SqlParser.Config sqlParserConfig) throws SqlParseException { SqlParser sqlParser = SqlParser.create(joinSql, sqlParserConfig); SqlNode sqlNode = sqlParser.parseStmt(); SqlNode rootNode = sqlParse(sqlNode); plan.add(rootNode); return plan; }
Example 12
Source File: SqlParseUtil.java From alchemy with Apache License 2.0 | 5 votes |
public static String parseSinkName(String sql) throws SqlParseException { SqlParser sqlParser = SqlParser.create(sql, CONFIG); SqlNode sqlNode = sqlParser.parseStmt(); SqlKind sqlKind = sqlNode.getKind(); if (sqlKind != SqlKind.INSERT) { throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql); } return findSinkName((SqlInsert)sqlNode); }
Example 13
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 14
Source File: MysqlSideFunction.java From alchemy with Apache License 2.0 | 5 votes |
private String modifySql(SideTable sideTable) throws SqlParseException { SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.MYSQL).build(); SqlParser sqlParser = SqlParser.create(sideTable.getSql(), config); SqlNode sqlNode = sqlParser.parseStmt(); if (SqlKind.SELECT != sqlNode.getKind()) { throw new UnsupportedOperationException( "MysqlAsyncReqRow only support query sql, sql:" + sideTable.getSql()); } SqlSelect sqlSelect = (SqlSelect)sqlNode; SqlNode whereNode = sqlSelect.getWhere(); SqlBinaryOperator and = new SqlBinaryOperator("AND", SqlKind.AND, 24, true, ReturnTypes.BOOLEAN_NULLABLE_OPTIMIZED, InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN); List<SqlBasicCall> conditionNodes = createConditionNodes(sideTable.getConditions(), sideTable.getSideAlias()); List<SqlNode> nodes = new ArrayList<>(); nodes.addAll(conditionNodes); if (whereNode != null) { nodes.add(whereNode); } else { SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE, InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED); SqlBasicCall andEqual = new SqlBasicCall(equal, SideParser.createEqualNodes(SqlKind.AND), new SqlParserPos(0, 0)); nodes.add(andEqual); } SqlBasicCall sqlBasicCall = new SqlBasicCall(and, nodes.toArray(new SqlNode[nodes.size()]), new SqlParserPos(0, 0)); sqlSelect.setWhere(sqlBasicCall); return sqlSelect.toString(); }
Example 15
Source File: SideParser.java From alchemy with Apache License 2.0 | 5 votes |
public static Deque<SqlNode> parse(String sql) throws SqlParseException { SqlParser sqlParser = SqlParser.create(sql, CONFIG); SqlNode sqlNode = sqlParser.parseStmt(); Deque<SqlNode> deque = new ArrayDeque<>(); parse(sqlNode, deque); return deque; }
Example 16
Source File: SqlConverter.java From Bats with Apache License 2.0 | 5 votes |
public SqlNode parse(String sql) { try { SqlParser parser = SqlParser.create(sql, parserConfig); return parser.parseStmt(); } catch (SqlParseException e) { UserException.Builder builder = UserException .parseError(e) .addContext("SQL Query", formatSQLParsingError(sql, e.getPos())); if (isInnerQuery) { builder.message("Failure parsing a view your query is dependent upon."); } throw builder.build(logger); } }
Example 17
Source File: TestDDLAliases.java From dremio-oss with Apache License 2.0 | 4 votes |
private SqlNode parse(String toParse) throws SqlParseException{ ParserConfig config = new ParserConfig(Quoting.DOUBLE_QUOTE, 255); SqlParser parser = SqlParser.create(toParse, config); return parser.parseStmt(); }
Example 18
Source File: TestTransformer.java From dremio-oss with Apache License 2.0 | 4 votes |
@Test public void testUpdateSql() throws Exception { final String sql = "select foo, bar as b from tbl"; SqlParser parser = SqlParser.create(sql, new ParserConfig(Quoting.DOUBLE_QUOTE, 128)); final SqlNode sqlNode = parser.parseStmt(); final JavaTypeFactory typeFactory = JavaTypeFactoryImpl.INSTANCE; final RelDataType rowType = new RelRecordType(Arrays.<RelDataTypeField>asList( new RelDataTypeFieldImpl("foo", 0, typeFactory.createSqlType(SqlTypeName.INTEGER)), new RelDataTypeFieldImpl("b", 0, typeFactory.createSqlType(SqlTypeName.INTEGER)) )); final QueryMetadata metadata = Mockito.mock(QueryMetadata.class); Mockito.when(metadata.getSqlNode()).thenReturn(Optional.of(sqlNode)); Mockito.when(metadata.getRowType()).thenReturn(rowType); Mockito.when(metadata.getQuerySql()).thenReturn(sql); Mockito.when(metadata.getReferredTables()).thenReturn(Optional.absent()); TransformActor actor = new TransformActor(state, false, "test_user", null) { @Override protected com.dremio.service.jobs.metadata.proto.QueryMetadata getMetadata(SqlQuery query) { return com.dremio.service.jobs.metadata.proto.QueryMetadata.newBuilder() .setState(QuerySemantics.extract(metadata).get()) .build(); } @Override protected boolean hasMetadata() { return true; } @Override protected com.dremio.service.jobs.metadata.proto.QueryMetadata getMetadata() { throw new IllegalStateException("not implemented"); } @Override protected Optional<BatchSchema> getBatchSchema() { throw new IllegalStateException("not implemented"); } @Override protected Optional<List<ParentDatasetInfo>> getParents() { throw new IllegalStateException("not implemented"); } @Override protected Optional<List<FieldOrigin>> getFieldOrigins() { throw new IllegalStateException("not implemented"); } @Override protected Optional<List<ParentDataset>> getGrandParents() { throw new IllegalStateException("not implemented"); } }; TransformResult result = new TransformUpdateSQL(sql).accept(actor); VirtualDatasetState newState = result.getNewState(); assertEquals(2, newState.getColumnsList().size()); assertEquals("foo", newState.getColumnsList().get(0).getName()); assertEquals(new ExpColumnReference("foo").wrap(), newState.getColumnsList().get(0).getValue()); assertEquals("b", newState.getColumnsList().get(1).getName()); assertEquals(new ExpColumnReference("bar").wrap(), newState.getColumnsList().get(1).getValue()); }
Example 19
Source File: TestAccelParser.java From dremio-oss with Apache License 2.0 | 4 votes |
private SqlNode parse(String toParse) throws SqlParseException{ ParserConfig config = new ParserConfig(Quoting.DOUBLE_QUOTE, 255); SqlParser parser = SqlParser.create(toParse, config); return parser.parseStmt(); }
Example 20
Source File: SqlAdvisor.java From calcite with Apache License 2.0 | 2 votes |
/** * Wrapper function to parse a SQL query (SELECT or VALUES, but not INSERT, * UPDATE, DELETE, CREATE, DROP etc.), throwing a {@link SqlParseException} * if the statement is not syntactically valid. * * @param sql SQL statement * @return parse tree * @throws SqlParseException if not syntactically valid */ protected SqlNode parseQuery(String sql) throws SqlParseException { SqlParser parser = SqlParser.create(sql, parserConfig); return parser.parseStmt(); }