Search in sources :

Example 6 with SymbolReference

use of io.prestosql.sql.tree.SymbolReference in project hetu-core by openlookeng.

the class TestExpressionVerifier method test.

@Test
public void test() {
    Expression actual = expression("NOT(orderkey = 3 AND custkey = 3 AND orderkey < 10)");
    SymbolAliases symbolAliases = SymbolAliases.builder().put("X", new SymbolReference("orderkey")).put("Y", new SymbolReference("custkey")).build();
    ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
    assertTrue(verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND X < 10)")));
    assertThrows(() -> verifier.process(actual, expression("NOT(X = 3 AND Y = 3 AND Z < 10)")));
    assertFalse(verifier.process(actual, expression("NOT(X = 3 AND X = 3 AND X < 10)")));
}
Also used : Expression(io.prestosql.sql.tree.Expression) SymbolReference(io.prestosql.sql.tree.SymbolReference) Test(org.testng.annotations.Test)

Example 7 with SymbolReference

use of io.prestosql.sql.tree.SymbolReference in project hetu-core by openlookeng.

the class CubeStatementGenerator method generate.

public static CubeStatement generate(String fromTable, AggregationNode aggregationNode, Map<String, Object> symbolMappings) {
    CubeStatement.Builder builder = CubeStatement.newBuilder();
    builder.from(fromTable);
    Map<Symbol, AggregationNode.Aggregation> aggregations = aggregationNode.getAggregations();
    // Extract selection and aggregation
    for (Symbol symbol : aggregationNode.getOutputSymbols()) {
        Object column = symbolMappings.get(symbol.getName());
        if (column instanceof ColumnHandle) {
            // Selection
            String columnName = ((ColumnHandle) column).getColumnName();
            builder.select(columnName);
        } else if (aggregations.containsKey(symbol)) {
            // Aggregation
            Map<Symbol, AggregationSignature> signatures = Collections.emptyMap();
            AggregationNode.Aggregation aggregation = aggregations.get(symbol);
            List<RowExpression> arguments = aggregation.getArguments();
            if (arguments.isEmpty()) {
                signatures = createSignature(aggregation, symbol, null);
            } else if (arguments.size() == 1) {
                RowExpression argument = arguments.get(0);
                if (OriginalExpressionUtils.isExpression(argument)) {
                    Expression argAsExpr = OriginalExpressionUtils.castToExpression(argument);
                    if (argAsExpr instanceof SymbolReference) {
                        signatures = createSignature(aggregation, symbol, symbolMappings.get(((SymbolReference) argAsExpr).getName()));
                    }
                } else if (argument instanceof VariableReferenceExpression) {
                    signatures = createSignature(aggregation, symbol, symbolMappings.get(((VariableReferenceExpression) argument).getName()));
                }
            }
            if (signatures.isEmpty()) {
                throw new IllegalArgumentException("Failed to generate aggregator signature");
            }
            for (Map.Entry<Symbol, AggregationSignature> aggEntry : signatures.entrySet()) {
                builder.aggregate(aggEntry.getValue());
            }
        } else {
            throw new IllegalArgumentException("Column " + column + " is not an actual column or expressions");
        }
    }
    // Extract group by
    for (Symbol symbol : aggregationNode.getGroupingKeys()) {
        Object column = symbolMappings.get(symbol.getName());
        if (column instanceof ColumnHandle) {
            builder.groupByAddString(((ColumnHandle) column).getColumnName());
        } else {
            // Don't know how to handle it
            throw new IllegalArgumentException("Column " + symbol + " is not an actual column");
        }
    }
    return builder.build();
}
Also used : ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) RowExpression(io.prestosql.spi.relation.RowExpression) CubeStatement(io.hetu.core.spi.cube.CubeStatement) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) List(java.util.List) Map(java.util.Map)

Example 8 with SymbolReference

use of io.prestosql.sql.tree.SymbolReference in project hetu-core by openlookeng.

the class QueryPlanner method project.

private PlanBuilder project(PlanBuilder subPlan, Iterable<Expression> expressions) {
    TranslationMap outputTranslations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
    Assignments.Builder projections = Assignments.builder();
    for (Expression expression : expressions) {
        if (expression instanceof SymbolReference) {
            Symbol symbol = from(expression);
            projections.put(symbol, castToRowExpression(expression));
            outputTranslations.put(expression, symbol);
            continue;
        }
        Symbol symbol = planSymbolAllocator.newSymbol(expression, analysis.getTypeWithCoercions(expression));
        projections.put(symbol, castToRowExpression(subPlan.rewrite(expression)));
        outputTranslations.put(expression, symbol);
    }
    return new PlanBuilder(outputTranslations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()));
}
Also used : OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 9 with SymbolReference

use of io.prestosql.sql.tree.SymbolReference in project hetu-core by openlookeng.

the class QueryPlanner method explicitCoercionFields.

private PlanBuilder explicitCoercionFields(PlanBuilder subPlan, Iterable<Expression> alreadyCoerced, Iterable<? extends Expression> uncoerced) {
    TranslationMap translations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
    Assignments.Builder projections = Assignments.builder();
    projections.putAll(coerce(uncoerced, subPlan, translations));
    for (Expression expression : alreadyCoerced) {
        if (expression instanceof SymbolReference) {
            // If this is an identity projection, no need to rewrite it
            // This is needed because certain synthetic identity expressions such as "group id" introduced when planning GROUPING
            // don't have a corresponding analysis, so the code below doesn't work for them
            projections.put(from(expression), castToRowExpression(expression));
            continue;
        }
        Symbol symbol = planSymbolAllocator.newSymbol(expression, analysis.getType(expression));
        Expression rewritten = subPlan.rewrite(expression);
        projections.put(symbol, castToRowExpression(rewritten));
        translations.put(expression, symbol);
    }
    return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()));
}
Also used : OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) Expression(io.prestosql.sql.tree.Expression) RowExpression(io.prestosql.spi.relation.RowExpression) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 10 with SymbolReference

use of io.prestosql.sql.tree.SymbolReference in project hetu-core by openlookeng.

the class RelationPlanner method addCoercions.

private RelationPlan addCoercions(RelationPlan plan, Type[] targetColumnTypes) {
    List<Symbol> oldSymbols = plan.getFieldMappings();
    RelationType oldDescriptor = plan.getDescriptor().withOnlyVisibleFields();
    verify(targetColumnTypes.length == oldSymbols.size());
    ImmutableList.Builder<Symbol> newSymbols = new ImmutableList.Builder<>();
    Field[] newFields = new Field[targetColumnTypes.length];
    Assignments.Builder assignments = Assignments.builder();
    for (int i = 0; i < targetColumnTypes.length; i++) {
        Symbol inputSymbol = oldSymbols.get(i);
        Type inputType = planSymbolAllocator.getTypes().get(inputSymbol);
        Type outputType = targetColumnTypes[i];
        if (!outputType.equals(inputType)) {
            Expression cast = new Cast(toSymbolReference(inputSymbol), outputType.getTypeSignature().toString());
            Symbol outputSymbol = planSymbolAllocator.newSymbol(cast, outputType);
            assignments.put(outputSymbol, castToRowExpression(cast));
            newSymbols.add(outputSymbol);
        } else {
            SymbolReference symbolReference = toSymbolReference(inputSymbol);
            Symbol outputSymbol = planSymbolAllocator.newSymbol(symbolReference, outputType);
            assignments.put(outputSymbol, castToRowExpression(symbolReference));
            newSymbols.add(outputSymbol);
        }
        Field oldField = oldDescriptor.getFieldByIndex(i);
        newFields[i] = new Field(oldField.getRelationAlias(), oldField.getName(), targetColumnTypes[i], oldField.isHidden(), oldField.getOriginTable(), oldField.getOriginColumnName(), oldField.isAliased());
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    return new RelationPlan(projectNode, Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(newFields)).build(), newSymbols.build());
}
Also used : Cast(io.prestosql.sql.tree.Cast) Symbol(io.prestosql.spi.plan.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Assignments(io.prestosql.spi.plan.Assignments) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Aggregations

SymbolReference (io.prestosql.sql.tree.SymbolReference)57 Symbol (io.prestosql.spi.plan.Symbol)30 Expression (io.prestosql.sql.tree.Expression)29 Test (org.testng.annotations.Test)25 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)23 Cast (io.prestosql.sql.tree.Cast)19 RowExpression (io.prestosql.spi.relation.RowExpression)17 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)17 LongLiteral (io.prestosql.sql.tree.LongLiteral)14 ImmutableList (com.google.common.collect.ImmutableList)13 ImmutableMap (com.google.common.collect.ImmutableMap)12 Type (io.prestosql.spi.type.Type)12 List (java.util.List)12 Map (java.util.Map)12 ProjectNode (io.prestosql.spi.plan.ProjectNode)11 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 ArrayList (java.util.ArrayList)10 Assignments (io.prestosql.spi.plan.Assignments)9 CallExpression (io.prestosql.spi.relation.CallExpression)9 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)9