Skip to content

Commit 8bde216

Browse files
committed
Merge branch '3.8-dev'
2 parents 16cf94e + 76598e1 commit 8bde216

File tree

7 files changed

+34
-1
lines changed
  • gremlin-core/src
  • gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin
  • gremlin-go/driver/cucumber
  • gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber
  • gremlin-python/src/main/python/radish
  • gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/map

7 files changed

+34
-1
lines changed

gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/util/NumberHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.math.BigDecimal;
2424
import java.math.BigInteger;
2525
import java.math.MathContext;
26+
import java.math.RoundingMode;
2627
import java.util.function.Function;
2728
import java.util.function.BiFunction;
2829

@@ -793,7 +794,12 @@ private static Long longValue(final Number num, final Class<? extends Number> ta
793794
}
794795

795796
try {
796-
if (num.getClass().equals(BigDecimal.class)) return ((BigDecimal) num).longValueExact();
797+
if (num.getClass().equals(BigDecimal.class)) {
798+
// need to truncate the decimal places or else longValueExact() will throw
799+
BigDecimal truncated = ((BigDecimal) num).setScale(0, RoundingMode.DOWN);
800+
return truncated.longValueExact();
801+
}
802+
797803
return num.getClass().equals(BigInteger.class) ? ((BigInteger) num).longValueExact() : num.longValue();
798804
} catch (ArithmeticException ae) {
799805
throw new ArithmeticException(msgOverflow);

gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsNumberStepTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public void testReturnTypes() {
5151
assertEquals(3.14f, __.__(3.14).asNumber(GType.FLOAT).next());
5252
assertEquals(3.14, __.__(3.14f).asNumber(GType.DOUBLE).next());
5353
assertEquals(3.14, __.__(3.14f).asNumber(GType.DOUBLE).next());
54+
assertEquals(3, __.__(new BigDecimal("3.14")).asNumber(GType.INT).next());
55+
assertEquals(3L, __.__(new BigDecimal("3.14")).asNumber(GType.LONG).next());
56+
assertEquals(-3, __.__(new BigDecimal("-3.14")).asNumber(GType.INT).next());
57+
assertEquals(-3L, __.__(new BigDecimal("-3.14")).asNumber(GType.LONG).next());
5458
assertEquals(1, __.__("1").asNumber(GType.INT).next());
5559
assertEquals(1, __.__("1").asNumber().next());
5660
assertEquals((byte) 1, __.__("1").asNumber(GType.BYTE).next());
@@ -273,6 +277,11 @@ public void shouldThrowOverflowExceptionWhenBigDecimalToShortOverflows() {
273277
__.__(new BigDecimal(Long.MAX_VALUE+"1")).asNumber(GType.SHORT).next();
274278
}
275279

280+
@Test(expected = ArithmeticException.class)
281+
public void shouldThrowOverflowExceptionWhenBigDecimalToLongOverflows() {
282+
__.__(new BigDecimal(Long.MAX_VALUE+"1")).asNumber(GType.LONG).next();
283+
}
284+
276285
@Test(expected = ArithmeticException.class)
277286
public void shouldThrowOverflowExceptionWhenFloatToLongOverflows() {
278287
__.__(new Float(Long.MAX_VALUE+"1")).asNumber(GType.LONG).next();

gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ private static IDictionary<string, List<Func<GraphTraversalSource, IDictionary<s
969969
{"g_injectXnullX_asNumberXGType_INTX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(null).AsNumber(GType.Int)}},
970970
{"g_V_asXaX_outXknowsX_asXbX_mathXa_plus_bX_byXageX_asNumberXGType_INTX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().As("a").Out("knows").As("b").Math("a + b").By("age").AsNumber(GType.Int)}},
971971
{"g_withSideEffectXx_100X_V_age_mathX__plus_xX_asNumberXGType_LONGX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.WithSideEffect("x", 100).V().Values<object>("age").Math("_ + x").AsNumber(GType.Long)}},
972+
{"g_V_valuesXageX_asString_asNumberXGType_DOUBLEX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Values<object>("age").AsString().AsNumber(GType.Double)}},
972973
{"g_injectX1_2X_asString", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(1, 2).AsString()}},
973974
{"g_injectX1_2X_asStringXlocalX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(1, 2).AsString<object>(Scope.Local)}},
974975
{"g_injectXlist_1_2X_asStringXlocalX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.Inject<object>(new List<object> { 1, 2 }).AsString<object>(Scope.Local)}},

gremlin-go/driver/cucumber/gremlin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
940940
"g_injectXnullX_asNumberXGType_INTX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(nil).AsNumber(gremlingo.GType.Int)}},
941941
"g_V_asXaX_outXknowsX_asXbX_mathXa_plus_bX_byXageX_asNumberXGType_INTX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().As("a").Out("knows").As("b").Math("a + b").By("age").AsNumber(gremlingo.GType.Int)}},
942942
"g_withSideEffectXx_100X_V_age_mathX__plus_xX_asNumberXGType_LONGX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.WithSideEffect("x", 100).V().Values("age").Math("_ + x").AsNumber(gremlingo.GType.Long)}},
943+
"g_V_valuesXageX_asString_asNumberXGType_DOUBLEX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Values("age").AsString().AsNumber(gremlingo.GType.Double)}},
943944
"g_injectX1_2X_asString": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(1, 2).AsString()}},
944945
"g_injectX1_2X_asStringXlocalX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject(1, 2).AsString(gremlingo.Scope.Local)}},
945946
"g_injectXlist_1_2X_asStringXlocalX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.Inject([]interface{}{1, 2}).AsString(gremlingo.Scope.Local)}},

gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gremlin-python/src/main/python/radish/gremlin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@
943943
'g_injectXnullX_asNumberXGType_INTX': [(lambda g:g.inject(None).as_number(GType.INT))],
944944
'g_V_asXaX_outXknowsX_asXbX_mathXa_plus_bX_byXageX_asNumberXGType_INTX': [(lambda g:g.V().as_('a').out('knows').as_('b').math('a + b').by('age').as_number(GType.INT))],
945945
'g_withSideEffectXx_100X_V_age_mathX__plus_xX_asNumberXGType_LONGX': [(lambda g:g.with_side_effect('x', 100).V().values('age').math('_ + x').as_number(GType.LONG))],
946+
'g_V_valuesXageX_asString_asNumberXGType_DOUBLEX': [(lambda g:g.V().values('age').as_string().as_number(GType.DOUBLE))],
946947
'g_injectX1_2X_asString': [(lambda g:g.inject(1, 2).as_string())],
947948
'g_injectX1_2X_asStringXlocalX': [(lambda g:g.inject(1, 2).as_string(Scope.local))],
948949
'g_injectXlist_1_2X_asStringXlocalX': [(lambda g:g.inject([1, 2]).as_string(Scope.local))],

gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/map/AsNumber.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,17 @@ Feature: Step - asNumber()
311311
| d[127].l |
312312
| d[132].l |
313313
| d[135].l |
314+
315+
Scenario: g_V_valuesXageX_asString_asNumberXGType_DOUBLEX
316+
Given the modern graph
317+
And the traversal of
318+
"""
319+
g.V().values("age").asString().asNumber(GType.DOUBLE)
320+
"""
321+
When iterated to list
322+
Then the result should be unordered
323+
| result |
324+
| d[29.0].d |
325+
| d[27.0].d |
326+
| d[32.0].d |
327+
| d[35.0].d |

0 commit comments

Comments
 (0)