@Override protected String visitWhenClause(WhenClause node, Void context) { return "WHEN " + process(node.getOperand(), context) + " THEN " + process(node.getResult(), 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)); }
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());
private List<Expression> getCaseResultExpressions(List<WhenClause> whenClauses, Optional<Expression> defaultValue) { List<Expression> resultExpressions = new ArrayList<>(); for (WhenClause whenClause : whenClauses) { resultExpressions.add(whenClause.getResult()); } defaultValue.ifPresent(resultExpressions::add); return resultExpressions; }
@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); }
private List<Expression> getCaseResultExpressions(List<WhenClause> whenClauses, Optional<Expression> defaultValue) { List<Expression> resultExpressions = new ArrayList<>(); for (WhenClause whenClause : whenClauses) { resultExpressions.add(whenClause.getResult()); } defaultValue.ifPresent(resultExpressions::add); return resultExpressions; }
@Override protected R visitWhenClause(WhenClause node, C context) { process(node.getOperand(), context); process(node.getResult(), context); return null; }
Object whenOperand = processWithExceptionHandling(whenClause.getOperand(), context); Object result = processWithExceptionHandling(whenClause.getResult(), context); 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()))) {
@Override public Node visitWhenClause(SqlBaseParser.WhenClauseContext context) { return new WhenClause(getLocation(context), (Expression) visit(context.condition), (Expression) visit(context.result)); }
@Override protected Boolean visitWhenClause(WhenClause actual, Node expected) { if (!(expected instanceof WhenClause)) { return false; } WhenClause expectedWhenClause = (WhenClause) expected; return process(actual.getOperand(), expectedWhenClause.getOperand()) && process(actual.getResult(), expectedWhenClause.getResult()); }
@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 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 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); }
Object whenOperand = processWithExceptionHandling(whenClause.getOperand(), context); Object result = processWithExceptionHandling(whenClause.getResult(), context); 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()))) {
private static Expression ensureScalarSubquery() { return new SimpleCaseExpression( new SymbolReference("is_distinct"), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCall( QualifiedName.of("fail"), ImmutableList.of( new LongLiteral(Long.toString(StandardErrorCode.SUBQUERY_MULTIPLE_ROWS.ordinal())), new StringLiteral("Scalar sub-query has returned multiple rows"))), StandardTypes.BOOLEAN))); } }
@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; }
new WhenClause(isGreaterThan(countMatchesSymbol, 0), booleanConstant(true)), new WhenClause(isGreaterThan(countNullMatchesSymbol, 0), booleanConstant(null))), Optional.of(booleanConstant(false))); return new ProjectNode(
@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(getType(clause)), getType(clause), process(clause.getOperand(), context), process(clause.getResult(), context))); } Type returnType = getType(node); arguments.add(node.getDefaultValue() .map((value) -> process(value, context)) .orElse(constantNull(returnType))); return call(switchSignature(returnType), returnType, arguments.build()); }
@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")))); } }