Search in sources :

Example 11 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class SimpleFilterProjectSemiJoinStatsRule method extractSemiJoinOutputFilter.

private Optional<SemiJoinOutputFilter> extractSemiJoinOutputFilter(RowExpression predicate, RowExpression input) {
    checkState(!isExpression(predicate));
    List<RowExpression> conjuncts = LogicalRowExpressions.extractConjuncts(predicate);
    List<RowExpression> semiJoinOutputReferences = conjuncts.stream().filter(conjunct -> isSemiJoinOutputReference(conjunct, input)).collect(toImmutableList());
    if (semiJoinOutputReferences.size() != 1) {
        return Optional.empty();
    }
    RowExpression semiJoinOutputReference = Iterables.getOnlyElement(semiJoinOutputReferences);
    RowExpression remainingPredicate = logicalRowExpressions.combineConjuncts(conjuncts.stream().filter(conjunct -> conjunct != semiJoinOutputReference).collect(toImmutableList()));
    boolean negated = isNotFunction(semiJoinOutputReference);
    return Optional.of(new SemiJoinOutputFilter(negated, remainingPredicate));
}
Also used : Iterables(com.google.common.collect.Iterables) Lookup(io.prestosql.sql.planner.iterative.Lookup) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) TypeProvider(io.prestosql.sql.planner.TypeProvider) Pattern(io.prestosql.matching.Pattern) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) CallExpression(io.prestosql.spi.relation.CallExpression) FilterNode(io.prestosql.spi.plan.FilterNode) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) ProjectNodeUtils.isIdentity(io.prestosql.sql.relational.ProjectNodeUtils.isIdentity) Symbol(io.prestosql.spi.plan.Symbol) SymbolUtils(io.prestosql.sql.planner.SymbolUtils) NotExpression(io.prestosql.sql.tree.NotExpression) UNKNOWN_FILTER_COEFFICIENT(io.prestosql.cost.FilterStatsCalculator.UNKNOWN_FILTER_COEFFICIENT) RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) SemiJoinStatsCalculator.computeSemiJoin(io.prestosql.cost.SemiJoinStatsCalculator.computeSemiJoin) SemiJoinStatsCalculator.computeAntiJoin(io.prestosql.cost.SemiJoinStatsCalculator.computeAntiJoin) ExpressionUtils.combineConjuncts(io.prestosql.sql.ExpressionUtils.combineConjuncts) Patterns.filter(io.prestosql.sql.planner.plan.Patterns.filter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PlanNode(io.prestosql.spi.plan.PlanNode) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) SymbolReference(io.prestosql.sql.tree.SymbolReference) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) Expression(io.prestosql.sql.tree.Expression) ExpressionUtils.extractConjuncts(io.prestosql.sql.ExpressionUtils.extractConjuncts) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression)

Example 12 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class TableDeleteOperator method getOutput.

@Override
public Page getOutput() {
    if (state == State.FINISHED || (sourceLayout.isPresent() && state != State.FINISHING)) {
        return null;
    }
    state = State.FINISHED;
    TableHandle tableHandleForDelete = tableHandle;
    if (sourceLayout.isPresent()) {
        // Replace the values from subqueries in filter predicates
        VariablesExtractor.extractUnique(filter.get()).stream().forEach(expr -> symbolExpressionMap.putIfAbsent(new Symbol(expr.getName()), expr));
        RowExpression rowExpression = RowExpressionVariableInliner.inlineVariables(node -> symbolExpressionMap.get(new Symbol(node.getName())), filter.get());
        // Create the tuple domain based on filter and values from source;
        RowExpressionDomainTranslator.ExtractionResult<VariableReferenceExpression> decomposedPredicate = (new RowExpressionDomainTranslator(metadata)).fromPredicate(session.toConnectorSession(), rowExpression, RowExpressionDomainTranslator.BASIC_COLUMN_EXTRACTOR);
        TupleDomain<ColumnHandle> tupleDomain = decomposedPredicate.getTupleDomain().transform(variableName -> assignments.get(new Symbol(variableName.getName())));
        Constraint constraint = new Constraint(tupleDomain);
        // Apply the constraint on the table handle
        Optional<TableHandle> tableHandleDelete = metadata.applyDelete(session, this.tableHandle, constraint);
        if (tableHandleDelete.isPresent()) {
            tableHandleForDelete = tableHandleDelete.get();
        }
    }
    OptionalLong rowsDeletedCount = metadata.executeDelete(session, tableHandleForDelete);
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(1, TYPES);
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    page.declarePosition();
    if (rowsDeletedCount.isPresent()) {
        BIGINT.writeLong(rowsBuilder, rowsDeletedCount.getAsLong());
    } else {
        rowsBuilder.appendNull();
    }
    return page.build();
}
Also used : ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Constraint(io.prestosql.spi.connector.Constraint) Symbol(io.prestosql.spi.plan.Symbol) RowExpressionDomainTranslator(io.prestosql.sql.relational.RowExpressionDomainTranslator) RowExpression(io.prestosql.spi.relation.RowExpression) PageBuilder(io.prestosql.spi.PageBuilder) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) OptionalLong(java.util.OptionalLong) TableHandle(io.prestosql.spi.metadata.TableHandle) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 13 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class TestScanFilterAndProjectOperator method testRecordCursorYield.

@Test
public void testRecordCursorYield() {
    // create a generic long function that yields for projection on every row
    // verify we will yield #row times totally
    // create a table with 15 rows
    int length = 15;
    Page input = SequencePageBuilder.createSequencePage(ImmutableList.of(BIGINT), length, 0);
    DriverContext driverContext = newDriverContext();
    // set up generic long function with a callback to force yield
    Metadata localMetadata = functionAssertions.getMetadata();
    localMetadata.getFunctionAndTypeManager().registerBuiltInFunctions(ImmutableList.of(new GenericLongFunction("record_cursor", value -> {
        driverContext.getYieldSignal().forceYieldForTesting();
        return value;
    })));
    ExpressionCompiler compiler = new ExpressionCompiler(localMetadata, new PageFunctionCompiler(localMetadata, 0));
    List<RowExpression> projections = ImmutableList.of(call(QualifiedObjectName.valueOfDefaultFunction("generic_long_record_cursor").toString(), new BuiltInFunctionHandle(internalScalarFunction(QualifiedObjectName.valueOfDefaultFunction("generic_long_record_cursor"), BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()))), BIGINT, field(0, BIGINT)));
    Supplier<CursorProcessor> cursorProcessor = compiler.compileCursorProcessor(Optional.empty(), projections, "key");
    Supplier<PageProcessor> pageProcessor = compiler.compilePageProcessor(Optional.empty(), projections);
    ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory factory = new ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory(0, new PlanNodeId("test"), new PlanNodeId("0"), (session, split, table, columns, dynamicFilter) -> new RecordPageSource(new PageRecordSet(ImmutableList.of(BIGINT), input)), cursorProcessor, pageProcessor, TEST_TABLE_HANDLE, ImmutableList.of(), null, ImmutableList.of(BIGINT), new DataSize(0, BYTE), 0, ReuseExchangeOperator.STRATEGY.REUSE_STRATEGY_DEFAULT, new UUID(0, 0), false, Optional.empty(), 0, 0);
    SourceOperator operator = factory.createOperator(driverContext);
    operator.addSplit(new Split(new CatalogName("test"), TestingSplit.createLocalSplit(), Lifespan.taskWide()));
    operator.noMoreSplits();
    // start driver; get null value due to yield for the first 15 times
    for (int i = 0; i < length; i++) {
        driverContext.getYieldSignal().setWithDelay(SECONDS.toNanos(1000), driverContext.getYieldExecutor());
        assertNull(operator.getOutput());
        driverContext.getYieldSignal().reset();
    }
    // the 16th yield is not going to prevent the operator from producing a page
    driverContext.getYieldSignal().setWithDelay(SECONDS.toNanos(1000), driverContext.getYieldExecutor());
    Page output = operator.getOutput();
    driverContext.getYieldSignal().reset();
    assertNotNull(output);
    assertEquals(toValues(BIGINT, output.getBlock(0)), toValues(BIGINT, input.getBlock(0)));
}
Also used : PageFunctionCompiler(io.prestosql.sql.gen.PageFunctionCompiler) CursorProcessor(io.prestosql.operator.project.CursorProcessor) Metadata(io.prestosql.metadata.Metadata) Page(io.prestosql.spi.Page) PageRecordSet(io.prestosql.operator.index.PageRecordSet) RecordPageSource(io.prestosql.spi.connector.RecordPageSource) PlanNodeId(io.prestosql.spi.plan.PlanNodeId) PageProcessor(io.prestosql.operator.project.PageProcessor) DataSize(io.airlift.units.DataSize) UUID(java.util.UUID) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) ExpressionCompiler(io.prestosql.sql.gen.ExpressionCompiler) CatalogName(io.prestosql.spi.connector.CatalogName) Split(io.prestosql.metadata.Split) TestingSplit(io.prestosql.testing.TestingSplit) Test(org.testng.annotations.Test)

Example 14 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class SqlQueryExecution method findMappingFromPlan.

private void findMappingFromPlan(Map<String, Set<String>> mapping, PlanNode sourceNode) {
    if (sourceNode != null && sourceNode instanceof ProjectNode) {
        ProjectNode projectNode = (ProjectNode) sourceNode;
        Map<Symbol, RowExpression> assignments = projectNode.getAssignments().getMap();
        for (Symbol symbol : assignments.keySet()) {
            if (mapping.containsKey(symbol.getName())) {
                Set<String> sets = mapping.get(symbol.getName());
                RowExpression expression = assignments.get(symbol);
                if (expression instanceof VariableReferenceExpression) {
                    sets.add(((VariableReferenceExpression) expression).getName());
                } else {
                    sets.add(expression.toString());
                }
            } else {
                for (Map.Entry<String, Set<String>> entry : mapping.entrySet()) {
                    if (entry.getValue().contains(symbol.getName())) {
                        RowExpression expression = assignments.get(symbol);
                        if (expression instanceof VariableReferenceExpression) {
                            entry.getValue().add(((VariableReferenceExpression) expression).getName());
                        } else {
                            entry.getValue().add(expression.toString());
                        }
                    }
                }
            }
        }
    }
    for (PlanNode planNode : sourceNode.getSources()) {
        findMappingFromPlan(mapping, planNode);
    }
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) PlanNode(io.prestosql.spi.plan.PlanNode) Symbol(io.prestosql.spi.plan.Symbol) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) StateMap(io.prestosql.spi.statestore.StateMap) HashMap(java.util.HashMap)

Example 15 with RowExpression

use of io.prestosql.spi.relation.RowExpression in project hetu-core by openlookeng.

the class TestDynamicFilters method testExtractStaticFilters.

@Test
public void testExtractStaticFilters() {
    LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), new FunctionResolution(metadata.getFunctionAndTypeManager()), metadata.getFunctionAndTypeManager());
    ConstantExpression staticExpression = new ConstantExpression(utf8Slice("age"), VarcharType.VARCHAR);
    VariableReferenceExpression variableExpression = new VariableReferenceExpression("col", VarcharType.VARCHAR);
    FunctionHandle functionHandle = metadata.getFunctionAndTypeManager().lookupFunction("rank", ImmutableList.of());
    RowExpression dynamicFilterExpression = call(DynamicFilters.Function.NAME, functionHandle, staticExpression.getType(), asList(staticExpression, variableExpression), Optional.empty());
    RowExpression combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression, dynamicFilterExpression);
    Optional<RowExpression> extractedExpression = DynamicFilters.extractStaticFilters(Optional.of(combinedExpression), metadata);
    assertEquals(extractedExpression.get(), staticExpression);
    RowExpression combinedStaticExpression = logicalRowExpressions.combineConjuncts(staticExpression, variableExpression);
    combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression, variableExpression, dynamicFilterExpression);
    extractedExpression = DynamicFilters.extractStaticFilters(Optional.of(combinedExpression), metadata);
    assertEquals(extractedExpression.get(), combinedStaticExpression);
}
Also used : RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Aggregations

RowExpression (io.prestosql.spi.relation.RowExpression)185 ArrayList (java.util.ArrayList)66 Symbol (io.prestosql.spi.plan.Symbol)62 CallExpression (io.prestosql.spi.relation.CallExpression)56 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)52 ImmutableList (com.google.common.collect.ImmutableList)45 Test (org.testng.annotations.Test)42 Type (io.prestosql.spi.type.Type)41 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)39 List (java.util.List)39 Map (java.util.Map)39 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)31 Optional (java.util.Optional)30 Expression (io.prestosql.sql.tree.Expression)29 Metadata (io.prestosql.metadata.Metadata)28 PlanNode (io.prestosql.spi.plan.PlanNode)27 ImmutableMap (com.google.common.collect.ImmutableMap)26 SpecialForm (io.prestosql.spi.relation.SpecialForm)25 FunctionHandle (io.prestosql.spi.function.FunctionHandle)24 ProjectNode (io.prestosql.spi.plan.ProjectNode)24