Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.sql;
 
 
 import java.util.List;
 
 import static com.facebook.presto.sql.tree.BooleanLiteral.FALSE_LITERAL;
 import static com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL;
 import static com.google.common.base.Predicates.not;
 import static com.google.common.collect.Iterables.filter;
 
 public final class ExpressionUtils
 {
     private ExpressionUtils() {}
 
     public static List<ExpressionextractConjuncts(Expression expression)
     {
         if (expression instanceof LogicalBinaryExpression && ((LogicalBinaryExpressionexpression).getType() == ..) {
             LogicalBinaryExpression and = (LogicalBinaryExpressionexpression;
             return ImmutableList.<Expression>builder()
                     .addAll(extractConjuncts(and.getLeft()))
                     .addAll(extractConjuncts(and.getRight()))
                     .build();
         }
 
         return ImmutableList.of(expression);
     }
 
     public static List<ExpressionextractDisjuncts(Expression expression)
     {
         if (expression instanceof LogicalBinaryExpression && ((LogicalBinaryExpressionexpression).getType() == ..) {
             LogicalBinaryExpression or = (LogicalBinaryExpressionexpression;
             return ImmutableList.<Expression>builder()
                     .addAll(extractDisjuncts(or.getLeft()))
                     .addAll(extractDisjuncts(or.getRight()))
                     .build();
         }
 
         return ImmutableList.of(expression);
     }
 
     public static Expression and(Expression... expressions)
     {
         return and(Arrays.asList(expressions));
     }
 
     public static Expression and(Iterable<Expressionexpressions)
     {
         return binaryExpression(..expressions);
     }
 
     public static Expression or(Expression... expressions)
     {
         return or(Arrays.asList(expressions));
     }
 
     public static Expression or(Iterable<Expressionexpressions)
     {
         return binaryExpression(..expressions);
     }
 
     public static Expression binaryExpression(LogicalBinaryExpression.Type typeIterable<Expressionexpressions)
     {
         Preconditions.checkNotNull(type"type is null");
         Preconditions.checkNotNull(expressions"expressions is null");
         Preconditions.checkArgument(!Iterables.isEmpty(expressions), "expressions is empty");
 
         Iterator<Expressioniterator = expressions.iterator();
 
         Expression result = iterator.next();
         while (iterator.hasNext()) {
            result = new LogicalBinaryExpression(typeresultiterator.next());
        }
        return result;
    }
    public static Expression combineConjuncts(Expression... expressions)
    {
        return combineConjuncts(Arrays.asList(expressions));
    }
    public static Expression combineConjuncts(Iterable<Expressionexpressions)
    {
        return combineConjunctsWithDefault(expressions);
    }
    public static Expression combineConjunctsWithDefault(Iterable<ExpressionexpressionsExpression emptyDefault)
    {
        Preconditions.checkNotNull(expressions"expressions is null");
        // Flatten all the expressions into their component conjuncts
        expressions = Iterables.concat(Iterables.transform(expressions, ExpressionUtils::extractConjuncts));
        // Strip out all true literal conjuncts
        expressions = Iterables.filter(expressionsnot(Predicates.<Expression>equalTo()));
        expressions = removeDuplicates(expressions);
        return Iterables.isEmpty(expressions) ? emptyDefault : and(expressions);
    }
    public static Expression combineDisjuncts(Expression... expressions)
    {
        return combineDisjuncts(Arrays.asList(expressions));
    }
    public static Expression combineDisjuncts(Iterable<Expressionexpressions)
    {
        return combineDisjunctsWithDefault(expressions);
    }
    public static Expression combineDisjunctsWithDefault(Iterable<ExpressionexpressionsExpression emptyDefault)
    {
        Preconditions.checkNotNull(expressions"expressions is null");
        // Flatten all the expressions into their component disjuncts
        expressions = Iterables.concat(Iterables.transform(expressions, ExpressionUtils::extractDisjuncts));
        // Strip out all false literal disjuncts
        expressions = Iterables.filter(expressionsnot(Predicates.<Expression>equalTo()));
        expressions = removeDuplicates(expressions);
        return Iterables.isEmpty(expressions) ? emptyDefault : or(expressions);
    }
    public static Expression stripNonDeterministicConjuncts(Expression expression)
    {
        return combineConjuncts(filter(extractConjuncts(expression), DeterminismEvaluator::isDeterministic));
    }
    public static Function<ExpressionExpressionexpressionOrNullSymbols(final Predicate<SymbolnullSymbolScope)
    {
        return expression -> {
            Iterable<Symbolsymbols = filter(DependencyExtractor.extractUnique(expression), nullSymbolScope);
            if (Iterables.isEmpty(symbols)) {
                return expression;
            }
            ImmutableList.Builder<ExpressionnullConjuncts = ImmutableList.builder();
            for (Symbol symbol : symbols) {
                nullConjuncts.add(new IsNullPredicate(new QualifiedNameReference(symbol.toQualifiedName())));
            }
            return or(expressionand(nullConjuncts.build()));
        };
    }
    private static Iterable<ExpressionremoveDuplicates(Iterable<Expressionexpressions)
    {
        // Capture all non-deterministic predicates
        Iterable<ExpressionnonDeterministicDisjuncts = Iterables.filter(expressionsnot(DeterminismEvaluator::isDeterministic));
        // Capture and de-dupe all deterministic predicates
        Iterable<ExpressiondeterministicDisjuncts = ImmutableSet.copyOf(Iterables.filter(expressions, DeterminismEvaluator::isDeterministic));
        return Iterables.concat(nonDeterministicDisjunctsdeterministicDisjuncts);
    }
New to GrepCode? Check out our FAQ X