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);
}
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));
}
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)));
}
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();
}
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());
}
Aggregations