-
Hey, I work in Rollbar, and we created a modified SQL language called RQL, so our users can access their data easily. I analyze the queries to find possible ways to improve the system. I found the sqlglot library an elegant way to complete this task, but I need some help with the followings:
Cheers, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
2 options: (1) No custom dialect Anonymous expressions are "valid". You can read their name: import sqlglot
from sqlglot import expressions as exp
sqlglot.parse_one("SELECT count_distinct(x)").find(exp.Anonymous).name
# 'count_distinct' (2) Custom dialect If you want to parse it as a normal "COUNT(DISTINCT ...)" expression: import sqlglot
from sqlglot import expressions as exp
from sqlglot.dialects.postgres import Postgres
def _count_distinct(args):
return exp.Count.from_arg_list([exp.Distinct(this=args[0])])
class RQL(Postgres): # If RQL is based on another dialect, subclass it
class Parser(Postgres.Parser):
FUNCTIONS = {
**Postgres.Parser.FUNCTIONS,
"COUNT_DISTINCT": _count_distinct
}
sqlglot.parse_one("SELECT count_distinct(x)", read="rql")
# (SELECT expressions:
# (COUNT this:
# (DISTINCT this:
# (COLUMN this:
# (IDENTIFIER this: x, quoted: False))))) You can also parse it into a custom function class (e.g. |
Beta Was this translation helpful? Give feedback.
2 options:
(1) No custom dialect
Anonymous expressions are "valid". You can read their name:
(2) Custom dialect
If you want to parse it as a normal "COUNT(DISTINCT ...)" expression: