Search in sources :

Example 16 with SqlType

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

the class LogicalPlanner method verifyJoin.

/**
 * @return the foreign key column if this is a foreign key join
 */
private Optional<Expression> verifyJoin(final JoinInfo joinInfo, final PlanNode leftNode, final PlanNode rightNode) {
    final JoinType joinType = joinInfo.getType();
    final Expression leftExpression = joinInfo.getLeftJoinExpression();
    final Expression rightExpression = joinInfo.getRightJoinExpression();
    if (leftNode.getNodeOutputType() == DataSourceType.KSTREAM) {
        if (rightNode.getNodeOutputType() == DataSourceType.KTABLE) {
            verifyStreamTableJoin(joinInfo, rightNode);
        }
    // stream-stream join detected: nothing to verify
    } else {
        if (rightNode.getNodeOutputType() == DataSourceType.KSTREAM) {
            throw new KsqlException(String.format("Invalid join order:" + " table-stream joins are not supported; only stream-table joins. Got %s %s %s.", joinInfo.getLeftSource().getDataSource().getName().text(), joinType, joinInfo.getRightSource().getDataSource().getName().text()));
        }
        if (joinOnNonKeyAttribute(rightExpression, rightNode, joinInfo.getRightSource())) {
            throw new KsqlException(String.format("Invalid join condition:" + " table-table joins require to join on the primary key of the right input" + " table. Got %s = %s.", joinInfo.getFlippedLeftJoinExpression(), joinInfo.getFlippedRightJoinExpression()));
        }
        if (joinOnNonKeyAttribute(leftExpression, leftNode, joinInfo.getLeftSource())) {
            return verifyForeignKeyJoin(joinInfo, leftNode, rightNode);
        } else {
            // primary key join detected
            final SqlType leftKeyType = Iterables.getOnlyElement(leftNode.getSchema().key()).type();
            final SqlType rightKeyType = Iterables.getOnlyElement(rightNode.getSchema().key()).type();
            verifyJoinConditionTypes(leftKeyType, rightKeyType, leftExpression, rightExpression, joinInfo.hasFlippedJoinCondition());
        }
    }
    return Optional.empty();
}
Also used : Expression(io.confluent.ksql.execution.expression.tree.Expression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) JoinType(io.confluent.ksql.planner.plan.JoinNode.JoinType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 17 with SqlType

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

the class SqlTypeParser method getType.

public Type getType(final SqlBaseParser.TypeContext type) {
    final Optional<NodeLocation> location = ParserUtil.getLocation(type);
    final SqlType sqlType = getSqlType(type);
    return new Type(location, sqlType);
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) NodeLocation(io.confluent.ksql.parser.NodeLocation) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 18 with SqlType

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

the class SqlTypeParser method getSqlType.

private SqlType getSqlType(final SqlBaseParser.TypeContext type) {
    if (type.baseType() != null) {
        final String baseType = baseTypeToString(type.baseType());
        if (SqlPrimitiveType.isPrimitiveTypeName(baseType)) {
            return SqlPrimitiveType.of(baseType);
        } else {
            return typeRegistry.resolveType(baseType).orElseThrow(() -> new KsqlException("Cannot resolve unknown type: " + baseType));
        }
    }
    if (type.DECIMAL() != null) {
        return SqlDecimal.of(ParserUtil.processIntegerNumber(type.number(0), "DECIMAL(PRECISION)"), ParserUtil.processIntegerNumber(type.number(1), "DECIMAL(SCALE)"));
    }
    if (type.ARRAY() != null) {
        return SqlArray.of(getSqlType(type.type(0)));
    }
    if (type.MAP() != null) {
        return SqlMap.of(getSqlType(type.type(0)), getSqlType(type.type(1)));
    }
    if (type.STRUCT() != null) {
        final SqlStruct.Builder builder = SqlStruct.builder();
        for (int i = 0; i < type.identifier().size(); i++) {
            final String fieldName = ParserUtil.getIdentifierText(type.identifier(i));
            final SqlType fieldType = getSqlType(type.type(i));
            builder.field(fieldName, fieldType);
        }
        return builder.build();
    }
    throw new IllegalArgumentException("Unsupported type specification: " + type.getText());
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 19 with SqlType

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

the class RegisterTypeFactory method create.

public RegisterTypeCommand create(final RegisterType statement) {
    final String name = statement.getName();
    final boolean ifNotExists = statement.getIfNotExists();
    final SqlType type = statement.getType().getSqlType();
    if (!ifNotExists && metaStore.resolveType(name).isPresent()) {
        throw new KsqlException("Cannot register custom type '" + name + "' " + "since it is already registered with type: " + metaStore.resolveType(name).get());
    }
    return new RegisterTypeCommand(type, name);
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) RegisterTypeCommand(io.confluent.ksql.execution.ddl.commands.RegisterTypeCommand)

Example 20 with SqlType

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

the class MaxAggFunctionFactory 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 IntegerMaxKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case BIGINT:
            return new LongMaxKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case DOUBLE:
            return new DoubleMaxKudaf(FUNCTION_NAME, initArgs.udafIndex());
        case DECIMAL:
            return new DecimalMaxKudaf(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)

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