Search in sources :

Example 1 with TypeRegistry

use of io.confluent.ksql.metastore.TypeRegistry in project ksql by confluentinc.

the class SchemaParser method parse.

public TableElements parse(final String schema) {
    if (schema.trim().isEmpty()) {
        return TableElements.of();
    }
    final SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString("(" + schema + ")")));
    final CommonTokenStream tokStream = new CommonTokenStream(lexer);
    final SqlBaseParser parser = new SqlBaseParser(tokStream);
    final BaseErrorListener errorListener = new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
            throw new KsqlException(String.format("Error parsing schema \"%s\" at %d:%d: %s", schema, line, charPositionInLine, msg), e);
        }
    };
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    final SqlTypeParser typeParser = SqlTypeParser.create(typeRegistry);
    final List<TableElement> elements = parser.tableElements().tableElement().stream().map(ctx -> new TableElement(getLocation(ctx), ColumnName.of(ParserUtil.getIdentifierText(ctx.identifier())), typeParser.getType(ctx.type()), ParserUtil.getColumnConstraints(ctx.columnConstraints()))).collect(Collectors.toList());
    return TableElements.of(elements);
}
Also used : Recognizer(org.antlr.v4.runtime.Recognizer) ColumnName(io.confluent.ksql.name.ColumnName) ParserUtil.getLocation(io.confluent.ksql.util.ParserUtil.getLocation) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TypeRegistry(io.confluent.ksql.metastore.TypeRegistry) Collectors(java.util.stream.Collectors) TableElement(io.confluent.ksql.parser.tree.TableElement) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) List(java.util.List) CharStreams(org.antlr.v4.runtime.CharStreams) RecognitionException(org.antlr.v4.runtime.RecognitionException) Objects.requireNonNull(java.util.Objects.requireNonNull) TableElements(io.confluent.ksql.parser.tree.TableElements) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) KsqlException(io.confluent.ksql.util.KsqlException) ParserUtil(io.confluent.ksql.util.ParserUtil) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) KsqlException(io.confluent.ksql.util.KsqlException) TableElement(io.confluent.ksql.parser.tree.TableElement) Recognizer(org.antlr.v4.runtime.Recognizer) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with TypeRegistry

use of io.confluent.ksql.metastore.TypeRegistry in project ksql by confluentinc.

the class DeprecatedStatementsCheckerTest method parse.

private Statement parse(final String statementText) {
    final TypeRegistry mockTypeRegistry = Mockito.mock(TypeRegistry.class);
    final AstBuilder statementBuilder = new AstBuilder(mockTypeRegistry);
    return statementBuilder.buildStatement(Iterables.getOnlyElement(STATEMENT_PARSER.parse(statementText)).getStatement());
}
Also used : AstBuilder(io.confluent.ksql.parser.AstBuilder) TypeRegistry(io.confluent.ksql.metastore.TypeRegistry)

Aggregations

TypeRegistry (io.confluent.ksql.metastore.TypeRegistry)2 ColumnName (io.confluent.ksql.name.ColumnName)1 AstBuilder (io.confluent.ksql.parser.AstBuilder)1 TableElement (io.confluent.ksql.parser.tree.TableElement)1 TableElements (io.confluent.ksql.parser.tree.TableElements)1 SqlTypeParser (io.confluent.ksql.schema.ksql.SqlTypeParser)1 KsqlException (io.confluent.ksql.util.KsqlException)1 ParserUtil (io.confluent.ksql.util.ParserUtil)1 ParserUtil.getLocation (io.confluent.ksql.util.ParserUtil.getLocation)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Collectors (java.util.stream.Collectors)1 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)1 CharStreams (org.antlr.v4.runtime.CharStreams)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 Recognizer (org.antlr.v4.runtime.Recognizer)1