Skip to content

Commit

Permalink
sort keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed Sep 8, 2024
1 parent 8d9ec32 commit 76da846
Showing 1 changed file with 88 additions and 96 deletions.
184 changes: 88 additions & 96 deletions mo_sql_parsing/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,151 +15,146 @@
NULL = keyword("null") / SQL_NULL
TRUE = keyword("true") / True
FALSE = keyword("false") / False
NOCASE = keyword("nocase")
ASC = keyword("asc")
DESC = keyword("desc")

# SIMPLE KEYWORDS
AS = keyword("as").suppress()
# SIMPLE KEYWORDS AND OPERATORS
ADD = Literal("+").set_parser_name("add")
ALL = keyword("all")
AND = keyword("and")
APPLY = keyword("apply")
AS = keyword("as").suppress()
ASC = keyword("asc")
ASSIGN = Literal(":=").set_parser_name("assign")
BEGIN = keyword("begin").suppress()
BETWEEN = keyword("between")
BINARY_AND = Literal("&").set_parser_name("binary_and")
BINARY_NOT = Literal("~").set_parser_name("binary_not")
BINARY_OR = Literal("|").set_parser_name("binary_or")
BY = keyword("by").suppress()
CASE = keyword("case").suppress()
CLUSTER = keyword("cluster")
COLLATE = keyword("collate")
CONCAT = Literal("||").set_parser_name("concat")
CONSTRAINT = keyword("constraint").suppress()
CREATE = keyword("create").suppress()
CROSS = keyword("cross")
DEQ = (
# decisive equality
# https://sparkbyexamples.com/apache-hive/hive-relational-arithmetic-logical-operators/
Literal("<=>").set_parser_name("eq!")
)
DESC = keyword("desc")
DESCRIBE = keyword("describe")
DISTINCT = keyword("distinct")
EXPLAIN = keyword("explain")
DIV = Literal("/").set_parser_name("div")
EEQ = (
# conservative equality https://github.com/klahnakoski/jx-sqlite/blob/dev/docs/Logical%20Equality.md#definitions
Literal("==")
| Literal("=")
).set_parser_name("eq")
ELSE = keyword("else").suppress()
END = keyword("end").suppress()
EXCEPT = keyword("except")
EXPLAIN = keyword("explain")
FETCH = keyword("fetch").suppress()
FOR = keyword("for").suppress()
FOREIGN = keyword("foreign").suppress()
FROM = keyword("from").suppress()
FULL = keyword("full")
GROUP = keyword("group").suppress()
GT = Literal(">").set_parser_name("gt")
GTE = Literal(">=").set_parser_name("gte")
HAVING = keyword("having").suppress()
IDF = (
# decisive equality
# https://prestodb.io/docs/current/functions/comparison.html#is-distinct-from-and-is-not-distinct-from
keyword("is distinct from").set_parser_name("eq!")
)
ILIKE = keyword("ilike")
IN = keyword("in")
INDEX = keyword("index").suppress()
INDF = (
# decisive equality
# https://prestodb.io/docs/current/functions/comparison.html#is-distinct-from-and-is-not-distinct-from
keyword("is not distinct from").set_parser_name("ne!")
)
INNER = keyword("inner")
INTERSECT = keyword("intersect")
INTERVAL = keyword("interval")
INTO = keyword("into").suppress()
IS = keyword("is")
JOIN = keyword("join")
JSON_CONTAINS = Literal("?").set_parser_name("json_contains")
JSON_CONTAINS_ALL = Literal("?&").set_parser_name("json_contains_all")
JSON_CONTAINS_ANY = Literal("?|").set_parser_name("json_contains_any")
JSON_GET = Literal("->").set_parser_name("json_get")
JSON_GET_TEXT = Literal("->>").set_parser_name("json_get_text")
JSON_PATH = Literal("#>").set_parser_name("json_path")
JSON_PATH_DEL = Literal("#-").set_parser_name("json_path_del")
JSON_PATH_TEXT = Literal("#>>").set_parser_name("json_path_text")
JSON_SUBSUMED_BY = Literal("<@").set_parser_name("json_subsumed_by")
JSON_SUBSUMES = Literal("@>").set_parser_name("json_subsumes")
KEY = keyword("key").suppress()
LATERAL = keyword("lateral")
LEFT = keyword("left")
LIKE = keyword("like")
LIMIT = keyword("limit").suppress()
LT = Literal("<").set_parser_name("lt")
LTE = Literal("<=").set_parser_name("lte")
MINUS = keyword("minus")
MOD = Literal("%").set_parser_name("mod")
MUL = Literal("*").set_parser_name("mul")
NATURAL = keyword("natural")
NEG = Literal("-").set_parser_name("neg")
NEQ = (Literal("!=") | Literal("<>")).set_parser_name("neq")
NOCASE = keyword("nocase")
NOT = keyword("not")
NOT_REGEXP_I = Literal("!~*").set_parser_name("not_regexp_i")
OFFSET = keyword("offset").suppress()
ON = keyword("on").suppress()
OR = keyword("or")
ORDER = keyword("order").suppress()
OUTER = keyword("outer")
OVER = keyword("over").suppress()
PARTITION = keyword("partition").suppress()
# PERCENT = keyword("percent").suppress()
PIVOT = keyword("pivot")
POS = Literal("+").set_parser_name("pos")
PRIMARY = keyword("primary").suppress()
QUALIFY = keyword("qualify").suppress()
RECURSIVE = keyword("recursive").suppress()
REFERENCES = keyword("references").suppress()
_REGEXP = keyword("regexp")
REGEXP = (_REGEXP | Literal("~")).set_parser_name("regexp")
REGEXP_I = Literal("~*").set_parser_name("regexp_i")
RIGHT = keyword("right")
RLIKE = keyword("rlike")
SAMPLE = keyword("sample").suppress()
SELECT = keyword("select").suppress()
SET = keyword("set").suppress()
STRAIGHT_JOIN = keyword("straight_join")
_SIMILAR = keyword("similar")
SUB = Literal("-").set_parser_name("sub")
TABLE = keyword("table").suppress()
TABLESAMPLE = keyword("tablesample").suppress()
THEN = keyword("then").suppress()
TO = Keyword("to", caseless=True).suppress()
TOP = keyword("top").suppress()
UNION = keyword("union")
UNIQUE = keyword("unique").suppress()
UNNEST = keyword("unnest")
INTERSECT = keyword("intersect")
UNPIVOT = keyword("unpivot")
USING = keyword("using").suppress()
VALUES = keyword("values").suppress()
VIEW = keyword("view")
WHEN = keyword("when").suppress()
WHERE = keyword("where").suppress()
WINDOW = keyword("window")
WITH = keyword("with").suppress()
WITHIN = keyword("within").suppress()
PRIMARY = keyword("primary").suppress()
FOREIGN = keyword("foreign").suppress()
KEY = keyword("key").suppress()
UNIQUE = keyword("unique").suppress()
INDEX = keyword("index").suppress()
REFERENCES = keyword("references").suppress()
RECURSIVE = keyword("recursive").suppress()
VALUES = keyword("values").suppress()
WINDOW = keyword("window")

PRIMARY_KEY = Group(PRIMARY + KEY).set_parser_name("primary_key")
FOREIGN_KEY = Group(FOREIGN + KEY).set_parser_name("foreign_key")


# SIMPLE OPERATORS
CONCAT = Literal("||").set_parser_name("concat")
MUL = Literal("*").set_parser_name("mul")
DIV = Literal("/").set_parser_name("div")
MOD = Literal("%").set_parser_name("mod")
POS = Literal("+").set_parser_name("pos")
NEG = Literal("-").set_parser_name("neg")
ADD = Literal("+").set_parser_name("add")
SUB = Literal("-").set_parser_name("sub")
BINARY_NOT = Literal("~").set_parser_name("binary_not")
BINARY_AND = Literal("&").set_parser_name("binary_and")
BINARY_OR = Literal("|").set_parser_name("binary_or")
GTE = Literal(">=").set_parser_name("gte")
LTE = Literal("<=").set_parser_name("lte")
LT = Literal("<").set_parser_name("lt")
GT = Literal(">").set_parser_name("gt")
EEQ = (
# conservative equality https://github.com/klahnakoski/jx-sqlite/blob/dev/docs/Logical%20Equality.md#definitions
Literal("==")
| Literal("=")
).set_parser_name("eq")
DEQ = (
# decisive equality
# https://sparkbyexamples.com/apache-hive/hive-relational-arithmetic-logical-operators/
Literal("<=>").set_parser_name("eq!")
)
IDF = (
# decisive equality
# https://prestodb.io/docs/current/functions/comparison.html#is-distinct-from-and-is-not-distinct-from
keyword("is distinct from").set_parser_name("eq!")
)
INDF = (
# decisive equality
# https://prestodb.io/docs/current/functions/comparison.html#is-distinct-from-and-is-not-distinct-from
keyword("is not distinct from").set_parser_name("ne!")
)
REGEXP = (keyword("regexp") | Literal("~")).set_parser_name("regexp")
REGEXP_I = Literal("~*").set_parser_name("regexp_i")
NOT_REGEXP_I = Literal("!~*").set_parser_name("not_regexp_i")
NEQ = (Literal("!=") | Literal("<>")).set_parser_name("neq")
ASSIGN = Literal(":=").set_parser_name("assign")

JSON_GET = Literal("->").set_parser_name("json_get")
JSON_GET_TEXT = Literal("->>").set_parser_name("json_get_text")
JSON_PATH = Literal("#>").set_parser_name("json_path")
JSON_PATH_TEXT = Literal("#>>").set_parser_name("json_path_text")
JSON_SUBSUMES = Literal("@>").set_parser_name("json_subsumes")
JSON_SUBSUMED_BY = Literal("<@").set_parser_name("json_subsumed_by")
JSON_CONTAINS = Literal("?").set_parser_name("json_contains")
JSON_CONTAINS_ANY = Literal("?|").set_parser_name("json_contains_any")
JSON_CONTAINS_ALL = Literal("?&").set_parser_name("json_contains_all")
JSON_PATH_DEL = Literal("#-").set_parser_name("json_path_del")

AND = keyword("and")
APPLY = keyword("apply")
BEGIN = keyword("begin").suppress()
BETWEEN = keyword("between")
CASE = keyword("case").suppress()
COLLATE = keyword("collate")
END = keyword("end").suppress()
ELSE = keyword("else").suppress()
IN = keyword("in")
IS = keyword("is")
OR = keyword("or")
LATERAL = keyword("lateral")
PIVOT = keyword("pivot")
UNPIVOT = keyword("unpivot")
VIEW = keyword("view")

# COMPOUND KEYWORDS


PRIMARY_KEY = Group(PRIMARY + KEY).set_parser_name("primary_key")
FOREIGN_KEY = Group(FOREIGN + KEY).set_parser_name("foreign_key")
STRAIGHT_JOIN = keyword("straight_join")
joins = (
Literal(",").set_parser_name("cross_join") / "cross join"
| (
Expand Down Expand Up @@ -188,11 +183,8 @@
NOT_LIKE = Group(NOT + LIKE).set_parser_name("not_like")
NOT_RLIKE = Group(NOT + RLIKE).set_parser_name("not_rlike")
NOT_IN = Group(NOT + IN).set_parser_name("nin")
NOT_REGEXP = Group(NOT + keyword("regexp") | Literal("!~")).set_parser_name("not_regexp")
NOT_REGEXP = Group(NOT + _REGEXP | Literal("!~")).set_parser_name("not_regexp")
IS_NOT = Group(IS + NOT).set_parser_name("is_not")

_SIMILAR = keyword("similar")
TO = Keyword("to", caseless=True).suppress()
SIMILAR_TO = Group(_SIMILAR + TO).set_parser_name("similar_to")
NOT_SIMILAR_TO = Group(NOT + _SIMILAR + TO).set_parser_name("not_similar_to")

Expand Down

0 comments on commit 76da846

Please sign in to comment.