Skip to content

Commit

Permalink
implementation for python
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentyn Kahamlyk authored and Valentyn Kahamlyk committed Aug 22, 2023
1 parent 0960cfe commit d6a093b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
*/
package org.apache.tinkerpop.gremlin.language.grammar;

import org.apache.tinkerpop.gremlin.process.traversal.*;
import org.apache.tinkerpop.gremlin.process.traversal.DT;
import org.apache.tinkerpop.gremlin.process.traversal.Merge;
import org.apache.tinkerpop.gremlin.process.traversal.Operator;
import org.apache.tinkerpop.gremlin.process.traversal.Order;
import org.apache.tinkerpop.gremlin.process.traversal.Pop;
import org.apache.tinkerpop.gremlin.process.traversal.SackFunctions;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
import org.apache.tinkerpop.gremlin.structure.Column;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.T;
Expand All @@ -27,7 +33,6 @@
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Visitor class to handle generic literal. All visitor methods return type is Object. It maybe used as a singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.TraverserRequirement;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import org.apache.tinkerpop.gremlin.util.DatetimeHelper;

import java.util.*;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Reference implementation for date parsing concatenation step.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import org.apache.tinkerpop.gremlin.process.traversal.step.StepTest;
import org.junit.Test;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static java.time.ZoneOffset.UTC;
import static org.junit.Assert.assertEquals;

public class DateAddStepTest extends StepTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def as_(self, *args):
self.bytecode.add_step("as", *args)
return self

def as_date(self, *args):
self.bytecode.add_step("asDate", *args)
return self

def barrier(self, *args):
self.bytecode.add_step("barrier", *args)
return self
Expand Down Expand Up @@ -409,6 +413,14 @@ def cyclic_path(self, *args):
self.bytecode.add_step("cyclicPath", *args)
return self

def date_add(self, *args):
self.bytecode.add_step("dateAdd", *args)
return self

def date_diff(self, *args):
self.bytecode.add_step("dateDiff", *args)
return self

def dedup(self, *args):
self.bytecode.add_step("dedup", *args)
return self
Expand Down Expand Up @@ -983,6 +995,10 @@ def and_(cls, *args):
def as_(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).as_(*args)

@classmethod
def as_date(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).as_date(*args)

@classmethod
def barrier(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).barrier(*args)
Expand Down Expand Up @@ -1063,6 +1079,14 @@ def cyclicPath(cls, *args):
def cyclic_path(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).cyclic_path(*args)

@classmethod
def date_add(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).date_add(*args)

@classmethod
def date_diff(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).date_diff(*args)

@classmethod
def dedup(cls, *args):
return cls.graph_traversal(None, None, Bytecode()).dedup(*args)
Expand Down Expand Up @@ -1639,6 +1663,10 @@ def as_(*args):
return __.as_(*args)


def as_date(*args):
return __.as_date(*args)


def barrier(*args):
return __.barrier(*args)

Expand Down Expand Up @@ -1707,6 +1735,14 @@ def cyclic_path(*args):
return __.cyclic_path(*args)


def date_add(*args):
return __.date_add(*args)


def date_diff(*args):
return __.date_diff(*args)


def dedup(*args):
return __.dedup(*args)

Expand Down Expand Up @@ -2107,6 +2143,8 @@ def where(*args):

statics.add_static('as_', as_)

statics.add_static('as_date', as_date)

statics.add_static('barrier', barrier)

statics.add_static('both', both)
Expand Down Expand Up @@ -2141,6 +2179,10 @@ def where(*args):

statics.add_static('cyclicpath', cyclic_path)

statics.add_static('date_add', date_add)

statics.add_static('date_diff', date_diff)

statics.add_static('dedup', dedup)

statics.add_static('drop', drop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def process(f):
statics.add_static('V1_0', GryoVersion.V1_0)
statics.add_static('V3_0', GryoVersion.V3_0)

DT = Enum('DT', ' second minute hour day')

statics.add_static('second', DT.second)
statics.add_static('minute', DT.minute)
statics.add_static('hour', DT.hour)
statics.add_static('day', DT.day)

Merge = Enum('Merge', ' on_create on_match out_v in_v')

statics.add_static('on_create', Merge.on_create)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from gremlin_python.statics import FloatType, BigDecimal, FunctionType, ShortType, IntType, LongType, BigIntType, \
TypeType, DictType, ListType, SetType, SingleByte, ByteBufferType, GremlinType, \
SingleChar
from gremlin_python.process.traversal import Barrier, Binding, Bytecode, Cardinality, Column, Direction, Merge, \
from gremlin_python.process.traversal import Barrier, Binding, Bytecode, Cardinality, Column, Direction, DT, Merge, \
Operator, Order, Pick, Pop, P, Scope, TextP, Traversal, Traverser, \
TraversalStrategy, T
from gremlin_python.process.graph_traversal import GraphTraversal
Expand Down Expand Up @@ -97,6 +97,7 @@ class DataType(Enum):
metrics = 0x2c
traversalmetrics = 0x2d
merge = 0x2e
dt = 0x2f
char = 0x80
duration = 0x81
inetaddress = 0x82 # todo
Expand Down Expand Up @@ -919,6 +920,11 @@ def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
return to_extend


class DTIO(_EnumIO):
graphbinary_type = DataType.dt
python_type = DT


class MergeIO(_EnumIO):
graphbinary_type = DataType.merge
python_type = Merge
Expand Down
3 changes: 3 additions & 0 deletions gremlin-python/src/main/python/radish/feature_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
#

from datetime import datetime
import json
import re
from gremlin_python.statics import long
Expand Down Expand Up @@ -198,6 +199,8 @@ def _convert(val, ctx):
return [] if val == "l[]" else list(map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
elif isinstance(val, str) and re.match(r"^s\[.*\]$", val): # parse set
return set() if val == "s[]" else set(map((lambda x: _convert(x, ctx)), val[2:-1].split(",")))
elif isinstance(val, str) and re.match(r"^dt\[.*\]$", val): # parse datetime
return datetime.fromtimestamp(int(val[3:-1]))
elif isinstance(val, str) and re.match(r"^d\[NaN\]$", val): # parse nan
return float("nan")
elif isinstance(val, str) and re.match(r"^d\[Infinity\]$", val): # parse +inf
Expand Down
13 changes: 13 additions & 0 deletions gremlin-python/src/main/python/radish/gremlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@
'g_addVXpersonX_propertyXname_joshX_propertyXage_nullX': [(lambda g:g.addV('person').property('name','josh').property('age',None)), (lambda g:g.V().has('person','age',None))],
'g_addVXpersonX_propertyXname_markoX_propertyXfriendWeight_null_acl_nullX': [(lambda g:g.addV('person').property('name','marko').property('friendWeight',None,'acl',None)), (lambda g:g.V().has('person','name','marko').has('friendWeight',None)), (lambda g:g.V().has('person','name','marko').properties('friendWeight').has('acl',None)), (lambda g:g.V().has('person','name','marko').properties('friendWeight').count())],
'g_V_hasXperson_name_aliceX_propertyXsingle_age_unionXage_constantX1XX_sumX': [(lambda g:g.addV('person').property('name','alice').property(Cardinality.single,'age',50)), (lambda g:g.V().has('person','name','alice').property('age',__.union(__.age,__.constant(1)).sum_())), (lambda g:g.V().has('person','age',50)), (lambda g:g.V().has('person','age',51))],
'g_injectXstrX_asDate': [(lambda g:g.inject('2022-08-02T00:00:00Z').asDate())],
'g_injectX1000X_asDate': [(lambda g:g.inject(1000).asDate())],
'g_injectX2000LX_asDate': [(lambda g, xx1=None:g.inject(xx1).asDate())],
'g_injectX3000dX_asDate': [(lambda g, xx1=None:g.inject(xx1).asDate())],
'g_injectX1_2X_asDate': [(lambda g, xx1=None:g.inject(xx1).asDate())],
'g_injectXnullX_asDate': [(lambda g:g.inject(None).asDate())],
'g_call': [(lambda g:g.call())],
'g_callXlistX': [(lambda g:g.call('--list'))],
'g_callXlistX_withXstring_stringX': [(lambda g:g.call('--list').with_('service','tinker.search'))],
Expand Down Expand Up @@ -501,6 +507,13 @@
'g_E_sampleX1X_count': [(lambda g:g.E().sample(1).count())],
'g_V_sampleX1X_byXageX_count': [(lambda g:g.V().sample(1).by('age').count())],
'g_V_order_byXnoX_count': [(lambda g:g.V().order().by('no').count())],
'g_injectXdatetimeXstrXX_dateAddXDT_hour_2X': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateAdd(DT.hour,2))],
'g_injectXdatetimeXstrXX_dateAddXhour_2X': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateAdd(DT.hour,2))],
'g_injectXdatetimeXstrXX_dateAddXhour_1X': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateAdd(DT.hour,-1))],
'g_injectXdatetimeXstrXX_dateAddXminute_10X': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateAdd(DT.minute,10))],
'g_injectXdatetimeXstrXX_dateAddXsecond_20X': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateAdd(DT.second,20))],
'g_injectXdatetimeXstr1XX_dateDiffXdatetimeXstr2XX': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1690934400000 / 1000.0)).dateDiff(datetime.datetime.utcfromtimestamp(1691539200000 / 1000.0)))],
'g_injectXdatetimeXstr1XX_dateDiffXinjectXdatetimeXstr2XXX': [(lambda g:g.inject(datetime.datetime.utcfromtimestamp(1691452800000 / 1000.0)).dateDiff(__.inject(datetime.datetime.utcfromtimestamp(1690848000000 / 1000.0))))],
'g_V_EX11X': [(lambda g, eid11=None:g.V().E(eid11))],
'g_EX11X_E': [(lambda g, eid11=None:g.E(eid11).E())],
'g_V_EXnullX': [(lambda g:g.V().E(None))],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryReader;
import org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryWriter;
import org.junit.Test;
import java.util.*;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import static org.junit.Assert.assertEquals;
Expand Down

0 comments on commit d6a093b

Please sign in to comment.