Search in sources :

Example 1 with Expression

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

the class AggregateExpressionRewriter method visitFunctionCall.

@Override
public Optional<Expression> visitFunctionCall(final FunctionCall node, final ExpressionTreeRewriter.Context<Void> context) {
    final FunctionName functionName = node.getName();
    if (functionRegistry.isAggregate(functionName)) {
        final ColumnName aggVarName = ColumnNames.aggregateColumn(aggVariableIndex);
        aggVariableIndex++;
        return Optional.of(new UnqualifiedColumnReferenceExp(node.getLocation(), aggVarName));
    } else {
        final List<Expression> arguments = new ArrayList<>();
        for (final Expression argExpression : node.getArguments()) {
            arguments.add(context.process(argExpression));
        }
        return Optional.of(new FunctionCall(node.getLocation(), node.getName(), arguments));
    }
}
Also used : FunctionName(io.confluent.ksql.name.FunctionName) ColumnName(io.confluent.ksql.name.ColumnName) Expression(io.confluent.ksql.execution.expression.tree.Expression) ArrayList(java.util.ArrayList) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)

Example 2 with Expression

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

the class FilterTypeValidator method getExpressionReturnType.

private SqlType getExpressionReturnType(final Expression exp) {
    final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    // Rewrite the expression with magic timestamps, so type checking can pass
    final Expression magicTimestampRewrite = new StatementRewriteForMagicPseudoTimestamp().rewrite(exp);
    try {
        return expressionTypeManager.getExpressionSqlType(magicTimestampRewrite);
    } catch (KsqlException e) {
        throw new KsqlStatementException("Error in " + filterType.name() + " expression: " + e.getMessage(), exp.toString());
    }
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Expression(io.confluent.ksql.execution.expression.tree.Expression) StatementRewriteForMagicPseudoTimestamp(io.confluent.ksql.engine.rewrite.StatementRewriteForMagicPseudoTimestamp) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 3 with Expression

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

the class LogicalPlanner method verifyForeignKeyJoin.

private Optional<Expression> verifyForeignKeyJoin(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 (joinInfo.getType().equals(JoinType.OUTER)) {
        throw new KsqlException(String.format("Invalid join type:" + " full-outer join not supported for foreign-key table-table join." + " Got %s %s %s.", joinInfo.getLeftSource().getDataSource().getName().text(), joinType, joinInfo.getRightSource().getDataSource().getName().text()));
    }
    // because a FK-join output table has the same PK as its left input table
    if (!(leftNode instanceof DataSourceNode) || !(rightNode instanceof DataSourceNode)) {
        throw new KsqlException(String.format("Invalid join condition:" + " foreign-key table-table joins are not supported as part of n-way joins." + " Got %s = %s.", joinInfo.getFlippedLeftJoinExpression(), joinInfo.getFlippedRightJoinExpression()));
    }
    final CodeGenRunner codeGenRunner = new CodeGenRunner(leftNode.getSchema(), ksqlConfig, metaStore);
    final VisitParentExpressionVisitor<Optional<Expression>, Context<Void>> unqualifiedRewritter = new VisitParentExpressionVisitor<Optional<Expression>, Context<Void>>(Optional.empty()) {

        @Override
        public Optional<Expression> visitQualifiedColumnReference(final QualifiedColumnReferenceExp node, final Context<Void> ctx) {
            return Optional.of(new UnqualifiedColumnReferenceExp(node.getColumnName()));
        }
    };
    final Expression leftExpressionUnqualified = ExpressionTreeRewriter.rewriteWith(unqualifiedRewritter::process, leftExpression);
    final ExpressionEvaluator expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(leftExpressionUnqualified, "Left Join Expression");
    final SqlType fkType = expressionEvaluator.getExpressionType();
    final SqlType rightKeyType = Iterables.getOnlyElement(rightNode.getSchema().key()).type();
    verifyJoinConditionTypes(fkType, rightKeyType, leftExpression, rightExpression, joinInfo.hasFlippedJoinCondition());
    if (((DataSourceNode) rightNode).isWindowed()) {
        throw new KsqlException("Foreign-key table-table joins are not supported on windowed tables.");
    }
    return Optional.of(leftExpression);
}
Also used : Context(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter.Context) DataSourceNode(io.confluent.ksql.planner.plan.DataSourceNode) CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) Optional(java.util.Optional) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) JoinType(io.confluent.ksql.planner.plan.JoinNode.JoinType) KsqlException(io.confluent.ksql.util.KsqlException) VisitParentExpressionVisitor(io.confluent.ksql.execution.expression.tree.VisitParentExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) ExpressionEvaluator(io.confluent.ksql.execution.transform.ExpressionEvaluator) 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) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 4 with Expression

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

the class LogicalPlanner method buildInternalRepartitionNode.

private PreJoinRepartitionNode buildInternalRepartitionNode(final PlanNode source, final String side, final Expression joinExpression, final BiFunction<Expression, Context<Void>, Optional<Expression>> plugin) {
    final Expression rewrittenPartitionBy = ExpressionTreeRewriter.rewriteWith(plugin, joinExpression);
    final LogicalSchema schema = buildRepartitionedSchema(source, Collections.singletonList(rewrittenPartitionBy));
    return new PreJoinRepartitionNode(new PlanNodeId(side + "SourceKeyed"), source, schema, rewrittenPartitionBy);
}
Also used : PlanNodeId(io.confluent.ksql.planner.plan.PlanNodeId) 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) PreJoinRepartitionNode(io.confluent.ksql.planner.plan.PreJoinRepartitionNode) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema)

Example 5 with Expression

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

the class LogicalPlanner method prepareSourceForJoin.

private PlanNode prepareSourceForJoin(final DataSourceNode sourceNode, final String side, final Expression joinExpression, final boolean isForeignKeyJoin) {
    final PlanNode preProjectNode;
    if (isForeignKeyJoin) {
        // we do not need to repartition for foreign key joins, as FK joins do not
        // have co-partitioning requirements
        preProjectNode = sourceNode;
    } else {
        // it is always safe to build the repartition node - this operation will be
        // a no-op if a repartition is not required. if the source is a table, and
        // a repartition is needed, then an exception will be thrown
        final VisitParentExpressionVisitor<Optional<Expression>, Context<Void>> rewriter = new VisitParentExpressionVisitor<Optional<Expression>, Context<Void>>(Optional.empty()) {

            @Override
            public Optional<Expression> visitQualifiedColumnReference(final QualifiedColumnReferenceExp node, final Context<Void> ctx) {
                return Optional.of(new UnqualifiedColumnReferenceExp(node.getColumnName()));
            }
        };
        preProjectNode = buildInternalRepartitionNode(sourceNode, side, joinExpression, rewriter::process);
    }
    return buildInternalProjectNode(preProjectNode, "PrependAlias" + side, sourceNode.getAlias());
}
Also used : Context(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter.Context) SingleSourcePlanNode(io.confluent.ksql.planner.plan.SingleSourcePlanNode) PlanNode(io.confluent.ksql.planner.plan.PlanNode) Optional(java.util.Optional) 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) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) VisitParentExpressionVisitor(io.confluent.ksql.execution.expression.tree.VisitParentExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)

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