use of io.trino.sql.planner.Symbol in project trino by trinodb.
the class UnwrapSingleColumnRowInApply method apply.
@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
Assignments.Builder inputAssignments = Assignments.builder().putIdentities(node.getInput().getOutputSymbols());
Assignments.Builder nestedPlanAssignments = Assignments.builder().putIdentities(node.getSubquery().getOutputSymbols());
boolean applied = false;
Assignments.Builder applyAssignments = Assignments.builder();
for (Map.Entry<Symbol, Expression> assignment : node.getSubqueryAssignments().entrySet()) {
Symbol output = assignment.getKey();
Expression expression = assignment.getValue();
Optional<Unwrapping> unwrapped = Optional.empty();
if (expression instanceof InPredicate) {
InPredicate predicate = (InPredicate) expression;
unwrapped = unwrapSingleColumnRow(context, predicate.getValue(), predicate.getValueList(), (value, list) -> new InPredicate(value.toSymbolReference(), list.toSymbolReference()));
} else if (expression instanceof QuantifiedComparisonExpression) {
QuantifiedComparisonExpression comparison = (QuantifiedComparisonExpression) expression;
unwrapped = unwrapSingleColumnRow(context, comparison.getValue(), comparison.getSubquery(), (value, list) -> new QuantifiedComparisonExpression(comparison.getOperator(), comparison.getQuantifier(), value.toSymbolReference(), list.toSymbolReference()));
}
if (unwrapped.isPresent()) {
applied = true;
Unwrapping unwrapping = unwrapped.get();
inputAssignments.add(unwrapping.getInputAssignment());
nestedPlanAssignments.add(unwrapping.getNestedPlanAssignment());
applyAssignments.put(output, unwrapping.getExpression());
} else {
applyAssignments.put(assignment);
}
}
if (!applied) {
return Result.empty();
}
return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new ApplyNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), inputAssignments.build()), new ProjectNode(context.getIdAllocator().getNextId(), node.getSubquery(), nestedPlanAssignments.build()), applyAssignments.build(), node.getCorrelation(), node.getOriginSubquery()), Assignments.identity(node.getOutputSymbols())));
}
use of io.trino.sql.planner.Symbol in project trino by trinodb.
the class UnwrapSingleColumnRowInApply method unwrapSingleColumnRow.
private Optional<Unwrapping> unwrapSingleColumnRow(Context context, Expression value, Expression list, BiFunction<Symbol, Symbol, Expression> function) {
Type type = typeAnalyzer.getType(context.getSession(), context.getSymbolAllocator().getTypes(), value);
if (type instanceof RowType) {
RowType rowType = (RowType) type;
if (rowType.getFields().size() == 1) {
Type elementType = rowType.getTypeParameters().get(0);
Symbol valueSymbol = context.getSymbolAllocator().newSymbol("input", elementType);
Symbol listSymbol = context.getSymbolAllocator().newSymbol("subquery", elementType);
Assignment inputAssignment = new Assignment(valueSymbol, new SubscriptExpression(value, new LongLiteral("1")));
Assignment nestedPlanAssignment = new Assignment(listSymbol, new SubscriptExpression(list, new LongLiteral("1")));
Expression comparison = function.apply(valueSymbol, listSymbol);
return Optional.of(new Unwrapping(comparison, inputAssignment, nestedPlanAssignment));
}
}
return Optional.empty();
}
use of io.trino.sql.planner.Symbol in project trino by trinodb.
the class SymbolMapper method map.
public GroupIdNode map(GroupIdNode node, PlanNode source) {
Map<Symbol, Symbol> newGroupingMappings = new HashMap<>();
ImmutableList.Builder<List<Symbol>> newGroupingSets = ImmutableList.builder();
for (List<Symbol> groupingSet : node.getGroupingSets()) {
ImmutableList.Builder<Symbol> newGroupingSet = ImmutableList.builder();
for (Symbol output : groupingSet) {
Symbol newOutput = map(output);
newGroupingMappings.putIfAbsent(newOutput, map(node.getGroupingColumns().get(output)));
newGroupingSet.add(newOutput);
}
newGroupingSets.add(newGroupingSet.build());
}
return new GroupIdNode(node.getId(), source, newGroupingSets.build(), newGroupingMappings, mapAndDistinct(node.getAggregationArguments()), map(node.getGroupIdSymbol()));
}
use of io.trino.sql.planner.Symbol in project trino by trinodb.
the class SymbolMapper method map.
public PatternRecognitionNode map(PatternRecognitionNode node, PlanNode source) {
ImmutableMap.Builder<Symbol, WindowNode.Function> newFunctions = ImmutableMap.builder();
node.getWindowFunctions().forEach((symbol, function) -> {
List<Expression> newArguments = function.getArguments().stream().map(this::map).collect(toImmutableList());
WindowNode.Frame newFrame = map(function.getFrame());
newFunctions.put(map(symbol), new WindowNode.Function(function.getResolvedFunction(), newArguments, newFrame, function.isIgnoreNulls()));
});
ImmutableMap.Builder<Symbol, Measure> newMeasures = ImmutableMap.builder();
node.getMeasures().forEach((symbol, measure) -> {
ExpressionAndValuePointers newExpression = map(measure.getExpressionAndValuePointers());
newMeasures.put(map(symbol), new Measure(newExpression, measure.getType()));
});
ImmutableMap.Builder<IrLabel, ExpressionAndValuePointers> newVariableDefinitions = ImmutableMap.builder();
node.getVariableDefinitions().forEach((label, expression) -> newVariableDefinitions.put(label, map(expression)));
return new PatternRecognitionNode(node.getId(), source, mapAndDistinct(node.getSpecification()), node.getHashSymbol().map(this::map), node.getPrePartitionedInputs().stream().map(this::map).collect(toImmutableSet()), node.getPreSortedOrderPrefix(), newFunctions.buildOrThrow(), newMeasures.buildOrThrow(), node.getCommonBaseFrame().map(this::map), node.getRowsPerMatch(), node.getSkipToLabel(), node.getSkipToPosition(), node.isInitial(), node.getPattern(), node.getSubsets(), newVariableDefinitions.buildOrThrow());
}
use of io.trino.sql.planner.Symbol in project trino by trinodb.
the class SymbolMapper method symbolReallocator.
public static SymbolMapper symbolReallocator(Map<Symbol, Symbol> mapping, SymbolAllocator symbolAllocator) {
return new SymbolMapper(symbol -> {
if (mapping.containsKey(symbol)) {
while (mapping.containsKey(symbol) && !mapping.get(symbol).equals(symbol)) {
symbol = mapping.get(symbol);
}
// do not remap the symbol further
mapping.put(symbol, symbol);
return symbol;
}
Symbol newSymbol = symbolAllocator.newSymbol(symbol);
mapping.put(symbol, newSymbol);
// do not remap the symbol further
mapping.put(newSymbol, newSymbol);
return newSymbol;
});
}
Aggregations