Skip to content

Introducing count_distinct() function #531

Closed Answered by barakalon
borsitsb asked this question in Q&A
Discussion options

You must be logged in to vote

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,
     …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tobymao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants