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