Search in sources :

Example 1 with TableElements

use of io.confluent.ksql.parser.tree.TableElements 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 TableElements

use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.

the class LogicalSchemaDeserializer method deserialize.

@Override
public LogicalSchema deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {
    final String text = jp.readValueAs(String.class);
    final TableElements tableElements = SchemaParser.parse(text, TypeRegistry.EMPTY);
    return tableElements.toLogicalSchema();
}
Also used : TableElements(io.confluent.ksql.parser.tree.TableElements)

Example 3 with TableElements

use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.

the class CreateSourceFactoryTest method shouldThrowIfTableIsMissingPrimaryKey.

@Test
public void shouldThrowIfTableIsMissingPrimaryKey() {
    // Given:
    ksqlConfig = new KsqlConfig(ImmutableMap.of(KsqlConfig.KSQL_WRAP_SINGLE_VALUES, false));
    final TableElements noKey = TableElements.of(ELEMENT1);
    final CreateTable statement = new CreateTable(SOME_NAME, noKey, false, true, withProperties, false);
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> createSourceFactory.createTableCommand(statement, ksqlConfig));
    // Then:
    assertThat(e.getMessage(), containsString("Tables require a PRIMARY KEY. Please define the PRIMARY KEY."));
}
Also used : TableElements(io.confluent.ksql.parser.tree.TableElements) CreateTable(io.confluent.ksql.parser.tree.CreateTable) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 4 with TableElements

use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.

the class SchemaParserTest method shouldParseQuotedSchema.

@Test
public void shouldParseQuotedSchema() {
    // Given:
    final String schema = "`END` VARCHAR";
    // When:
    final TableElements elements = parser.parse(schema);
    // Then:
    assertThat(elements, hasItem(new TableElement(ColumnName.of("END"), new Type(SqlTypes.STRING))));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) TableElements(io.confluent.ksql.parser.tree.TableElements) Matchers.containsString(org.hamcrest.Matchers.containsString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 5 with TableElements

use of io.confluent.ksql.parser.tree.TableElements in project ksql by confluentinc.

the class SchemaParserTest method shouldParseValidSchemaWithPrimaryKeyField.

@Test
public void shouldParseValidSchemaWithPrimaryKeyField() {
    // Given:
    final String schema = "K STRING PRIMARY KEY, bar INT";
    // When:
    final TableElements elements = parser.parse(schema);
    // Then:
    assertThat(elements, contains(new TableElement(ColumnName.of("K"), new Type(SqlTypes.STRING), PRIMARY_KEY_CONSTRAINT), new TableElement(BAR, new Type(SqlTypes.INTEGER))));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) TableElements(io.confluent.ksql.parser.tree.TableElements) Matchers.containsString(org.hamcrest.Matchers.containsString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Aggregations

TableElements (io.confluent.ksql.parser.tree.TableElements)15 TableElement (io.confluent.ksql.parser.tree.TableElement)11 Test (org.junit.Test)11 Type (io.confluent.ksql.execution.expression.tree.Type)9 Matchers.containsString (org.hamcrest.Matchers.containsString)8 CreateSource (io.confluent.ksql.parser.tree.CreateSource)3 KsqlException (io.confluent.ksql.util.KsqlException)3 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 SetView (com.google.common.collect.Sets.SetView)1 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1 CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)1 TypeRegistry (io.confluent.ksql.metastore.TypeRegistry)1 ColumnName (io.confluent.ksql.name.ColumnName)1