private static Expression orNullHashCode(Expression expression) { return new CoalesceExpression(expression, new LongLiteral(String.valueOf(NULL_HASH_CODE))); }
@Override protected R visitCoalesceExpression(CoalesceExpression node, C context) { for (Expression operand : node.getOperands()) { process(operand, context); } return null; }
List<Object> values = node.getOperands().stream() .map(value -> processWithExceptionHandling(value, context)) .filter(Objects::nonNull) .flatMap(expression -> { if (expression instanceof CoalesceExpression) { return ((CoalesceExpression) expression).getOperands().stream(); return getOnlyElement(expressions); return new CoalesceExpression(expressions);
List<Object> values = node.getOperands().stream() .map(value -> processWithExceptionHandling(value, context)) .filter(Objects::nonNull) .flatMap(expression -> { if (expression instanceof CoalesceExpression) { return ((CoalesceExpression) expression).getOperands().stream(); return getOnlyElement(expressions); return new CoalesceExpression(expressions);
private static Expression orNullHashCode(Expression expression) { return new CoalesceExpression(expression, new LongLiteral(String.valueOf(NULL_HASH_CODE))); }
@Override protected Boolean visitCoalesceExpression(CoalesceExpression node, Void context) { return node.getOperands().stream().allMatch(expression -> process(expression, context)); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(identifier(column), new StringLiteral("")), identifier(alias)); }
@Override protected Boolean visitCoalesceExpression(CoalesceExpression node, Void context) { return node.getOperands().stream().allMatch(expression -> process(expression, context)); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(identifier(column), new StringLiteral("")), identifier(alias)); }
@Override protected String visitCoalesceExpression(CoalesceExpression node, Void context) { return "COALESCE(" + joinExpressions(node.getOperands()) + ")"; }
public static Optional<Expression> getHashExpression(List<Symbol> symbols) { if (symbols.isEmpty()) { return Optional.empty(); } Expression result = new LongLiteral(String.valueOf(INITIAL_HASH_VALUE)); for (Symbol symbol : symbols) { Expression hashField = new FunctionCall( QualifiedName.of(HASH_CODE), Optional.empty(), false, ImmutableList.of(new SymbolReference(symbol.getName()))); hashField = new CoalesceExpression(hashField, new LongLiteral(String.valueOf(NULL_HASH_CODE))); result = new FunctionCall(QualifiedName.of("combine_hash"), ImmutableList.of(result, hashField)); } return Optional.of(result); }
@Override protected String visitCoalesceExpression(CoalesceExpression node, Void context) { return "COALESCE(" + joinExpressions(node.getOperands()) + ")"; }
public static Optional<Expression> getHashExpression(List<Symbol> symbols) { if (symbols.isEmpty()) { return Optional.empty(); } Expression result = new LongLiteral(String.valueOf(INITIAL_HASH_VALUE)); for (Symbol symbol : symbols) { Expression hashField = new FunctionCall( QualifiedName.of(HASH_CODE), Optional.empty(), false, ImmutableList.of(new SymbolReference(symbol.getName()))); hashField = new CoalesceExpression(hashField, new LongLiteral(String.valueOf(NULL_HASH_CODE))); result = new FunctionCall(QualifiedName.of("combine_hash"), ImmutableList.of(result, hashField)); } return Optional.of(result); }
@Override protected R visitCoalesceExpression(CoalesceExpression node, C context) { for (Expression operand : node.getOperands()) { process(operand, context); } return null; }
for (Symbol symbol : outerJoin.getOutputSymbols()) { if (aggregationNode.getAggregations().containsKey(symbol)) { assignmentsBuilder.put(symbol, new CoalesceExpression(symbol.toSymbolReference(), sourceAggregationToOverNullMapping.get(symbol).toSymbolReference()));
public static Optional<Symbol> rewriteExpression(Map<Symbol, Expression> assignments, Expression expression) { checkArgument(expression instanceof CoalesceExpression, "The rewrite can only handle CoalesceExpression"); // We are using the property that the result of coalesce from full outer join keys would not be null despite of the order // of the arguments. Thus we extract and compare the symbols of the CoalesceExpression as a set rather than compare the // CoalesceExpression directly. for (Map.Entry<Symbol, Expression> entry : assignments.entrySet()) { if (entry.getValue() instanceof CoalesceExpression) { Set<Symbol> symbolsInAssignment = ((CoalesceExpression) entry.getValue()).getOperands().stream() .filter(SymbolReference.class::isInstance) .map(Symbol::from) .collect(toImmutableSet()); Set<Symbol> symbolInExpression = ((CoalesceExpression) expression).getOperands().stream() .filter(SymbolReference.class::isInstance) .map(Symbol::from) .collect(toImmutableSet()); if (symbolsInAssignment.containsAll(symbolInExpression)) { return Optional.of(entry.getKey()); } } } return Optional.empty(); } }
for (Symbol symbol : outerJoin.getOutputSymbols()) { if (aggregationNode.getAggregations().containsKey(symbol)) { assignmentsBuilder.put(symbol, new CoalesceExpression(symbol.toSymbolReference(), sourceAggregationToOverNullMapping.get(symbol).toSymbolReference()));
@Override protected Type visitCoalesceExpression(CoalesceExpression node, StackableAstVisitorContext<Context> context) { Type type = coerceToSingleType(context, "All COALESCE operands must be the same type: %s", node.getOperands()); return setExpressionType(node, type); }
@Test(timeOut = 10_000) public void testPossibleExponentialOptimizationTime() { Expression expression = new LongLiteral("1"); ImmutableMap.Builder<NodeRef<Expression>, Type> types = ImmutableMap.builder(); types.put(NodeRef.of(expression), BIGINT); for (int i = 0; i < 100; i++) { expression = new CoalesceExpression(expression, new LongLiteral("2")); types.put(NodeRef.of(expression), BIGINT); } translateAndOptimize(expression, types.build()); }
@Override protected SymbolStatsEstimate visitCoalesceExpression(CoalesceExpression node, Void context) { requireNonNull(node, "node is null"); SymbolStatsEstimate result = null; for (Expression operand : node.getOperands()) { SymbolStatsEstimate operandEstimates = process(operand); if (result != null) { result = estimateCoalesce(result, operandEstimates); } else { result = operandEstimates; } } return requireNonNull(result, "result is null"); }