Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TINKERPOP-3083 Change split to parse a string into individual characters if the separator is an empty string #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
* Made `new` keyword optional in the Gremlin grammar.
* Added TypeScript & ECMAScript module support.
* Improved graph structures type definitions in TypeScript.
* Modified the `split()` step to split a string into a list of its characters if the given separator is an empty string.

== TinkerPop 3.7.0 (Gremfir Master of the Pan Flute)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/dev/provider/gremlin-semantics.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ link:https://tinkerpop.apache.org/docs/x.y.z/reference/#rTrim-step[reference]
*Arguments:*

* `separator` - The string character(s) used as delimiter to split the input string. Nullable, a null separator will split on
whitespaces.
whitespaces. An empty string separator will split on each character.
* `scope` - Determines the type of traverser it operates on. Both scopes will operate on the level of individual traversers.
The `global` scope will operate on individual string traverser. The `local` scope will operate on list traverser with string elements inside.

Expand Down
10 changes: 6 additions & 4 deletions docs/src/reference/the-traversal.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4455,21 +4455,23 @@ link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gre
[[split-step]]
=== Split Step
The `split()`-step (*map*) returns a list of strings created by splitting the incoming string traverser around the
matches of the given separator. A null separator will split the string by whitespaces. Null values from the incoming
traversers are not processed and remain as null when returned. If the incoming traverser is a non-String value then an
matches of the given separator. A null separator will split the string by whitespaces. An empty string separator will split on each character.
Null values from the incoming traversers are not processed and remain as null when returned. If the incoming traverser is a non-String value then an
IllegalArgumentException will be thrown.

[gremlin-groovy,modern]
----
g.inject("that", "this", "test", null).split("h") <1>
g.V().hasLabel("person").values("name").split("a") <2>
g.inject("helloworld", "hello world", "hello world").split(null) <3>
g.V().hasLabel("person").values("name").fold().split(local, "a") <4>
g.inject("hello", "world", null).split("") <4>
g.V().hasLabel("person").values("name").fold().split(local, "a") <5>
----
<1> Split the strings by "h".
<2> Split person names by "a".
<3> Splitting by null will split by whitespaces.
<4> Use `Scope.local` to operate on individual string elements inside incoming list, which will return a list of results.
<4> Splitting by "" will split by each character.
<5> Use `Scope.local` to operate on individual string elements inside incoming list, which will return a list of results.

*Additional References*
link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#split(java.lang.String)++[`split(String)`]
Expand Down
16 changes: 15 additions & 1 deletion docs/src/upgrade/release-4.x.x.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ these were deserialized into arrays.

==== Gremlin Grammar Changes

A number of changes have been introduce to the Gremlin grammar to help make it be more consistent and easier to use.
A number of changes have been introduced to the Gremlin grammar to help make it be more consistent and easier to use.

*`new` keyword is now optional*

Expand Down Expand Up @@ -113,6 +113,20 @@ The `none()` step, which was primarily used by `iterate()` to discard traversal
renamed to `discard()`. In its place is a new list filtering step `none()`, which takes a predicate as an argument and
passes lists with no elements matching the predicate.

==== Splitting a string into characters using split()
The `split()` step will now split a string into a list of its characters if the given separator is an empty string.
[source,groovy]
----
// previous implementation
g.inject("Hello").split("")
==>[Hello]

// new implementation
g.inject("Hello").split("")
==>[H,e,l,l,o]
----
See: link:https://issues.apache.org/jira/browse/TINKERPOP-3083[TINKERPOP-3083]

==== Improved handling of integer overflows

Integer overflows caused by addition and multiplication operations will throw an exception instead of being silently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
*/
package org.apache.tinkerpop.gremlin.process.traversal.step.map;

import org.apache.commons.lang3.StringUtils;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement;
import org.apache.tinkerpop.gremlin.util.StringUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -57,7 +55,7 @@ protected E map(final Traverser.Admin<S> traverser) {
}

// we will pass null values to next step
return null == item? null : (E) Arrays.asList(StringUtils.splitByWholeSeparator((String) item, this.separator));
return null == item? null : (E) StringUtil.split((String)item, this.separator);
}

public String getSeparator() {
Expand All @@ -75,4 +73,5 @@ public int hashCode() {
result = 31 * result + (null != this.separator ? this.separator.hashCode() : 0);
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.process.traversal.step.map;

import org.apache.commons.lang3.StringUtils;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.step.TraversalParent;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.StringLocalStep;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.apache.tinkerpop.gremlin.util.StringUtil;

/**
* Reference implementation for substring step, a mid-traversal step which returns a list of strings created by
Expand All @@ -50,7 +43,7 @@ public SplitLocalStep(final Traversal.Admin traversal, final String separator) {

@Override
protected E applyStringOperation(String item) {
return (E) Arrays.asList((StringUtils.splitByWholeSeparator(item, this.separator)));
return (E) StringUtil.split(item, this.separator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.util;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
* Utility class for Strings.
*
* @author Andrea Child
*/
public final class StringUtil {

private StringUtil() {
}

/**
* Splits the provided string into a List of string by the given separator, which will not be included in the returned List.
*
* @param toSplit the string to split
* @param separator the separator string to split by - empty will split into a List of characters
* @return a List of parsed strings, not including the given separator
*/
public static List<String> split(final String toSplit, final String separator) {
if (toSplit == null) {
throw new IllegalArgumentException("toSplit cannot be null");
}
if (StringUtils.EMPTY.equals(separator)) {
// split into a list of character strings
// ie. "ab cd" -> ["a","b"," ","c","d"]
return Arrays.asList(toSplit.split(StringUtils.EMPTY));
}
return Arrays.asList(StringUtils.splitByWholeSeparator(toSplit, separator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testReturnTypes() {
assertArrayEquals(new String[]{"helloworld"}, __.__("helloworld").split(null).next().toArray());
assertArrayEquals(new String[]{"hello", "world"}, __.__("hello world").split(null).next().toArray());
assertArrayEquals(new String[]{"hello", "world"}, __.__("hello world").split(null).next().toArray());
assertArrayEquals(new String[]{"hello", "world"}, __.__("hello world").split("").next().toArray());
assertArrayEquals(new String[]{"h","e","l","l","o"," ", "w","o","r","l","d"}, __.__("hello world").split("").next().toArray());
assertArrayEquals(new String[]{"hello", "world"}, __.__("hello world").split(" ").next().toArray());
assertArrayEquals(new String[]{"hello world"}, __.__("hello world").split(" ").next().toArray());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected List<Traversal> getTraversals() {
public void testReturnTypes() {
assertArrayEquals(new String[]{"h", "llo"}, __.__("hello").split(Scope.local, "e").next().toArray());
assertArrayEquals(new String[]{"ab", "bc"}, __.__("abc|abc").split(Scope.local, "c|a").next().toArray());
assertArrayEquals(new String[]{"t","e","s","t"}, __.__("test").split(Scope.local, "").next().toArray());

List<List<String>> resList = new ArrayList<>();
resList.add(Arrays.asList("helloworld"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.util;

import java.util.Arrays;
import org.apache.tinkerpop.gremlin.AssertHelper;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author Andrea Child
*/
public class StringUtilTest {

@Test
public void shouldBeUtilityClass() throws Exception {
AssertHelper.assertIsUtilityClass(StringUtil.class);
}

@Test
public void shouldSplitBySeparatorAndNotContainSeparator() {
assertEquals(Arrays.asList("h","llo"), StringUtil.split("hello", "e"));
}

@Test
public void shouldNotBeSplitIfSeparatorNotPresentInString() {
final String toSplit = "test";
assertEquals(Arrays.asList(toSplit), StringUtil.split(toSplit, "x"));
}

@Test
public void shouldSplitByCharacterIfSeparatorIsEmpty() {
assertEquals(Arrays.asList("t","e","s","t"," ","1","2","3"), StringUtil.split("test 123", ""));
}

@Test
public void shouldSplitByWhitespaceIfSeparatorIsNull() {
assertEquals(Arrays.asList("test","123"), StringUtil.split("test 123", null));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIfStringToSplitIsNull() {
StringUtil.split(null, "test");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,11 @@ private static IDictionary<string, List<Func<GraphTraversalSource, IDictionary<s
{"g_V_hasXname_vadasX_shortestPath_distanceXweightX_maxDistanceX1_3X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Has("name", "vadas").ShortestPath().With("~tinkerpop.shortestPath.distance", "weight").With("~tinkerpop.shortestPath.maxDistance", 1.3)}},
{"g_injectXthat_this_testX_spiltXhX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>("that", "this", "test", null).Split("h")}},
{"g_injectXhello_worldX_spiltXnullX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>("hello world").Split(null)}},
{"g_injectXthat_this_test_nullX_splitXemptyX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>("that", "this", "test", null).Split("")}},
{"g_injectXListXa_bXcX_splitXa_bX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(p["xx1"]).Split("a")}},
{"g_V_hasLabelXpersonX_valueXnameX_splitXnullX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().HasLabel("person").Values<object>("name").Split(null)}},
{"g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_aX_unfold", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().HasLabel("person").Values<object>("name").Order().Fold().Split<object>(Scope.Local, "a").Unfold<object>()}},
{"g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_emptyX_unfold", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().HasLabel("person").Values<object>("name").Order().Fold().Split<object>(Scope.Local, "").Unfold<object>()}},
{"g_injectXthat_this_testX_substringX1_8X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>("test", "hello world", null).Substring(1, 8)}},
{"g_injectXListXa_bXcX_substringX1_2X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(p["xx1"]).Substring(1, 2)}},
{"g_V_hasLabelXpersonX_valueXnameX_substringX2X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().HasLabel("software").Values<object>("name").Substring(2)}},
Expand Down
2 changes: 2 additions & 0 deletions gremlin-go/driver/cucumber/gremlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,9 +1175,11 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
"g_V_hasXname_vadasX_shortestPath_distanceXweightX_maxDistanceX1_3X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Has("name", "vadas").ShortestPath().With("~tinkerpop.shortestPath.distance", "weight").With("~tinkerpop.shortestPath.maxDistance", 1.3)}},
"g_injectXthat_this_testX_spiltXhX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject("that", "this", "test", nil).Split("h")}},
"g_injectXhello_worldX_spiltXnullX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject("hello world").Split(nil)}},
"g_injectXthat_this_test_nullX_splitXemptyX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject("that", "this", "test", nil).Split("")}},
"g_injectXListXa_bXcX_splitXa_bX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(p["xx1"]).Split("a")}},
"g_V_hasLabelXpersonX_valueXnameX_splitXnullX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().HasLabel("person").Values("name").Split(nil)}},
"g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_aX_unfold": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().HasLabel("person").Values("name").Order().Fold().Split(gremlingo.Scope.Local, "a").Unfold()}},
"g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_emptyX_unfold": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().HasLabel("person").Values("name").Order().Fold().Split(gremlingo.Scope.Local, "").Unfold()}},
"g_injectXthat_this_testX_substringX1_8X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject("test", "hello world", nil).Substring(1, 8)}},
"g_injectXListXa_bXcX_substringX1_2X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(p["xx1"]).Substring(1, 2)}},
"g_V_hasLabelXpersonX_valueXnameX_substringX2X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().HasLabel("software").Values("name").Substring(2)}},
Expand Down

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

2 changes: 2 additions & 0 deletions gremlin-python/src/main/python/radish/gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,11 @@
'g_V_hasXname_vadasX_shortestPath_distanceXweightX_maxDistanceX1_3X': [(lambda g:g.V().has('name', 'vadas').shortest_path().with_('~tinkerpop.shortestPath.distance', 'weight').with_('~tinkerpop.shortestPath.maxDistance', 1.3))],
'g_injectXthat_this_testX_spiltXhX': [(lambda g:g.inject('that', 'this', 'test', None).split('h'))],
'g_injectXhello_worldX_spiltXnullX': [(lambda g:g.inject('hello world').split(None))],
'g_injectXthat_this_test_nullX_splitXemptyX': [(lambda g:g.inject('that', 'this', 'test', None).split(''))],
'g_injectXListXa_bXcX_splitXa_bX': [(lambda g, xx1=None:g.inject(xx1).split('a'))],
'g_V_hasLabelXpersonX_valueXnameX_splitXnullX': [(lambda g:g.V().has_label('person').values('name').split(None))],
'g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_aX_unfold': [(lambda g:g.V().has_label('person').values('name').order().fold().split(Scope.local, 'a').unfold())],
'g_V_hasLabelXpersonX_valueXnameX_order_fold_splitXlocal_emptyX_unfold': [(lambda g:g.V().has_label('person').values('name').order().fold().split(Scope.local, '').unfold())],
'g_injectXthat_this_testX_substringX1_8X': [(lambda g:g.inject('test', 'hello world', None).substring(1, 8))],
'g_injectXListXa_bXcX_substringX1_2X': [(lambda g, xx1=None:g.inject(xx1).substring(1, 2))],
'g_V_hasLabelXpersonX_valueXnameX_substringX2X': [(lambda g:g.V().has_label('software').values('name').substring(2))],
Expand Down
Loading