Divij Vaidya created TINKERPOP-2710:
---------------------------------------

             Summary: 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
    Affects Versions: 3.6.0
            Reporter: Divij Vaidya
             Fix For: 3.6.0


*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 fail at 
insertion.

*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

g.V().hasKey('age'){noformat}
 

After the change:

 

 
{noformat}
// the following query returns an error
gremlin> g.V().hasKey('age')
hasKey() cannot be applied to traverser of type Element{noformat}
 

 

 



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to