imbajin commented on code in PR #3068: URL: https://github.com/apache/hugegraph/pull/3068#discussion_r3474027215
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/HugeConnectiveLabelStepStrategy.java: ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hugegraph.traversal.optimize; + +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Set; + +import org.apache.tinkerpop.gremlin.process.traversal.Step; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent; +import org.apache.tinkerpop.gremlin.process.traversal.step.filter.AndStep; +import org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper; +import org.apache.tinkerpop.gremlin.structure.T; + +public final class HugeConnectiveLabelStepStrategy + extends AbstractTraversalStrategy<TraversalStrategy.OptimizationStrategy> + implements TraversalStrategy.OptimizationStrategy { + + private static final long serialVersionUID = 2532355470697047377L; + + private static final HugeConnectiveLabelStepStrategy INSTANCE = + new HugeConnectiveLabelStepStrategy(); + + private HugeConnectiveLabelStepStrategy() { + // pass + } + + @Override + public void apply(Traversal.Admin<?, ?> traversal) { + Set<Traversal.Admin<?, ?>> visited = + Collections.newSetFromMap(new IdentityHashMap<>()); + markConnectiveLabelSteps(traversal, visited); + } + + @Override + public Set<Class<? extends TraversalStrategy.OptimizationStrategy>> applyPost() { + return Collections.singleton(InlineFilterStrategy.class); + } + + public static HugeConnectiveLabelStepStrategy instance() { + return INSTANCE; + } + + private static void markConnectiveLabelSteps(Traversal.Admin<?, ?> traversal, + Set<Traversal.Admin<?, ?>> visited) { + if (!visited.add(traversal)) { + return; + } + + for (AndStep<?> step : TraversalHelper.getStepsOfClass(AndStep.class, + traversal)) { + if (!(step.getPreviousStep() instanceof HasStep)) { + continue; + } + for (Traversal.Admin<?, ?> child : step.getLocalChildren()) { + markLabelOnlyTraversal(child); + } + } + + for (Step<?, ?> step : traversal.getSteps()) { + if (!(step instanceof TraversalParent)) { + continue; + } + TraversalParent parent = (TraversalParent) step; + for (Traversal.Admin<?, ?> child : parent.getLocalChildren()) { + markConnectiveLabelSteps(child, visited); + } + for (Traversal.Admin<?, ?> child : parent.getGlobalChildren()) { + markConnectiveLabelSteps(child, visited); + } + } + } + + private static void markLabelOnlyTraversal(Traversal.Admin<?, ?> traversal) { + for (Step<?, ?> step : traversal.getSteps()) { + if (!(step instanceof HasStep)) { + return; + } + HasStep<?> hasStep = (HasStep<?>) step; + if (!hasOnlyLabelContainers(hasStep)) { + return; + } + } + + for (Step<?, ?> step : traversal.getSteps()) { + TraversalUtil.markConnectiveLabelStep(step); + } + } + + private static boolean hasOnlyLabelContainers(HasStep<?> step) { + if (step.getHasContainers().isEmpty()) { + return false; + } + for (HasContainer has : step.getHasContainers()) { Review Comment: ⚠️ Related edge case to guard while fixing the marker logic: this check only verifies that each container key is `T.label`, but it does not constrain the label predicate itself. That means label predicates such as `hasLabel(P.neq("el2"))`, `hasLabel(P.without("el2"))`, or other broad/negative forms can also be marked as connective label steps and then passed to `TraversalUtil.extractLabelHasContainers()`. That is a wider class than the concrete positive label filters covered by the new regression test (`hasLabel("el2")`). Please consider only marking/extracting bounded positive label predicates, for example label equality or a finite positive label set, and leave negative/unsupported label predicates local. A small regression around `and(__.hasLabel(P.neq("el2")))` or `P.without(...)` would make this boundary explicit. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
