sqlglot.dialects.redshift
1from __future__ import annotations 2 3import typing as t 4 5from sqlglot import exp, transforms 6from sqlglot.dialects.dialect import concat_to_dpipe_sql, rename_func 7from sqlglot.dialects.postgres import Postgres 8from sqlglot.helper import seq_get 9from sqlglot.tokens import TokenType 10 11 12def _json_sql(self: Postgres.Generator, expression: exp.JSONExtract | exp.JSONExtractScalar) -> str: 13 return f'{self.sql(expression, "this")}."{expression.expression.name}"' 14 15 16class Redshift(Postgres): 17 # https://docs.aws.amazon.com/redshift/latest/dg/r_names.html 18 RESOLVES_IDENTIFIERS_AS_UPPERCASE = None 19 20 TIME_FORMAT = "'YYYY-MM-DD HH:MI:SS'" 21 TIME_MAPPING = { 22 **Postgres.TIME_MAPPING, 23 "MON": "%b", 24 "HH": "%H", 25 } 26 27 class Parser(Postgres.Parser): 28 FUNCTIONS = { 29 **Postgres.Parser.FUNCTIONS, 30 "DATEADD": lambda args: exp.DateAdd( 31 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 32 expression=seq_get(args, 1), 33 unit=seq_get(args, 0), 34 ), 35 "DATEDIFF": lambda args: exp.DateDiff( 36 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 37 expression=exp.TsOrDsToDate(this=seq_get(args, 1)), 38 unit=seq_get(args, 0), 39 ), 40 "NVL": exp.Coalesce.from_arg_list, 41 "STRTOL": exp.FromBase.from_arg_list, 42 } 43 44 def _parse_types( 45 self, check_func: bool = False, schema: bool = False 46 ) -> t.Optional[exp.Expression]: 47 this = super()._parse_types(check_func=check_func, schema=schema) 48 49 if ( 50 isinstance(this, exp.DataType) 51 and this.is_type("varchar") 52 and this.expressions 53 and this.expressions[0].this == exp.column("MAX") 54 ): 55 this.set("expressions", [exp.var("MAX")]) 56 57 return this 58 59 def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]: 60 to = self._parse_types() 61 self._match(TokenType.COMMA) 62 this = self._parse_bitwise() 63 return self.expression(exp.TryCast, this=this, to=to) 64 65 class Tokenizer(Postgres.Tokenizer): 66 BIT_STRINGS = [] 67 HEX_STRINGS = [] 68 STRING_ESCAPES = ["\\"] 69 70 KEYWORDS = { 71 **Postgres.Tokenizer.KEYWORDS, 72 "HLLSKETCH": TokenType.HLLSKETCH, 73 "SUPER": TokenType.SUPER, 74 "SYSDATE": TokenType.CURRENT_TIMESTAMP, 75 "TIME": TokenType.TIMESTAMP, 76 "TIMETZ": TokenType.TIMESTAMPTZ, 77 "TOP": TokenType.TOP, 78 "UNLOAD": TokenType.COMMAND, 79 "VARBYTE": TokenType.VARBINARY, 80 } 81 82 # Redshift allows # to appear as a table identifier prefix 83 SINGLE_TOKENS = Postgres.Tokenizer.SINGLE_TOKENS.copy() 84 SINGLE_TOKENS.pop("#") 85 86 class Generator(Postgres.Generator): 87 LOCKING_READS_SUPPORTED = False 88 RENAME_TABLE_WITH_DB = False 89 90 TYPE_MAPPING = { 91 **Postgres.Generator.TYPE_MAPPING, 92 exp.DataType.Type.BINARY: "VARBYTE", 93 exp.DataType.Type.VARBINARY: "VARBYTE", 94 exp.DataType.Type.INT: "INTEGER", 95 } 96 97 PROPERTIES_LOCATION = { 98 **Postgres.Generator.PROPERTIES_LOCATION, 99 exp.LikeProperty: exp.Properties.Location.POST_WITH, 100 } 101 102 TRANSFORMS = { 103 **Postgres.Generator.TRANSFORMS, 104 exp.Concat: concat_to_dpipe_sql, 105 exp.CurrentTimestamp: lambda self, e: "SYSDATE", 106 exp.DateAdd: lambda self, e: self.func( 107 "DATEADD", exp.var(e.text("unit") or "day"), e.expression, e.this 108 ), 109 exp.DateDiff: lambda self, e: self.func( 110 "DATEDIFF", exp.var(e.text("unit") or "day"), e.expression, e.this 111 ), 112 exp.DistKeyProperty: lambda self, e: f"DISTKEY({e.name})", 113 exp.DistStyleProperty: lambda self, e: self.naked_property(e), 114 exp.FromBase: rename_func("STRTOL"), 115 exp.JSONExtract: _json_sql, 116 exp.JSONExtractScalar: _json_sql, 117 exp.SafeConcat: concat_to_dpipe_sql, 118 exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]), 119 exp.SortKeyProperty: lambda self, e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})", 120 exp.TsOrDsToDate: lambda self, e: self.sql(e.this), 121 } 122 123 # Postgres maps exp.Pivot to no_pivot_sql, but Redshift support pivots 124 TRANSFORMS.pop(exp.Pivot) 125 126 # Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres) 127 TRANSFORMS.pop(exp.Pow) 128 129 RESERVED_KEYWORDS = {*Postgres.Generator.RESERVED_KEYWORDS, "snapshot", "type"} 130 131 def values_sql(self, expression: exp.Values) -> str: 132 """ 133 Converts `VALUES...` expression into a series of unions. 134 135 Note: If you have a lot of unions then this will result in a large number of recursive statements to 136 evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be 137 very slow. 138 """ 139 140 # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example 141 if not expression.find_ancestor(exp.From, exp.Join): 142 return super().values_sql(expression) 143 144 column_names = expression.alias and expression.args["alias"].columns 145 146 selects = [] 147 rows = [tuple_exp.expressions for tuple_exp in expression.expressions] 148 149 for i, row in enumerate(rows): 150 if i == 0 and column_names: 151 row = [ 152 exp.alias_(value, column_name) 153 for value, column_name in zip(row, column_names) 154 ] 155 156 selects.append(exp.Select(expressions=row)) 157 158 subquery_expression: exp.Select | exp.Union = selects[0] 159 if len(selects) > 1: 160 for select in selects[1:]: 161 subquery_expression = exp.union(subquery_expression, select, distinct=False) 162 163 return self.subquery_sql(subquery_expression.subquery(expression.alias)) 164 165 def with_properties(self, properties: exp.Properties) -> str: 166 """Redshift doesn't have `WITH` as part of their with_properties so we remove it""" 167 return self.properties(properties, prefix=" ", suffix="") 168 169 def datatype_sql(self, expression: exp.DataType) -> str: 170 """ 171 Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean 172 VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type 173 without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert 174 `TEXT` to `VARCHAR`. 175 """ 176 if expression.is_type("text"): 177 expression = expression.copy() 178 expression.set("this", exp.DataType.Type.VARCHAR) 179 precision = expression.args.get("expressions") 180 181 if not precision: 182 expression.append("expressions", exp.var("MAX")) 183 184 return super().datatype_sql(expression)
17class Redshift(Postgres): 18 # https://docs.aws.amazon.com/redshift/latest/dg/r_names.html 19 RESOLVES_IDENTIFIERS_AS_UPPERCASE = None 20 21 TIME_FORMAT = "'YYYY-MM-DD HH:MI:SS'" 22 TIME_MAPPING = { 23 **Postgres.TIME_MAPPING, 24 "MON": "%b", 25 "HH": "%H", 26 } 27 28 class Parser(Postgres.Parser): 29 FUNCTIONS = { 30 **Postgres.Parser.FUNCTIONS, 31 "DATEADD": lambda args: exp.DateAdd( 32 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 33 expression=seq_get(args, 1), 34 unit=seq_get(args, 0), 35 ), 36 "DATEDIFF": lambda args: exp.DateDiff( 37 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 38 expression=exp.TsOrDsToDate(this=seq_get(args, 1)), 39 unit=seq_get(args, 0), 40 ), 41 "NVL": exp.Coalesce.from_arg_list, 42 "STRTOL": exp.FromBase.from_arg_list, 43 } 44 45 def _parse_types( 46 self, check_func: bool = False, schema: bool = False 47 ) -> t.Optional[exp.Expression]: 48 this = super()._parse_types(check_func=check_func, schema=schema) 49 50 if ( 51 isinstance(this, exp.DataType) 52 and this.is_type("varchar") 53 and this.expressions 54 and this.expressions[0].this == exp.column("MAX") 55 ): 56 this.set("expressions", [exp.var("MAX")]) 57 58 return this 59 60 def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]: 61 to = self._parse_types() 62 self._match(TokenType.COMMA) 63 this = self._parse_bitwise() 64 return self.expression(exp.TryCast, this=this, to=to) 65 66 class Tokenizer(Postgres.Tokenizer): 67 BIT_STRINGS = [] 68 HEX_STRINGS = [] 69 STRING_ESCAPES = ["\\"] 70 71 KEYWORDS = { 72 **Postgres.Tokenizer.KEYWORDS, 73 "HLLSKETCH": TokenType.HLLSKETCH, 74 "SUPER": TokenType.SUPER, 75 "SYSDATE": TokenType.CURRENT_TIMESTAMP, 76 "TIME": TokenType.TIMESTAMP, 77 "TIMETZ": TokenType.TIMESTAMPTZ, 78 "TOP": TokenType.TOP, 79 "UNLOAD": TokenType.COMMAND, 80 "VARBYTE": TokenType.VARBINARY, 81 } 82 83 # Redshift allows # to appear as a table identifier prefix 84 SINGLE_TOKENS = Postgres.Tokenizer.SINGLE_TOKENS.copy() 85 SINGLE_TOKENS.pop("#") 86 87 class Generator(Postgres.Generator): 88 LOCKING_READS_SUPPORTED = False 89 RENAME_TABLE_WITH_DB = False 90 91 TYPE_MAPPING = { 92 **Postgres.Generator.TYPE_MAPPING, 93 exp.DataType.Type.BINARY: "VARBYTE", 94 exp.DataType.Type.VARBINARY: "VARBYTE", 95 exp.DataType.Type.INT: "INTEGER", 96 } 97 98 PROPERTIES_LOCATION = { 99 **Postgres.Generator.PROPERTIES_LOCATION, 100 exp.LikeProperty: exp.Properties.Location.POST_WITH, 101 } 102 103 TRANSFORMS = { 104 **Postgres.Generator.TRANSFORMS, 105 exp.Concat: concat_to_dpipe_sql, 106 exp.CurrentTimestamp: lambda self, e: "SYSDATE", 107 exp.DateAdd: lambda self, e: self.func( 108 "DATEADD", exp.var(e.text("unit") or "day"), e.expression, e.this 109 ), 110 exp.DateDiff: lambda self, e: self.func( 111 "DATEDIFF", exp.var(e.text("unit") or "day"), e.expression, e.this 112 ), 113 exp.DistKeyProperty: lambda self, e: f"DISTKEY({e.name})", 114 exp.DistStyleProperty: lambda self, e: self.naked_property(e), 115 exp.FromBase: rename_func("STRTOL"), 116 exp.JSONExtract: _json_sql, 117 exp.JSONExtractScalar: _json_sql, 118 exp.SafeConcat: concat_to_dpipe_sql, 119 exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]), 120 exp.SortKeyProperty: lambda self, e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})", 121 exp.TsOrDsToDate: lambda self, e: self.sql(e.this), 122 } 123 124 # Postgres maps exp.Pivot to no_pivot_sql, but Redshift support pivots 125 TRANSFORMS.pop(exp.Pivot) 126 127 # Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres) 128 TRANSFORMS.pop(exp.Pow) 129 130 RESERVED_KEYWORDS = {*Postgres.Generator.RESERVED_KEYWORDS, "snapshot", "type"} 131 132 def values_sql(self, expression: exp.Values) -> str: 133 """ 134 Converts `VALUES...` expression into a series of unions. 135 136 Note: If you have a lot of unions then this will result in a large number of recursive statements to 137 evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be 138 very slow. 139 """ 140 141 # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example 142 if not expression.find_ancestor(exp.From, exp.Join): 143 return super().values_sql(expression) 144 145 column_names = expression.alias and expression.args["alias"].columns 146 147 selects = [] 148 rows = [tuple_exp.expressions for tuple_exp in expression.expressions] 149 150 for i, row in enumerate(rows): 151 if i == 0 and column_names: 152 row = [ 153 exp.alias_(value, column_name) 154 for value, column_name in zip(row, column_names) 155 ] 156 157 selects.append(exp.Select(expressions=row)) 158 159 subquery_expression: exp.Select | exp.Union = selects[0] 160 if len(selects) > 1: 161 for select in selects[1:]: 162 subquery_expression = exp.union(subquery_expression, select, distinct=False) 163 164 return self.subquery_sql(subquery_expression.subquery(expression.alias)) 165 166 def with_properties(self, properties: exp.Properties) -> str: 167 """Redshift doesn't have `WITH` as part of their with_properties so we remove it""" 168 return self.properties(properties, prefix=" ", suffix="") 169 170 def datatype_sql(self, expression: exp.DataType) -> str: 171 """ 172 Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean 173 VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type 174 without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert 175 `TEXT` to `VARCHAR`. 176 """ 177 if expression.is_type("text"): 178 expression = expression.copy() 179 expression.set("this", exp.DataType.Type.VARCHAR) 180 precision = expression.args.get("expressions") 181 182 if not precision: 183 expression.append("expressions", exp.var("MAX")) 184 185 return super().datatype_sql(expression)
Inherited Members
- sqlglot.dialects.dialect.Dialect
- UNNEST_COLUMN_ONLY
- ALIAS_POST_TABLESAMPLE
- IDENTIFIERS_CAN_START_WITH_DIGIT
- STRICT_STRING_CONCAT
- NORMALIZE_FUNCTIONS
- DATE_FORMAT
- DATEINT_FORMAT
- FORMAT_MAPPING
- get_or_raise
- format_time
- normalize_identifier
- case_sensitive
- can_identify
- quote_identifier
- parse
- parse_into
- generate
- transpile
- tokenize
- tokenizer
- parser
- generator
28 class Parser(Postgres.Parser): 29 FUNCTIONS = { 30 **Postgres.Parser.FUNCTIONS, 31 "DATEADD": lambda args: exp.DateAdd( 32 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 33 expression=seq_get(args, 1), 34 unit=seq_get(args, 0), 35 ), 36 "DATEDIFF": lambda args: exp.DateDiff( 37 this=exp.TsOrDsToDate(this=seq_get(args, 2)), 38 expression=exp.TsOrDsToDate(this=seq_get(args, 1)), 39 unit=seq_get(args, 0), 40 ), 41 "NVL": exp.Coalesce.from_arg_list, 42 "STRTOL": exp.FromBase.from_arg_list, 43 } 44 45 def _parse_types( 46 self, check_func: bool = False, schema: bool = False 47 ) -> t.Optional[exp.Expression]: 48 this = super()._parse_types(check_func=check_func, schema=schema) 49 50 if ( 51 isinstance(this, exp.DataType) 52 and this.is_type("varchar") 53 and this.expressions 54 and this.expressions[0].this == exp.column("MAX") 55 ): 56 this.set("expressions", [exp.var("MAX")]) 57 58 return this 59 60 def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]: 61 to = self._parse_types() 62 self._match(TokenType.COMMA) 63 this = self._parse_bitwise() 64 return self.expression(exp.TryCast, this=this, to=to)
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
- NO_PAREN_FUNCTIONS
- NESTED_TYPE_TOKENS
- ENUM_TYPE_TOKENS
- TYPE_TOKENS
- SUBQUERY_PREDICATES
- RESERVED_KEYWORDS
- DB_CREATABLES
- CREATABLES
- ID_VAR_TOKENS
- INTERVAL_VARS
- TABLE_ALIAS_TOKENS
- COMMENT_TABLE_ALIAS_TOKENS
- UPDATE_ALIAS_TOKENS
- TRIM_TYPES
- FUNC_TOKENS
- CONJUNCTION
- EQUALITY
- COMPARISON
- TERM
- FACTOR
- TIMESTAMPS
- SET_OPERATIONS
- JOIN_METHODS
- JOIN_SIDES
- JOIN_KINDS
- JOIN_HINTS
- LAMBDAS
- COLUMN_OPERATORS
- EXPRESSION_PARSERS
- STATEMENT_PARSERS
- UNARY_PARSERS
- PRIMARY_PARSERS
- PLACEHOLDER_PARSERS
- PROPERTY_PARSERS
- CONSTRAINT_PARSERS
- ALTER_PARSERS
- SCHEMA_UNNAMED_CONSTRAINTS
- NO_PAREN_FUNCTION_PARSERS
- FUNCTIONS_WITH_ALIASED_ARGS
- QUERY_MODIFIER_PARSERS
- SET_PARSERS
- SHOW_PARSERS
- TYPE_LITERAL_PARSERS
- MODIFIABLES
- DDL_SELECT_TOKENS
- PRE_VOLATILE_TOKENS
- TRANSACTION_KIND
- TRANSACTION_CHARACTERISTICS
- INSERT_ALTERNATIVES
- CLONE_KINDS
- TABLE_INDEX_HINT_TOKENS
- WINDOW_ALIAS_TOKENS
- WINDOW_BEFORE_PAREN_TOKENS
- WINDOW_SIDES
- ADD_CONSTRAINT_TOKENS
- PREFIXED_PIVOT_COLUMNS
- IDENTIFY_PIVOT_STRINGS
- LOG_BASE_FIRST
- LOG_DEFAULTS_TO_LN
- UNNEST_COLUMN_ONLY
- ALIAS_POST_TABLESAMPLE
- STRICT_STRING_CONCAT
- FORMAT_MAPPING
- error_level
- error_message_context
- max_errors
- reset
- parse
- parse_into
- check_errors
- raise_error
- expression
- validate_expression
- errors
- sql
66 class Tokenizer(Postgres.Tokenizer): 67 BIT_STRINGS = [] 68 HEX_STRINGS = [] 69 STRING_ESCAPES = ["\\"] 70 71 KEYWORDS = { 72 **Postgres.Tokenizer.KEYWORDS, 73 "HLLSKETCH": TokenType.HLLSKETCH, 74 "SUPER": TokenType.SUPER, 75 "SYSDATE": TokenType.CURRENT_TIMESTAMP, 76 "TIME": TokenType.TIMESTAMP, 77 "TIMETZ": TokenType.TIMESTAMPTZ, 78 "TOP": TokenType.TOP, 79 "UNLOAD": TokenType.COMMAND, 80 "VARBYTE": TokenType.VARBINARY, 81 } 82 83 # Redshift allows # to appear as a table identifier prefix 84 SINGLE_TOKENS = Postgres.Tokenizer.SINGLE_TOKENS.copy() 85 SINGLE_TOKENS.pop("#")
Inherited Members
87 class Generator(Postgres.Generator): 88 LOCKING_READS_SUPPORTED = False 89 RENAME_TABLE_WITH_DB = False 90 91 TYPE_MAPPING = { 92 **Postgres.Generator.TYPE_MAPPING, 93 exp.DataType.Type.BINARY: "VARBYTE", 94 exp.DataType.Type.VARBINARY: "VARBYTE", 95 exp.DataType.Type.INT: "INTEGER", 96 } 97 98 PROPERTIES_LOCATION = { 99 **Postgres.Generator.PROPERTIES_LOCATION, 100 exp.LikeProperty: exp.Properties.Location.POST_WITH, 101 } 102 103 TRANSFORMS = { 104 **Postgres.Generator.TRANSFORMS, 105 exp.Concat: concat_to_dpipe_sql, 106 exp.CurrentTimestamp: lambda self, e: "SYSDATE", 107 exp.DateAdd: lambda self, e: self.func( 108 "DATEADD", exp.var(e.text("unit") or "day"), e.expression, e.this 109 ), 110 exp.DateDiff: lambda self, e: self.func( 111 "DATEDIFF", exp.var(e.text("unit") or "day"), e.expression, e.this 112 ), 113 exp.DistKeyProperty: lambda self, e: f"DISTKEY({e.name})", 114 exp.DistStyleProperty: lambda self, e: self.naked_property(e), 115 exp.FromBase: rename_func("STRTOL"), 116 exp.JSONExtract: _json_sql, 117 exp.JSONExtractScalar: _json_sql, 118 exp.SafeConcat: concat_to_dpipe_sql, 119 exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]), 120 exp.SortKeyProperty: lambda self, e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})", 121 exp.TsOrDsToDate: lambda self, e: self.sql(e.this), 122 } 123 124 # Postgres maps exp.Pivot to no_pivot_sql, but Redshift support pivots 125 TRANSFORMS.pop(exp.Pivot) 126 127 # Redshift uses the POW | POWER (expr1, expr2) syntax instead of expr1 ^ expr2 (postgres) 128 TRANSFORMS.pop(exp.Pow) 129 130 RESERVED_KEYWORDS = {*Postgres.Generator.RESERVED_KEYWORDS, "snapshot", "type"} 131 132 def values_sql(self, expression: exp.Values) -> str: 133 """ 134 Converts `VALUES...` expression into a series of unions. 135 136 Note: If you have a lot of unions then this will result in a large number of recursive statements to 137 evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be 138 very slow. 139 """ 140 141 # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example 142 if not expression.find_ancestor(exp.From, exp.Join): 143 return super().values_sql(expression) 144 145 column_names = expression.alias and expression.args["alias"].columns 146 147 selects = [] 148 rows = [tuple_exp.expressions for tuple_exp in expression.expressions] 149 150 for i, row in enumerate(rows): 151 if i == 0 and column_names: 152 row = [ 153 exp.alias_(value, column_name) 154 for value, column_name in zip(row, column_names) 155 ] 156 157 selects.append(exp.Select(expressions=row)) 158 159 subquery_expression: exp.Select | exp.Union = selects[0] 160 if len(selects) > 1: 161 for select in selects[1:]: 162 subquery_expression = exp.union(subquery_expression, select, distinct=False) 163 164 return self.subquery_sql(subquery_expression.subquery(expression.alias)) 165 166 def with_properties(self, properties: exp.Properties) -> str: 167 """Redshift doesn't have `WITH` as part of their with_properties so we remove it""" 168 return self.properties(properties, prefix=" ", suffix="") 169 170 def datatype_sql(self, expression: exp.DataType) -> str: 171 """ 172 Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean 173 VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type 174 without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert 175 `TEXT` to `VARCHAR`. 176 """ 177 if expression.is_type("text"): 178 expression = expression.copy() 179 expression.set("this", exp.DataType.Type.VARCHAR) 180 precision = expression.args.get("expressions") 181 182 if not precision: 183 expression.append("expressions", exp.var("MAX")) 184 185 return super().datatype_sql(expression)
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
132 def values_sql(self, expression: exp.Values) -> str: 133 """ 134 Converts `VALUES...` expression into a series of unions. 135 136 Note: If you have a lot of unions then this will result in a large number of recursive statements to 137 evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be 138 very slow. 139 """ 140 141 # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example 142 if not expression.find_ancestor(exp.From, exp.Join): 143 return super().values_sql(expression) 144 145 column_names = expression.alias and expression.args["alias"].columns 146 147 selects = [] 148 rows = [tuple_exp.expressions for tuple_exp in expression.expressions] 149 150 for i, row in enumerate(rows): 151 if i == 0 and column_names: 152 row = [ 153 exp.alias_(value, column_name) 154 for value, column_name in zip(row, column_names) 155 ] 156 157 selects.append(exp.Select(expressions=row)) 158 159 subquery_expression: exp.Select | exp.Union = selects[0] 160 if len(selects) > 1: 161 for select in selects[1:]: 162 subquery_expression = exp.union(subquery_expression, select, distinct=False) 163 164 return self.subquery_sql(subquery_expression.subquery(expression.alias))
Converts VALUES...
expression into a series of unions.
Note: If you have a lot of unions then this will result in a large number of recursive statements to
evaluate the expression. You may need to increase sys.setrecursionlimit
to run and it can also be
very slow.
166 def with_properties(self, properties: exp.Properties) -> str: 167 """Redshift doesn't have `WITH` as part of their with_properties so we remove it""" 168 return self.properties(properties, prefix=" ", suffix="")
Redshift doesn't have WITH
as part of their with_properties so we remove it
170 def datatype_sql(self, expression: exp.DataType) -> str: 171 """ 172 Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean 173 VARCHAR of max length which is `VARCHAR(max)` in Redshift. Therefore if we get a `TEXT` data type 174 without precision we convert it to `VARCHAR(max)` and if it does have precision then we just convert 175 `TEXT` to `VARCHAR`. 176 """ 177 if expression.is_type("text"): 178 expression = expression.copy() 179 expression.set("this", exp.DataType.Type.VARCHAR) 180 precision = expression.args.get("expressions") 181 182 if not precision: 183 expression.append("expressions", exp.var("MAX")) 184 185 return super().datatype_sql(expression)
Redshift converts the TEXT
data type to VARCHAR(255)
by default when people more generally mean
VARCHAR of max length which is VARCHAR(max)
in Redshift. Therefore if we get a TEXT
data type
without precision we convert it to VARCHAR(max)
and if it does have precision then we just convert
TEXT
to VARCHAR
.
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
- NULL_ORDERING_SUPPORTED
- EXPLICIT_UNION
- WRAP_DERIVED_VALUES
- CREATE_FUNCTION_RETURN_AS
- MATCHED_BY_SOURCE
- INTERVAL_ALLOWS_PLURAL_FORM
- TABLESAMPLE_WITH_METHOD
- TABLESAMPLE_SIZE_IS_PERCENT
- LIMIT_FETCH
- GROUPINGS_SEP
- INDEX_ON
- IS_BOOL_ALLOWED
- STAR_MAPPING
- TIME_PART_SINGULARS
- TOKEN_MAPPING
- STRUCT_DELIMITER
- WITH_SEPARATED_COMMENTS
- UNWRAPPED_INTERVAL_VALUES
- SENTINEL_LINE_BREAK
- UNNEST_COLUMN_ONLY
- ALIAS_POST_TABLESAMPLE
- IDENTIFIERS_CAN_START_WITH_DIGIT
- STRICT_STRING_CONCAT
- NORMALIZE_FUNCTIONS
- pretty
- identify
- normalize
- pad
- unsupported_level
- max_unsupported
- leading_comma
- max_text_width
- comments
- normalize_functions
- unsupported_messages
- 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
- createable_sql
- create_sql
- clone_sql
- describe_sql
- prepend_ctes
- with_sql
- cte_sql
- tablealias_sql
- bitstring_sql
- hexstring_sql
- bytestring_sql
- rawstring_sql
- datatypesize_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
- 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
- withtablehint_sql
- indextablehint_sql
- table_sql
- tablesample_sql
- pivot_sql
- tuple_sql
- update_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
- after_limit_modifiers
- select_sql
- schema_sql
- schema_columns_sql
- star_sql
- parameter_sql
- sessionparameter_sql
- placeholder_sql
- subquery_sql
- qualify_sql
- union_sql
- union_op
- unnest_sql
- where_sql
- window_sql
- partition_by_sql
- windowspec_sql
- withingroup_sql
- between_sql
- safebracket_sql
- all_sql
- any_sql
- exists_sql
- case_sql
- constraint_sql
- nextvaluefor_sql
- extract_sql
- trim_sql
- safeconcat_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
- oncluster_sql
- clusteredbyproperty_sql
- anyvalue_sql