Search in sources :

Example 1 with ParseFailedException

use of io.confluent.ksql.parser.exception.ParseFailedException in project ksql by confluentinc.

the class KsqlParserTest method testShouldFailIfWrongKeyword.

@Test
public void testShouldFailIfWrongKeyword() throws Exception {
    try {
        String simpleQuery = "SELLECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
        Statement statement = KSQL_PARSER.buildAst(simpleQuery, metaStore).get(0);
        Assert.fail();
    } catch (ParseFailedException e) {
        String errorMessage = e.getMessage();
        Assert.assertTrue(errorMessage.toLowerCase().contains(("line 1:1: mismatched input 'SELLECT'" + " expecting").toLowerCase()));
    }
}
Also used : Statement(io.confluent.ksql.parser.tree.Statement) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) Test(org.junit.Test)

Example 2 with ParseFailedException

use of io.confluent.ksql.parser.exception.ParseFailedException in project ksql by confluentinc.

the class KsqlParser method buildAst.

/**
 * Builds an AST from the given query string.
 */
public List<Statement> buildAst(String sql, MetaStore metaStore) {
    try {
        ParserRuleContext tree = getParseTree(sql);
        SqlBaseParser.StatementsContext statementsContext = (SqlBaseParser.StatementsContext) tree;
        List<Statement> astNodes = new ArrayList<>();
        for (SqlBaseParser.SingleStatementContext statementContext : statementsContext.singleStatement()) {
            DataSourceExtractor dataSourceExtractor = new DataSourceExtractor(metaStore);
            dataSourceExtractor.extractDataSources(statementContext);
            Node root = new AstBuilder(dataSourceExtractor).visit(statementContext);
            Statement statement = (Statement) root;
            astNodes.add(statement);
        }
        return astNodes;
    } catch (Exception e) {
        // if we fail, parse with LL mode
        throw new ParseFailedException(e.getMessage(), e);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DataSourceExtractor(io.confluent.ksql.util.DataSourceExtractor) Statement(io.confluent.ksql.parser.tree.Statement) Node(io.confluent.ksql.parser.tree.Node) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) ArrayList(java.util.ArrayList) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 3 with ParseFailedException

use of io.confluent.ksql.parser.exception.ParseFailedException in project ksql by confluentinc.

the class KsqlParser method getStatements.

public List<SqlBaseParser.SingleStatementContext> getStatements(String sql) {
    try {
        ParserRuleContext tree = getParseTree(sql);
        SqlBaseParser.StatementsContext statementsContext = (SqlBaseParser.StatementsContext) tree;
        return statementsContext.singleStatement();
    } catch (Exception e) {
        throw new ParseFailedException(e.getMessage(), e);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 4 with ParseFailedException

use of io.confluent.ksql.parser.exception.ParseFailedException 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)

Aggregations

ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)4 Statement (io.confluent.ksql.parser.tree.Statement)3 DataSourceExtractor (io.confluent.ksql.util.DataSourceExtractor)2 ArrayList (java.util.ArrayList)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 MetaStore (io.confluent.ksql.metastore.MetaStore)1 KsqlParser (io.confluent.ksql.parser.KsqlParser)1 SqlBaseParser (io.confluent.ksql.parser.SqlBaseParser)1 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)1 Node (io.confluent.ksql.parser.tree.Node)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Pair (io.confluent.ksql.util.Pair)1 Test (org.junit.Test)1