Search in sources :

Example 1 with OR

use of com.facebook.presto.spi.relation.SpecialFormExpression.Form.OR in project presto by prestodb.

the class RowExpressionDomainTranslator method extractDisjuncts.

private List<RowExpression> extractDisjuncts(Type type, DiscreteValues discreteValues, RowExpression reference) {
    List<RowExpression> values = discreteValues.getValues().stream().map(object -> toRowExpression(reference.getSourceLocation(), object, type)).collect(toList());
    // If values is empty, then the equatableValues was either ALL or NONE, both of which should already have been checked for
    checkState(!values.isEmpty());
    RowExpression predicate;
    if (values.size() == 1) {
        predicate = equal(reference, getOnlyElement(values));
    } else {
        predicate = in(reference, values);
    }
    if (!discreteValues.isWhiteList()) {
        predicate = not(functionResolution, predicate);
    }
    return ImmutableList.of(predicate);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) SATURATED_FLOOR_CAST(com.facebook.presto.metadata.CastType.SATURATED_FLOOR_CAST) IS_NULL(com.facebook.presto.spi.relation.SpecialFormExpression.Form.IS_NULL) DiscreteValues(com.facebook.presto.common.predicate.DiscreteValues) RowExpressionVisitor(com.facebook.presto.spi.relation.RowExpressionVisitor) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) SortedRangeSet(com.facebook.presto.common.predicate.SortedRangeSet) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution) PeekingIterator(com.google.common.collect.PeekingIterator) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) EQUAL(com.facebook.presto.common.function.OperatorType.EQUAL) FALSE_CONSTANT(com.facebook.presto.expressions.LogicalRowExpressions.FALSE_CONSTANT) AND(com.facebook.presto.spi.relation.SpecialFormExpression.Form.AND) LESS_THAN(com.facebook.presto.common.function.OperatorType.LESS_THAN) CallExpression(com.facebook.presto.spi.relation.CallExpression) Marker(com.facebook.presto.common.predicate.Marker) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) IN(com.facebook.presto.spi.relation.SpecialFormExpression.Form.IN) RowExpressionInterpreter(com.facebook.presto.sql.planner.RowExpressionInterpreter) NullableValue(com.facebook.presto.common.predicate.NullableValue) InterpretedFunctionInvoker(com.facebook.presto.sql.InterpretedFunctionInvoker) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DomainTranslator(com.facebook.presto.spi.relation.DomainTranslator) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) TRUE_CONSTANT(com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT) Range(com.facebook.presto.common.predicate.Range) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ConnectorSession(com.facebook.presto.spi.ConnectorSession) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) List(java.util.List) OperatorNotFoundException(com.facebook.presto.metadata.OperatorNotFoundException) Optional(java.util.Optional) InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) CAST(com.facebook.presto.metadata.CastType.CAST) Ranges(com.facebook.presto.common.predicate.Ranges) DeterminismEvaluator(com.facebook.presto.spi.relation.DeterminismEvaluator) NOT_EQUAL(com.facebook.presto.common.function.OperatorType.NOT_EQUAL) Utils(com.facebook.presto.common.Utils) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) LESS_THAN_OR_EQUAL(com.facebook.presto.common.function.OperatorType.LESS_THAN_OR_EQUAL) ImmutableList(com.google.common.collect.ImmutableList) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) Form(com.facebook.presto.spi.relation.SpecialFormExpression.Form) OR(com.facebook.presto.spi.relation.SpecialFormExpression.Form.OR) Iterators.peekingIterator(com.google.common.collect.Iterators.peekingIterator) Type(com.facebook.presto.common.type.Type) Nullable(javax.annotation.Nullable) RowExpression(com.facebook.presto.spi.relation.RowExpression) LogicalRowExpressions.and(com.facebook.presto.expressions.LogicalRowExpressions.and) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) OperatorType(com.facebook.presto.common.function.OperatorType) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) GREATER_THAN_OR_EQUAL(com.facebook.presto.common.function.OperatorType.GREATER_THAN_OR_EQUAL) GREATER_THAN(com.facebook.presto.common.function.OperatorType.GREATER_THAN) OPTIMIZED(com.facebook.presto.spi.relation.ExpressionOptimizer.Level.OPTIMIZED) Collectors.toList(java.util.stream.Collectors.toList) BETWEEN(com.facebook.presto.common.function.OperatorType.BETWEEN) LogicalRowExpressions.or(com.facebook.presto.expressions.LogicalRowExpressions.or) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Block(com.facebook.presto.common.block.Block) ValueSet(com.facebook.presto.common.predicate.ValueSet) Metadata(com.facebook.presto.metadata.Metadata) LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 2 with OR

use of com.facebook.presto.spi.relation.SpecialFormExpression.Form.OR in project presto by prestodb.

the class TestLogicalRowExpressions method testBigExpressions.

@Test
public void testBigExpressions() {
    // Do not expand big list (a0 && b0) || (a1 && b1) || ....
    RowExpression bigExpression = or(IntStream.range(0, 1000).boxed().map(i -> and(name("a" + i), name("b" + i))).toArray(RowExpression[]::new));
    assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(bigExpression), bigExpression);
    // extract common predicates on (a && b0) || (a && b1) || ....
    RowExpression bigExpressionWithCommonPredicate = or(IntStream.range(0, 10001).boxed().map(i -> and(name("a"), name("b" + i))).toArray(RowExpression[]::new));
    assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(bigExpressionWithCommonPredicate), or(IntStream.range(0, 10001).boxed().map(i -> and(name("a"), name("b" + i))).toArray(RowExpression[]::new)));
    // a || (a && b0) || (a && b1) || ... can be simplified to a but if conjunctive is very large, we will skip reduction.
    assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(a, bigExpressionWithCommonPredicate)), or(Stream.concat(Stream.of(name("a")), IntStream.range(0, 10001).boxed().map(i -> and(name("a"), name("b" + i)))).toArray(RowExpression[]::new)));
    assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(or(a, bigExpressionWithCommonPredicate)), or(Stream.concat(Stream.of(name("a")), IntStream.range(0, 10001).boxed().map(i -> and(name("a"), name("b" + i)))).toArray(RowExpression[]::new)));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) IntStream(java.util.stream.IntStream) IS_NULL(com.facebook.presto.spi.relation.SpecialFormExpression.Form.IS_NULL) Arrays(java.util.Arrays) NOT_EQUAL(com.facebook.presto.common.function.OperatorType.NOT_EQUAL) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assert.assertEquals(org.testng.Assert.assertEquals) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) LESS_THAN_OR_EQUAL(com.facebook.presto.common.function.OperatorType.LESS_THAN_OR_EQUAL) ImmutableList(com.google.common.collect.ImmutableList) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) EQUAL(com.facebook.presto.common.function.OperatorType.EQUAL) FALSE_CONSTANT(com.facebook.presto.expressions.LogicalRowExpressions.FALSE_CONSTANT) AND(com.facebook.presto.spi.relation.SpecialFormExpression.Form.AND) LESS_THAN(com.facebook.presto.common.function.OperatorType.LESS_THAN) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) CallExpression(com.facebook.presto.spi.relation.CallExpression) OR(com.facebook.presto.spi.relation.SpecialFormExpression.Form.OR) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) BeforeClass(org.testng.annotations.BeforeClass) OperatorType(com.facebook.presto.common.function.OperatorType) TRUE_CONSTANT(com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT) GREATER_THAN_OR_EQUAL(com.facebook.presto.common.function.OperatorType.GREATER_THAN_OR_EQUAL) GREATER_THAN(com.facebook.presto.common.function.OperatorType.GREATER_THAN) List(java.util.List) LogicalRowExpressions.extractPredicates(com.facebook.presto.expressions.LogicalRowExpressions.extractPredicates) Stream(java.util.stream.Stream) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) Optional(java.util.Optional) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Aggregations

OperatorType (com.facebook.presto.common.function.OperatorType)2 EQUAL (com.facebook.presto.common.function.OperatorType.EQUAL)2 GREATER_THAN (com.facebook.presto.common.function.OperatorType.GREATER_THAN)2 GREATER_THAN_OR_EQUAL (com.facebook.presto.common.function.OperatorType.GREATER_THAN_OR_EQUAL)2 LESS_THAN (com.facebook.presto.common.function.OperatorType.LESS_THAN)2 LESS_THAN_OR_EQUAL (com.facebook.presto.common.function.OperatorType.LESS_THAN_OR_EQUAL)2 NOT_EQUAL (com.facebook.presto.common.function.OperatorType.NOT_EQUAL)2 BOOLEAN (com.facebook.presto.common.type.BooleanType.BOOLEAN)2 LogicalRowExpressions (com.facebook.presto.expressions.LogicalRowExpressions)2 FALSE_CONSTANT (com.facebook.presto.expressions.LogicalRowExpressions.FALSE_CONSTANT)2 TRUE_CONSTANT (com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT)2 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)2 CallExpression (com.facebook.presto.spi.relation.CallExpression)2 Utils (com.facebook.presto.common.Utils)1 Block (com.facebook.presto.common.block.Block)1 BETWEEN (com.facebook.presto.common.function.OperatorType.BETWEEN)1 IS_DISTINCT_FROM (com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM)1 DiscreteValues (com.facebook.presto.common.predicate.DiscreteValues)1 Domain (com.facebook.presto.common.predicate.Domain)1 Marker (com.facebook.presto.common.predicate.Marker)1