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);
}
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();
}
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."));
}
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))));
}
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))));
}
Aggregations