use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.
the class TimestampExtractionPolicyFactory method create.
public static TimestampExtractionPolicy create(final KsqlConfig ksqlConfig, final LogicalSchema schema, final Optional<TimestampColumn> timestampColumn) {
if (!timestampColumn.isPresent()) {
return new MetadataTimestampExtractionPolicy(getDefaultTimestampExtractor(ksqlConfig));
}
final ColumnName col = timestampColumn.get().getColumn();
final Optional<String> timestampFormat = timestampColumn.get().getFormat();
final Column column = schema.findColumn(col).orElseThrow(() -> new KsqlException("The TIMESTAMP column set in the WITH clause does not exist in the schema: '" + col.toString(FormatOptions.noEscape()) + "'"));
final SqlBaseType tsColumnType = column.type().baseType();
if (tsColumnType == SqlBaseType.STRING) {
final String format = timestampFormat.orElseThrow(() -> new KsqlException("A String timestamp field has been specified without" + " also specifying the " + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY.toLowerCase()));
return new StringTimestampExtractionPolicy(col, format);
}
if (timestampFormat.isPresent()) {
throw new KsqlException("'" + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY + "' set in the WITH clause can only be used " + "when the timestamp column is of type STRING.");
}
if (tsColumnType == SqlBaseType.BIGINT) {
return new LongColumnTimestampExtractionPolicy(col);
}
if (tsColumnType == SqlBaseType.TIMESTAMP) {
return new TimestampColumnTimestampExtractionPolicy(col);
}
throw new KsqlException("Timestamp column, " + col + ", should be LONG(INT64), TIMESTAMP," + " or a String with a " + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY.toLowerCase() + " specified.");
}
use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.
the class JoinParamsFactoryTest method shouldBuildCorrectSyntheticKeyedSchema.
@Test
public void shouldBuildCorrectSyntheticKeyedSchema() {
// Given:
final ColumnName keyName = ColumnName.of("OTHER");
// When:
final JoinParams joinParams = JoinParamsFactory.create(keyName, LEFT_SCHEMA, RIGHT_SCHEMA);
// Then:
assertThat(joinParams.getSchema(), is(LogicalSchema.builder().keyColumn(keyName, SqlTypes.STRING).valueColumn(ColumnName.of("L_BLUE"), SqlTypes.STRING).valueColumn(ColumnName.of("L_GREEN"), SqlTypes.INTEGER).valueColumn(ColumnName.of("L_K"), SqlTypes.STRING).valueColumn(ColumnName.of("R_RED"), SqlTypes.BIGINT).valueColumn(ColumnName.of("R_ORANGE"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("R_K"), SqlTypes.STRING).valueColumn(keyName, SqlTypes.STRING).build()));
}
use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.
the class JoinParamsFactoryTest method shouldBuildCorrectLeftKeyedSchema.
@Test
public void shouldBuildCorrectLeftKeyedSchema() {
// Given:
final ColumnName keyName = Iterables.getOnlyElement(LEFT_SCHEMA.key()).name();
// When:
final JoinParams joinParams = JoinParamsFactory.create(keyName, LEFT_SCHEMA, RIGHT_SCHEMA);
// Then:
assertThat(joinParams.getSchema(), is(LogicalSchema.builder().keyColumn(ColumnName.of("L_K"), SqlTypes.STRING).valueColumn(ColumnName.of("L_BLUE"), SqlTypes.STRING).valueColumn(ColumnName.of("L_GREEN"), SqlTypes.INTEGER).valueColumn(ColumnName.of("L_K"), SqlTypes.STRING).valueColumn(ColumnName.of("R_RED"), SqlTypes.BIGINT).valueColumn(ColumnName.of("R_ORANGE"), SqlTypes.DOUBLE).valueColumn(ColumnName.of("R_K"), SqlTypes.STRING).build()));
}
use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.
the class GenericRecordFactory method build.
public KsqlGenericRecord build(final List<ColumnName> columnNames, final List<Expression> expressions, final LogicalSchema schema, final DataSourceType dataSourceType) {
final List<ColumnName> columns = columnNames.isEmpty() ? implicitColumns(schema) : columnNames;
if (columns.size() != expressions.size()) {
throw new KsqlException("Expected a value for each column." + " Expected Columns: " + columnNames + ". Got " + expressions);
}
final LogicalSchema schemaWithPseudoColumns = withPseudoColumns(schema, config);
for (ColumnName col : columns) {
if (!schemaWithPseudoColumns.findColumn(col).isPresent()) {
throw new KsqlException("Column name " + col + " does not exist.");
}
if (SystemColumns.isDisallowedForInsertValues(col, config)) {
throw new KsqlException("Inserting into column " + col + " is not allowed.");
}
}
final Map<ColumnName, Object> values = resolveValues(columns, expressions, schemaWithPseudoColumns, functionRegistry, config);
if (dataSourceType == DataSourceType.KTABLE) {
final String noValue = schemaWithPseudoColumns.key().stream().map(Column::name).filter(colName -> !values.containsKey(colName)).map(ColumnName::text).collect(Collectors.joining(", "));
if (!noValue.isEmpty()) {
throw new KsqlException("Value for primary key column(s) " + noValue + " is required for tables");
}
}
final long ts = (long) values.getOrDefault(SystemColumns.ROWTIME_NAME, clock.getAsLong());
final GenericKey key = buildKey(schema, values);
final GenericRow value = buildValue(schema, values);
return KsqlGenericRecord.of(key, value, ts);
}
use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.
the class GenericRecordFactory method resolveValues.
private static Map<ColumnName, Object> resolveValues(final List<ColumnName> columns, final List<Expression> expressions, final LogicalSchema schema, final FunctionRegistry functionRegistry, final KsqlConfig config) {
final Map<ColumnName, Object> values = new HashMap<>();
for (int i = 0; i < columns.size(); i++) {
final ColumnName column = columns.get(i);
final SqlType columnType = columnType(column, schema);
final Expression valueExp = expressions.get(i);
final Object value = new GenericExpressionResolver(columnType, column, functionRegistry, config, "insert value", false).resolve(valueExp);
values.put(column, value);
}
return values;
}
Aggregations