Search in sources :

Example 11 with Expression

use of io.confluent.ksql.execution.expression.tree.Expression in project ksql by confluentinc.

the class QueryFilterNode method validateWhereClauseAndCheckTableScan.

private boolean validateWhereClauseAndCheckTableScan() {
    for (Expression disjunct : disjuncts) {
        final Validator validator = new Validator();
        validator.process(disjunct, null);
        if (validator.requiresTableScan) {
            return true;
        }
        if (!validator.isKeyedQuery) {
            if (queryPlannerOptions.getTableScansEnabled()) {
                return true;
            } else {
                throw invalidWhereClauseException("WHERE clause missing key column for disjunct: " + disjunct.toString(), isWindowed);
            }
        }
        if (!validator.seenKeys.isEmpty() && validator.seenKeys.cardinality() != schema.key().size()) {
            if (queryPlannerOptions.getTableScansEnabled()) {
                return true;
            } else {
                final List<ColumnName> seenKeyNames = validator.seenKeys.stream().boxed().map(i -> schema.key().get(i)).map(Column::name).collect(Collectors.toList());
                throw invalidWhereClauseException("Multi-column sources must specify every key in the WHERE clause. Specified: " + seenKeyNames + " Expected: " + schema.key(), isWindowed);
            }
        }
    }
    return false;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) PullQueryValidator(io.confluent.ksql.analyzer.PullQueryValidator)

Example 12 with Expression

use of io.confluent.ksql.execution.expression.tree.Expression in project ksql by confluentinc.

the class QueryFilterNode method extractLookupConstraints.

/**
 * The WHERE clause is in DNF and this method extracts key constraints from each disjunct.
 * In order to do that successfully, a given disjunct must have equality conditions on the keys.
 * For example, for "KEY = 1 AND WINDOWSTART > 0 OR COUNT > 5 AND WINDOWEND < 10", the disjunct
 * "KEY = 1 AND WINDOWSTART > 0" has a key equality constraint for value 1. The second
 * disjunct "COUNT > 5 AND WINDOWEND < 10" does not and so has an unbound key constraint.
 * seenKeys is used to make sure that all columns of a multi-column
 * key are constrained via an equality condition.
 * keyContents has the key values for each columns of a key.
 * @return the constraints on the key values used to to do keyed lookup.
 */
private ImmutableList<LookupConstraint> extractLookupConstraints() {
    if (requiresTableScan) {
        LOG.debug("Skipping extracting key value extraction. Already requires table scan");
        return ImmutableList.of(new NonKeyConstraint());
    }
    final ImmutableList.Builder<LookupConstraint> constraintPerDisjunct = ImmutableList.builder();
    for (Expression disjunct : disjuncts) {
        final KeyValueExtractor keyValueExtractor = new KeyValueExtractor();
        keyValueExtractor.process(disjunct, null);
        // Validation and extractions of window bounds
        final Optional<WindowBounds> optionalWindowBounds;
        if (isWindowed) {
            final WindowBounds windowBounds = new WindowBounds();
            new WindowBoundsExtractor().process(disjunct, windowBounds);
            optionalWindowBounds = Optional.of(windowBounds);
        } else {
            optionalWindowBounds = Optional.empty();
        }
        final LookupConstraint constraint = keyValueExtractor.getLookupConstraint(optionalWindowBounds);
        constraintPerDisjunct.add(constraint);
    }
    return constraintPerDisjunct.build();
}
Also used : LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) ImmutableList(com.google.common.collect.ImmutableList)

Example 13 with Expression

use of io.confluent.ksql.execution.expression.tree.Expression in project ksql by confluentinc.

the class SelectionUtil method resolveSingleColumn.

private static Stream<SelectExpression> resolveSingleColumn(final int idx, final PlanNode parentNode, final SingleColumn column, final Optional<Column> targetColumn) {
    final Expression expression = parentNode.resolveSelect(idx, column.getExpression());
    final ColumnName alias = column.getAlias().orElseThrow(() -> new IllegalStateException("Alias should be present by this point"));
    return Stream.of(SelectExpression.of(alias, targetColumn.map(col -> ImplicitlyCastResolver.resolve(expression, col.type())).orElse(expression)));
}
Also used : IntStream(java.util.stream.IntStream) Expression(io.confluent.ksql.execution.expression.tree.Expression) ColumnName(io.confluent.ksql.name.ColumnName) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Set(java.util.Set) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) SelectItem(io.confluent.ksql.parser.tree.SelectItem) Namespace(io.confluent.ksql.schema.ksql.Column.Namespace) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) Stream(java.util.stream.Stream) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Optional(java.util.Optional) AllColumns(io.confluent.ksql.parser.tree.AllColumns) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) Column(io.confluent.ksql.schema.ksql.Column) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) ColumnName(io.confluent.ksql.name.ColumnName) Expression(io.confluent.ksql.execution.expression.tree.Expression) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression)

Example 14 with Expression

use of io.confluent.ksql.execution.expression.tree.Expression in project ksql by confluentinc.

the class JoinNode method validateKeyPresent.

@Override
void validateKeyPresent(final SourceName sinkName, final Projection projection) {
    if (joinKey.isForeignKey()) {
        final DataSourceNode leftInputTable = getLeftmostSourceNode();
        final SourceName leftInputTableName = leftInputTable.getAlias();
        final List<Column> leftInputTableKeys = leftInputTable.getDataSource().getSchema().key();
        final List<Column> missingKeys = leftInputTableKeys.stream().filter(k -> !projection.containsExpression(new QualifiedColumnReferenceExp(leftInputTableName, k.name())) && !projection.containsExpression(new UnqualifiedColumnReferenceExp(ColumnNames.generatedJoinColumnAlias(leftInputTableName, k.name())))).collect(Collectors.toList());
        if (!missingKeys.isEmpty()) {
            throwMissingKeyColumnForFkJoinException(missingKeys, leftInputTableName);
        }
    } else {
        final boolean atLeastOneKey = joinKey.getAllViableKeys(schema).stream().anyMatch(projection::containsExpression);
        if (!atLeastOneKey) {
            final boolean synthetic = joinKey.isSynthetic();
            final List<? extends Expression> viable = joinKey.getOriginalViableKeys(schema);
            throwKeysNotIncludedError(sinkName, "join expression", viable, false, synthetic);
        }
    }
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) Arrays(java.util.Arrays) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) BiFunction(java.util.function.BiFunction) Pair(io.confluent.ksql.util.Pair) Immutable(com.google.errorprone.annotations.Immutable) Map(java.util.Map) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) ColumnNames(io.confluent.ksql.schema.ksql.ColumnNames) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) Collection(java.util.Collection) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Streams(com.google.common.collect.Streams) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) FormatInfo(io.confluent.ksql.serde.FormatInfo) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) SchemaKTable(io.confluent.ksql.structured.SchemaKTable) KeyFormat(io.confluent.ksql.serde.KeyFormat) QueryContext(io.confluent.ksql.execution.context.QueryContext) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) DataSourceType(io.confluent.ksql.metastore.model.DataSource.DataSourceType) KafkaTopicClient(io.confluent.ksql.services.KafkaTopicClient) Supplier(java.util.function.Supplier) NoneFormat(io.confluent.ksql.serde.none.NoneFormat) ImmutableList(com.google.common.collect.ImmutableList) ForeignKeyJoinParamsFactory(io.confluent.ksql.execution.streams.ForeignKeyJoinParamsFactory) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) Projection(io.confluent.ksql.planner.Projection) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) RequiredColumns(io.confluent.ksql.planner.RequiredColumns) JoinParamsFactory(io.confluent.ksql.execution.streams.JoinParamsFactory) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) Context(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter.Context) ExpressionTreeRewriter(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter) Collections(java.util.Collections) Column(io.confluent.ksql.schema.ksql.Column) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SourceName(io.confluent.ksql.name.SourceName) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)

Example 15 with Expression

use of io.confluent.ksql.execution.expression.tree.Expression in project ksql by confluentinc.

the class LogicRewriter method extractDisjuncts.

public static List<Expression> extractDisjuncts(final Expression expression) {
    final Expression dnf = rewriteDNF(expression);
    final DisjunctExtractor disjunctExtractor = new DisjunctExtractor();
    disjunctExtractor.process(dnf, null);
    return disjunctExtractor.getDisjuncts();
}
Also used : NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression)

Aggregations

Expression (io.confluent.ksql.execution.expression.tree.Expression)343 Test (org.junit.Test)297 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)213 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)195 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)179 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)170 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)170 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)159 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)150 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)143 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)142 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)138 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)125 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)114 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)112 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)99 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)94 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)89 KsqlException (io.confluent.ksql.util.KsqlException)72 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)53