Search in sources :

Example 6 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class RowExpressionVerifier method visitSimpleCaseExpression.

@Override
protected Boolean visitSimpleCaseExpression(SimpleCaseExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialForm && ((SpecialForm) actual).getForm().equals(SWITCH))) {
        return false;
    }
    SpecialForm actualCase = (SpecialForm) actual;
    if (!process(expected.getOperand(), actualCase.getArguments().get(0))) {
        return false;
    }
    List<RowExpression> whenClauses;
    Optional<RowExpression> elseValue;
    RowExpression last = actualCase.getArguments().get(actualCase.getArguments().size() - 1);
    if (last instanceof SpecialForm && ((SpecialForm) last).getForm().equals(WHEN)) {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size());
        elseValue = Optional.empty();
    } else {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size() - 1);
        elseValue = Optional.of(last);
    }
    if (!process(expected.getWhenClauses(), whenClauses)) {
        return false;
    }
    return process(expected.getDefaultValue(), elseValue);
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 7 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class TestExpressionOptimizer method testIfConstantOptimization.

@Test
public void testIfConstantOptimization() {
    assertEquals(optimizer.optimize(ifExpression(constant(true, BOOLEAN), 1L, 2L)), constant(1L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(false, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    assertEquals(optimizer.optimize(ifExpression(constant(null, BOOLEAN), 1L, 2L)), constant(2L, BIGINT));
    FunctionHandle bigintEquals = functionAndTypeManager.resolveOperatorFunctionHandle(EQUAL, fromTypes(BIGINT, BIGINT));
    RowExpression condition = new CallExpression(EQUAL.name(), bigintEquals, BOOLEAN, ImmutableList.of(constant(3L, BIGINT), constant(3L, BIGINT)), Optional.empty());
    assertEquals(optimizer.optimize(ifExpression(condition, 1L, 2L)), constant(1L, BIGINT));
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 8 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class TestExpressionOptimizer method testCastWithJsonParseOptimization.

@Test
public void testCastWithJsonParseOptimization() {
    FunctionHandle jsonParseFunctionHandle = functionAndTypeManager.lookupFunction("json_parse", fromTypes(VARCHAR));
    // constant
    FunctionHandle jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(integer)"));
    RowExpression jsonCastExpression = new CallExpression(CAST.name(), jsonCastFunctionHandle, new ArrayType(INTEGER), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, constant(utf8Slice("[1, 2]"), VARCHAR))), Optional.empty());
    RowExpression resultExpression = optimizer.optimize(jsonCastExpression);
    assertInstanceOf(resultExpression, ConstantExpression.class);
    Object resultValue = ((ConstantExpression) resultExpression).getValue();
    assertInstanceOf(resultValue, IntArrayBlock.class);
    assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
    // varchar to array
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, new ArrayType(VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ARRAY_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ARRAY_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("array(varchar)")), new ArrayType(VARCHAR), field(1, VARCHAR)));
    // varchar to row
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("row(varchar,bigint)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_ROW_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ROW_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("row(varchar,bigint)")), RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), field(1, VARCHAR)));
    // varchar to map
    jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("map(integer,varchar)"));
    jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, mapType(INTEGER, VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
    resultExpression = optimizer.optimize(jsonCastExpression);
    assertEquals(resultExpression, call(JSON_TO_MAP_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_MAP_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("map(integer, varchar)")), mapType(INTEGER, VARCHAR), field(1, VARCHAR)));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) IntArrayBlock(io.prestosql.spi.block.IntArrayBlock) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 9 with RowExpression

use of io.prestosql.spi.relation.RowExpression 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 10 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class ProjectStatsRule method doCalculate.

@Override
protected Optional<PlanNodeStatsEstimate> doCalculate(ProjectNode node, StatsProvider statsProvider, Lookup lookup, Session session, TypeProvider types) {
    PlanNodeStatsEstimate sourceStats = statsProvider.getStats(node.getSource());
    PlanNodeStatsEstimate.Builder calculatedStats = PlanNodeStatsEstimate.builder().setOutputRowCount(sourceStats.getOutputRowCount());
    Map<Integer, Symbol> layout = null;
    for (Map.Entry<Symbol, RowExpression> entry : node.getAssignments().entrySet()) {
        RowExpression expression = entry.getValue();
        if (isExpression(expression)) {
            calculatedStats.addSymbolStatistics(entry.getKey(), scalarStatsCalculator.calculate(castToExpression(expression), sourceStats, session, types));
        } else {
            if (layout == null) {
                layout = SymbolUtils.toLayOut(node.getOutputSymbols());
            }
            calculatedStats.addSymbolStatistics(entry.getKey(), scalarStatsCalculator.calculate(expression, sourceStats, session, layout));
        }
    }
    return Optional.of(calculatedStats.build());
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) RowExpression(io.prestosql.spi.relation.RowExpression) Map(java.util.Map)

Aggregations

RowExpression (io.prestosql.spi.relation.RowExpression)185 ArrayList (java.util.ArrayList)66 Symbol (io.prestosql.spi.plan.Symbol)62 CallExpression (io.prestosql.spi.relation.CallExpression)56 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)52 ImmutableList (com.google.common.collect.ImmutableList)45 Test (org.testng.annotations.Test)42 Type (io.prestosql.spi.type.Type)41 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)39 List (java.util.List)39 Map (java.util.Map)39 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)31 Optional (java.util.Optional)30 Expression (io.prestosql.sql.tree.Expression)29 Metadata (io.prestosql.metadata.Metadata)28 PlanNode (io.prestosql.spi.plan.PlanNode)27 ImmutableMap (com.google.common.collect.ImmutableMap)26 SpecialForm (io.prestosql.spi.relation.SpecialForm)25 FunctionHandle (io.prestosql.spi.function.FunctionHandle)24 ProjectNode (io.prestosql.spi.plan.ProjectNode)24