[ https://issues.apache.org/jira/browse/TINKERPOP-2710?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17497006#comment-17497006 ]
Divij Vaidya edited comment on TINKERPOP-2710 at 2/23/22, 7:10 PM: ------------------------------------------------------------------- > We've taken the posture lately that we want to generate less exceptions in > Gremlin The root cause of this error is absence of strongly defined type semantics in Gremlin language. Any step can be placed after any other step in a traversal. At runtime, the code does the validation and if step is used incorrectly we throw an error, e.g. {noformat} gremlin> g.V().union(outE(),out()).in() org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex Type ':help' or ':h' for help. Display stack trace? [yN]{noformat} Is there a difference between the syntax error for hasKey() posted in the Jira and the query above? I would categorize them into the same bucket where our parser cannot detect incorrect queries since the incoming traversers to the steps are not strongly typed. In such cases, we should always throw an error (since the step usage is incorrect) since it is a wrong query. Can you please provide me with examples where we are not throwing errors for cases when the incoming traverser is not compatible with the step? was (Author: divijvaidya): > We've taken the posture lately that we want to generate less exceptions in > Gremlin The root cause of this error is absence of strongly defined type semantics in Gremlin language. Any step can be placed after any other step in a traversal. At runtime, the code does the validation and if step is used incorrectly we throw an error, e.g. {noformat} gremlin> g.V().union(outE(),out()).in() org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex Type ':help' or ':h' for help. Display stack trace? [yN]{noformat} Is there a difference between the syntax error for hasKey() posted in the Jira and the query above? > hasKey() should throw an error when invoked on Elements > ------------------------------------------------------- > > Key: TINKERPOP-2710 > URL: https://issues.apache.org/jira/browse/TINKERPOP-2710 > Project: TinkerPop > Issue Type: Bug > Components: language, process > Affects Versions: 3.6.0 > Reporter: Divij Vaidya > Priority: Major > > *Context* > hasKey() step can be added to the traversal when the traverser is of type > Element or when it is of type Property. > When step is invoked for traverser of type property, the behaviour is well > defined as, "Remove the {{Property}} traverser if it does not match one of > the provided keys." Example query: > {noformat} > gremlin> g.V().properties().hasKey('age').value() > ==>29 > ==>27 > ==>32 > ==>35{noformat} > When the step is invoked for traverser of type Element, the behaviour of the > step is not documented. [Looking at the > code|https://github.com/apache/tinkerpop/blob/master/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java#L59] > implementation, it "removes the element traverser if element has a property > with key as '~key' and the value of that property matches 'age'". > But [this condition is > unsatisfiable|https://github.com/apache/tinkerpop/blob/6a0b71b2af79b1b45f1e2db41946fe85529ed32e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/ElementHelper.java#L84] > because Element can never have a property with key "~key". It would always > fail at insertion i.e. the output of hasKey() on Element traverser will > always be empty. > *Change proposed* > Hence, I propose the following change: > If hasKey() is applied to a traverser of Element type, then it should throw > an error. > The code change will be made > [here|https://github.com/apache/tinkerpop/blob/master/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/util/HasContainer.java#L59] > . We would introduce a change as follows: > {noformat} > if (this.key.equals(T.key.getAccessor())) { > throw new IllegalArgumentException("hasKey() cannot be applied to > traverser of type Element") > }{noformat} > *Impact of the change* > Before the change: > {noformat} > // the following query always returns empty results gremlin> > g.V().hasKey('age').fold() > gremlin> g.V().hasKey('age').fold() > ==>[]{noformat} > > After the change: > {noformat} > // the following query returns an error > gremlin> g.V().hasKey('age').fold() > hasKey() cannot be applied to traverser of type Element{noformat} > > > -- This message was sent by Atlassian Jira (v8.20.1#820001)