One of the recent items I have heard quite a few requests for, and had
several on and off conversations about, is the ability to cast values
within a Gremlin traversal. Due to the schemaless nature of Gremlin it is
very easy to get into a situation where you have a single property that
contains different datatypes across elements and it would be very nice to
be able to create a consistent datatype to allow comparisons. I'd like to
throw this proposal out here to see what people think.
What if we added a step to handle converting to a defined subset of
datatypes:
Function Signature:
cast(Enum, string)
Enum - This would be an enum containing the datatypes you can cast to:
* DT.short
* DT.int
* DT.long
* DT.float
* DT.double
* DT.char
* DT.boolean
* DT.string
string - The property name to cast
Example usages:
g.V().project('name', 'age').by(cast(DT.String, 'name')).by(cast(DT.int,
'age'))
g.V().order().by(cast(DT.int, 'age'))
g.V().group().by(cast(DT.int, 'age'))
So what happens if the value cannot be cast to the defined type:
I think that the default behavior in this case should be to throw an error.
I would like to provide two configuration options (via with)
with(CAST.remove) - in any case where the value cannot be cast the
traverser would be filtered out
with(CAST.default, -999) - in any case the value cannot be cast the
specified default value (e.g. -999) would be used. This would reduce the
complexity and increase the readability of many queries by no longer
requiring the use of a coalesce() to handle creation of default values .
This would also enable customers to handle use cases where data is of
varying types which cannot be handled by the current coalesce() pattern.
Dave