@Override protected Object visitCoalesceExpression(CoalesceExpression node, Object context) { Type type = type(node); List<Object> values = node.getOperands().stream() .map(value -> processWithExceptionHandling(value, context)) .filter(value -> value != null) .collect(Collectors.toList()); if ((!values.isEmpty() && !(values.get(0) instanceof Expression)) || values.size() == 1) { return values.get(0); } List<Expression> expressions = values.stream() .map(value -> toExpression(value, type)) .collect(Collectors.toList()); if (expressions.isEmpty()) { return null; } return new CoalesceExpression(expressions); }
@Override protected RowExpression visitCoalesceExpression(CoalesceExpression node, Void context) { List<RowExpression> arguments = node.getOperands().stream() .map(value -> process(value, context)) .collect(toImmutableList()); List<Type> argumentTypes = arguments.stream().map(RowExpression::getType).collect(toImmutableList()); return call(coalesceSignature(types.get(node), argumentTypes), types.get(node), arguments); }
private static Expression oneIfNull(Optional<Symbol> symbol) { if (symbol.isPresent()) { return new CoalesceExpression(new QualifiedNameReference(symbol.get().toQualifiedName()), new LongLiteral("1")); } else { return new LongLiteral("1"); } }
@Override protected Type visitCoalesceExpression(CoalesceExpression node, StackableAstVisitorContext<AnalysisContext> context) { Type type = coerceToSingleType(context, "All COALESCE operands must be the same type: %s", node.getOperands()); expressionTypes.put(node, type); return type; }
@Override protected String visitCoalesceExpression(CoalesceExpression node, Void context) { return "COALESCE(" + joinExpressions(node.getOperands()) + ")"; }
@Override protected String visitCoalesceExpression(CoalesceExpression node, StackableAstVisitorContext<Integer> indent) { return "COALESCE(" + joinExpressions(node.getOperands(), indent) + ")"; }
private static Expression orNullHashCode(Expression expression) { return new CoalesceExpression(expression, new LongLiteral(String.valueOf(TypeUtils.NULL_HASH_CODE))); }
@Override protected Boolean visitCoalesceExpression(CoalesceExpression node, Void context) { return node.getOperands().stream().allMatch(expression -> process(expression, context)); }
@Override protected String visitCoalesceExpression(CoalesceExpression node, Boolean unmangleNames) { return "COALESCE(" + joinExpressions(node.getOperands(), unmangleNames) + ")"; }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(nameReference(column), new StringLiteral("")), alias); }