Copilot commented on code in PR #3034:
URL: https://github.com/apache/hugegraph/pull/3034#discussion_r3280662611


##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/TraversalUtil.java:
##########
@@ -166,20 +166,17 @@ public static void trySetGraph(Step<?, ?> step, HugeGraph 
graph) {
 
     public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
                                            Traversal.Admin<?, ?> traversal) {
-        Step<?, ?> step = newStep;
-        do {
-            step = step.getNextStep();
+        Step<?, ?> step = newStep.getNextStep();
+        while (step instanceof HasStep || step instanceof NoOpBarrierStep) {
             if (step instanceof HasStep) {
                 HasContainerHolder holder = (HasContainerHolder) step;
-                for (HasContainer has : holder.getHasContainers()) {
-                    if (!GraphStep.processHasContainerIds(newStep, has)) {
-                        newStep.addHasContainer(has);
-                    }
+                if (extractHasContainers(newStep, holder)) {
+                    TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
+                    traversal.removeStep(step);
                 }
-                TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
-                traversal.removeStep(step);
             }
-        } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
+            step = step.getNextStep();
+        }

Review Comment:
   `step = step.getNextStep()` is executed after `traversal.removeStep(step)`. 
In TinkerPop, removing a step can rewire (and may detach) the removed step’s 
next/prev pointers; calling `getNextStep()` on the removed instance can return 
`EmptyStep` or skip over subsequent `HasStep`s, causing incomplete extraction. 
Capture `nextStep = step.getNextStep()` before removal, then assign `step = 
nextStep` after the optional `removeStep`.



##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/traversal/optimize/TraversalUtilOptimizeTest.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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 org.apache.hugegraph.testutil.Assert;
+import org.apache.tinkerpop.gremlin.process.traversal.P;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer;
+import org.junit.Test;
+
+public class TraversalUtilOptimizeTest {
+
+    @Test
+    public void testCanExtractHasContainerWithoutGraph() {
+        Assert.assertTrue(TraversalUtil.canExtractHasContainer(
+                null, new HasContainer("~label", P.eq("person"))));
+        Assert.assertFalse(TraversalUtil.canExtractHasContainer(
+                null, new HasContainer("name", P.eq("marko"))));
+    }

Review Comment:
   The new logic depends on schema lookup (`graph.propertyKey(...)`) for 
non-system keys. It would be useful to add a test case asserting behavior when 
a non-system key is not present in schema (i.e., ensure 
`canExtractHasContainer()` does not throw and returns a consistent value, 
likely `false` to prevent extraction). This guards the newly introduced 
schema-dependent branch.



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/TraversalUtil.java:
##########
@@ -188,16 +185,79 @@ public static void extractHasContainer(HugeVertexStep<?> 
newStep,
         do {
             if (step instanceof HasStep) {
                 HasContainerHolder holder = (HasContainerHolder) step;
-                for (HasContainer has : holder.getHasContainers()) {
-                    newStep.addHasContainer(has);
+                if (extractHasContainers(newStep, holder)) {
+                    TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
+                    traversal.removeStep(step);
                 }
-                TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
-                traversal.removeStep(step);
             }
             step = step.getNextStep();
         } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
     }
 
+    private static boolean extractHasContainers(HugeGraphStep<?, ?> newStep,
+                                                HasContainerHolder holder) {
+        HugeGraph graph = TraversalUtil.tryGetGraph(newStep);
+        List<HasContainer> hasContainers = holder.getHasContainers();
+        if (!canExtractHasContainers(graph, hasContainers)) {
+            return false;
+        }
+        for (HasContainer has : hasContainers) {
+            if (!GraphStep.processHasContainerIds(newStep, has)) {
+                newStep.addHasContainer(has);
+            }
+        }
+        return true;
+    }
+
+    private static boolean extractHasContainers(HugeVertexStep<?> newStep,
+                                                HasContainerHolder holder) {
+        HugeGraph graph = TraversalUtil.tryGetGraph(newStep);
+        List<HasContainer> hasContainers = holder.getHasContainers();
+        if (!canExtractHasContainers(graph, hasContainers)) {
+            return false;
+        }
+        for (HasContainer has : hasContainers) {
+            newStep.addHasContainer(has);
+        }
+        return true;
+    }
+
+    private static boolean canExtractHasContainers(HugeGraph graph,
+                                                   List<HasContainer> 
hasContainers) {
+        for (HasContainer has : hasContainers) {
+            if (!canExtractHasContainer(graph, has)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    static boolean canExtractHasContainer(HugeGraph graph,
+                                          HasContainer has) {
+        if (isSysProp(has.getKey())) {
+            return true;
+        }
+        if (graph == null) {
+            return false;
+        }
+
+        PropertyKey pkey = graph.propertyKey(has.getKey());

Review Comment:
   `graph.propertyKey(has.getKey())` may throw if the property key doesn’t 
exist in schema (e.g., user query references an unknown key). That would make 
optimization throw earlier (and potentially with a different exception) instead 
of letting the normal traversal execution handle it. Consider handling missing 
keys by catching the schema lookup exception and returning `false` (avoid 
extraction when schema is unknown) or otherwise aligning with existing query 
validation behavior.
   



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/TraversalUtil.java:
##########
@@ -188,16 +185,79 @@ public static void extractHasContainer(HugeVertexStep<?> 
newStep,
         do {
             if (step instanceof HasStep) {
                 HasContainerHolder holder = (HasContainerHolder) step;
-                for (HasContainer has : holder.getHasContainers()) {
-                    newStep.addHasContainer(has);
+                if (extractHasContainers(newStep, holder)) {
+                    TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
+                    traversal.removeStep(step);
                 }
-                TraversalHelper.copyLabels(step, step.getPreviousStep(), 
false);
-                traversal.removeStep(step);
             }
             step = step.getNextStep();
         } while (step instanceof HasStep || step instanceof NoOpBarrierStep);

Review Comment:
   Same issue as above: `step.getNextStep()` is invoked after the step may have 
been removed from the traversal. This can prematurely terminate the loop or 
skip steps. Store `nextStep` before the possible removal and advance using that 
stored reference.



-- 
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]

Reply via email to