public Type getTypeForSymbol(Symbol symbol) { return types.get(symbol); } }
private Type checkedTypeLookup(Symbol symbol) { Type type = types.get(symbol); checkArgument(type != null, "Types is missing info for symbol: %s", symbol); return type; }
private boolean canSkipHashGeneration(List<Symbol> partitionSymbols) { // HACK: bigint grouped aggregation has special operators that do not use precomputed hash, so we can skip hash generation return partitionSymbols.isEmpty() || (partitionSymbols.size() == 1 && types.get(Iterables.getOnlyElement(partitionSymbols)).equals(BIGINT)); }
private String formatOutputs(Iterable<Symbol> symbols) { return Joiner.on(", ").join(Iterables.transform(symbols, input -> input + ":" + types.get(input).getDisplayName())); }
/** * Returns estimated data size. * Unknown value is represented by {@link Double#NaN} */ public double getOutputSizeInBytes(Collection<Symbol> outputSymbols, TypeProvider types) { requireNonNull(outputSymbols, "outputSymbols is null"); return outputSymbols.stream() .mapToDouble(symbol -> getOutputSizeForSymbol(getSymbolStatistics(symbol), types.get(symbol))) .sum(); }
private NumericValues(Symbol column, T min, T integerNegative, T fractionalNegative, T integerPositive, T fractionalPositive, T max) { this.column = requireNonNull(column, "column is null"); this.type = requireNonNull(TYPES.get(column), "type for column not found: " + column); this.min = requireNonNull(min, "min is null"); this.integerNegative = requireNonNull(integerNegative, "integerNegative is null"); this.fractionalNegative = requireNonNull(fractionalNegative, "fractionalNegative is null"); this.integerPositive = requireNonNull(integerPositive, "integerPositive is null"); this.fractionalPositive = requireNonNull(fractionalPositive, "fractionalPositive is null"); this.max = requireNonNull(max, "max is null"); }
@Override public Expression rewriteSymbolReference(SymbolReference node, Context context, ExpressionTreeRewriter<Context> treeRewriter) { Symbol symbol = Symbol.from(node); Integer channel = symbolToChannelMapping.get(symbol); if (channel == null) { checkArgument(context.isInLambda(), "Cannot resolve symbol %s", node.getName()); return node; } Type type = types.get(symbol); checkArgument(type != null, "Cannot resolve symbol %s", node.getName()); int parameter = fieldToParameter.computeIfAbsent(channel, field -> { inputChannels.add(field); inputTypes.add(type); return nextParameter++; }); return new FieldReference(parameter); }
@Override public Optional<PlanNodeStatsEstimate> calculate(ValuesNode node, StatsProvider sourceStats, Lookup lookup, Session session, TypeProvider types) { PlanNodeStatsEstimate.Builder statsBuilder = PlanNodeStatsEstimate.builder(); statsBuilder.setOutputRowCount(node.getRows().size()); for (int symbolId = 0; symbolId < node.getOutputSymbols().size(); ++symbolId) { Symbol symbol = node.getOutputSymbols().get(symbolId); List<Object> symbolValues = getSymbolValues(node, symbolId, session, types.get(symbol)); statsBuilder.addSymbolStatistics(symbol, buildSymbolStatistics(symbolValues, session, types.get(symbol))); } return Optional.of(statsBuilder.build()); }
private void checkSignature(Symbol symbol, Signature signature) { TypeSignature expectedTypeSignature = types.get(symbol).getTypeSignature(); TypeSignature actualTypeSignature = signature.getReturnType(); verifyTypeSignature(symbol, expectedTypeSignature, actualTypeSignature); }
@Override public Void visitUnion(UnionNode node, Void context) { visitPlan(node, context); ListMultimap<Symbol, Symbol> symbolMapping = node.getSymbolMapping(); for (Symbol keySymbol : symbolMapping.keySet()) { List<Symbol> valueSymbols = symbolMapping.get(keySymbol); Type expectedType = types.get(keySymbol); for (Symbol valueSymbol : valueSymbols) { verifyTypeSignature(keySymbol, expectedType.getTypeSignature(), types.get(valueSymbol).getTypeSignature()); } } return null; }
private void checkCall(Symbol symbol, FunctionCall call) { Type expectedType = types.get(symbol); Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, sqlParser, types, call, emptyList(), warningCollector); Type actualType = expressionTypes.get(NodeRef.<Expression>of(call)); verifyTypeSignature(symbol, expectedType.getTypeSignature(), actualType.getTypeSignature()); }
@Override public Void visitProject(ProjectNode node, Void context) { visitPlan(node, context); for (Map.Entry<Symbol, Expression> entry : node.getAssignments().entrySet()) { Type expectedType = types.get(entry.getKey()); if (entry.getValue() instanceof SymbolReference) { SymbolReference symbolReference = (SymbolReference) entry.getValue(); verifyTypeSignature(entry.getKey(), expectedType.getTypeSignature(), types.get(Symbol.from(symbolReference)).getTypeSignature()); continue; } Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, sqlParser, types, entry.getValue(), emptyList(), warningCollector); Type actualType = expressionTypes.get(NodeRef.of(entry.getValue())); verifyTypeSignature(entry.getKey(), expectedType.getTypeSignature(), actualType.getTypeSignature()); } return null; }
private boolean checkAllEquatableTypes(AggregateInfo aggregateInfo) { for (Symbol symbol : aggregateInfo.getOriginalNonDistinctAggregateArgs()) { Type type = symbolAllocator.getTypes().get(symbol); if (!type.isComparable()) { return false; } } if (!symbolAllocator.getTypes().get(aggregateInfo.getMask()).isComparable()) { return false; } return true; }
private Signature getFunctionSignature(QualifiedName functionName, Symbol argument) { return metadata.getFunctionRegistry() .resolveFunction( functionName, ImmutableList.of(new TypeSignatureProvider(symbolAllocator.getTypes().get(argument).getTypeSignature()))); }
private JoinFilterFunctionFactory compileJoinFilterFunction( Expression filterExpression, Map<Symbol, Integer> probeLayout, Map<Symbol, Integer> buildLayout, TypeProvider types, Session session) { Map<Symbol, Integer> joinSourcesLayout = createJoinSourcesLayout(buildLayout, probeLayout); Map<Integer, Type> sourceTypes = joinSourcesLayout.entrySet().stream() .collect(toImmutableMap(Map.Entry::getValue, entry -> types.get(entry.getKey()))); Expression rewrittenFilter = new SymbolToInputRewriter(joinSourcesLayout).rewrite(filterExpression); Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypesFromInput( session, metadata, sqlParser, sourceTypes, rewrittenFilter, emptyList(), /* parameters have already been replaced */ NOOP); RowExpression translatedFilter = toRowExpression(rewrittenFilter, expressionTypes); return joinFilterFunctionCompiler.compileJoinFilterFunction(translatedFilter, buildLayout.size()); }
private Type getType(Expression expression) { if (expression instanceof SymbolReference) { Symbol symbol = Symbol.from(expression); return requireNonNull(types.get(symbol), () -> format("No type for symbol %s", symbol)); } ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries( metadata.getFunctionRegistry(), metadata.getTypeManager(), session, types, ImmutableList.of(), // At this stage, there should be no subqueries in the plan. node -> new VerifyException("Unexpected subquery"), WarningCollector.NOOP, false); return expressionAnalyzer.analyze(expression, Scope.create()); }
private PlanNode appendMarkers(PlanNode source, int markerIndex, List<Symbol> markers, Map<Symbol, SymbolReference> projections) { Assignments.Builder assignments = Assignments.builder(); // add existing intersect symbols to projection for (Map.Entry<Symbol, SymbolReference> entry : projections.entrySet()) { Symbol symbol = symbolAllocator.newSymbol(entry.getKey().getName(), symbolAllocator.getTypes().get(entry.getKey())); assignments.put(symbol, entry.getValue()); } // add extra marker fields to the projection for (int i = 0; i < markers.size(); ++i) { Expression expression = (i == markerIndex) ? TRUE_LITERAL : new Cast(new NullLiteral(), StandardTypes.BOOLEAN); assignments.put(symbolAllocator.newSymbol(markers.get(i).getName(), BOOLEAN), expression); } return new ProjectNode(idAllocator.getNextId(), source, assignments.build()); }
private InPredicate in(Symbol symbol, List<?> values) { return in(symbol.toSymbolReference(), TYPES.get(symbol), values); }
@Override protected Type visitSymbolReference(SymbolReference node, StackableAstVisitorContext<Context> context) { if (context.getContext().isInLambda()) { Optional<ResolvedField> resolvedField = context.getContext().getScope().tryResolveField(node, QualifiedName.of(node.getName())); if (resolvedField.isPresent() && context.getContext().getFieldToLambdaArgumentDeclaration().containsKey(FieldId.from(resolvedField.get()))) { return setExpressionType(node, resolvedField.get().getType()); } } Type type = symbolTypes.get(Symbol.from(node)); return setExpressionType(node, type); }
@Override public PlanNode visitJoin(JoinNode node, RewriteContext<Void> context) { PlanNode left = context.rewrite(node.getLeft()); PlanNode right = context.rewrite(node.getRight()); List<JoinNode.EquiJoinClause> canonicalCriteria = canonicalizeJoinCriteria(node.getCriteria()); Optional<Expression> canonicalFilter = node.getFilter().map(this::canonicalize); Optional<Symbol> canonicalLeftHashSymbol = canonicalize(node.getLeftHashSymbol()); Optional<Symbol> canonicalRightHashSymbol = canonicalize(node.getRightHashSymbol()); if (node.getType().equals(INNER)) { canonicalCriteria.stream() .filter(clause -> types.get(clause.getLeft()).equals(types.get(clause.getRight()))) .filter(clause -> node.getOutputSymbols().contains(clause.getLeft())) .forEach(clause -> map(clause.getRight(), clause.getLeft())); } return new JoinNode(node.getId(), node.getType(), left, right, canonicalCriteria, canonicalizeAndDistinct(node.getOutputSymbols()), canonicalFilter, canonicalLeftHashSymbol, canonicalRightHashSymbol, node.getDistributionType()); }