-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added GSQL lexer * encased keywords in 'words' function added link to language reference * added additional word functions
- Loading branch information
Showing
4 changed files
with
1,812 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import re | ||
|
||
from pygments.lexer import RegexLexer, include, bygroups, using, this, words | ||
from pygments.token import Keyword, Punctuation, Comment, Operator, Name,\ | ||
String, Number, Whitespace | ||
|
||
|
||
__all__ = ["GSQLLexer"] | ||
|
||
class GSQLLexer(RegexLexer): | ||
|
||
""" | ||
For `GSQL <https://docs.tigergraph.com/dev/gsql-ref>`_ queries (version 3.x). | ||
.. versionadded:: 2.10 | ||
""" | ||
|
||
name = 'GSQL' | ||
aliases = ['gsql'] | ||
filenames = ['*.gsql'] | ||
|
||
flags = re.MULTILINE | re.IGNORECASE | ||
|
||
tokens = { | ||
'root': [ | ||
include('comment'), | ||
include('keywords'), | ||
include('clauses'), | ||
include('accums'), | ||
include('relations'), | ||
include('strings'), | ||
include('whitespace'), | ||
include('barewords'), | ||
include('operators'), | ||
], | ||
'comment': [ | ||
(r'.*\#.*\n', Comment.Single), | ||
(r'.*\/\*\s*.*\s*\*\/', Comment.Multiline), | ||
], | ||
'keywords': [ | ||
(words(( | ||
'ACCUM', 'AND', 'ANY', 'API', 'AS', 'ASC', 'AVG', 'BAG', 'BATCH', 'BETWEEN', 'BOOL', 'BOTH', | ||
'BREAK', 'BY', 'CASE', 'CATCH', 'COALESCE', 'COMPRESS', 'CONTINUE', 'COUNT', | ||
'CREATE', 'DATETIME', 'DATETIME_ADD', 'DATETIME_SUB', 'DELETE', 'DESC', 'DISTRIBUTED', 'DO', | ||
'DOUBLE', 'EDGE', 'ELSE', 'END', 'ESCAPE', 'EXCEPTION', 'FALSE', 'FILE', 'FILTER', 'FLOAT', 'FOREACH', 'FOR', | ||
'FROM', 'GRAPH', 'GROUP', 'GSQL_INT_MAX', 'GSQL_INT_MIN', 'GSQL_UINT_MAX', 'HAVING', 'IF', | ||
'IN', 'INSERT', 'INT', 'INTERPRET', 'INTERSECT', 'INTERVAL', 'INTO', 'IS', 'ISEMPTY', 'JSONARRAY', 'JSONOBJECT', 'LASTHOP', | ||
'LEADING', 'LIKE', 'LIMIT', 'LIST', 'LOAD_ACCUM', 'LOG', 'MAP', 'MATCH', 'MAX', 'MIN', 'MINUS', 'NOT', | ||
'NOW', 'NULL', 'OFFSET', 'OR', 'ORDER', 'PATH', 'PER', 'PINNED', 'POST_ACCUM', 'POST-ACCUM', 'PRIMARY_ID', 'PRINT', | ||
'QUERY', 'RAISE', 'RANGE', 'REPLACE', 'RESET_COLLECTION_ACCUM', 'RETURN', 'RETURNS', 'RUN', 'SAMPLE', 'SELECT', 'SELECT_VERTEX', | ||
'SET', 'SRC', 'STATIC', 'STRING', 'SUM', 'SYNTAX', 'TARGET', 'TAGSTGT', 'THEN', 'TO', 'TO_CSV', 'TO_DATETIME', 'TRAILING', 'TRIM', 'TRUE', | ||
'TRY', 'TUPLE', 'TYPEDEF', 'UINT', 'UNION', 'UPDATE', 'VALUES', 'VERTEX', 'WHEN', 'WHERE', 'WHILE', 'WITH'), prefix=r'(?<!\.)', suffix=r'\b'), Name.Builtin) | ||
], | ||
'clauses': [ | ||
(words(('accum', 'having', 'limit', 'order', 'postAccum', 'sample', 'where')), Name.Builtin) | ||
], | ||
'accums': [ | ||
(words(('andaccum', 'arrayaccum', 'avgaccum', 'bagaccum', 'bitwiseandaccum', | ||
'bitwiseoraccum', 'groupbyaccum', 'heapaccum', 'listaccum', 'MapAccum', | ||
'maxaccum', 'minaccum', 'oraccum', 'setaccum', 'sumaccum')), Name.Builtin), | ||
], | ||
# 'name': [ | ||
# (r'(\@\@w+)\b', Name), | ||
# ], | ||
'relations': [ | ||
(r'(-\s?)(\(.*\:\w?\))(\s?-)', bygroups(Operator, using(this), Operator)), | ||
(r'->|<-', Operator), | ||
(r'[.*{}]', Punctuation), | ||
], | ||
'strings': [ | ||
(r'"(?:\\[tbnrf\'"\\]|[^\\"])*"', String), | ||
(r'@{1,2}\w+', Name.Variable), | ||
(r'(\<\w+)?\<(\w+\>?\,?\s?)+\>+', Name.Constant), | ||
], | ||
'whitespace': [ | ||
(r'\s+', Whitespace), | ||
], | ||
'barewords': [ | ||
(r'[a-z]\w*', Name), | ||
(r'(\d+\.\d+|\d+)', Number), | ||
], | ||
'operators': [ | ||
(r'[^0-9|\/|\-](\-\=|\+\=|\*\=|\\\=|\=|\=\=|\=\=\=|\+|\-|\*|\\|\+\=|\>|\<)[^\>|\/]', Operator), | ||
(r'(\(|\)|\,|\;|\=|\-|\+|\*|\/|\>|\<|\:)', Operator), | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
CREATE QUERY Member_Likeness(VERTEX <motionMember> m1, STRING inDate) FOR GRAPH motionData { | ||
# TYPEDEF TUPLE <x FLOAT, y FLOAT> XYPair; | ||
MapAccum<VERTEX<motionMember>, MapAccum<STRING, FLOAT>> @@likenessAccum; | ||
MapAccum<VERTEX<motionMember>, FLOAT> @@BirthYearAccum; | ||
MapAccum<VERTEX<motionMember>, FLOAT> @@HeightAccum; | ||
MapAccum<VERTEX<motionMember>, FLOAT> @@WeightAccum; | ||
ListAccum<VERTEX<location>> @@MemLocAccum; | ||
MapAccum<VERTEX<motionMember>, FLOAT> @@LocAccum; | ||
MapAccum<VERTEX<motionMember>, BagAccum<VERTEX<incentive>>> @@DayIncentives; | ||
MapAccum<VERTEX<motionMember>, MapAccum<STRING, FLOAT>> @@MemberStats; | ||
BagAccum<VERTEX<incentive>> @MemberIncentives; | ||
AvgAccum @@StepsAccum; | ||
AvgAccum @@BoutsAccum; | ||
AvgAccum @@MilesAccum; | ||
|
||
# Universal Vars | ||
INT lastMax = 0; | ||
INT lastMin = 1000; | ||
FLOAT mult; | ||
|
||
# Age Vars | ||
INT ageRange; | ||
INT birthYear; | ||
|
||
# Height Vars | ||
INT heightRange; | ||
INT height; | ||
|
||
# Height Vars | ||
INT weightRange; | ||
INT weight; | ||
|
||
# Location Vars | ||
FLOAT locRange; | ||
VERTEX memLoc; | ||
|
||
# Activity Vars | ||
DATETIME lastRecording; | ||
|
||
# lastRecording = to_datetime("2018-05-19"); | ||
lastRecording = to_datetime(inDate); | ||
|
||
/* | ||
|
||
test comment block | ||
|
||
*/ | ||
|
||
|
||
members = {motionMember.*}; | ||
|
||
birthYear = Get_Birth_Year(m1); | ||
height = m1.Height; | ||
weight = m1.Weight; | ||
temp = SELECT loc FROM members:member -(:e) - location: loc | ||
WHERE member == m1 | ||
ACCUM | ||
@@MemLocAccum += loc; | ||
FOREACH loc in @@MemLocAccum DO | ||
memLoc = loc; | ||
END; | ||
|
||
PRINT memLoc; | ||
PRINT birthYear; | ||
PRINT height; | ||
PRINT weight; | ||
|
||
results = SELECT member FROM members:member | ||
ACCUM | ||
@@BirthYearAccum += (member -> abs(Get_Birth_Year(member) - birthYear)); | ||
|
||
FOREACH (member,bys) in @@BirthYearAccum DO | ||
IF bys > lastMax THEN | ||
lastMax = bys; | ||
END; | ||
IF bys < lastMin THEN | ||
lastMin = bys; | ||
END; | ||
END; | ||
|
||
ageRange = lastMax - lastMin; | ||
print ageRange; | ||
mult = 1.0/ageRange; | ||
|
||
|
||
FOREACH (member,bys) in @@BirthYearAccum DO | ||
bys = 1 - bys * mult; | ||
@@likenessAccum += (member -> ("age" -> bys)); | ||
END; | ||
|
||
lastMax = 0; | ||
lastMin = 1000; | ||
mult = 0; | ||
|
||
results = SELECT member FROM members:member | ||
ACCUM | ||
@@HeightAccum += (member -> abs(member.Height - height)); | ||
|
||
FOREACH (member,heights) in @@HeightAccum DO | ||
IF heights < height THEN | ||
IF heights > lastMax THEN | ||
lastMax = heights; | ||
END; | ||
IF heights < lastMin THEN | ||
lastMin = heights; | ||
END; | ||
END; | ||
END; | ||
|
||
heightRange = lastMax - lastMin; | ||
print heightRange; | ||
mult = 1.0/heightRange; | ||
|
||
|
||
FOREACH (member,heights) in @@HeightAccum DO | ||
IF heights < height THEN | ||
heights = 1 - heights * mult; | ||
ELSE | ||
heights = 0; | ||
END; | ||
@@likenessAccum += (member -> ("height" -> heights)); | ||
END; | ||
|
||
lastMax = 0; | ||
lastMin = 1000; | ||
mult = 0; | ||
|
||
results = SELECT member FROM members:member | ||
ACCUM | ||
@@WeightAccum += (member -> abs(member.Weight - weight)); | ||
|
||
FOREACH (member,weights) in @@WeightAccum DO | ||
IF weights < weight THEN | ||
IF weights > lastMax THEN | ||
lastMax = weights; | ||
END; | ||
IF weights < lastMin THEN | ||
lastMin = weights; | ||
END; | ||
END; | ||
END; | ||
|
||
weightRange = lastMax - lastMin; | ||
print weightRange; | ||
mult = 1.0/weightRange; | ||
|
||
|
||
FOREACH (member,weights) in @@WeightAccum DO | ||
IF weights < weight THEN | ||
weights = 1 - weights * mult; | ||
ELSE | ||
weights = 0; | ||
END; | ||
@@likenessAccum += (member -> ("weight" -> weights)); | ||
END; | ||
|
||
lastMax = 0; | ||
lastMin = 1000; | ||
mult = 0; | ||
|
||
resultsLoc = SELECT loc FROM members:member -(:e) - location: loc | ||
ACCUM | ||
@@LocAccum += (member -> Check_Distance(loc,memLoc)); | ||
FOREACH (member,loc) in @@LocAccum DO | ||
IF loc < 5800 THEN | ||
IF loc > lastMax THEN | ||
lastMax = ceil(loc); | ||
END; | ||
IF loc < lastMin THEN | ||
lastMin = floor(loc); | ||
END; | ||
END; | ||
END; | ||
|
||
PRINT lastMax; | ||
PRINT lastMin; | ||
|
||
locRange = lastMax - lastMin; | ||
print locRange; | ||
mult = 1.0/locRange; | ||
|
||
FOREACH (member,loc) in @@LocAccum DO | ||
IF loc > 5800 THEN | ||
loc = -1; | ||
ELSE | ||
loc = 1 - loc * mult; | ||
END; | ||
@@likenessAccum += (member -> ("distance" -> loc)); | ||
END; | ||
|
||
lastMax = 0; | ||
lastMin = 1000; | ||
mult = 0; | ||
|
||
incentives = {incentive.*}; | ||
|
||
incentives = SELECT incent FROM incentives:incent - (:e) - lookupRule:rule WHERE | ||
rule.RuleName == "Tenacity" AND incent.IncentiveDate > datetime_sub(lastRecording, INTERVAL 1 MONTH) AND incent.IncentiveDate < lastRecording; | ||
|
||
members = SELECT member FROM incentives:incent - (:e) - motionMember:member | ||
ACCUM | ||
@@DayIncentives += (member -> (incent)); | ||
|
||
FOREACH (member, incent) IN @@DayIncentives DO | ||
@@StepsAccum = 0; | ||
FOREACH Incentive IN incent DO | ||
@@StepsAccum += Incentive.TotalSteps; | ||
@@BoutsAccum += Incentive.TotalBouts; | ||
@@MilesAccum += Incentive.Miles; | ||
END; | ||
@@MemberStats += (member -> ("stepsAvg" -> @@StepsAccum)); | ||
@@MemberStats += (member -> ("stepsSlope" -> Linear_Regression(incent, 1))); | ||
# @@MemberStats += (member -> ("boutsAvg" -> @@BoutsAccum)); | ||
# @@MemberStats += (member -> ("boutsSlope" -> Linear_Regression(incent, 2))); | ||
@@MemberStats += (member -> ("milesAvg" -> @@MilesAccum)); | ||
@@MemberStats += (member -> ("milesSlope" -> Linear_Regression(incent, 3))); | ||
END; | ||
|
||
|
||
PRINT @@MemberStats; | ||
PRINT @@likenessAccum; | ||
|
||
} |
Oops, something went wrong.