sqlglot.dialects.teradata
1from __future__ import annotations 2 3from sqlglot import exp, generator, parser 4from sqlglot.dialects.dialect import Dialect 5from sqlglot.tokens import TokenType 6 7 8class Teradata(Dialect): 9 class Parser(parser.Parser): 10 CHARSET_TRANSLATORS = { 11 "GRAPHIC_TO_KANJISJIS", 12 "GRAPHIC_TO_LATIN", 13 "GRAPHIC_TO_UNICODE", 14 "GRAPHIC_TO_UNICODE_PadSpace", 15 "KANJI1_KanjiEBCDIC_TO_UNICODE", 16 "KANJI1_KanjiEUC_TO_UNICODE", 17 "KANJI1_KANJISJIS_TO_UNICODE", 18 "KANJI1_SBC_TO_UNICODE", 19 "KANJISJIS_TO_GRAPHIC", 20 "KANJISJIS_TO_LATIN", 21 "KANJISJIS_TO_UNICODE", 22 "LATIN_TO_GRAPHIC", 23 "LATIN_TO_KANJISJIS", 24 "LATIN_TO_UNICODE", 25 "LOCALE_TO_UNICODE", 26 "UNICODE_TO_GRAPHIC", 27 "UNICODE_TO_GRAPHIC_PadGraphic", 28 "UNICODE_TO_GRAPHIC_VarGraphic", 29 "UNICODE_TO_KANJI1_KanjiEBCDIC", 30 "UNICODE_TO_KANJI1_KanjiEUC", 31 "UNICODE_TO_KANJI1_KANJISJIS", 32 "UNICODE_TO_KANJI1_SBC", 33 "UNICODE_TO_KANJISJIS", 34 "UNICODE_TO_LATIN", 35 "UNICODE_TO_LOCALE", 36 "UNICODE_TO_UNICODE_FoldSpace", 37 "UNICODE_TO_UNICODE_Fullwidth", 38 "UNICODE_TO_UNICODE_Halfwidth", 39 "UNICODE_TO_UNICODE_NFC", 40 "UNICODE_TO_UNICODE_NFD", 41 "UNICODE_TO_UNICODE_NFKC", 42 "UNICODE_TO_UNICODE_NFKD", 43 } 44 45 FUNCTION_PARSERS = { 46 **parser.Parser.FUNCTION_PARSERS, # type: ignore 47 "TRANSLATE": lambda self: self._parse_translate(self.STRICT_CAST), 48 } 49 50 def _parse_translate(self, strict: bool) -> exp.Expression: 51 this = self._parse_conjunction() 52 53 if not self._match(TokenType.USING): 54 self.raise_error("Expected USING in TRANSLATE") 55 56 if self._match_texts(self.CHARSET_TRANSLATORS): 57 charset_split = self._prev.text.split("_TO_") 58 to = self.expression(exp.CharacterSet, this=charset_split[1]) 59 else: 60 self.raise_error("Expected a character set translator after USING in TRANSLATE") 61 62 return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to) 63 64 # FROM before SET in Teradata UPDATE syntax 65 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 66 def _parse_update(self) -> exp.Expression: 67 return self.expression( 68 exp.Update, 69 **{ # type: ignore 70 "this": self._parse_table(alias_tokens=self.UPDATE_ALIAS_TOKENS), 71 "from": self._parse_from(), 72 "expressions": self._match(TokenType.SET) 73 and self._parse_csv(self._parse_equality), 74 "where": self._parse_where(), 75 }, 76 ) 77 78 class Generator(generator.Generator): 79 PROPERTIES_LOCATION = { 80 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 81 exp.PartitionedByProperty: exp.Properties.Location.POST_INDEX, 82 } 83 84 def partitionedbyproperty_sql(self, expression: exp.PartitionedByProperty) -> str: 85 return f"PARTITION BY {self.sql(expression, 'this')}" 86 87 # FROM before SET in Teradata UPDATE syntax 88 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 89 def update_sql(self, expression: exp.Update) -> str: 90 this = self.sql(expression, "this") 91 from_sql = self.sql(expression, "from") 92 set_sql = self.expressions(expression, flat=True) 93 where_sql = self.sql(expression, "where") 94 sql = f"UPDATE {this}{from_sql} SET {set_sql}{where_sql}" 95 return self.prepend_ctes(expression, sql)
9class Teradata(Dialect): 10 class Parser(parser.Parser): 11 CHARSET_TRANSLATORS = { 12 "GRAPHIC_TO_KANJISJIS", 13 "GRAPHIC_TO_LATIN", 14 "GRAPHIC_TO_UNICODE", 15 "GRAPHIC_TO_UNICODE_PadSpace", 16 "KANJI1_KanjiEBCDIC_TO_UNICODE", 17 "KANJI1_KanjiEUC_TO_UNICODE", 18 "KANJI1_KANJISJIS_TO_UNICODE", 19 "KANJI1_SBC_TO_UNICODE", 20 "KANJISJIS_TO_GRAPHIC", 21 "KANJISJIS_TO_LATIN", 22 "KANJISJIS_TO_UNICODE", 23 "LATIN_TO_GRAPHIC", 24 "LATIN_TO_KANJISJIS", 25 "LATIN_TO_UNICODE", 26 "LOCALE_TO_UNICODE", 27 "UNICODE_TO_GRAPHIC", 28 "UNICODE_TO_GRAPHIC_PadGraphic", 29 "UNICODE_TO_GRAPHIC_VarGraphic", 30 "UNICODE_TO_KANJI1_KanjiEBCDIC", 31 "UNICODE_TO_KANJI1_KanjiEUC", 32 "UNICODE_TO_KANJI1_KANJISJIS", 33 "UNICODE_TO_KANJI1_SBC", 34 "UNICODE_TO_KANJISJIS", 35 "UNICODE_TO_LATIN", 36 "UNICODE_TO_LOCALE", 37 "UNICODE_TO_UNICODE_FoldSpace", 38 "UNICODE_TO_UNICODE_Fullwidth", 39 "UNICODE_TO_UNICODE_Halfwidth", 40 "UNICODE_TO_UNICODE_NFC", 41 "UNICODE_TO_UNICODE_NFD", 42 "UNICODE_TO_UNICODE_NFKC", 43 "UNICODE_TO_UNICODE_NFKD", 44 } 45 46 FUNCTION_PARSERS = { 47 **parser.Parser.FUNCTION_PARSERS, # type: ignore 48 "TRANSLATE": lambda self: self._parse_translate(self.STRICT_CAST), 49 } 50 51 def _parse_translate(self, strict: bool) -> exp.Expression: 52 this = self._parse_conjunction() 53 54 if not self._match(TokenType.USING): 55 self.raise_error("Expected USING in TRANSLATE") 56 57 if self._match_texts(self.CHARSET_TRANSLATORS): 58 charset_split = self._prev.text.split("_TO_") 59 to = self.expression(exp.CharacterSet, this=charset_split[1]) 60 else: 61 self.raise_error("Expected a character set translator after USING in TRANSLATE") 62 63 return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to) 64 65 # FROM before SET in Teradata UPDATE syntax 66 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 67 def _parse_update(self) -> exp.Expression: 68 return self.expression( 69 exp.Update, 70 **{ # type: ignore 71 "this": self._parse_table(alias_tokens=self.UPDATE_ALIAS_TOKENS), 72 "from": self._parse_from(), 73 "expressions": self._match(TokenType.SET) 74 and self._parse_csv(self._parse_equality), 75 "where": self._parse_where(), 76 }, 77 ) 78 79 class Generator(generator.Generator): 80 PROPERTIES_LOCATION = { 81 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 82 exp.PartitionedByProperty: exp.Properties.Location.POST_INDEX, 83 } 84 85 def partitionedbyproperty_sql(self, expression: exp.PartitionedByProperty) -> str: 86 return f"PARTITION BY {self.sql(expression, 'this')}" 87 88 # FROM before SET in Teradata UPDATE syntax 89 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 90 def update_sql(self, expression: exp.Update) -> str: 91 this = self.sql(expression, "this") 92 from_sql = self.sql(expression, "from") 93 set_sql = self.expressions(expression, flat=True) 94 where_sql = self.sql(expression, "where") 95 sql = f"UPDATE {this}{from_sql} SET {set_sql}{where_sql}" 96 return self.prepend_ctes(expression, sql)
Inherited Members
10 class Parser(parser.Parser): 11 CHARSET_TRANSLATORS = { 12 "GRAPHIC_TO_KANJISJIS", 13 "GRAPHIC_TO_LATIN", 14 "GRAPHIC_TO_UNICODE", 15 "GRAPHIC_TO_UNICODE_PadSpace", 16 "KANJI1_KanjiEBCDIC_TO_UNICODE", 17 "KANJI1_KanjiEUC_TO_UNICODE", 18 "KANJI1_KANJISJIS_TO_UNICODE", 19 "KANJI1_SBC_TO_UNICODE", 20 "KANJISJIS_TO_GRAPHIC", 21 "KANJISJIS_TO_LATIN", 22 "KANJISJIS_TO_UNICODE", 23 "LATIN_TO_GRAPHIC", 24 "LATIN_TO_KANJISJIS", 25 "LATIN_TO_UNICODE", 26 "LOCALE_TO_UNICODE", 27 "UNICODE_TO_GRAPHIC", 28 "UNICODE_TO_GRAPHIC_PadGraphic", 29 "UNICODE_TO_GRAPHIC_VarGraphic", 30 "UNICODE_TO_KANJI1_KanjiEBCDIC", 31 "UNICODE_TO_KANJI1_KanjiEUC", 32 "UNICODE_TO_KANJI1_KANJISJIS", 33 "UNICODE_TO_KANJI1_SBC", 34 "UNICODE_TO_KANJISJIS", 35 "UNICODE_TO_LATIN", 36 "UNICODE_TO_LOCALE", 37 "UNICODE_TO_UNICODE_FoldSpace", 38 "UNICODE_TO_UNICODE_Fullwidth", 39 "UNICODE_TO_UNICODE_Halfwidth", 40 "UNICODE_TO_UNICODE_NFC", 41 "UNICODE_TO_UNICODE_NFD", 42 "UNICODE_TO_UNICODE_NFKC", 43 "UNICODE_TO_UNICODE_NFKD", 44 } 45 46 FUNCTION_PARSERS = { 47 **parser.Parser.FUNCTION_PARSERS, # type: ignore 48 "TRANSLATE": lambda self: self._parse_translate(self.STRICT_CAST), 49 } 50 51 def _parse_translate(self, strict: bool) -> exp.Expression: 52 this = self._parse_conjunction() 53 54 if not self._match(TokenType.USING): 55 self.raise_error("Expected USING in TRANSLATE") 56 57 if self._match_texts(self.CHARSET_TRANSLATORS): 58 charset_split = self._prev.text.split("_TO_") 59 to = self.expression(exp.CharacterSet, this=charset_split[1]) 60 else: 61 self.raise_error("Expected a character set translator after USING in TRANSLATE") 62 63 return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to) 64 65 # FROM before SET in Teradata UPDATE syntax 66 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 67 def _parse_update(self) -> exp.Expression: 68 return self.expression( 69 exp.Update, 70 **{ # type: ignore 71 "this": self._parse_table(alias_tokens=self.UPDATE_ALIAS_TOKENS), 72 "from": self._parse_from(), 73 "expressions": self._match(TokenType.SET) 74 and self._parse_csv(self._parse_equality), 75 "where": self._parse_where(), 76 }, 77 )
Parser consumes a list of tokens produced by the sqlglot.tokens.Tokenizer
and produces
a parsed syntax tree.
Arguments:
- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- 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
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
Inherited Members
79 class Generator(generator.Generator): 80 PROPERTIES_LOCATION = { 81 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 82 exp.PartitionedByProperty: exp.Properties.Location.POST_INDEX, 83 } 84 85 def partitionedbyproperty_sql(self, expression: exp.PartitionedByProperty) -> str: 86 return f"PARTITION BY {self.sql(expression, 'this')}" 87 88 # FROM before SET in Teradata UPDATE syntax 89 # https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/Teradata-VantageTM-SQL-Data-Manipulation-Language-17.20/Statement-Syntax/UPDATE/UPDATE-Syntax-Basic-Form-FROM-Clause 90 def update_sql(self, expression: exp.Update) -> str: 91 this = self.sql(expression, "this") 92 from_sql = self.sql(expression, "from") 93 set_sql = self.expressions(expression, flat=True) 94 where_sql = self.sql(expression, "where") 95 sql = f"UPDATE {this}{from_sql} SET {set_sql}{where_sql}" 96 return self.prepend_ctes(expression, sql)
Generator interprets the given syntax tree and produces a SQL string as an output.
Arguments:
- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- identify (bool): if set to True all identifiers will be delimited by the corresponding character.
- normalize (bool): if set to True all identifiers will lower cased
- string_escape (str): specifies a string escape character. Default: '.
- identifier_escape (str): specifies an identifier escape character. Default: ".
- pad (int): determines padding in a formatted string. Default: 2.
- indent (int): determines the size of indentation in a formatted string. Default: 4.
- unnest_column_only (bool): if true unnest table aliases are considered only as column aliases
- normalize_functions (str): normalize function names, "upper", "lower", or None Default: "upper"
- alias_post_tablesample (bool): if the table alias comes after tablesample Default: False
- unsupported_level (ErrorLevel): determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- null_ordering (str): Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
- max_unsupported (int): 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 (bool): if the the comma is leading or trailing in select statements 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
90 def update_sql(self, expression: exp.Update) -> str: 91 this = self.sql(expression, "this") 92 from_sql = self.sql(expression, "from") 93 set_sql = self.expressions(expression, flat=True) 94 where_sql = self.sql(expression, "where") 95 sql = f"UPDATE {this}{from_sql} SET {set_sql}{where_sql}" 96 return self.prepend_ctes(expression, sql)
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
- columndef_sql
- columnconstraint_sql
- autoincrementcolumnconstraint_sql
- checkcolumnconstraint_sql
- commentcolumnconstraint_sql
- collatecolumnconstraint_sql
- encodecolumnconstraint_sql
- defaultcolumnconstraint_sql
- generatedasidentitycolumnconstraint_sql
- notnullcolumnconstraint_sql
- primarykeycolumnconstraint_sql
- uniquecolumnconstraint_sql
- create_sql
- describe_sql
- prepend_ctes
- with_sql
- cte_sql
- tablealias_sql
- bitstring_sql
- hexstring_sql
- datatype_sql
- directory_sql
- delete_sql
- drop_sql
- except_sql
- except_op
- fetch_sql
- filter_sql
- hint_sql
- index_sql
- identifier_sql
- national_sql
- partition_sql
- properties_sql
- root_properties
- properties
- with_properties
- locate_properties
- property_sql
- likeproperty_sql
- fallbackproperty_sql
- journalproperty_sql
- freespaceproperty_sql
- afterjournalproperty_sql
- checksumproperty_sql
- mergeblockratioproperty_sql
- datablocksizeproperty_sql
- blockcompressionproperty_sql
- isolatedloadingproperty_sql
- insert_sql
- intersect_sql
- intersect_op
- introducer_sql
- pseudotype_sql
- rowformatdelimitedproperty_sql
- table_sql
- tablesample_sql
- pivot_sql
- tuple_sql
- values_sql
- var_sql
- into_sql
- from_sql
- group_sql
- having_sql
- join_sql
- lambda_sql
- lateral_sql
- limit_sql
- offset_sql
- lock_sql
- literal_sql
- loaddata_sql
- null_sql
- boolean_sql
- order_sql
- cluster_sql
- distribute_sql
- sort_sql
- ordered_sql
- matchrecognize_sql
- query_modifiers
- select_sql
- schema_sql
- star_sql
- structkwarg_sql
- parameter_sql
- sessionparameter_sql
- placeholder_sql
- subquery_sql
- qualify_sql
- union_sql
- union_op
- unnest_sql
- where_sql
- window_sql
- partition_by_sql
- window_spec_sql
- withingroup_sql
- between_sql
- bracket_sql
- all_sql
- any_sql
- exists_sql
- case_sql
- constraint_sql
- extract_sql
- trim_sql
- concat_sql
- check_sql
- foreignkey_sql
- primarykey_sql
- unique_sql
- if_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
- 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
- div_sql
- distance_sql
- dot_sql
- eq_sql
- escape_sql
- glob_sql
- gt_sql
- gte_sql
- ilike_sql
- is_sql
- like_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
- format_args
- text_width
- format_time
- expressions
- op_expressions
- naked_property
- set_operation
- tag_sql
- token_sql
- userdefinedfunction_sql
- userdefinedfunctionkwarg_sql
- joinhint_sql
- kwarg_sql
- when_sql
- merge_sql