sqlglot.dialects.clickhouse API documentation

Edit on GitHub

# sqlglot.dialects.clickhouse

View Source

      1from __future__ import annotations
      2
      3import typing as t
      4
      5from sqlglot import exp, generator, parser, tokens
      6from sqlglot.dialects.dialect import (
      7    Dialect,
      8    inline_array_sql,
      9    no_pivot_sql,
     10    rename_func,
     11    var_map_sql,
     12)
     13from sqlglot.errors import ParseError
     14from sqlglot.parser import parse_var_map
     15from sqlglot.tokens import Token, TokenType
     16
     17
     18def _lower_func(sql: str) -> str:
     19    index = sql.index("(")
     20    return sql[:index].lower() + sql[index:]
     21
     22
     23class ClickHouse(Dialect):
     24    NORMALIZE_FUNCTIONS: bool | str = False
     25    NULL_ORDERING = "nulls_are_last"
     26    STRICT_STRING_CONCAT = True
     27
     28    class Tokenizer(tokens.Tokenizer):
     29        COMMENTS = ["--", "#", "#!", ("/*", "*/")]
     30        IDENTIFIERS = ['"', "`"]
     31        STRING_ESCAPES = ["'", "\\"]
     32        BIT_STRINGS = [("0b", "")]
     33        HEX_STRINGS = [("0x", ""), ("0X", "")]
     34
     35        KEYWORDS = {
     36            **tokens.Tokenizer.KEYWORDS,
     37            "ATTACH": TokenType.COMMAND,
     38            "DATETIME64": TokenType.DATETIME64,
     39            "DICTIONARY": TokenType.DICTIONARY,
     40            "FINAL": TokenType.FINAL,
     41            "FLOAT32": TokenType.FLOAT,
     42            "FLOAT64": TokenType.DOUBLE,
     43            "GLOBAL": TokenType.GLOBAL,
     44            "INT128": TokenType.INT128,
     45            "INT16": TokenType.SMALLINT,
     46            "INT256": TokenType.INT256,
     47            "INT32": TokenType.INT,
     48            "INT64": TokenType.BIGINT,
     49            "INT8": TokenType.TINYINT,
     50            "MAP": TokenType.MAP,
     51            "TUPLE": TokenType.STRUCT,
     52            "UINT128": TokenType.UINT128,
     53            "UINT16": TokenType.USMALLINT,
     54            "UINT256": TokenType.UINT256,
     55            "UINT32": TokenType.UINT,
     56            "UINT64": TokenType.UBIGINT,
     57            "UINT8": TokenType.UTINYINT,
     58        }
     59
     60    class Parser(parser.Parser):
     61        FUNCTIONS = {
     62            **parser.Parser.FUNCTIONS,
     63            "ANY": exp.AnyValue.from_arg_list,
     64            "MAP": parse_var_map,
     65            "MATCH": exp.RegexpLike.from_arg_list,
     66            "UNIQ": exp.ApproxDistinct.from_arg_list,
     67        }
     68
     69        FUNCTIONS_WITH_ALIASED_ARGS = {*parser.Parser.FUNCTIONS_WITH_ALIASED_ARGS, "TUPLE"}
     70
     71        FUNCTION_PARSERS = {
     72            **parser.Parser.FUNCTION_PARSERS,
     73            "QUANTILE": lambda self: self._parse_quantile(),
     74        }
     75
     76        FUNCTION_PARSERS.pop("MATCH")
     77
     78        NO_PAREN_FUNCTION_PARSERS = parser.Parser.NO_PAREN_FUNCTION_PARSERS.copy()
     79        NO_PAREN_FUNCTION_PARSERS.pop(TokenType.ANY)
     80
     81        RANGE_PARSERS = {
     82            **parser.Parser.RANGE_PARSERS,
     83            TokenType.GLOBAL: lambda self, this: self._match(TokenType.IN)
     84            and self._parse_in(this, is_global=True),
     85        }
     86
     87        # The PLACEHOLDER entry is popped because 1) it doesn't affect Clickhouse (it corresponds to
     88        # the postgres-specific JSONBContains parser) and 2) it makes parsing the ternary op simpler.
     89        COLUMN_OPERATORS = parser.Parser.COLUMN_OPERATORS.copy()
     90        COLUMN_OPERATORS.pop(TokenType.PLACEHOLDER)
     91
     92        JOIN_KINDS = {
     93            *parser.Parser.JOIN_KINDS,
     94            TokenType.ANY,
     95            TokenType.ASOF,
     96            TokenType.ANTI,
     97            TokenType.SEMI,
     98        }
     99
    100        TABLE_ALIAS_TOKENS = {*parser.Parser.TABLE_ALIAS_TOKENS} - {
    101            TokenType.ANY,
    102            TokenType.SEMI,
    103            TokenType.ANTI,
    104            TokenType.SETTINGS,
    105            TokenType.FORMAT,
    106        }
    107
    108        LOG_DEFAULTS_TO_LN = True
    109
    110        QUERY_MODIFIER_PARSERS = {
    111            **parser.Parser.QUERY_MODIFIER_PARSERS,
    112            "settings": lambda self: self._parse_csv(self._parse_conjunction)
    113            if self._match(TokenType.SETTINGS)
    114            else None,
    115            "format": lambda self: self._parse_id_var() if self._match(TokenType.FORMAT) else None,
    116        }
    117
    118        def _parse_conjunction(self) -> t.Optional[exp.Expression]:
    119            this = super()._parse_conjunction()
    120
    121            if self._match(TokenType.PLACEHOLDER):
    122                return self.expression(
    123                    exp.If,
    124                    this=this,
    125                    true=self._parse_conjunction(),
    126                    false=self._match(TokenType.COLON) and self._parse_conjunction(),
    127                )
    128
    129            return this
    130
    131        def _parse_placeholder(self) -> t.Optional[exp.Expression]:
    132            """
    133            Parse a placeholder expression like SELECT {abc: UInt32} or FROM {table: Identifier}
    134            https://clickhouse.com/docs/en/sql-reference/syntax#defining-and-using-query-parameters
    135            """
    136            if not self._match(TokenType.L_BRACE):
    137                return None
    138
    139            this = self._parse_id_var()
    140            self._match(TokenType.COLON)
    141            kind = self._parse_types(check_func=False) or (
    142                self._match_text_seq("IDENTIFIER") and "Identifier"
    143            )
    144
    145            if not kind:
    146                self.raise_error("Expecting a placeholder type or 'Identifier' for tables")
    147            elif not self._match(TokenType.R_BRACE):
    148                self.raise_error("Expecting }")
    149
    150            return self.expression(exp.Placeholder, this=this, kind=kind)
    151
    152        def _parse_in(self, this: t.Optional[exp.Expression], is_global: bool = False) -> exp.In:
    153            this = super()._parse_in(this)
    154            this.set("is_global", is_global)
    155            return this
    156
    157        def _parse_table(
    158            self, schema: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None
    159        ) -> t.Optional[exp.Expression]:
    160            this = super()._parse_table(schema=schema, alias_tokens=alias_tokens)
    161
    162            if self._match(TokenType.FINAL):
    163                this = self.expression(exp.Final, this=this)
    164
    165            return this
    166
    167        def _parse_position(self, haystack_first: bool = False) -> exp.StrPosition:
    168            return super()._parse_position(haystack_first=True)
    169
    170        # https://clickhouse.com/docs/en/sql-reference/statements/select/with/
    171        def _parse_cte(self) -> exp.CTE:
    172            index = self._index
    173            try:
    174                # WITH <identifier> AS <subquery expression>
    175                return super()._parse_cte()
    176            except ParseError:
    177                # WITH <expression> AS <identifier>
    178                self._retreat(index)
    179                statement = self._parse_statement()
    180
    181                if statement and isinstance(statement.this, exp.Alias):
    182                    self.raise_error("Expected CTE to have alias")
    183
    184                return self.expression(exp.CTE, this=statement, alias=statement and statement.this)
    185
    186        def _parse_join_parts(
    187            self,
    188        ) -> t.Tuple[t.Optional[Token], t.Optional[Token], t.Optional[Token]]:
    189            is_global = self._match(TokenType.GLOBAL) and self._prev
    190            kind_pre = self._match_set(self.JOIN_KINDS, advance=False) and self._prev
    191
    192            if kind_pre:
    193                kind = self._match_set(self.JOIN_KINDS) and self._prev
    194                side = self._match_set(self.JOIN_SIDES) and self._prev
    195                return is_global, side, kind
    196
    197            return (
    198                is_global,
    199                self._match_set(self.JOIN_SIDES) and self._prev,
    200                self._match_set(self.JOIN_KINDS) and self._prev,
    201            )
    202
    203        def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]:
    204            join = super()._parse_join(skip_join_token)
    205
    206            if join:
    207                join.set("global", join.args.pop("method", None))
    208            return join
    209
    210        def _parse_function(
    211            self,
    212            functions: t.Optional[t.Dict[str, t.Callable]] = None,
    213            anonymous: bool = False,
    214            optional_parens: bool = True,
    215        ) -> t.Optional[exp.Expression]:
    216            func = super()._parse_function(
    217                functions=functions, anonymous=anonymous, optional_parens=optional_parens
    218            )
    219
    220            if isinstance(func, exp.Anonymous):
    221                params = self._parse_func_params(func)
    222
    223                if params:
    224                    return self.expression(
    225                        exp.ParameterizedAgg,
    226                        this=func.this,
    227                        expressions=func.expressions,
    228                        params=params,
    229                    )
    230
    231            return func
    232
    233        def _parse_func_params(
    234            self, this: t.Optional[exp.Func] = None
    235        ) -> t.Optional[t.List[t.Optional[exp.Expression]]]:
    236            if self._match_pair(TokenType.R_PAREN, TokenType.L_PAREN):
    237                return self._parse_csv(self._parse_lambda)
    238
    239            if self._match(TokenType.L_PAREN):
    240                params = self._parse_csv(self._parse_lambda)
    241                self._match_r_paren(this)
    242                return params
    243
    244            return None
    245
    246        def _parse_quantile(self) -> exp.Quantile:
    247            this = self._parse_lambda()
    248            params = self._parse_func_params()
    249            if params:
    250                return self.expression(exp.Quantile, this=params[0], quantile=this)
    251            return self.expression(exp.Quantile, this=this, quantile=exp.Literal.number(0.5))
    252
    253        def _parse_wrapped_id_vars(
    254            self, optional: bool = False
    255        ) -> t.List[t.Optional[exp.Expression]]:
    256            return super()._parse_wrapped_id_vars(optional=True)
    257
    258        def _parse_primary_key(
    259            self, wrapped_optional: bool = False, in_props: bool = False
    260        ) -> exp.PrimaryKeyColumnConstraint | exp.PrimaryKey:
    261            return super()._parse_primary_key(
    262                wrapped_optional=wrapped_optional or in_props, in_props=in_props
    263            )
    264
    265        def _parse_on_property(self) -> t.Optional[exp.Expression]:
    266            index = self._index
    267            if self._match_text_seq("CLUSTER"):
    268                this = self._parse_id_var()
    269                if this:
    270                    return self.expression(exp.OnCluster, this=this)
    271                else:
    272                    self._retreat(index)
    273            return None
    274
    275    class Generator(generator.Generator):
    276        STRUCT_DELIMITER = ("(", ")")
    277
    278        TYPE_MAPPING = {
    279            **generator.Generator.TYPE_MAPPING,
    280            exp.DataType.Type.ARRAY: "Array",
    281            exp.DataType.Type.BIGINT: "Int64",
    282            exp.DataType.Type.DATETIME64: "DateTime64",
    283            exp.DataType.Type.DOUBLE: "Float64",
    284            exp.DataType.Type.FLOAT: "Float32",
    285            exp.DataType.Type.INT: "Int32",
    286            exp.DataType.Type.INT128: "Int128",
    287            exp.DataType.Type.INT256: "Int256",
    288            exp.DataType.Type.MAP: "Map",
    289            exp.DataType.Type.NULLABLE: "Nullable",
    290            exp.DataType.Type.SMALLINT: "Int16",
    291            exp.DataType.Type.STRUCT: "Tuple",
    292            exp.DataType.Type.TINYINT: "Int8",
    293            exp.DataType.Type.UBIGINT: "UInt64",
    294            exp.DataType.Type.UINT: "UInt32",
    295            exp.DataType.Type.UINT128: "UInt128",
    296            exp.DataType.Type.UINT256: "UInt256",
    297            exp.DataType.Type.USMALLINT: "UInt16",
    298            exp.DataType.Type.UTINYINT: "UInt8",
    299        }
    300
    301        TRANSFORMS = {
    302            **generator.Generator.TRANSFORMS,
    303            exp.AnyValue: rename_func("any"),
    304            exp.ApproxDistinct: rename_func("uniq"),
    305            exp.Array: inline_array_sql,
    306            exp.CastToStrType: rename_func("CAST"),
    307            exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL",
    308            exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)),
    309            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    310            exp.Pivot: no_pivot_sql,
    311            exp.Quantile: lambda self, e: self.func("quantile", e.args.get("quantile"))
    312            + f"({self.sql(e, 'this')})",
    313            exp.RegexpLike: lambda self, e: f"match({self.format_args(e.this, e.expression)})",
    314            exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})",
    315            exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)),
    316        }
    317
    318        PROPERTIES_LOCATION = {
    319            **generator.Generator.PROPERTIES_LOCATION,
    320            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    321            exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA,
    322            exp.OnCluster: exp.Properties.Location.POST_NAME,
    323        }
    324
    325        JOIN_HINTS = False
    326        TABLE_HINTS = False
    327        EXPLICIT_UNION = True
    328        GROUPINGS_SEP = ""
    329
    330        # there's no list in docs, but it can be found in Clickhouse code
    331        # see `ClickHouse/src/Parsers/ParserCreate*.cpp`
    332        ON_CLUSTER_TARGETS = {
    333            "DATABASE",
    334            "TABLE",
    335            "VIEW",
    336            "DICTIONARY",
    337            "INDEX",
    338            "FUNCTION",
    339            "NAMED COLLECTION",
    340        }
    341
    342        def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    343            # Clickhouse errors out if we try to cast a NULL value to TEXT
    344            return self.func(
    345                "CONCAT",
    346                *[
    347                    exp.func("if", e.is_(exp.null()), e, exp.cast(e, "text"))
    348                    for e in t.cast(t.List[exp.Condition], expression.expressions)
    349                ],
    350            )
    351
    352        def cte_sql(self, expression: exp.CTE) -> str:
    353            if isinstance(expression.this, exp.Alias):
    354                return self.sql(expression, "this")
    355
    356            return super().cte_sql(expression)
    357
    358        def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    359            return super().after_limit_modifiers(expression) + [
    360                self.seg("SETTINGS ") + self.expressions(expression, key="settings", flat=True)
    361                if expression.args.get("settings")
    362                else "",
    363                self.seg("FORMAT ") + self.sql(expression, "format")
    364                if expression.args.get("format")
    365                else "",
    366            ]
    367
    368        def parameterizedagg_sql(self, expression: exp.Anonymous) -> str:
    369            params = self.expressions(expression, "params", flat=True)
    370            return self.func(expression.name, *expression.expressions) + f"({params})"
    371
    372        def placeholder_sql(self, expression: exp.Placeholder) -> str:
    373            return f"{{{expression.name}: {self.sql(expression, 'kind')}}}"
    374
    375        def oncluster_sql(self, expression: exp.OnCluster) -> str:
    376            return f"ON CLUSTER {self.sql(expression, 'this')}"
    377
    378        def createable_sql(
    379            self,
    380            expression: exp.Create,
    381            locations: dict[exp.Properties.Location, list[exp.Property]],
    382        ) -> str:
    383            kind = self.sql(expression, "kind").upper()
    384            if kind in self.ON_CLUSTER_TARGETS and locations.get(exp.Properties.Location.POST_NAME):
    385                this_name = self.sql(expression.this, "this")
    386                this_properties = " ".join(
    387                    [self.sql(prop) for prop in locations[exp.Properties.Location.POST_NAME]]
    388                )
    389                this_schema = self.schema_columns_sql(expression.this)
    390                return f"{this_name}{self.sep()}{this_properties}{self.sep()}{this_schema}"
    391            return super().createable_sql(expression, locations)

class ClickHouse(sqlglot.dialects.dialect.Dialect): View Source

     24class ClickHouse(Dialect):
     25    NORMALIZE_FUNCTIONS: bool | str = False
     26    NULL_ORDERING = "nulls_are_last"
     27    STRICT_STRING_CONCAT = True
     28
     29    class Tokenizer(tokens.Tokenizer):
     30        COMMENTS = ["--", "#", "#!", ("/*", "*/")]
     31        IDENTIFIERS = ['"', "`"]
     32        STRING_ESCAPES = ["'", "\\"]
     33        BIT_STRINGS = [("0b", "")]
     34        HEX_STRINGS = [("0x", ""), ("0X", "")]
     35
     36        KEYWORDS = {
     37            **tokens.Tokenizer.KEYWORDS,
     38            "ATTACH": TokenType.COMMAND,
     39            "DATETIME64": TokenType.DATETIME64,
     40            "DICTIONARY": TokenType.DICTIONARY,
     41            "FINAL": TokenType.FINAL,
     42            "FLOAT32": TokenType.FLOAT,
     43            "FLOAT64": TokenType.DOUBLE,
     44            "GLOBAL": TokenType.GLOBAL,
     45            "INT128": TokenType.INT128,
     46            "INT16": TokenType.SMALLINT,
     47            "INT256": TokenType.INT256,
     48            "INT32": TokenType.INT,
     49            "INT64": TokenType.BIGINT,
     50            "INT8": TokenType.TINYINT,
     51            "MAP": TokenType.MAP,
     52            "TUPLE": TokenType.STRUCT,
     53            "UINT128": TokenType.UINT128,
     54            "UINT16": TokenType.USMALLINT,
     55            "UINT256": TokenType.UINT256,
     56            "UINT32": TokenType.UINT,
     57            "UINT64": TokenType.UBIGINT,
     58            "UINT8": TokenType.UTINYINT,
     59        }
     60
     61    class Parser(parser.Parser):
     62        FUNCTIONS = {
     63            **parser.Parser.FUNCTIONS,
     64            "ANY": exp.AnyValue.from_arg_list,
     65            "MAP": parse_var_map,
     66            "MATCH": exp.RegexpLike.from_arg_list,
     67            "UNIQ": exp.ApproxDistinct.from_arg_list,
     68        }
     69
     70        FUNCTIONS_WITH_ALIASED_ARGS = {*parser.Parser.FUNCTIONS_WITH_ALIASED_ARGS, "TUPLE"}
     71
     72        FUNCTION_PARSERS = {
     73            **parser.Parser.FUNCTION_PARSERS,
     74            "QUANTILE": lambda self: self._parse_quantile(),
     75        }
     76
     77        FUNCTION_PARSERS.pop("MATCH")
     78
     79        NO_PAREN_FUNCTION_PARSERS = parser.Parser.NO_PAREN_FUNCTION_PARSERS.copy()
     80        NO_PAREN_FUNCTION_PARSERS.pop(TokenType.ANY)
     81
     82        RANGE_PARSERS = {
     83            **parser.Parser.RANGE_PARSERS,
     84            TokenType.GLOBAL: lambda self, this: self._match(TokenType.IN)
     85            and self._parse_in(this, is_global=True),
     86        }
     87
     88        # The PLACEHOLDER entry is popped because 1) it doesn't affect Clickhouse (it corresponds to
     89        # the postgres-specific JSONBContains parser) and 2) it makes parsing the ternary op simpler.
     90        COLUMN_OPERATORS = parser.Parser.COLUMN_OPERATORS.copy()
     91        COLUMN_OPERATORS.pop(TokenType.PLACEHOLDER)
     92
     93        JOIN_KINDS = {
     94            *parser.Parser.JOIN_KINDS,
     95            TokenType.ANY,
     96            TokenType.ASOF,
     97            TokenType.ANTI,
     98            TokenType.SEMI,
     99        }
    100
    101        TABLE_ALIAS_TOKENS = {*parser.Parser.TABLE_ALIAS_TOKENS} - {
    102            TokenType.ANY,
    103            TokenType.SEMI,
    104            TokenType.ANTI,
    105            TokenType.SETTINGS,
    106            TokenType.FORMAT,
    107        }
    108
    109        LOG_DEFAULTS_TO_LN = True
    110
    111        QUERY_MODIFIER_PARSERS = {
    112            **parser.Parser.QUERY_MODIFIER_PARSERS,
    113            "settings": lambda self: self._parse_csv(self._parse_conjunction)
    114            if self._match(TokenType.SETTINGS)
    115            else None,
    116            "format": lambda self: self._parse_id_var() if self._match(TokenType.FORMAT) else None,
    117        }
    118
    119        def _parse_conjunction(self) -> t.Optional[exp.Expression]:
    120            this = super()._parse_conjunction()
    121
    122            if self._match(TokenType.PLACEHOLDER):
    123                return self.expression(
    124                    exp.If,
    125                    this=this,
    126                    true=self._parse_conjunction(),
    127                    false=self._match(TokenType.COLON) and self._parse_conjunction(),
    128                )
    129
    130            return this
    131
    132        def _parse_placeholder(self) -> t.Optional[exp.Expression]:
    133            """
    134            Parse a placeholder expression like SELECT {abc: UInt32} or FROM {table: Identifier}
    135            https://clickhouse.com/docs/en/sql-reference/syntax#defining-and-using-query-parameters
    136            """
    137            if not self._match(TokenType.L_BRACE):
    138                return None
    139
    140            this = self._parse_id_var()
    141            self._match(TokenType.COLON)
    142            kind = self._parse_types(check_func=False) or (
    143                self._match_text_seq("IDENTIFIER") and "Identifier"
    144            )
    145
    146            if not kind:
    147                self.raise_error("Expecting a placeholder type or 'Identifier' for tables")
    148            elif not self._match(TokenType.R_BRACE):
    149                self.raise_error("Expecting }")
    150
    151            return self.expression(exp.Placeholder, this=this, kind=kind)
    152
    153        def _parse_in(self, this: t.Optional[exp.Expression], is_global: bool = False) -> exp.In:
    154            this = super()._parse_in(this)
    155            this.set("is_global", is_global)
    156            return this
    157
    158        def _parse_table(
    159            self, schema: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None
    160        ) -> t.Optional[exp.Expression]:
    161            this = super()._parse_table(schema=schema, alias_tokens=alias_tokens)
    162
    163            if self._match(TokenType.FINAL):
    164                this = self.expression(exp.Final, this=this)
    165
    166            return this
    167
    168        def _parse_position(self, haystack_first: bool = False) -> exp.StrPosition:
    169            return super()._parse_position(haystack_first=True)
    170
    171        # https://clickhouse.com/docs/en/sql-reference/statements/select/with/
    172        def _parse_cte(self) -> exp.CTE:
    173            index = self._index
    174            try:
    175                # WITH <identifier> AS <subquery expression>
    176                return super()._parse_cte()
    177            except ParseError:
    178                # WITH <expression> AS <identifier>
    179                self._retreat(index)
    180                statement = self._parse_statement()
    181
    182                if statement and isinstance(statement.this, exp.Alias):
    183                    self.raise_error("Expected CTE to have alias")
    184
    185                return self.expression(exp.CTE, this=statement, alias=statement and statement.this)
    186
    187        def _parse_join_parts(
    188            self,
    189        ) -> t.Tuple[t.Optional[Token], t.Optional[Token], t.Optional[Token]]:
    190            is_global = self._match(TokenType.GLOBAL) and self._prev
    191            kind_pre = self._match_set(self.JOIN_KINDS, advance=False) and self._prev
    192
    193            if kind_pre:
    194                kind = self._match_set(self.JOIN_KINDS) and self._prev
    195                side = self._match_set(self.JOIN_SIDES) and self._prev
    196                return is_global, side, kind
    197
    198            return (
    199                is_global,
    200                self._match_set(self.JOIN_SIDES) and self._prev,
    201                self._match_set(self.JOIN_KINDS) and self._prev,
    202            )
    203
    204        def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]:
    205            join = super()._parse_join(skip_join_token)
    206
    207            if join:
    208                join.set("global", join.args.pop("method", None))
    209            return join
    210
    211        def _parse_function(
    212            self,
    213            functions: t.Optional[t.Dict[str, t.Callable]] = None,
    214            anonymous: bool = False,
    215            optional_parens: bool = True,
    216        ) -> t.Optional[exp.Expression]:
    217            func = super()._parse_function(
    218                functions=functions, anonymous=anonymous, optional_parens=optional_parens
    219            )
    220
    221            if isinstance(func, exp.Anonymous):
    222                params = self._parse_func_params(func)
    223
    224                if params:
    225                    return self.expression(
    226                        exp.ParameterizedAgg,
    227                        this=func.this,
    228                        expressions=func.expressions,
    229                        params=params,
    230                    )
    231
    232            return func
    233
    234        def _parse_func_params(
    235            self, this: t.Optional[exp.Func] = None
    236        ) -> t.Optional[t.List[t.Optional[exp.Expression]]]:
    237            if self._match_pair(TokenType.R_PAREN, TokenType.L_PAREN):
    238                return self._parse_csv(self._parse_lambda)
    239
    240            if self._match(TokenType.L_PAREN):
    241                params = self._parse_csv(self._parse_lambda)
    242                self._match_r_paren(this)
    243                return params
    244
    245            return None
    246
    247        def _parse_quantile(self) -> exp.Quantile:
    248            this = self._parse_lambda()
    249            params = self._parse_func_params()
    250            if params:
    251                return self.expression(exp.Quantile, this=params[0], quantile=this)
    252            return self.expression(exp.Quantile, this=this, quantile=exp.Literal.number(0.5))
    253
    254        def _parse_wrapped_id_vars(
    255            self, optional: bool = False
    256        ) -> t.List[t.Optional[exp.Expression]]:
    257            return super()._parse_wrapped_id_vars(optional=True)
    258
    259        def _parse_primary_key(
    260            self, wrapped_optional: bool = False, in_props: bool = False
    261        ) -> exp.PrimaryKeyColumnConstraint | exp.PrimaryKey:
    262            return super()._parse_primary_key(
    263                wrapped_optional=wrapped_optional or in_props, in_props=in_props
    264            )
    265
    266        def _parse_on_property(self) -> t.Optional[exp.Expression]:
    267            index = self._index
    268            if self._match_text_seq("CLUSTER"):
    269                this = self._parse_id_var()
    270                if this:
    271                    return self.expression(exp.OnCluster, this=this)
    272                else:
    273                    self._retreat(index)
    274            return None
    275
    276    class Generator(generator.Generator):
    277        STRUCT_DELIMITER = ("(", ")")
    278
    279        TYPE_MAPPING = {
    280            **generator.Generator.TYPE_MAPPING,
    281            exp.DataType.Type.ARRAY: "Array",
    282            exp.DataType.Type.BIGINT: "Int64",
    283            exp.DataType.Type.DATETIME64: "DateTime64",
    284            exp.DataType.Type.DOUBLE: "Float64",
    285            exp.DataType.Type.FLOAT: "Float32",
    286            exp.DataType.Type.INT: "Int32",
    287            exp.DataType.Type.INT128: "Int128",
    288            exp.DataType.Type.INT256: "Int256",
    289            exp.DataType.Type.MAP: "Map",
    290            exp.DataType.Type.NULLABLE: "Nullable",
    291            exp.DataType.Type.SMALLINT: "Int16",
    292            exp.DataType.Type.STRUCT: "Tuple",
    293            exp.DataType.Type.TINYINT: "Int8",
    294            exp.DataType.Type.UBIGINT: "UInt64",
    295            exp.DataType.Type.UINT: "UInt32",
    296            exp.DataType.Type.UINT128: "UInt128",
    297            exp.DataType.Type.UINT256: "UInt256",
    298            exp.DataType.Type.USMALLINT: "UInt16",
    299            exp.DataType.Type.UTINYINT: "UInt8",
    300        }
    301
    302        TRANSFORMS = {
    303            **generator.Generator.TRANSFORMS,
    304            exp.AnyValue: rename_func("any"),
    305            exp.ApproxDistinct: rename_func("uniq"),
    306            exp.Array: inline_array_sql,
    307            exp.CastToStrType: rename_func("CAST"),
    308            exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL",
    309            exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)),
    310            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    311            exp.Pivot: no_pivot_sql,
    312            exp.Quantile: lambda self, e: self.func("quantile", e.args.get("quantile"))
    313            + f"({self.sql(e, 'this')})",
    314            exp.RegexpLike: lambda self, e: f"match({self.format_args(e.this, e.expression)})",
    315            exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})",
    316            exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)),
    317        }
    318
    319        PROPERTIES_LOCATION = {
    320            **generator.Generator.PROPERTIES_LOCATION,
    321            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    322            exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA,
    323            exp.OnCluster: exp.Properties.Location.POST_NAME,
    324        }
    325
    326        JOIN_HINTS = False
    327        TABLE_HINTS = False
    328        EXPLICIT_UNION = True
    329        GROUPINGS_SEP = ""
    330
    331        # there's no list in docs, but it can be found in Clickhouse code
    332        # see `ClickHouse/src/Parsers/ParserCreate*.cpp`
    333        ON_CLUSTER_TARGETS = {
    334            "DATABASE",
    335            "TABLE",
    336            "VIEW",
    337            "DICTIONARY",
    338            "INDEX",
    339            "FUNCTION",
    340            "NAMED COLLECTION",
    341        }
    342
    343        def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    344            # Clickhouse errors out if we try to cast a NULL value to TEXT
    345            return self.func(
    346                "CONCAT",
    347                *[
    348                    exp.func("if", e.is_(exp.null()), e, exp.cast(e, "text"))
    349                    for e in t.cast(t.List[exp.Condition], expression.expressions)
    350                ],
    351            )
    352
    353        def cte_sql(self, expression: exp.CTE) -> str:
    354            if isinstance(expression.this, exp.Alias):
    355                return self.sql(expression, "this")
    356
    357            return super().cte_sql(expression)
    358
    359        def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    360            return super().after_limit_modifiers(expression) + [
    361                self.seg("SETTINGS ") + self.expressions(expression, key="settings", flat=True)
    362                if expression.args.get("settings")
    363                else "",
    364                self.seg("FORMAT ") + self.sql(expression, "format")
    365                if expression.args.get("format")
    366                else "",
    367            ]
    368
    369        def parameterizedagg_sql(self, expression: exp.Anonymous) -> str:
    370            params = self.expressions(expression, "params", flat=True)
    371            return self.func(expression.name, *expression.expressions) + f"({params})"
    372
    373        def placeholder_sql(self, expression: exp.Placeholder) -> str:
    374            return f"{{{expression.name}: {self.sql(expression, 'kind')}}}"
    375
    376        def oncluster_sql(self, expression: exp.OnCluster) -> str:
    377            return f"ON CLUSTER {self.sql(expression, 'this')}"
    378
    379        def createable_sql(
    380            self,
    381            expression: exp.Create,
    382            locations: dict[exp.Properties.Location, list[exp.Property]],
    383        ) -> str:
    384            kind = self.sql(expression, "kind").upper()
    385            if kind in self.ON_CLUSTER_TARGETS and locations.get(exp.Properties.Location.POST_NAME):
    386                this_name = self.sql(expression.this, "this")
    387                this_properties = " ".join(
    388                    [self.sql(prop) for prop in locations[exp.Properties.Location.POST_NAME]]
    389                )
    390                this_schema = self.schema_columns_sql(expression.this)
    391                return f"{this_name}{self.sep()}{this_properties}{self.sep()}{this_schema}"
    392            return super().createable_sql(expression, locations)

##### Inherited Members

sqlglot.dialects.dialect.Dialect
  get_or_raise
  format_time
  normalize_identifier
  case_sensitive
  can_identify
  quote_identifier
  parse
  parse_into
  generate
  transpile
  tokenize
  parser
  generator

class ClickHouse.Tokenizer(sqlglot.tokens.Tokenizer): View Source

    29    class Tokenizer(tokens.Tokenizer):
    30        COMMENTS = ["--", "#", "#!", ("/*", "*/")]
    31        IDENTIFIERS = ['"', "`"]
    32        STRING_ESCAPES = ["'", "\\"]
    33        BIT_STRINGS = [("0b", "")]
    34        HEX_STRINGS = [("0x", ""), ("0X", "")]
    35
    36        KEYWORDS = {
    37            **tokens.Tokenizer.KEYWORDS,
    38            "ATTACH": TokenType.COMMAND,
    39            "DATETIME64": TokenType.DATETIME64,
    40            "DICTIONARY": TokenType.DICTIONARY,
    41            "FINAL": TokenType.FINAL,
    42            "FLOAT32": TokenType.FLOAT,
    43            "FLOAT64": TokenType.DOUBLE,
    44            "GLOBAL": TokenType.GLOBAL,
    45            "INT128": TokenType.INT128,
    46            "INT16": TokenType.SMALLINT,
    47            "INT256": TokenType.INT256,
    48            "INT32": TokenType.INT,
    49            "INT64": TokenType.BIGINT,
    50            "INT8": TokenType.TINYINT,
    51            "MAP": TokenType.MAP,
    52            "TUPLE": TokenType.STRUCT,
    53            "UINT128": TokenType.UINT128,
    54            "UINT16": TokenType.USMALLINT,
    55            "UINT256": TokenType.UINT256,
    56            "UINT32": TokenType.UINT,
    57            "UINT64": TokenType.UBIGINT,
    58            "UINT8": TokenType.UTINYINT,
    59        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize
  peek

class ClickHouse.Parser(sqlglot.parser.Parser): View Source

     61    class Parser(parser.Parser):
     62        FUNCTIONS = {
     63            **parser.Parser.FUNCTIONS,
     64            "ANY": exp.AnyValue.from_arg_list,
     65            "MAP": parse_var_map,
     66            "MATCH": exp.RegexpLike.from_arg_list,
     67            "UNIQ": exp.ApproxDistinct.from_arg_list,
     68        }
     69
     70        FUNCTIONS_WITH_ALIASED_ARGS = {*parser.Parser.FUNCTIONS_WITH_ALIASED_ARGS, "TUPLE"}
     71
     72        FUNCTION_PARSERS = {
     73            **parser.Parser.FUNCTION_PARSERS,
     74            "QUANTILE": lambda self: self._parse_quantile(),
     75        }
     76
     77        FUNCTION_PARSERS.pop("MATCH")
     78
     79        NO_PAREN_FUNCTION_PARSERS = parser.Parser.NO_PAREN_FUNCTION_PARSERS.copy()
     80        NO_PAREN_FUNCTION_PARSERS.pop(TokenType.ANY)
     81
     82        RANGE_PARSERS = {
     83            **parser.Parser.RANGE_PARSERS,
     84            TokenType.GLOBAL: lambda self, this: self._match(TokenType.IN)
     85            and self._parse_in(this, is_global=True),
     86        }
     87
     88        # The PLACEHOLDER entry is popped because 1) it doesn't affect Clickhouse (it corresponds to
     89        # the postgres-specific JSONBContains parser) and 2) it makes parsing the ternary op simpler.
     90        COLUMN_OPERATORS = parser.Parser.COLUMN_OPERATORS.copy()
     91        COLUMN_OPERATORS.pop(TokenType.PLACEHOLDER)
     92
     93        JOIN_KINDS = {
     94            *parser.Parser.JOIN_KINDS,
     95            TokenType.ANY,
     96            TokenType.ASOF,
     97            TokenType.ANTI,
     98            TokenType.SEMI,
     99        }
    100
    101        TABLE_ALIAS_TOKENS = {*parser.Parser.TABLE_ALIAS_TOKENS} - {
    102            TokenType.ANY,
    103            TokenType.SEMI,
    104            TokenType.ANTI,
    105            TokenType.SETTINGS,
    106            TokenType.FORMAT,
    107        }
    108
    109        LOG_DEFAULTS_TO_LN = True
    110
    111        QUERY_MODIFIER_PARSERS = {
    112            **parser.Parser.QUERY_MODIFIER_PARSERS,
    113            "settings": lambda self: self._parse_csv(self._parse_conjunction)
    114            if self._match(TokenType.SETTINGS)
    115            else None,
    116            "format": lambda self: self._parse_id_var() if self._match(TokenType.FORMAT) else None,
    117        }
    118
    119        def _parse_conjunction(self) -> t.Optional[exp.Expression]:
    120            this = super()._parse_conjunction()
    121
    122            if self._match(TokenType.PLACEHOLDER):
    123                return self.expression(
    124                    exp.If,
    125                    this=this,
    126                    true=self._parse_conjunction(),
    127                    false=self._match(TokenType.COLON) and self._parse_conjunction(),
    128                )
    129
    130            return this
    131
    132        def _parse_placeholder(self) -> t.Optional[exp.Expression]:
    133            """
    134            Parse a placeholder expression like SELECT {abc: UInt32} or FROM {table: Identifier}
    135            https://clickhouse.com/docs/en/sql-reference/syntax#defining-and-using-query-parameters
    136            """
    137            if not self._match(TokenType.L_BRACE):
    138                return None
    139
    140            this = self._parse_id_var()
    141            self._match(TokenType.COLON)
    142            kind = self._parse_types(check_func=False) or (
    143                self._match_text_seq("IDENTIFIER") and "Identifier"
    144            )
    145
    146            if not kind:
    147                self.raise_error("Expecting a placeholder type or 'Identifier' for tables")
    148            elif not self._match(TokenType.R_BRACE):
    149                self.raise_error("Expecting }")
    150
    151            return self.expression(exp.Placeholder, this=this, kind=kind)
    152
    153        def _parse_in(self, this: t.Optional[exp.Expression], is_global: bool = False) -> exp.In:
    154            this = super()._parse_in(this)
    155            this.set("is_global", is_global)
    156            return this
    157
    158        def _parse_table(
    159            self, schema: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None
    160        ) -> t.Optional[exp.Expression]:
    161            this = super()._parse_table(schema=schema, alias_tokens=alias_tokens)
    162
    163            if self._match(TokenType.FINAL):
    164                this = self.expression(exp.Final, this=this)
    165
    166            return this
    167
    168        def _parse_position(self, haystack_first: bool = False) -> exp.StrPosition:
    169            return super()._parse_position(haystack_first=True)
    170
    171        # https://clickhouse.com/docs/en/sql-reference/statements/select/with/
    172        def _parse_cte(self) -> exp.CTE:
    173            index = self._index
    174            try:
    175                # WITH <identifier> AS <subquery expression>
    176                return super()._parse_cte()
    177            except ParseError:
    178                # WITH <expression> AS <identifier>
    179                self._retreat(index)
    180                statement = self._parse_statement()
    181
    182                if statement and isinstance(statement.this, exp.Alias):
    183                    self.raise_error("Expected CTE to have alias")
    184
    185                return self.expression(exp.CTE, this=statement, alias=statement and statement.this)
    186
    187        def _parse_join_parts(
    188            self,
    189        ) -> t.Tuple[t.Optional[Token], t.Optional[Token], t.Optional[Token]]:
    190            is_global = self._match(TokenType.GLOBAL) and self._prev
    191            kind_pre = self._match_set(self.JOIN_KINDS, advance=False) and self._prev
    192
    193            if kind_pre:
    194                kind = self._match_set(self.JOIN_KINDS) and self._prev
    195                side = self._match_set(self.JOIN_SIDES) and self._prev
    196                return is_global, side, kind
    197
    198            return (
    199                is_global,
    200                self._match_set(self.JOIN_SIDES) and self._prev,
    201                self._match_set(self.JOIN_KINDS) and self._prev,
    202            )
    203
    204        def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]:
    205            join = super()._parse_join(skip_join_token)
    206
    207            if join:
    208                join.set("global", join.args.pop("method", None))
    209            return join
    210
    211        def _parse_function(
    212            self,
    213            functions: t.Optional[t.Dict[str, t.Callable]] = None,
    214            anonymous: bool = False,
    215            optional_parens: bool = True,
    216        ) -> t.Optional[exp.Expression]:
    217            func = super()._parse_function(
    218                functions=functions, anonymous=anonymous, optional_parens=optional_parens
    219            )
    220
    221            if isinstance(func, exp.Anonymous):
    222                params = self._parse_func_params(func)
    223
    224                if params:
    225                    return self.expression(
    226                        exp.ParameterizedAgg,
    227                        this=func.this,
    228                        expressions=func.expressions,
    229                        params=params,
    230                    )
    231
    232            return func
    233
    234        def _parse_func_params(
    235            self, this: t.Optional[exp.Func] = None
    236        ) -> t.Optional[t.List[t.Optional[exp.Expression]]]:
    237            if self._match_pair(TokenType.R_PAREN, TokenType.L_PAREN):
    238                return self._parse_csv(self._parse_lambda)
    239
    240            if self._match(TokenType.L_PAREN):
    241                params = self._parse_csv(self._parse_lambda)
    242                self._match_r_paren(this)
    243                return params
    244
    245            return None
    246
    247        def _parse_quantile(self) -> exp.Quantile:
    248            this = self._parse_lambda()
    249            params = self._parse_func_params()
    250            if params:
    251                return self.expression(exp.Quantile, this=params[0], quantile=this)
    252            return self.expression(exp.Quantile, this=this, quantile=exp.Literal.number(0.5))
    253
    254        def _parse_wrapped_id_vars(
    255            self, optional: bool = False
    256        ) -> t.List[t.Optional[exp.Expression]]:
    257            return super()._parse_wrapped_id_vars(optional=True)
    258
    259        def _parse_primary_key(
    260            self, wrapped_optional: bool = False, in_props: bool = False
    261        ) -> exp.PrimaryKeyColumnConstraint | exp.PrimaryKey:
    262            return super()._parse_primary_key(
    263                wrapped_optional=wrapped_optional or in_props, in_props=in_props
    264            )
    265
    266        def _parse_on_property(self) -> t.Optional[exp.Expression]:
    267            index = self._index
    268            if self._match_text_seq("CLUSTER"):
    269                this = self._parse_id_var()
    270                if this:
    271                    return self.expression(exp.OnCluster, this=this)
    272                else:
    273                    self._retreat(index)
    274            return None

Parser consumes a list of tokens produced by the Tokenizer and produces a parsed syntax tree.

###### Arguments:

- error_level: The desired error level. Default: ErrorLevel.IMMEDIATE
- error_message_context: Determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 100
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3

##### Inherited Members

sqlglot.parser.Parser
  Parser
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression

class ClickHouse.Generator(sqlglot.generator.Generator): View Source

    276    class Generator(generator.Generator):
    277        STRUCT_DELIMITER = ("(", ")")
    278
    279        TYPE_MAPPING = {
    280            **generator.Generator.TYPE_MAPPING,
    281            exp.DataType.Type.ARRAY: "Array",
    282            exp.DataType.Type.BIGINT: "Int64",
    283            exp.DataType.Type.DATETIME64: "DateTime64",
    284            exp.DataType.Type.DOUBLE: "Float64",
    285            exp.DataType.Type.FLOAT: "Float32",
    286            exp.DataType.Type.INT: "Int32",
    287            exp.DataType.Type.INT128: "Int128",
    288            exp.DataType.Type.INT256: "Int256",
    289            exp.DataType.Type.MAP: "Map",
    290            exp.DataType.Type.NULLABLE: "Nullable",
    291            exp.DataType.Type.SMALLINT: "Int16",
    292            exp.DataType.Type.STRUCT: "Tuple",
    293            exp.DataType.Type.TINYINT: "Int8",
    294            exp.DataType.Type.UBIGINT: "UInt64",
    295            exp.DataType.Type.UINT: "UInt32",
    296            exp.DataType.Type.UINT128: "UInt128",
    297            exp.DataType.Type.UINT256: "UInt256",
    298            exp.DataType.Type.USMALLINT: "UInt16",
    299            exp.DataType.Type.UTINYINT: "UInt8",
    300        }
    301
    302        TRANSFORMS = {
    303            **generator.Generator.TRANSFORMS,
    304            exp.AnyValue: rename_func("any"),
    305            exp.ApproxDistinct: rename_func("uniq"),
    306            exp.Array: inline_array_sql,
    307            exp.CastToStrType: rename_func("CAST"),
    308            exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL",
    309            exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)),
    310            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    311            exp.Pivot: no_pivot_sql,
    312            exp.Quantile: lambda self, e: self.func("quantile", e.args.get("quantile"))
    313            + f"({self.sql(e, 'this')})",
    314            exp.RegexpLike: lambda self, e: f"match({self.format_args(e.this, e.expression)})",
    315            exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})",
    316            exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)),
    317        }
    318
    319        PROPERTIES_LOCATION = {
    320            **generator.Generator.PROPERTIES_LOCATION,
    321            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    322            exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA,
    323            exp.OnCluster: exp.Properties.Location.POST_NAME,
    324        }
    325
    326        JOIN_HINTS = False
    327        TABLE_HINTS = False
    328        EXPLICIT_UNION = True
    329        GROUPINGS_SEP = ""
    330
    331        # there's no list in docs, but it can be found in Clickhouse code
    332        # see `ClickHouse/src/Parsers/ParserCreate*.cpp`
    333        ON_CLUSTER_TARGETS = {
    334            "DATABASE",
    335            "TABLE",
    336            "VIEW",
    337            "DICTIONARY",
    338            "INDEX",
    339            "FUNCTION",
    340            "NAMED COLLECTION",
    341        }
    342
    343        def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    344            # Clickhouse errors out if we try to cast a NULL value to TEXT
    345            return self.func(
    346                "CONCAT",
    347                *[
    348                    exp.func("if", e.is_(exp.null()), e, exp.cast(e, "text"))
    349                    for e in t.cast(t.List[exp.Condition], expression.expressions)
    350                ],
    351            )
    352
    353        def cte_sql(self, expression: exp.CTE) -> str:
    354            if isinstance(expression.this, exp.Alias):
    355                return self.sql(expression, "this")
    356
    357            return super().cte_sql(expression)
    358
    359        def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    360            return super().after_limit_modifiers(expression) + [
    361                self.seg("SETTINGS ") + self.expressions(expression, key="settings", flat=True)
    362                if expression.args.get("settings")
    363                else "",
    364                self.seg("FORMAT ") + self.sql(expression, "format")
    365                if expression.args.get("format")
    366                else "",
    367            ]
    368
    369        def parameterizedagg_sql(self, expression: exp.Anonymous) -> str:
    370            params = self.expressions(expression, "params", flat=True)
    371            return self.func(expression.name, *expression.expressions) + f"({params})"
    372
    373        def placeholder_sql(self, expression: exp.Placeholder) -> str:
    374            return f"{{{expression.name}: {self.sql(expression, 'kind')}}}"
    375
    376        def oncluster_sql(self, expression: exp.OnCluster) -> str:
    377            return f"ON CLUSTER {self.sql(expression, 'this')}"
    378
    379        def createable_sql(
    380            self,
    381            expression: exp.Create,
    382            locations: dict[exp.Properties.Location, list[exp.Property]],
    383        ) -> str:
    384            kind = self.sql(expression, "kind").upper()
    385            if kind in self.ON_CLUSTER_TARGETS and locations.get(exp.Properties.Location.POST_NAME):
    386                this_name = self.sql(expression.this, "this")
    387                this_properties = " ".join(
    388                    [self.sql(prop) for prop in locations[exp.Properties.Location.POST_NAME]]
    389                )
    390                this_schema = self.schema_columns_sql(expression.this)
    391                return f"{this_name}{self.sep()}{this_properties}{self.sep()}{this_schema}"
    392            return super().createable_sql(expression, locations)

Generator converts a given syntax tree to the corresponding SQL string.

###### Arguments:

- pretty: Whether or not to format the produced SQL string. Default: False.
- identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
- normalize: Whether or not to normalize identifiers to lowercase. Default: False.
- pad: Determines the pad size in a formatted string. Default: 2.
- indent: Determines the indentation size in a formatted string. Default: 2.
- normalize_functions: Whether or not to normalize all function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
- unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma: Determines whether or not the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

def safeconcat_sql(self, expression: sqlglot.expressions.SafeConcat) -> str: View Source

    343        def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    344            # Clickhouse errors out if we try to cast a NULL value to TEXT
    345            return self.func(
    346                "CONCAT",
    347                *[
    348                    exp.func("if", e.is_(exp.null()), e, exp.cast(e, "text"))
    349                    for e in t.cast(t.List[exp.Condition], expression.expressions)
    350                ],
    351            )

def cte_sql(self, expression: sqlglot.expressions.CTE) -> str: View Source

    353        def cte_sql(self, expression: exp.CTE) -> str:
    354            if isinstance(expression.this, exp.Alias):
    355                return self.sql(expression, "this")
    356
    357            return super().cte_sql(expression)

def after_limit_modifiers(self, expression: sqlglot.expressions.Expression) -> List[str]: View Source

    359        def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    360            return super().after_limit_modifiers(expression) + [
    361                self.seg("SETTINGS ") + self.expressions(expression, key="settings", flat=True)
    362                if expression.args.get("settings")
    363                else "",
    364                self.seg("FORMAT ") + self.sql(expression, "format")
    365                if expression.args.get("format")
    366                else "",
    367            ]

def parameterizedagg_sql(self, expression: sqlglot.expressions.Anonymous) -> str: View Source

    369        def parameterizedagg_sql(self, expression: exp.Anonymous) -> str:
    370            params = self.expressions(expression, "params", flat=True)
    371            return self.func(expression.name, *expression.expressions) + f"({params})"

def placeholder_sql(self, expression: sqlglot.expressions.Placeholder) -> str: View Source

    373        def placeholder_sql(self, expression: exp.Placeholder) -> str:
    374            return f"{{{expression.name}: {self.sql(expression, 'kind')}}}"

def oncluster_sql(self, expression: sqlglot.expressions.OnCluster) -> str: View Source

    376        def oncluster_sql(self, expression: exp.OnCluster) -> str:
    377            return f"ON CLUSTER {self.sql(expression, 'this')}"

def createable_sql( self, expression: sqlglot.expressions.Create, locations: dict[sqlglot.expressions.Properties.Location, list[sqlglot.expressions.Property]]) -> str: View Source

    379        def createable_sql(
    380            self,
    381            expression: exp.Create,
    382            locations: dict[exp.Properties.Location, list[exp.Property]],
    383        ) -> str:
    384            kind = self.sql(expression, "kind").upper()
    385            if kind in self.ON_CLUSTER_TARGETS and locations.get(exp.Properties.Location.POST_NAME):
    386                this_name = self.sql(expression.this, "this")
    387                this_properties = " ".join(
    388                    [self.sql(prop) for prop in locations[exp.Properties.Location.POST_NAME]]
    389                )
    390                this_schema = self.schema_columns_sql(expression.this)
    391                return f"{this_name}{self.sep()}{this_properties}{self.sep()}{this_schema}"
    392            return super().createable_sql(expression, locations)

@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

    247    @classmethod
    248    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
    249        """Checks if text can be identified given an identify option.
    250
    251        Args:
    252            text: The text to check.
    253            identify:
    254                "always" or `True`: Always returns true.
    255                "safe": True if the identifier is case-insensitive.
    256
    257        Returns:
    258            Whether or not the given text can be identified.
    259        """
    260        if identify is True or identify == "always":
    261            return True
    262
    263        if identify == "safe":
    264            return not cls.case_sensitive(text)
    265
    266        return False

Checks if text can be identified given an identify option.

###### Arguments:

- text: The text to check.
- identify: "always" or `True`: Always returns true. "safe": True if the identifier is case-insensitive.

###### Returns:

> Whether or not the given text can be identified.

##### Inherited Members

sqlglot.generator.Generator
  Generator
  generate
  unsupported
  sep
  seg
  pad_comment
  maybe_comment
  wrap
  no_identify
  normalize_func
  indent
  sql
  uncache_sql
  cache_sql
  characterset_sql
  column_sql
  columnposition_sql
  columndef_sql
  columnconstraint_sql
  autoincrementcolumnconstraint_sql
  compresscolumnconstraint_sql
  generatedasidentitycolumnconstraint_sql
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  create_sql
  clone_sql
  describe_sql
  prepend_ctes
  with_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  rawstring_sql
  datatypesize_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  onconflict_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  escape_str
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  offset_limit_modifiers
  after_having_modifiers
  select_sql
  schema_sql
  schema_columns_sql
  star_sql
  parameter_sql
  sessionparameter_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  openjsoncolumndef_sql
  openjson_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  cast_sql
  currentdate_sql
  collate_sql
  command_sql
  comment_sql
  mergetreettlaction_sql
  mergetreettl_sql
  transaction_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  safedpipe_sql
  div_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql
  tochar_sql
  dictproperty_sql
  dictrange_sql
  dictsubproperty_sql