Search in sources :

Example 1 with KsqlParser

use of io.confluent.ksql.parser.KsqlParser in project ksql by confluentinc.

the class KsqlEngine method parseQueries.

// Visible for Testing
List<Pair<String, Statement>> parseQueries(final String queriesString, final Map<String, Object> overriddenProperties, final MetaStore tempMetaStore) {
    try {
        MetaStore tempMetaStoreForParser = tempMetaStore.clone();
        // Parse and AST creation
        KsqlParser ksqlParser = new KsqlParser();
        List<SqlBaseParser.SingleStatementContext> parsedStatements = ksqlParser.getStatements(queriesString);
        List<Pair<String, Statement>> queryList = new ArrayList<>();
        for (SqlBaseParser.SingleStatementContext singleStatementContext : parsedStatements) {
            Pair<Statement, DataSourceExtractor> statementInfo = ksqlParser.prepareStatement(singleStatementContext, tempMetaStoreForParser);
            Statement statement = statementInfo.getLeft();
            Pair<String, Statement> queryPair = buildSingleQueryAst(statement, getStatementString(singleStatementContext), tempMetaStore, tempMetaStoreForParser, overriddenProperties);
            if (queryPair != null) {
                queryList.add(queryPair);
            }
        }
        return queryList;
    } catch (Exception e) {
        throw new ParseFailedException("Parsing failed on KsqlEngine msg:" + e.getMessage(), e);
    }
}
Also used : DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) Statement(io.confluent.ksql.parser.tree.Statement) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) ArrayList(java.util.ArrayList) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) KsqlException(io.confluent.ksql.util.KsqlException) MetaStore(io.confluent.ksql.metastore.MetaStore) SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) KsqlParser(io.confluent.ksql.parser.KsqlParser) Pair(io.confluent.ksql.util.Pair)

Example 2 with KsqlParser

use of io.confluent.ksql.parser.KsqlParser in project ksql by confluentinc.

the class KsqlResource method getStatementStrings.

public List<String> getStatementStrings(String ksqlString) {
    List<SqlBaseParser.SingleStatementContext> statementContexts = new KsqlParser().getStatements(ksqlString);
    List<String> result = new ArrayList<>(statementContexts.size());
    for (SqlBaseParser.SingleStatementContext statementContext : statementContexts) {
        // Taken from http://stackoverflow
        // .com/questions/16343288/how-do-i-get-the-original-text-that-an-antlr4-rule-matched
        CharStream charStream = statementContext.start.getInputStream();
        result.add(charStream.getText(new Interval(statementContext.start.getStartIndex(), statementContext.stop.getStopIndex())));
    }
    return result;
}
Also used : SqlBaseParser(io.confluent.ksql.parser.SqlBaseParser) KsqlParser(io.confluent.ksql.parser.KsqlParser) ArrayList(java.util.ArrayList) CharStream(org.antlr.v4.runtime.CharStream) Interval(org.antlr.v4.runtime.misc.Interval)

Aggregations

KsqlParser (io.confluent.ksql.parser.KsqlParser)2 SqlBaseParser (io.confluent.ksql.parser.SqlBaseParser)2 ArrayList (java.util.ArrayList)2 MetaStore (io.confluent.ksql.metastore.MetaStore)1 ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)1 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)1 Statement (io.confluent.ksql.parser.tree.Statement)1 DataSourceExtractor (io.confluent.ksql.util.DataSourceExtractor)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Pair (io.confluent.ksql.util.Pair)1 CharStream (org.antlr.v4.runtime.CharStream)1 Interval (org.antlr.v4.runtime.misc.Interval)1