@Override protected String visitSimpleCaseExpression(SimpleCaseExpression node, Void context) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE").add(process(node.getOperand(), context)); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, context)); } if (node.getDefaultValue() != null) { parts.add("ELSE").add(process(node.getDefaultValue(), context)); } parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected String visitSimpleCaseExpression(SimpleCaseExpression node, StackableAstVisitorContext<Integer> indent) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE") .add(process(node.getOperand(), indent)); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, indent)); } node.getDefaultValue() .ifPresent((value) -> parts.add("ELSE").add(process(value, indent))); parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected RowExpression visitSimpleCaseExpression(SimpleCaseExpression node, Void context) { ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder(); arguments.add(process(node.getOperand(), context)); for (WhenClause clause : node.getWhenClauses()) { arguments.add(call(whenSignature(types.get(clause)), types.get(clause), process(clause.getOperand(), context), process(clause.getResult(), context))); } Type returnType = types.get(node); arguments.add(node.getDefaultValue() .map((value) -> process(value, context)) .orElse(constantNull(returnType))); return call(switchSignature(returnType), returnType, arguments.build()); }
@Override protected Boolean visitSimpleCaseExpression(SimpleCaseExpression node, Void context) { if (!process(node.getOperand(), context)) { return false; } for (WhenClause whenClause : node.getWhenClauses()) { if (!process(whenClause.getOperand(), context) || !process(whenClause.getResult(), context)) { return false; } } if (node.getDefaultValue().isPresent() && !process(node.getDefaultValue().get(), context)) { return false; } return true; }
@Override protected String visitSimpleCaseExpression(SimpleCaseExpression node, Boolean unmangleNames) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE") .add(process(node.getOperand(), unmangleNames)); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, unmangleNames)); } node.getDefaultValue() .ifPresent((value) -> parts.add("ELSE").add(process(value, unmangleNames))); parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected Object visitSimpleCaseExpression(SimpleCaseExpression node, Object context) { Object operand = processWithExceptionHandling(node.getOperand(), context); Type operandType = type(node.getOperand()); // evaluate defaultClause Expression defaultClause = node.getDefaultValue().orElse(null); Object defaultResult = processWithExceptionHandling(defaultClause, context); // if operand is null, return defaultValue if (operand == null) { return defaultResult; } List<WhenClause> whenClauses = new ArrayList<>(); for (WhenClause whenClause : node.getWhenClauses()) { Object whenOperand = processWithExceptionHandling(whenClause.getOperand(), context); Object result = processWithExceptionHandling(whenClause.getResult(), context); if (whenOperand instanceof Expression || operand instanceof Expression) { // cannot fully evaluate, add updated whenClause whenClauses.add(new WhenClause( toExpression(whenOperand, type(whenClause.getOperand())), toExpression(result, type(whenClause.getResult())))); } else if (whenOperand != null && isEqual(operand, operandType, whenOperand, type(whenClause.getOperand()))) { // condition is true, use this as defaultResult defaultResult = result; break; } } if (whenClauses.isEmpty()) { return defaultResult; } Expression defaultExpression = (defaultResult == null) ? null : toExpression(defaultResult, type(node)); return new SimpleCaseExpression(toExpression(operand, type(node.getOperand())), whenClauses, Optional.ofNullable(defaultExpression)); }
@Override public Node visitSimpleCase(SqlBaseParser.SimpleCaseContext context) { return new SimpleCaseExpression( getLocation(context), (Expression) visit(context.valueExpression()), visit(context.whenClause(), WhenClause.class), visitIfPresent(context.elseExpression, Expression.class)); }