[Bug] clone query affects original cloned query

[X] I am using the latest release (3.7.2)
I believe the clone function code in gremlin-javascript contains a bug:

Notes:
  • I renamed
    process
    to
    gProcess
    as it's a global variable in Nodejs
  • The following code is written in Typescript
import { process as gProcess } from "gremlin";

export async function example(
  q: gProcess.GraphTraversalSource<gProcess.GraphTraversal>
) {
  const first = q.V().hasLabel("Book").range(0, 100);
  const cloned = first.clone().count();

  console.log("first.getByteCode()", first.getBytecode());
  console.log("cloned.getByteCode()", cloned.getBytecode());
}

The example code outputs:
first.getByteCode() Bytecode {
  sourceInstructions: [],
  stepInstructions: [ [ 'V' ], [ 'hasLabel', 'Book' ], [ 'range', 0, 100 ], [ 'count' ] ]
}
cloned.getByteCode() Bytecode {
  sourceInstructions: [],
  stepInstructions: [ [ 'V' ], [ 'hasLabel', 'Book' ], [ 'range', 0, 100 ], [ 'count' ] ]
}

The
count
step affects both
first
and
cloned
.

I may be using
clone
wrong, but the behavior I'm looking for is cloning an existing query.
I have searched the source code and believe the
clone
function is improperly implemented.
Commit it was added:
https://github.com/apache/tinkerpop/commit/e3960486459152c31565642f772a8df0c57ca9f6#diff-38672d19a01405e314993c9e06586f19ef66b0a89f7c9f34d17273848cef3c1dR208

Thank you in advance ๐Ÿ™
Solution
Hi @Jonathan Fridja, thanks for reporting the issue, I agree that this appears to be a bug. Unlike the clone() implementation in the java driver which this was based off of, the js clone only creates a shallow copy which does not clone the underlying bytecode.

For reference this is how clone is implemented in Java: https://github.com/apache/tinkerpop/blob/76190de1086d8be4e207e69d1cc599c9d036a8b5/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversal.java#L267-L290.

Would you be willing to submit a PR to the 3.6-dev with a fix to fix to create a deep copy of the bytecode? If you could, that would be great, otherwise I can open up a JIRA to track this bug.
GitHub
Apache TinkerPop - a graph computing framework. Contribute to apache/tinkerpop development by creating an account on GitHub.
Was this page helpful?