[ https://issues.apache.org/jira/browse/TINKERPOP-2710?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17508546#comment-17508546 ]
Dave Bechberger edited comment on TINKERPOP-2710 at 3/18/22, 3:21 AM: ---------------------------------------------------------------------- I am generally against throwing errors when if the intent of the query is able to be determined as errors generally lead to a lot more undifferentiated work for a consumer to manage.. Given the query {code:java} g.V().hasKey('age').fold(){code} It seems to be that the intent of this query is to find all the vertices with a key of `age` which seems equivalent to {code:java} g.V().has('age'){code} and return the elements with the key. {code:java} gremlin> g.V().has('age') ==>v[1] ==>v[2] ==>v[4] ==>v[6]{code} This would also be a breaking change but would lead to fewer errors being thrown and greater consistency among seemingly identical steps. was (Author: bechbd): I am generally against throwing errors when if the intent of the query is able to be determined as errors generally lead to a lot more undifferentiated work for a consumer to manage.. Given the query ``` g.V().hasKey('age').fold() ``` It seems to be that the intent of this query is to find all the vertices with a key of `age` which seems equivalent to `g.V().has('age')` and return the elements with the key. ``` gremlin> g.V().has('age') ==>v[1] ==>v[2] ==>v[4] ==>v[6] ``` This would also be a breaking change but would lead to fewer errors being thrown and greater consistency among seemingly identical steps. > 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)