@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 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)); }
public static Expression caseWhen(Expression operand, Expression result) { return new SearchedCaseExpression(ImmutableList.of(new WhenClause(operand, result)), Optional.empty()); }
for(WhenClause when : sce.getWhenClauses()){ orderKey = SelectParser.visitDereferenceExpression( (DereferenceExpression)((IsNullPredicate)when.getOperand()).getValue());
@Override public Node visitSearchedCase(SqlBaseParser.SearchedCaseContext context) { return new SearchedCaseExpression( getLocation(context), visit(context.whenClause(), WhenClause.class), visitIfPresent(context.elseExpression, Expression.class)); }
@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 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)); }
@Override public Expression rewriteIfExpression(IfExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) { Expression condition = treeRewriter.rewrite(node.getCondition(), context); Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context); Optional<Expression> falseValue = node.getFalseValue().map((value) -> treeRewriter.rewrite(value, context)); return new SearchedCaseExpression(ImmutableList.of(new WhenClause(condition, trueValue)), falseValue); }
@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); }
public Expression rewriteUsingBounds(QuantifiedComparisonExpression quantifiedComparison, Symbol minValue, Symbol maxValue, Symbol countAllValue, Symbol countNonNullValue) { BooleanLiteral emptySetResult = quantifiedComparison.getQuantifier().equals(ALL) ? TRUE_LITERAL : FALSE_LITERAL; Function<List<Expression>, Expression> quantifier = quantifiedComparison.getQuantifier().equals(ALL) ? ExpressionUtils::combineConjuncts : ExpressionUtils::combineDisjuncts; Expression comparisonWithExtremeValue = getBoundComparisons(quantifiedComparison, minValue, maxValue); return new SimpleCaseExpression( countAllValue.toSymbolReference(), ImmutableList.of(new WhenClause( new GenericLiteral("bigint", "0"), emptySetResult)), Optional.of(quantifier.apply(ImmutableList.of( comparisonWithExtremeValue, new SearchedCaseExpression( ImmutableList.of( new WhenClause( new ComparisonExpression(NOT_EQUAL, countAllValue.toSymbolReference(), countNonNullValue.toSymbolReference()), new Cast(new NullLiteral(), BooleanType.BOOLEAN.toString()))), Optional.of(emptySetResult)))))); }
@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; }
SearchedCaseExpression inPredicateEquivalent = new SearchedCaseExpression( ImmutableList.of( new WhenClause(isGreaterThan(countMatchesSymbol, 0), booleanConstant(true)),
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)
@Test public void testExpressionsThatMayReturnNullOnNonNullInput() { List<Expression> candidates = ImmutableList.of( new Cast(nameReference("b"), "BIGINT", true), // try_cast new FunctionCall(QualifiedName.of("try"), ImmutableList.of(nameReference("b"))), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), identifier("x")), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b"))); for (Expression candidate : candidates) { EqualityInference.Builder builder = new EqualityInference.Builder(); builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x"))); builder.extractInferenceCandidates(equals(nameReference("a"), candidate)); EqualityInference inference = builder.build(); List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities(); assertEquals(equalities.size(), 1); assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x")))); } }
@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()) + ")"; }
public static Expression caseWhen(Expression operand, Expression result) { return new SearchedCaseExpression(ImmutableList.of(new WhenClause(operand, result)), Optional.empty()); }
@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; }
public static Expression caseWhen(Expression operand, Expression result) { return new SearchedCaseExpression(ImmutableList.of(new WhenClause(operand, result)), Optional.empty()); }
@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 public Node visitSearchedCase(SqlBaseParser.SearchedCaseContext context) { return new SearchedCaseExpression( getLocation(context), visit(context.whenClause(), WhenClause.class), visitIfPresent(context.elseExpression, Expression.class)); }