@Override protected String visitSearchedCaseExpression(SearchedCaseExpression node, Void context) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE"); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, context)); } node.getDefaultValue() .ifPresent((value) -> parts.add("ELSE").add(process(value, context))); parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected R visitSearchedCaseExpression(SearchedCaseExpression node, C context) { for (WhenClause clause : node.getWhenClauses()) { process(clause, context); } node.getDefaultValue() .ifPresent(value -> process(value, context)); return null; }
@Override protected Boolean visitSearchedCaseExpression(SearchedCaseExpression node, Void context) { for (WhenClause whenClause : node.getWhenClauses()) { if (!process(whenClause.getOperand(), context) || !process(whenClause.getResult(), context)) { return false; } } return !node.getDefaultValue().isPresent() || process(node.getDefaultValue().get(), context); }
@Override protected Object visitSearchedCaseExpression(SearchedCaseExpression node, Object context) { Object defaultResult = processWithExceptionHandling(node.getDefaultValue().orElse(null), context); 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) { // cannot fully evaluate, add updated whenClause whenClauses.add(new WhenClause( toExpression(whenOperand, type(whenClause.getOperand())), toExpression(result, type(whenClause.getResult())))); } else if (Boolean.TRUE.equals(whenOperand)) { // condition is true, use this as defaultResult defaultResult = result; break; } } if (whenClauses.isEmpty()) { return defaultResult; } Expression resultExpression = (defaultResult == null) ? null : toExpression(defaultResult, type(node)); return new SearchedCaseExpression(whenClauses, Optional.ofNullable(resultExpression)); }
private static class ChangeTypeVisitor implements RowExpressionVisitor<RowExpression, Void> { private final Type targetType; private ChangeTypeVisitor(Type targetType) { this.targetType = targetType; } @Override public RowExpression visitCall(CallExpression call, Void context) { return new CallExpression(call.getSignature(), targetType, call.getArguments()); } @Override public RowExpression visitInputReference(InputReferenceExpression reference, Void context) { return field(reference.getField(), targetType); } @Override public RowExpression visitConstant(ConstantExpression literal, Void context) { return constant(literal.getValue(), targetType); } @Override public RowExpression visitLambda(LambdaDefinitionExpression lambda, Void context)
@Override protected Type visitSearchedCaseExpression(SearchedCaseExpression node, StackableAstVisitorContext<Context> context) { for (WhenClause whenClause : node.getWhenClauses()) { coerceType(context, whenClause.getOperand(), BOOLEAN, "CASE WHEN clause"); } Type type = coerceToSingleType(context, "All CASE results must be the same type: %s", getCaseResultExpressions(node.getWhenClauses(), node.getDefaultValue())); setExpressionType(node, type); for (WhenClause whenClause : node.getWhenClauses()) { Type whenClauseType = process(whenClause.getResult(), context); setExpressionType(whenClause, whenClauseType); } return type; }
@Override protected String visitSearchedCaseExpression(SearchedCaseExpression node, Void context) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE"); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, context)); } node.getDefaultValue() .ifPresent((value) -> parts.add("ELSE").add(process(value, context))); parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected String visitSearchedCaseExpression(SearchedCaseExpression node, Boolean unmangleNames) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE"); 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 String visitSearchedCaseExpression(SearchedCaseExpression node, Boolean unmangleNames) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE"); 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 String visitSearchedCaseExpression(SearchedCaseExpression node, Void context) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.add("CASE"); for (WhenClause whenClause : node.getWhenClauses()) { parts.add(process(whenClause, context)); } node.getDefaultValue() .ifPresent((value) -> parts.add("ELSE").add(process(value, context))); parts.add("END"); return "(" + Joiner.on(' ').join(parts.build()) + ")"; }
@Override protected R visitSearchedCaseExpression(SearchedCaseExpression node, C context) { for (WhenClause clause : node.getWhenClauses()) { process(clause, context); } node.getDefaultValue() .ifPresent(value -> process(value, context)); return null; }
@Override protected Boolean visitSearchedCaseExpression(SearchedCaseExpression node, Void context) { for (WhenClause whenClause : node.getWhenClauses()) { if (!process(whenClause.getOperand(), context) || !process(whenClause.getResult(), context)) { return false; } } return !node.getDefaultValue().isPresent() || process(node.getDefaultValue().get(), context); }
@Override protected R visitSearchedCaseExpression(SearchedCaseExpression node, C context) { for (WhenClause clause : node.getWhenClauses()) { process(clause, context); } node.getDefaultValue() .ifPresent(value -> process(value, context)); return null; }
private static class ChangeTypeVisitor implements RowExpressionVisitor<Void, RowExpression> { private final Type targetType; private ChangeTypeVisitor(Type targetType) { this.targetType = targetType; } @Override public RowExpression visitCall(CallExpression call, Void context) { return new CallExpression(call.getSignature(), targetType, call.getArguments()); } @Override public RowExpression visitInputReference(InputReferenceExpression reference, Void context) { return new InputReferenceExpression(reference.getField(), targetType); } @Override public RowExpression visitConstant(ConstantExpression literal, Void context) { return new ConstantExpression(literal.getValue(), targetType); } }
@Override protected Type visitSearchedCaseExpression(SearchedCaseExpression node, StackableAstVisitorContext<AnalysisContext> context) { for (WhenClause whenClause : node.getWhenClauses()) { coerceType(context, whenClause.getOperand(), BOOLEAN, "CASE WHEN clause"); } Type type = coerceToSingleType(context, "All CASE results must be the same type: %s", getCaseResultExpressions(node.getWhenClauses(), node.getDefaultValue())); expressionTypes.put(node, type); for (WhenClause whenClause : node.getWhenClauses()) { Type whenClauseType = process(whenClause.getResult(), context); requireNonNull(whenClauseType, format("Expression types does not contain an entry for %s", whenClause)); expressionTypes.put(whenClause, whenClauseType); } return type; }
@Override protected Object visitSearchedCaseExpression(SearchedCaseExpression node, Object context) { Object defaultResult = processWithExceptionHandling(node.getDefaultValue().orElse(null), context); 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) { // cannot fully evaluate, add updated whenClause whenClauses.add(new WhenClause( toExpression(whenOperand, type(whenClause.getOperand())), toExpression(result, type(whenClause.getResult())))); } else if (Boolean.TRUE.equals(whenOperand)) { // condition is true, use this as defaultResult defaultResult = result; break; } } if (whenClauses.isEmpty()) { return defaultResult; } Expression resultExpression = (defaultResult == null) ? null : toExpression(defaultResult, type(node)); return new SearchedCaseExpression(whenClauses, Optional.ofNullable(resultExpression)); }