Search in sources :

Example 11 with ColumnName

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.");
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) Column(io.confluent.ksql.schema.ksql.Column) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 12 with ColumnName

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()));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Test(org.junit.Test)

Example 13 with ColumnName

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()));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Test(org.junit.Test)

Example 14 with ColumnName

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);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ColumnName(io.confluent.ksql.name.ColumnName) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) GenericKey(io.confluent.ksql.GenericKey) KsqlException(io.confluent.ksql.util.KsqlException)

Example 15 with ColumnName

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;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) HashMap(java.util.HashMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Aggregations

ColumnName (io.confluent.ksql.name.ColumnName)63 Test (org.junit.Test)32 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)31 Expression (io.confluent.ksql.execution.expression.tree.Expression)23 KsqlException (io.confluent.ksql.util.KsqlException)13 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)11 Column (io.confluent.ksql.schema.ksql.Column)11 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)10 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)9 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)7 List (java.util.List)7 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)6 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)6 Set (java.util.Set)6 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)5 QualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp)5 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)5