use of io.confluent.ksql.schema.ksql.types.SqlBaseType in project ksql by confluentinc.
the class SqlTypeCodeGenTest method shouldGenerateWorkingCodeForAllSqlBaseTypes.
@Test
public void shouldGenerateWorkingCodeForAllSqlBaseTypes() {
for (final SqlBaseType baseType : SqlBaseType.values()) {
// When:
final String code = SqlTypeCodeGen.generateCode(TypeInstances.typeInstanceFor(baseType));
// Then:
final Object result = CodeGenTestUtil.cookAndEval(code, SqlType.class);
assertThat(result, is(instanceOf(SqlType.class)));
assertThat(((SqlType) result).baseType(), is(baseType));
}
}
use of io.confluent.ksql.schema.ksql.types.SqlBaseType 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.schema.ksql.types.SqlBaseType in project ksql by confluentinc.
the class OperatorTest method shouldWorkUsingSameRulesAsBaseTypeUpCastRules.
@Test
public void shouldWorkUsingSameRulesAsBaseTypeUpCastRules() {
allOperations().forEach(op -> {
for (final SqlBaseType leftBaseType : SqlBaseType.values()) {
// Given:
final Map<Boolean, List<SqlBaseType>> partitioned = Arrays.stream(SqlBaseType.values()).collect(Collectors.partitioningBy(rightBaseType -> shouldBeSupported(op, leftBaseType, rightBaseType)));
final List<SqlBaseType> shouldUpCast = partitioned.getOrDefault(true, ImmutableList.of());
final List<SqlBaseType> shouldNotUpCast = partitioned.getOrDefault(false, ImmutableList.of());
// Then:
shouldUpCast.forEach(rightBaseType -> assertThat("should support " + op + " on (" + leftBaseType + ", " + rightBaseType + ")", op.resultType(getType(leftBaseType), getType(rightBaseType)), is(notNullValue())));
shouldNotUpCast.forEach(rightBaseType -> {
try {
op.resultType(getType(leftBaseType), getType(rightBaseType));
fail("should not support " + op + " on (" + leftBaseType + ", " + rightBaseType + ")");
} catch (final KsqlException e) {
// Expected
}
});
}
});
}
use of io.confluent.ksql.schema.ksql.types.SqlBaseType in project ksql by confluentinc.
the class KsqlDelimitedDeserializer method buildParsers.
private static List<Parser> buildParsers(final PersistenceSchema schema) {
final List<Parser> parsers = new ArrayList<>(schema.columns().size());
for (final SimpleColumn column : schema.columns()) {
final SqlBaseType baseType = column.type().baseType();
final ParserFactory parserFactory = PARSERS.get(baseType);
if (parserFactory == null) {
throw new KsqlException("The '" + FormatFactory.DELIMITED.name() + "' format does not support type '" + baseType + "', column: " + column.name());
}
parsers.add(parserFactory.build(column.type()));
}
return parsers;
}
use of io.confluent.ksql.schema.ksql.types.SqlBaseType in project ksql by confluentinc.
the class ComparisonInterpreter method doCompareTo.
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static Optional<ComparisonFunction> doCompareTo(final Term left, final Term right) {
// CHECKSTYLE_RULES.ON: CyclomaticComplexity
final SqlBaseType leftType = left.getSqlType().baseType();
final SqlBaseType rightType = right.getSqlType().baseType();
if (either(leftType, rightType, SqlBaseType.DECIMAL)) {
final ComparableCastFunction<BigDecimal> castLeft = castToBigDecimalFunction(left.getSqlType());
final ComparableCastFunction<BigDecimal> castRight = castToBigDecimalFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.TIMESTAMP)) {
final ComparableCastFunction<Date> castLeft = castToTimestampFunction(left.getSqlType());
final ComparableCastFunction<Date> castRight = castToTimestampFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.DATE)) {
final ComparableCastFunction<Date> castLeft = castToDateFunction(left.getSqlType());
final ComparableCastFunction<Date> castRight = castToDateFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.TIME)) {
final ComparableCastFunction<Date> castLeft = castToTimeFunction(left.getSqlType());
final ComparableCastFunction<Date> castRight = castToTimeFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (leftType == SqlBaseType.STRING) {
return Optional.of((o1, o2) -> o1.toString().compareTo(o2.toString()));
} else if (leftType == SqlBaseType.BYTES && rightType == SqlBaseType.BYTES) {
final ComparableCastFunction<ByteBuffer> castLeft = castToBytesFunction(left.getSqlType());
final ComparableCastFunction<ByteBuffer> castRight = castToBytesFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.DOUBLE)) {
final ComparableCastFunction<Double> castLeft = castToDoubleFunction(left.getSqlType());
final ComparableCastFunction<Double> castRight = castToDoubleFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.BIGINT)) {
final ComparableCastFunction<Long> castLeft = castToLongFunction(left.getSqlType());
final ComparableCastFunction<Long> castRight = castToLongFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
} else if (either(leftType, rightType, SqlBaseType.INTEGER)) {
final ComparableCastFunction<Integer> castLeft = castToIntegerFunction(left.getSqlType());
final ComparableCastFunction<Integer> castRight = castToIntegerFunction(right.getSqlType());
return Optional.of((o1, o2) -> castLeft.cast(o1).compareTo(castRight.cast(o2)));
}
return Optional.empty();
}
Aggregations