Search in sources :

Example 56 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class Round method provideDecimalSchemaWithDecimalPlaces.

// Invoked via reflection
@SuppressWarnings("unused")
@UdfSchemaProvider
public static SqlType provideDecimalSchemaWithDecimalPlaces(final List<SqlArgument> params) {
    final SqlType s0 = params.get(0).getSqlTypeOrThrow();
    if (s0.baseType() != SqlBaseType.DECIMAL) {
        throw new KsqlException("The schema provider method for round expects a BigDecimal parameter" + "type as first parameter.");
    }
    final SqlType s1 = params.get(1).getSqlTypeOrThrow();
    if (s1.baseType() != SqlBaseType.INTEGER) {
        throw new KsqlException("The schema provider method for round expects an Integer parameter" + "type as second parameter.");
    }
    // the scale of the return type. See https://github.com/confluentinc/ksql/issues/6235.
    return s0;
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) UdfSchemaProvider(io.confluent.ksql.function.udf.UdfSchemaProvider)

Example 57 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class MinAggFunctionFactory method createAggregateFunction.

@Override
public KsqlAggregateFunction createAggregateFunction(final List<SqlArgument> argTypeList, final AggregateFunctionInitArguments initArgs) {
    KsqlPreconditions.checkArgument(argTypeList.size() == 1, "expected exactly one argument to aggregate MAX function");
    final SqlType argSchema = argTypeList.get(0).getSqlTypeOrThrow();
    switch(argSchema.baseType()) {
        case INTEGER:
            return new IntegerMinKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case BIGINT:
            return new LongMinKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case DOUBLE:
            return new DoubleMinKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case DECIMAL:
            return new DecimalMinKudaf(FUNCTION_NAME, initArgs.udafIndex(), argSchema);
        default:
            throw new KsqlException("No Max aggregate function with " + argTypeList.get(0) + " " + " argument type exists!");
    }
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 58 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class TopKAggregateFunctionFactory method createAggregateFunction.

@Override
public KsqlAggregateFunction createAggregateFunction(final List<SqlArgument> argumentType, final AggregateFunctionInitArguments initArgs) {
    if (argumentType.isEmpty()) {
        throw new KsqlException("TOPK function should have two arguments.");
    }
    final int tkValFromArg = (Integer) (initArgs.arg(0));
    final SqlType argSchema = argumentType.get(0).getSqlTypeOrThrow();
    switch(argSchema.baseType()) {
        case INTEGER:
            return new TopkKudaf<>(NAME, initArgs.udafIndex(), tkValFromArg, SqlTypes.array(SqlTypes.INTEGER), Collections.singletonList(ParamTypes.INTEGER), Integer.class);
        case BIGINT:
            return new TopkKudaf<>(NAME, initArgs.udafIndex(), tkValFromArg, SqlTypes.array(SqlTypes.BIGINT), Collections.singletonList(ParamTypes.LONG), Long.class);
        case DOUBLE:
            return new TopkKudaf<>(NAME, initArgs.udafIndex(), tkValFromArg, SqlTypes.array(SqlTypes.DOUBLE), Collections.singletonList(ParamTypes.DOUBLE), Double.class);
        case STRING:
            return new TopkKudaf<>(NAME, initArgs.udafIndex(), tkValFromArg, SqlTypes.array(SqlTypes.STRING), Collections.singletonList(ParamTypes.STRING), String.class);
        default:
            throw new KsqlException("No TOPK aggregate function with " + argumentType.get(0) + " argument type exists!");
    }
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 59 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class ExpressionEvaluatorParityTest method init.

@Before
public void init() {
    metaStore = MetaStoreFixture.getNewMetaStore(TestFunctionRegistry.INSTANCE.get());
    ksqlConfig = new KsqlConfig(Collections.emptyMap());
    SqlType itemInfoType = metaStore.getSource(SourceName.of("ORDERS")).getSchema().findColumn(ColumnName.of("ITEMINFO")).get().type();
    final Schema itemInfoTypeSchema = SchemaConverters.sqlToConnectConverter().toConnectSchema(itemInfoType);
    final Schema categorySchema = itemInfoTypeSchema.field("CATEGORY").schema();
    Struct itemInfo = new Struct(itemInfoTypeSchema).put("ITEMID", ITEM_ITEM_ID).put("NAME", ITEM_NAME).put("CATEGORY", new Struct(categorySchema).put("ID", CATEGORY_ID).put("NAME", CATEGORY_NAME));
    final List<Double> doubleArray = ImmutableList.of(3.5d, 5.25d);
    final Map<String, Double> map = ImmutableMap.of("abc", 6.75d, "def", 9.5d);
    // Note key isn't included first since it's assumed that it's provided as a value
    ordersRow = GenericRow.genericRow(ORDER_ID, ITEM_ID, itemInfo, ORDER_UNITS, doubleArray, map, null, TIMESTAMP, TIME, DATE, BYTES, ROW_TIME, ROWPARTITION, ROWOFFSET, ORDER_TIME);
}
Also used : Schema(org.apache.kafka.connect.data.Schema) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Struct(org.apache.kafka.connect.data.Struct) Before(org.junit.Before)

Example 60 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class CastInterpreter method castToMapFunction.

public static CastFunction castToMapFunction(final SqlType from, final SqlType to, final KsqlConfig config) {
    if (from.baseType() == SqlBaseType.MAP && to.baseType() == SqlBaseType.MAP) {
        final SqlMap fromMap = (SqlMap) from;
        final SqlMap toMap = (SqlMap) to;
        final CastFunction keyCastFunction = castFunction(fromMap.getKeyType(), toMap.getKeyType(), config);
        final CastFunction valueCastFunction = castFunction(fromMap.getValueType(), toMap.getValueType(), config);
        return o -> CastEvaluator.castMap((Map<?, ?>) o, keyCastFunction::cast, valueCastFunction::cast);
    }
    throw new KsqlException("Unsupported cast to " + to + ": " + from);
}
Also used : CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) DecimalUtil(io.confluent.ksql.util.DecimalUtil) Time(java.sql.Time) Date(java.util.Date) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ByteBuffer(java.nio.ByteBuffer) SqlDoubles(io.confluent.ksql.schema.ksql.SqlDoubles) SqlTimeTypes(io.confluent.ksql.schema.ksql.SqlTimeTypes) BigDecimal(java.math.BigDecimal) Term(io.confluent.ksql.execution.interpreter.terms.Term) Map(java.util.Map) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) CastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.CastFunction) SqlBooleans(io.confluent.ksql.schema.ksql.SqlBooleans) Timestamp(java.sql.Timestamp) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Objects(java.util.Objects) CastEvaluator(io.confluent.ksql.execution.codegen.helpers.CastEvaluator) List(java.util.List) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) KsqlException(io.confluent.ksql.util.KsqlException) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) CastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.CastFunction) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

SqlType (io.confluent.ksql.schema.ksql.types.SqlType)140 Test (org.junit.Test)80 Expression (io.confluent.ksql.execution.expression.tree.Expression)47 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)38 KsqlException (io.confluent.ksql.util.KsqlException)33 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)30 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)29 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)29 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)29 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)29 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)29 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)29 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)29 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)29 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)29 Optional (java.util.Optional)20 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)15 Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 Struct (org.apache.kafka.connect.data.Struct)14 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13