Skip to content

Commit

Permalink
Fix mergeV().option(Merge, GValue<Map>, Cardinality) failing to apply…
Browse files Browse the repository at this point in the history
… cardinalities to properties in the GValue<Map>
  • Loading branch information
Cole-Greer committed Dec 3, 2024
1 parent c1aa9f2 commit d71a8a2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4545,9 +4545,25 @@ public default <M, E2> GraphTraversal<S, E> option(final M token, final GValue<M
* @since 3.7.3
*/
public default <M, E2> GraphTraversal<S, E> option(final Merge merge, final GValue<Map<Object, Object>> m, final VertexProperty.Cardinality cardinality) {
final Step<?, ?> lastStep = this.asAdmin().getEndStep();

// CardinalityValueTraversal doesn't make sense for any prior step other than mergeV()
if (!(lastStep instanceof MergeVertexStep)) {
throw new IllegalStateException("option() with the Cardinality argument can only be used following mergeV()");
}

this.asAdmin().getGremlinLang().addStep(GraphTraversal.Symbols.option, merge, m, cardinality);

((TraversalOptionParent<M, E, E2>) this.asAdmin().getEndStep()).addChildOption((M) merge, (Traversal.Admin<E, E2>) new ConstantTraversal<>(m).asAdmin());
// do explicit cardinality for every single pair in the map.
// copy to new map to avoid modifying the existing map which was added to GremlinLang.
Map cardinalitiesMap = new LinkedHashMap(m.get());
for (Object k : cardinalitiesMap.keySet()) {
final Object o = cardinalitiesMap.get(k);
if (!(o instanceof CardinalityValueTraversal))
cardinalitiesMap.put(k, new CardinalityValueTraversal(cardinality, o));
}

((TraversalOptionParent<M, E, E2>) lastStep).addChildOption((M) merge, (Traversal.Admin<E, E2>) new ConstantTraversal<>(GValue.ofMap(m.getName(), cardinalitiesMap)).asAdmin());
return this;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gremlin-python/src/main/python/radish/gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@
'g_mergeVXname_markoX_optionXonMatch_age_setX31XX': [(lambda g:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, { 'age': CardinalityValue.set_(31) })), (lambda g:g.V().has('person', 'name', 'marko').has('age', 31)), (lambda g:g.V().has('person', 'name', 'marko').has('age')), (lambda g:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_singleX33XX': [(lambda g:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, { 'age': CardinalityValue.single(33) })), (lambda g:g.V().has('person', 'name', 'marko').has('age', 33)), (lambda g:g.V().has('person', 'name', 'marko').has('age')), (lambda g:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_33_singleX': [(lambda g:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, { 'age': 33 }, Cardinality.single)), (lambda g:g.V().has('person', 'name', 'marko').has('age', 33)), (lambda g:g.V().has('person', 'name', 'marko').has('age')), (lambda g:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_33_var_singleX': [(lambda g, xx1=None:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g, xx1=None:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, xx1, Cardinality.single)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age', 33)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age')), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_33_var_setX': [(lambda g, xx1=None:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g, xx1=None:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, xx1, Cardinality.set_)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age', 33)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age')), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_32_var_setX': [(lambda g, xx1=None:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g, xx1=None:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, xx1, Cardinality.set_)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age', 32)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age')), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_age_32_var_listX': [(lambda g, xx1=None:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g, xx1=None:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, xx1, Cardinality.list_)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age', 32)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').has('age')), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').properties('age')), (lambda g, xx1=None:g.V().has('person', 'name', 'marko').properties('age').where(__.value().is_(32)))],
'g_mergeVXname_markoX_optionXonMatch_name_allen_age_setX31X_singleX': [(lambda g:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, { 'name': 'allen', 'age': CardinalityValue.set_(31) }, Cardinality.single)), (lambda g:g.V().has('person', 'name', 'marko')), (lambda g:g.V().has('person', 'name', 'allen').has('age', 31)), (lambda g:g.V().has('person', 'name', 'allen').has('age')), (lambda g:g.V().has('person', 'name', 'allen').properties('age'))],
'g_mergeVXname_markoX_optionXonMatch_name_allen_var_singleX': [(lambda g, xx1=None:g.add_v('person').property('name', 'marko')), (lambda g, xx1=None:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, xx1, Cardinality.single)), (lambda g, xx1=None:g.V().has('person', 'name', 'marko')), (lambda g, xx1=None:g.V().has('person', 'name', 'allen'))],
'g_mergeVXname_markoX_optionXonMatch_name_allen_age_singleX31X_singleX': [(lambda g:g.add_v('person').property('name', 'marko').property(Cardinality.list_, 'age', 29).property(Cardinality.list_, 'age', 31).property(Cardinality.list_, 'age', 32)), (lambda g:g.merge_v({ 'name': 'marko' }).option(Merge.on_match, { 'name': 'allen', 'age': CardinalityValue.single(31) }, Cardinality.single)), (lambda g:g.V().has('person', 'name', 'marko')), (lambda g:g.V().has('person', 'name', 'allen').has('age', 33)), (lambda g:g.V().has('person', 'name', 'allen').has('age', 31)), (lambda g:g.V().has('person', 'name', 'allen').has('age')), (lambda g:g.V().has('person', 'name', 'allen').properties('age'))],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,83 @@ Feature: Step - mergeV()
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\")"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\")"

@MultiProperties
Scenario: g_mergeVXname_markoX_optionXonMatch_age_33_var_singleX
Given the empty graph
And the graph initializer of
"""
g.addV("person").property("name", "marko").property(Cardinality.list, "age", 29).property(Cardinality.list, "age", 31).property(Cardinality.list, "age", 32)
"""
And using the parameter xx1 defined as "m[{\"age\":33}]"
And the traversal of
"""
g.mergeV([name: "marko"]).
option(Merge.onMatch, xx1, Cardinality.single)
"""
When iterated to list
Then the result should have a count of 1
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\", 33)"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\")"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\")"

@MultiProperties
Scenario: g_mergeVXname_markoX_optionXonMatch_age_33_var_setX
Given the empty graph
And the graph initializer of
"""
g.addV("person").property("name", "marko").property(Cardinality.list, "age", 29).property(Cardinality.list, "age", 31).property(Cardinality.list, "age", 32)
"""
And using the parameter xx1 defined as "m[{\"age\":33}]"
And the traversal of
"""
g.mergeV([name: "marko"]).
option(Merge.onMatch, xx1, Cardinality.set)
"""
When iterated to list
Then the result should have a count of 1
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\", 33)"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\")"
And the graph should return 4 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\")"

@MultiProperties
Scenario: g_mergeVXname_markoX_optionXonMatch_age_32_var_setX
Given the empty graph
And the graph initializer of
"""
g.addV("person").property("name", "marko").property(Cardinality.list, "age", 29).property(Cardinality.list, "age", 31).property(Cardinality.list, "age", 32)
"""
And using the parameter xx1 defined as "m[{\"age\":32}]"
And the traversal of
"""
g.mergeV([name: "marko"]).
option(Merge.onMatch, xx1, Cardinality.set)
"""
When iterated to list
Then the result should have a count of 1
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\", 32)"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\")"
And the graph should return 3 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\")"

@MultiProperties
Scenario: g_mergeVXname_markoX_optionXonMatch_age_32_var_listX
Given the empty graph
And the graph initializer of
"""
g.addV("person").property("name", "marko").property(Cardinality.list, "age", 29).property(Cardinality.list, "age", 31).property(Cardinality.list, "age", 32)
"""
And using the parameter xx1 defined as "m[{\"age\":32}]"
And the traversal of
"""
g.mergeV([name: "marko"]).
option(Merge.onMatch, xx1, Cardinality.list)
"""
When iterated to list
Then the result should have a count of 1
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\", 32)"
And the graph should return 1 for count of "g.V().has(\"person\",\"name\",\"marko\").has(\"age\")"
And the graph should return 4 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\")"
And the graph should return 2 for count of "g.V().has(\"person\",\"name\",\"marko\").properties(\"age\").where(value().is(32))"

@MultiProperties
Scenario: g_mergeVXname_markoX_optionXonMatch_name_allen_age_setX31X_singleX
Given the empty graph
Expand Down

0 comments on commit d71a8a2

Please sign in to comment.