Search in sources :

Example 1 with SemiJoinNode

use of io.prestosql.sql.planner.plan.SemiJoinNode in project hetu-core by openlookeng.

the class SqlStageExecution method traverseNodesForDynamicFiltering.

private void traverseNodesForDynamicFiltering(List<PlanNode> nodes) {
    for (PlanNode node : nodes) {
        if (node instanceof JoinNode) {
            JoinNode joinNode = (JoinNode) node;
            dynamicFilterService.registerTasks(joinNode, allTasks, getScheduledNodes(), stateMachine);
        } else if (node instanceof SemiJoinNode) {
            SemiJoinNode semiJoinNode = (SemiJoinNode) node;
            dynamicFilterService.registerTasks(semiJoinNode, allTasks, getScheduledNodes(), stateMachine);
        }
        traverseNodesForDynamicFiltering(node.getSources());
    }
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode)

Example 2 with SemiJoinNode

use of io.prestosql.sql.planner.plan.SemiJoinNode in project hetu-core by openlookeng.

the class TransformUncorrelatedInPredicateSubqueryToSemiJoin method apply.

@Override
public Result apply(ApplyNode applyNode, Captures captures, Context context) {
    if (applyNode.getSubqueryAssignments().size() != 1) {
        return Result.empty();
    }
    Expression expression = castToExpression(getOnlyElement(applyNode.getSubqueryAssignments().getExpressions()));
    if (!(expression instanceof InPredicate)) {
        return Result.empty();
    }
    InPredicate inPredicate = (InPredicate) expression;
    Symbol semiJoinSymbol = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols());
    SemiJoinNode replacement = new SemiJoinNode(context.getIdAllocator().getNextId(), applyNode.getInput(), applyNode.getSubquery(), SymbolUtils.from(inPredicate.getValue()), SymbolUtils.from(inPredicate.getValueList()), semiJoinSymbol, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    return Result.ofPlanNode(replacement);
}
Also used : OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) InPredicate(io.prestosql.sql.tree.InPredicate) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode)

Example 3 with SemiJoinNode

use of io.prestosql.sql.planner.plan.SemiJoinNode in project hetu-core by openlookeng.

the class DynamicFilterService method registerTasksHelper.

private void registerTasksHelper(PlanNode node, Symbol buildSymbol, Map<String, Symbol> dynamicFiltersMap, Set<TaskId> taskIds, Set<InternalNode> workers, StageStateMachine stateMachine) {
    final StateStore stateStore = stateStoreProvider.getStateStore();
    String queryId = stateMachine.getSession().getQueryId().toString();
    for (Map.Entry<String, Symbol> entry : dynamicFiltersMap.entrySet()) {
        Symbol buildSymbolToCheck = buildSymbol != null ? buildSymbol : node.getOutputSymbols().contains(entry.getValue()) ? entry.getValue() : null;
        if (buildSymbolToCheck != null && entry.getValue().getName().equals(buildSymbol.getName())) {
            String filterId = entry.getKey();
            stateStore.createStateCollection(createKey(DynamicFilterUtils.TASKSPREFIX, filterId, queryId), SET);
            stateStore.createStateCollection(createKey(DynamicFilterUtils.PARTIALPREFIX, filterId, queryId), SET);
            dynamicFilters.putIfAbsent(queryId, new ConcurrentHashMap<>());
            Map<String, DynamicFilterRegistryInfo> filters = dynamicFilters.get(queryId);
            if (node instanceof JoinNode) {
                filters.put(filterId, extractDynamicFilterRegistryInfo((JoinNode) node, stateMachine.getSession(), filterId));
            } else if (node instanceof SemiJoinNode) {
                filters.put(filterId, extractDynamicFilterRegistryInfo((SemiJoinNode) node, stateMachine.getSession()));
            }
            dynamicFiltersToTask.putIfAbsent(filterId + "-" + queryId, new CopyOnWriteArraySet<>());
            CopyOnWriteArraySet<TaskId> taskSet = dynamicFiltersToTask.get(filterId + "-" + queryId);
            taskSet.addAll(taskIds);
            log.debug("registerTasks source " + filterId + " filters:" + filters + ", workers: " + workers.stream().map(x -> x.getNodeIdentifier()).collect(Collectors.joining(",")) + ", taskIds: " + taskIds.stream().map(TaskId::toString).collect(Collectors.joining(",")));
        }
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DynamicFilter(io.prestosql.spi.dynamicfilter.DynamicFilter) Inject(com.google.inject.Inject) StateStore(io.prestosql.spi.statestore.StateStore) DynamicFilterUtils.findFilterNodeInStage(io.prestosql.utils.DynamicFilterUtils.findFilterNodeInStage) CallExpression(io.prestosql.spi.relation.CallExpression) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FilterNode(io.prestosql.spi.plan.FilterNode) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) SystemSessionProperties.getDynamicFilteringDataType(io.prestosql.SystemSessionProperties.getDynamicFilteringDataType) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DynamicFilters(io.prestosql.sql.DynamicFilters) PlanNode(io.prestosql.spi.plan.PlanNode) StateSet(io.prestosql.spi.statestore.StateSet) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Threads.threadsNamed(io.airlift.concurrent.Threads.threadsNamed) Collectors(java.util.stream.Collectors) DynamicFilterUtils.getDynamicFilterDataType(io.prestosql.utils.DynamicFilterUtils.getDynamicFilterDataType) Executors(java.util.concurrent.Executors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Type(io.prestosql.spi.dynamicfilter.DynamicFilter.Type) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) GENERIC_INTERNAL_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) Optional(java.util.Optional) LOCAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.LOCAL) TaskId(io.prestosql.execution.TaskId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(io.airlift.log.Logger) HASHSET(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.HASHSET) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BloomFilter(io.prestosql.spi.util.BloomFilter) DataType(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) BloomFilterDynamicFilter(io.prestosql.spi.dynamicfilter.BloomFilterDynamicFilter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) QueryId(io.prestosql.spi.QueryId) JoinNode(io.prestosql.spi.plan.JoinNode) Symbol(io.prestosql.spi.plan.Symbol) StageStateMachine(io.prestosql.execution.StageStateMachine) StateCollection(io.prestosql.spi.statestore.StateCollection) InternalNode(io.prestosql.metadata.InternalNode) IOException(java.io.IOException) BLOOM_FILTER(io.prestosql.spi.dynamicfilter.DynamicFilter.DataType.BLOOM_FILTER) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) TimeUnit(java.util.concurrent.TimeUnit) GLOBAL(io.prestosql.spi.dynamicfilter.DynamicFilter.Type.GLOBAL) DynamicFilterUtils(io.prestosql.utils.DynamicFilterUtils) DynamicFilterUtils.createKey(io.prestosql.utils.DynamicFilterUtils.createKey) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) StateStoreProvider(io.prestosql.statestore.StateStoreProvider) RowExpression(io.prestosql.spi.relation.RowExpression) MAP(io.prestosql.spi.statestore.StateCollection.Type.MAP) DynamicFilterFactory(io.prestosql.spi.dynamicfilter.DynamicFilterFactory) Collections(java.util.Collections) SET(io.prestosql.spi.statestore.StateCollection.Type.SET) TaskId(io.prestosql.execution.TaskId) Symbol(io.prestosql.spi.plan.Symbol) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) StateStore(io.prestosql.spi.statestore.StateStore) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StateMap(io.prestosql.spi.statestore.StateMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with SemiJoinNode

use of io.prestosql.sql.planner.plan.SemiJoinNode in project hetu-core by openlookeng.

the class TransformFilteringSemiJoinToInnerJoin method apply.

@Override
public Result apply(FilterNode filterNode, Captures captures, Context context) {
    SemiJoinNode semiJoin = captures.get(SEMI_JOIN);
    // Do no transform semi-join in context of DELETE
    if (PlanNodeSearcher.searchFrom(semiJoin.getSource(), context.getLookup()).where(node -> node instanceof TableScanNode && ((TableScanNode) node).isForDelete()).matches()) {
        return Result.empty();
    }
    Symbol semiJoinSymbol = semiJoin.getSemiJoinOutput();
    TypeProvider types = context.getSymbolAllocator().getTypes();
    Predicate<RowExpression> isSemiJoinSymbol = expression -> expression.equals(toVariableReference(semiJoinSymbol, types));
    LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), new FunctionResolution(metadata.getFunctionAndTypeManager()), metadata.getFunctionAndTypeManager());
    List<RowExpression> conjuncts = LogicalRowExpressions.extractConjuncts(filterNode.getPredicate());
    if (conjuncts.stream().noneMatch(isSemiJoinSymbol)) {
        return Result.empty();
    }
    RowExpression filteredPredicate = LogicalRowExpressions.and(conjuncts.stream().filter(expression -> !expression.equals(toVariableReference(semiJoinSymbol, types))).collect(toImmutableList()));
    RowExpression simplifiedPredicate = inlineVariables(variable -> {
        if (variable.equals(toVariableReference(semiJoinSymbol, types))) {
            return TRUE_CONSTANT;
        }
        return variable;
    }, filteredPredicate);
    Optional<RowExpression> joinFilter = simplifiedPredicate.equals(TRUE_CONSTANT) ? Optional.empty() : Optional.of(simplifiedPredicate);
    PlanNode filteringSourceDistinct = new AggregationNode(context.getIdAllocator().getNextId(), semiJoin.getFilteringSource(), ImmutableMap.of(), singleGroupingSet(ImmutableList.of(semiJoin.getFilteringSourceJoinSymbol())), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
    JoinNode innerJoin = new JoinNode(semiJoin.getId(), INNER, semiJoin.getSource(), filteringSourceDistinct, ImmutableList.of(new EquiJoinClause(semiJoin.getSourceJoinSymbol(), semiJoin.getFilteringSourceJoinSymbol())), semiJoin.getSource().getOutputSymbols(), joinFilter.isPresent() ? Optional.of(joinFilter.get()) : Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), // TODO: dynamic filter from SemiJoinNode
    ImmutableMap.of());
    ProjectNode project = new ProjectNode(context.getIdAllocator().getNextId(), innerJoin, Assignments.builder().putAll(AssignmentUtils.identityAsSymbolReferences(innerJoin.getOutputSymbols())).put(semiJoinSymbol, TRUE_CONSTANT).build());
    return Result.ofPlanNode(project);
}
Also used : TRUE_CONSTANT(io.prestosql.expressions.LogicalRowExpressions.TRUE_CONSTANT) Patterns.source(io.prestosql.sql.planner.plan.Patterns.source) EquiJoinClause(io.prestosql.spi.plan.JoinNode.EquiJoinClause) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) TypeProvider(io.prestosql.sql.planner.TypeProvider) Pattern(io.prestosql.matching.Pattern) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) AggregationNode(io.prestosql.spi.plan.AggregationNode) SystemSessionProperties.isRewriteFilteringSemiJoinToInnerJoin(io.prestosql.SystemSessionProperties.isRewriteFilteringSemiJoinToInnerJoin) Capture.newCapture(io.prestosql.matching.Capture.newCapture) ImmutableList(com.google.common.collect.ImmutableList) FilterNode(io.prestosql.spi.plan.FilterNode) SINGLE(io.prestosql.spi.plan.AggregationNode.Step.SINGLE) RowExpressionVariableInliner.inlineVariables(io.prestosql.sql.planner.RowExpressionVariableInliner.inlineVariables) Session(io.prestosql.Session) JoinNode(io.prestosql.spi.plan.JoinNode) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) Symbol(io.prestosql.spi.plan.Symbol) AssignmentUtils(io.prestosql.sql.planner.plan.AssignmentUtils) RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) ImmutableMap(com.google.common.collect.ImmutableMap) Assignments(io.prestosql.spi.plan.Assignments) Rule(io.prestosql.sql.planner.iterative.Rule) Predicate(java.util.function.Predicate) Patterns.filter(io.prestosql.sql.planner.plan.Patterns.filter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) Patterns.semiJoin(io.prestosql.sql.planner.plan.Patterns.semiJoin) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) VariableReferenceSymbolConverter.toVariableReference(io.prestosql.sql.planner.VariableReferenceSymbolConverter.toVariableReference) Metadata(io.prestosql.metadata.Metadata) Captures(io.prestosql.matching.Captures) List(java.util.List) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) PlanNodeSearcher(io.prestosql.sql.planner.optimizations.PlanNodeSearcher) Capture(io.prestosql.matching.Capture) RowExpression(io.prestosql.spi.relation.RowExpression) INNER(io.prestosql.spi.plan.JoinNode.Type.INNER) Optional(java.util.Optional) RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) Symbol(io.prestosql.spi.plan.Symbol) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) EquiJoinClause(io.prestosql.spi.plan.JoinNode.EquiJoinClause) LogicalRowExpressions(io.prestosql.expressions.LogicalRowExpressions) TypeProvider(io.prestosql.sql.planner.TypeProvider) RowExpression(io.prestosql.spi.relation.RowExpression) AggregationNode(io.prestosql.spi.plan.AggregationNode) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) PlanNode(io.prestosql.spi.plan.PlanNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 5 with SemiJoinNode

use of io.prestosql.sql.planner.plan.SemiJoinNode in project hetu-core by openlookeng.

the class SemiJoinMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    SemiJoinNode semiJoinNode = (SemiJoinNode) node;
    if (!(symbolAliases.get(sourceSymbolAlias).equals(toSymbolReference(semiJoinNode.getSourceJoinSymbol())) && symbolAliases.get(filteringSymbolAlias).equals(toSymbolReference(semiJoinNode.getFilteringSourceJoinSymbol())))) {
        return NO_MATCH;
    }
    if (distributionType.isPresent() && !distributionType.equals(semiJoinNode.getDistributionType())) {
        return NO_MATCH;
    }
    if (hasDynamicFilter.isPresent()) {
        if (hasDynamicFilter.get()) {
            if (!semiJoinNode.getDynamicFilterId().isPresent()) {
                return NO_MATCH;
            }
            String dynamicFilterId = semiJoinNode.getDynamicFilterId().get();
            List<DynamicFilters.Descriptor> matchingDescriptors = searchFrom(semiJoinNode.getSource()).where(FilterNode.class::isInstance).findAll().stream().flatMap(filterNode -> extractExpressions(filterNode).stream()).flatMap(expression -> extractDynamicFilters(expression).getDynamicConjuncts().stream()).filter(descriptor -> descriptor.getId().equals(dynamicFilterId)).collect(toImmutableList());
            boolean sourceSymbolsMatch = matchingDescriptors.stream().map(descriptor -> new Symbol(((VariableReferenceExpression) descriptor.getInput()).getName())).allMatch(sourceSymbol -> symbolAliases.get(sourceSymbolAlias).equals(toSymbolReference(sourceSymbol)));
            if (!matchingDescriptors.isEmpty() && sourceSymbolsMatch) {
                return match(outputAlias, toSymbolReference(semiJoinNode.getSemiJoinOutput()));
            }
            return NO_MATCH;
        }
        if (semiJoinNode.getDynamicFilterId().isPresent()) {
            return NO_MATCH;
        }
    }
    return match(outputAlias, toSymbolReference(semiJoinNode.getSemiJoinOutput()));
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) StatsProvider(io.prestosql.cost.StatsProvider) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DynamicFilters.extractDynamicFilters(io.prestosql.sql.DynamicFilters.extractDynamicFilters) MatchResult.match(io.prestosql.sql.planner.assertions.MatchResult.match) DynamicFilters(io.prestosql.sql.DynamicFilters) PlanNode(io.prestosql.spi.plan.PlanNode) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Metadata(io.prestosql.metadata.Metadata) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanNodeSearcher.searchFrom(io.prestosql.sql.planner.optimizations.PlanNodeSearcher.searchFrom) ExpressionExtractor.extractExpressions(io.prestosql.sql.planner.ExpressionExtractor.extractExpressions) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) FilterNode(io.prestosql.spi.plan.FilterNode) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) Optional(java.util.Optional) NO_MATCH(io.prestosql.sql.planner.assertions.MatchResult.NO_MATCH) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) SemiJoinNode(io.prestosql.sql.planner.plan.SemiJoinNode)

Aggregations

SemiJoinNode (io.prestosql.sql.planner.plan.SemiJoinNode)9 PlanNode (io.prestosql.spi.plan.PlanNode)7 Symbol (io.prestosql.spi.plan.Symbol)5 Session (io.prestosql.Session)4 FilterNode (io.prestosql.spi.plan.FilterNode)4 JoinNode (io.prestosql.spi.plan.JoinNode)4 RowExpression (io.prestosql.spi.relation.RowExpression)4 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Metadata (io.prestosql.metadata.Metadata)3 ProjectNode (io.prestosql.spi.plan.ProjectNode)3 CallExpression (io.prestosql.spi.relation.CallExpression)3 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)3 List (java.util.List)3 Optional (java.util.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 LogicalRowExpressions (io.prestosql.expressions.LogicalRowExpressions)2 Pattern (io.prestosql.matching.Pattern)2 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)2 TypeProvider (io.prestosql.sql.planner.TypeProvider)2