-
Notifications
You must be signed in to change notification settings - Fork 830
TINKERPOP-3166 Implement asNumber() step #3153
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
base: 3.8-dev
Are you sure you want to change the base?
Changes from 4 commits
e4ec141
8bee410
01c2d78
20827f8
7ef100a
66294ec
edd6f81
c3d5762
0ae19cd
13a51e9
b59199b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -829,6 +829,37 @@ g.inject(datetime("2023-08-24T00:00:00Z")).asDate() <3> | |
|
||
link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#asDate()++[`asDate()`] | ||
|
||
[[asNumber-step]] | ||
=== AsNumber Step | ||
|
||
The `asNumber()`-step (*map*) converts the incoming traverser to the nearest parsable type if no argument is provided, or to the desired numerical type, based on the number token (N) provided. | ||
|
||
Numerical input will pass through unless a type is specified by the number token, with the exception that any float number will be converted into double. `ArithmeticException` will be thrown for any overflow during narrowing of types. | ||
xiazcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
String input will be parsed. By default, the smalled unit of number to be parsed into is `int` if no number token is provided. `NumberFormatException` will be thrown for any unparsable strings. | ||
xiazcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
All other input types will result in `IllegalArgumentException`. | ||
|
||
[gremlin-groovy,modern] | ||
---- | ||
g.inject(1).asNumber() <1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect the basic happy path example to be included, something like |
||
g.inject(1.76).asNumber() <2> | ||
g.inject(1.76).asNumber(N.nint) <3> | ||
xiazcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
g.inject("1b").asNumber() <4> | ||
g.inject(33550336).asNumber(N.nbyte) <5> | ||
---- | ||
|
||
<1> An int will be passed through. | ||
<2> A double will be passed through. | ||
<3> A double is converted into an in. | ||
xiazcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<4> String containing any character other than numerical ones will result in `NumberFormatException`. | ||
<5> Narrowing of int to byte that overflows will throw `ArithmeticException`. | ||
|
||
*Additional References* | ||
|
||
link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#asNumber()++[`asNumber()`] | ||
link:++https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#asNumber(org.apache.tinkerpop.gremlin.process.traversal.N)++[`asNumber(N)`] | ||
|
||
[[barrier-step]] | ||
=== Barrier Step | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.apache.tinkerpop.gremlin.language.grammar.GremlinParser; | ||
import org.apache.tinkerpop.gremlin.process.traversal.DT; | ||
import org.apache.tinkerpop.gremlin.process.traversal.Merge; | ||
import org.apache.tinkerpop.gremlin.process.traversal.N; | ||
import org.apache.tinkerpop.gremlin.process.traversal.Operator; | ||
import org.apache.tinkerpop.gremlin.process.traversal.Order; | ||
import org.apache.tinkerpop.gremlin.process.traversal.P; | ||
|
@@ -207,6 +208,12 @@ public Void visitTraversalDT(final GremlinParser.TraversalDTContext ctx) { | |
return null; | ||
} | ||
|
||
@Override | ||
public Void visitTraversalN(final GremlinParser.TraversalNContext ctx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a few cases to GremlinTranslatorTest to ensure |
||
appendExplicitNaming(ctx.getText(), N.class.getSimpleName()); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Void visitTraversalPredicate(final GremlinParser.TraversalPredicateContext ctx) { | ||
switch(ctx.getChildCount()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.process.traversal; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.step.map.AsNumberStep; | ||
|
||
/** | ||
* Tokens that are used to denote different units of number. | ||
* Used with {@link AsNumberStep} step. | ||
*/ | ||
public enum N { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Radical idea at this point but does |
||
nbyte("Byte"), | ||
xiazcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nshort("Short"), | ||
nint("Integer"), | ||
nlong("Long"), | ||
nfloat("Float"), | ||
ndouble("Double"), | ||
nbigInt("BigInt"), | ||
nbigDecimal("BigDecimal"),; | ||
|
||
private final String typeName; | ||
|
||
N(String name) {typeName = name;} | ||
|
||
@Override | ||
public String toString() { | ||
return typeName; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "step" not "steps"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new steps like this should probably come with an addition to the semantics doc.