Search in sources :

Example 76 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression 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 77 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class FunctionAssertions method executeFilterWithAll.

private List<Boolean> executeFilterWithAll(String filter, Session session, boolean executeWithNoInputColumns, ExpressionCompiler compiler) {
    requireNonNull(filter, "filter is null");
    Expression filterExpression = createExpression(session, filter, metadata, SYMBOL_TYPES);
    RowExpression filterRowExpression = toRowExpression(session, filterExpression);
    List<Boolean> results = new ArrayList<>();
    // execute as standalone operator
    OperatorFactory operatorFactory = compileFilterProject(session.getSqlFunctionProperties(), Optional.of(filterRowExpression), constant(true, BOOLEAN), compiler);
    results.add(executeFilter(operatorFactory, session));
    if (executeWithNoInputColumns) {
        // execute as standalone operator
        operatorFactory = compileFilterWithNoInputColumns(session.getSqlFunctionProperties(), filterRowExpression, compiler);
        results.add(executeFilterWithNoInputColumns(operatorFactory, session));
    }
    // interpret
    Boolean interpretedValue = (Boolean) interpret(filterExpression, BOOLEAN, session);
    if (interpretedValue == null) {
        interpretedValue = false;
    }
    results.add(interpretedValue);
    // execute over normal operator
    SourceOperatorFactory scanProjectOperatorFactory = compileScanFilterProject(session.getSqlFunctionProperties(), Optional.of(filterRowExpression), constant(true, BOOLEAN), compiler);
    boolean scanOperatorValue = executeFilter(scanProjectOperatorFactory, createNormalSplit(), session);
    results.add(scanOperatorValue);
    // execute over record set
    boolean recordValue = executeFilter(scanProjectOperatorFactory, createRecordSetSplit(), session);
    results.add(recordValue);
    // If the filter does not need bound values, execute query using full engine
    if (!needsBoundValue(filterExpression)) {
        MaterializedResult result = runner.execute("SELECT TRUE WHERE " + filter);
        assertEquals(result.getTypes().size(), 1);
        Boolean queryResult;
        if (result.getMaterializedRows().isEmpty()) {
            queryResult = false;
        } else {
            assertEquals(result.getMaterializedRows().size(), 1);
            queryResult = (Boolean) Iterables.getOnlyElement(result.getMaterializedRows()).getField(0);
        }
        results.add(queryResult);
    }
    return results;
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) FilterAndProjectOperatorFactory(com.facebook.presto.operator.FilterAndProjectOperator.FilterAndProjectOperatorFactory) SourceOperatorFactory(com.facebook.presto.operator.SourceOperatorFactory) OperatorFactory(com.facebook.presto.operator.OperatorFactory) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.spi.relation.RowExpression) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MaterializedResult(com.facebook.presto.testing.MaterializedResult) SourceOperatorFactory(com.facebook.presto.operator.SourceOperatorFactory)

Example 78 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestConnectorOptimization method testPushFilterToTableScan.

@Test
public void testPushFilterToTableScan() {
    RowExpression expectedPredicate = and(newBigintVariable("a"), newBigintVariable("b"));
    PlanNode plan = output(filter(tableScan("cat1", "a", "b"), expectedPredicate), "a");
    PlanNode actual = optimize(plan, ImmutableMap.of(new ConnectorId("cat1"), ImmutableSet.of(filterPushdown())));
    // assert structure; FilterNode is removed
    assertPlanMatch(actual, PlanMatchPattern.output(SimpleTableScanMatcher.tableScan("cat1", expectedPredicate)));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) ConnectorId(com.facebook.presto.spi.ConnectorId) Test(org.testng.annotations.Test)

Example 79 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestJoinNodeFlattener method testCombinesCriteriaAndFilters.

@Test
public void testCombinesCriteriaAndFilters() {
    PlanBuilder p = planBuilder();
    VariableReferenceExpression a1 = p.variable("A1");
    VariableReferenceExpression b1 = p.variable("B1");
    VariableReferenceExpression b2 = p.variable("B2");
    VariableReferenceExpression c1 = p.variable("C1");
    VariableReferenceExpression c2 = p.variable("C2");
    ValuesNode valuesA = p.values(a1);
    ValuesNode valuesB = p.values(b1, b2);
    ValuesNode valuesC = p.values(c1, c2);
    RowExpression bcFilter = and(call(OperatorType.GREATER_THAN.name(), functionResolution.comparisonFunction(OperatorType.GREATER_THAN, c2.getType(), BIGINT), BOOLEAN, ImmutableList.of(c2, constant(0L, BIGINT))), call(OperatorType.NOT_EQUAL.name(), functionResolution.comparisonFunction(OperatorType.NOT_EQUAL, c2.getType(), BIGINT), BOOLEAN, ImmutableList.of(c2, constant(7L, BIGINT))), call(OperatorType.GREATER_THAN.name(), functionResolution.comparisonFunction(OperatorType.GREATER_THAN, b2.getType(), c2.getType()), BOOLEAN, ImmutableList.of(b2, c2)));
    RowExpression add = call(OperatorType.ADD.name(), functionResolution.arithmeticFunction(OperatorType.ADD, a1.getType(), c1.getType()), a1.getType(), ImmutableList.of(a1, c1));
    RowExpression abcFilter = call(OperatorType.LESS_THAN.name(), functionResolution.comparisonFunction(OperatorType.LESS_THAN, add.getType(), b1.getType()), BOOLEAN, ImmutableList.of(add, b1));
    JoinNode joinNode = p.join(INNER, valuesA, p.join(INNER, valuesB, valuesC, ImmutableList.of(equiJoinClause(b1, c1)), ImmutableList.of(b1, b2, c1, c2), Optional.of(bcFilter)), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1, b2, c1, c2), Optional.of(abcFilter));
    MultiJoinNode expected = new MultiJoinNode(new LinkedHashSet<>(ImmutableList.of(valuesA, valuesB, valuesC)), and(createEqualsExpression(b1, c1), createEqualsExpression(a1, b1), bcFilter, abcFilter), ImmutableList.of(a1, b1, b2, c1, c2));
    assertEquals(toMultiJoinNode(joinNode, noLookup(), DEFAULT_JOIN_LIMIT, functionResolution, determinismEvaluator), expected);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) Test(org.testng.annotations.Test)

Example 80 with RowExpression

use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.

the class TestJdbcComputePushdown method testJdbcComputePushdownBooleanOperations.

@Test
public void testJdbcComputePushdownBooleanOperations() {
    String table = "test_table";
    String schema = "test_schema";
    String expression = "(((c1 + c2) - c2 <> c2) OR c2 = c1) AND c1 <> c2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("c1", BIGINT, "c2", BIGINT));
    RowExpression rowExpression = sqlToRowExpressionTranslator.translateAndOptimize(expression(expression), typeProvider);
    Set<ColumnHandle> columns = Stream.of("c1", "c2").map(TestJdbcComputePushdown::integerJdbcColumnHandle).collect(Collectors.toSet());
    PlanNode original = filter(jdbcTableScan(schema, table, BIGINT, "c1", "c2"), rowExpression);
    JdbcTableHandle jdbcTableHandle = new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName(schema, table), CATALOG_NAME, schema, table);
    ConnectorSession session = new TestingConnectorSession(ImmutableList.of());
    JdbcTableLayoutHandle jdbcTableLayoutHandle = new JdbcTableLayoutHandle(session.getSqlFunctionProperties(), jdbcTableHandle, TupleDomain.none(), Optional.of(new JdbcExpression("((((((('c1' + 'c2') - 'c2') <> 'c2')) OR (('c2' = 'c1')))) AND (('c1' <> 'c2')))")));
    PlanNode actual = this.jdbcComputePushdown.optimize(original, session, null, ID_ALLOCATOR);
    assertPlanMatch(actual, PlanMatchPattern.filter(expression, JdbcTableScanMatcher.jdbcTableScanPattern(jdbcTableLayoutHandle, columns)));
}
Also used : JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) JdbcTableLayoutHandle(com.facebook.presto.plugin.jdbc.JdbcTableLayoutHandle) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) JdbcTableHandle(com.facebook.presto.plugin.jdbc.JdbcTableHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Test(org.testng.annotations.Test)

Aggregations

RowExpression (com.facebook.presto.spi.relation.RowExpression)237 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)97 Test (org.testng.annotations.Test)87 ImmutableList (com.google.common.collect.ImmutableList)58 CallExpression (com.facebook.presto.spi.relation.CallExpression)52 Map (java.util.Map)49 List (java.util.List)42 Type (com.facebook.presto.common.type.Type)41 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)40 ImmutableMap (com.google.common.collect.ImmutableMap)38 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)37 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)35 Optional (java.util.Optional)35 Expression (com.facebook.presto.sql.tree.Expression)31 ColumnHandle (com.facebook.presto.spi.ColumnHandle)27 Objects.requireNonNull (java.util.Objects.requireNonNull)27 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)24 Set (java.util.Set)24 ArrayList (java.util.ArrayList)23