sqlglot.dialects.oracle
1from __future__ import annotations 2 3import string 4import typing as t 5 6from sqlglot import exp, tokens 7from sqlglot.dialects.dialect import ( 8 Dialect, 9 NormalizationStrategy, 10) 11from sqlglot.generators.oracle import OracleGenerator 12from sqlglot.parsers.oracle import OracleParser 13from sqlglot.tokens import TokenType 14 15 16class Oracle(Dialect): 17 # Assume NLS_TERRITORY = 'AMERICA' for the DAY, DY and D truncation formats. 18 WEEK_OFFSET = -1 19 ALIAS_POST_TABLESAMPLE = True 20 LOCKING_READS_SUPPORTED = True 21 TABLESAMPLE_SIZE_IS_PERCENT = True 22 NULL_ORDERING = "nulls_are_large" 23 ON_CONDITION_EMPTY_BEFORE_ERROR = False 24 ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False 25 DISABLES_ALIAS_REF_EXPANSION = True 26 INTERSECT_BINDS_TIGHTER_THAN_UNION_AND_EXCEPT = False 27 28 # See section 8: https://docs.oracle.com/cd/A97630_01/server.920/a96540/sql_elements9a.htm 29 NORMALIZATION_STRATEGY = NormalizationStrategy.UPPERCASE 30 31 # https://docs.oracle.com/database/121/SQLRF/sql_elements004.htm#SQLRF00212 32 # https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes 33 TIME_MAPPING = { 34 "D": "%u", # Day of week (1-7) 35 "DAY": "%A", # name of day 36 "DD": "%d", # day of month (1-31) 37 "DDD": "%j", # day of year (1-366) 38 "DY": "%a", # abbreviated name of day 39 "HH": "%I", # Hour of day (1-12) 40 "HH12": "%I", # alias for HH 41 "HH24": "%H", # Hour of day (0-23) 42 "IW": "%V", # Calendar week of year (1-52 or 1-53), as defined by the ISO 8601 standard 43 "IYYY": "%G", # 4-digit year of the ISO 8601 week-numbering calendar 44 "MI": "%M", # Minute (0-59) 45 "MM": "%m", # Month (01-12; January = 01) 46 "MON": "%b", # Abbreviated name of month 47 "MONTH": "%B", # Name of month 48 "SS": "%S", # Second (0-59) 49 "WW": "%W", # Week of year (1-53) 50 "YY": "%y", # 15 51 "YYYY": "%Y", # 2015 52 "FF6": "%f", # only 6 digits are supported in python formats 53 } 54 55 PSEUDOCOLUMNS = {"ROWNUM", "ROWID", "OBJECT_ID", "OBJECT_VALUE", "LEVEL"} 56 57 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 58 # Disable quoting for pseudocolumns as it may break queries e.g 59 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 60 return ( 61 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 62 ) and super().can_quote(identifier, identify=identify) 63 64 class Tokenizer(tokens.Tokenizer): 65 VAR_SINGLE_TOKENS = {"@", "$", "#"} 66 67 UNICODE_STRINGS = [ 68 (prefix + q, q) 69 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 70 for prefix in ("U", "u") 71 ] 72 73 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html 74 # Alternative quoting, e.g. q'[it's]': the delimiters are part of the quote, so the 75 # text in between is taken as is, without escapes 76 RAW_STRINGS = [ 77 (f"{prefix}'{start}", f"{end}'") 78 for prefix in ("q", "Q") 79 for start, end in ( 80 ("[", "]"), 81 ("{", "}"), 82 ("(", ")"), 83 ("<", ">"), 84 *( 85 (c, c) 86 for c in string.ascii_letters + string.digits + string.punctuation 87 if c not in "[{(<" 88 ), 89 ) 90 ] 91 NATIONAL_RAW_STRINGS = [ 92 (prefix + start, end) 93 for start, end in t.cast(list[tuple[str, str]], RAW_STRINGS) 94 for prefix in ("n", "N") 95 ] 96 STRING_ESCAPES_ALLOWED_IN_RAW_STRINGS = False 97 98 NESTED_COMMENTS = False 99 100 KEYWORDS = { 101 **tokens.Tokenizer.KEYWORDS, 102 "(+)": TokenType.JOIN_MARKER, 103 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Comparison-Conditions.html 104 "^=": TokenType.NEQ, 105 "BINARY_DOUBLE": TokenType.DOUBLE, 106 "BINARY_FLOAT": TokenType.FLOAT, 107 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 108 "COLUMNS": TokenType.COLUMN, 109 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 110 "MINUS": TokenType.EXCEPT, 111 "NVARCHAR2": TokenType.NVARCHAR, 112 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 113 "SAMPLE": TokenType.TABLE_SAMPLE, 114 "START": TokenType.BEGIN, 115 "SUBPARTITION": TokenType.PARTITION, 116 "VARCHAR2": TokenType.VARCHAR, 117 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 118 } 119 120 Parser = OracleParser 121 122 Generator = OracleGenerator
17class Oracle(Dialect): 18 # Assume NLS_TERRITORY = 'AMERICA' for the DAY, DY and D truncation formats. 19 WEEK_OFFSET = -1 20 ALIAS_POST_TABLESAMPLE = True 21 LOCKING_READS_SUPPORTED = True 22 TABLESAMPLE_SIZE_IS_PERCENT = True 23 NULL_ORDERING = "nulls_are_large" 24 ON_CONDITION_EMPTY_BEFORE_ERROR = False 25 ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False 26 DISABLES_ALIAS_REF_EXPANSION = True 27 INTERSECT_BINDS_TIGHTER_THAN_UNION_AND_EXCEPT = False 28 29 # See section 8: https://docs.oracle.com/cd/A97630_01/server.920/a96540/sql_elements9a.htm 30 NORMALIZATION_STRATEGY = NormalizationStrategy.UPPERCASE 31 32 # https://docs.oracle.com/database/121/SQLRF/sql_elements004.htm#SQLRF00212 33 # https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes 34 TIME_MAPPING = { 35 "D": "%u", # Day of week (1-7) 36 "DAY": "%A", # name of day 37 "DD": "%d", # day of month (1-31) 38 "DDD": "%j", # day of year (1-366) 39 "DY": "%a", # abbreviated name of day 40 "HH": "%I", # Hour of day (1-12) 41 "HH12": "%I", # alias for HH 42 "HH24": "%H", # Hour of day (0-23) 43 "IW": "%V", # Calendar week of year (1-52 or 1-53), as defined by the ISO 8601 standard 44 "IYYY": "%G", # 4-digit year of the ISO 8601 week-numbering calendar 45 "MI": "%M", # Minute (0-59) 46 "MM": "%m", # Month (01-12; January = 01) 47 "MON": "%b", # Abbreviated name of month 48 "MONTH": "%B", # Name of month 49 "SS": "%S", # Second (0-59) 50 "WW": "%W", # Week of year (1-53) 51 "YY": "%y", # 15 52 "YYYY": "%Y", # 2015 53 "FF6": "%f", # only 6 digits are supported in python formats 54 } 55 56 PSEUDOCOLUMNS = {"ROWNUM", "ROWID", "OBJECT_ID", "OBJECT_VALUE", "LEVEL"} 57 58 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 59 # Disable quoting for pseudocolumns as it may break queries e.g 60 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 61 return ( 62 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 63 ) and super().can_quote(identifier, identify=identify) 64 65 class Tokenizer(tokens.Tokenizer): 66 VAR_SINGLE_TOKENS = {"@", "$", "#"} 67 68 UNICODE_STRINGS = [ 69 (prefix + q, q) 70 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 71 for prefix in ("U", "u") 72 ] 73 74 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html 75 # Alternative quoting, e.g. q'[it's]': the delimiters are part of the quote, so the 76 # text in between is taken as is, without escapes 77 RAW_STRINGS = [ 78 (f"{prefix}'{start}", f"{end}'") 79 for prefix in ("q", "Q") 80 for start, end in ( 81 ("[", "]"), 82 ("{", "}"), 83 ("(", ")"), 84 ("<", ">"), 85 *( 86 (c, c) 87 for c in string.ascii_letters + string.digits + string.punctuation 88 if c not in "[{(<" 89 ), 90 ) 91 ] 92 NATIONAL_RAW_STRINGS = [ 93 (prefix + start, end) 94 for start, end in t.cast(list[tuple[str, str]], RAW_STRINGS) 95 for prefix in ("n", "N") 96 ] 97 STRING_ESCAPES_ALLOWED_IN_RAW_STRINGS = False 98 99 NESTED_COMMENTS = False 100 101 KEYWORDS = { 102 **tokens.Tokenizer.KEYWORDS, 103 "(+)": TokenType.JOIN_MARKER, 104 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Comparison-Conditions.html 105 "^=": TokenType.NEQ, 106 "BINARY_DOUBLE": TokenType.DOUBLE, 107 "BINARY_FLOAT": TokenType.FLOAT, 108 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 109 "COLUMNS": TokenType.COLUMN, 110 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 111 "MINUS": TokenType.EXCEPT, 112 "NVARCHAR2": TokenType.NVARCHAR, 113 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 114 "SAMPLE": TokenType.TABLE_SAMPLE, 115 "START": TokenType.BEGIN, 116 "SUBPARTITION": TokenType.PARTITION, 117 "VARCHAR2": TokenType.VARCHAR, 118 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 119 } 120 121 Parser = OracleParser 122 123 Generator = OracleGenerator
First day of the week in DATE_TRUNC(week). Defaults to 0 (Monday). -1 would be Sunday.
Default NULL ordering method to use if not explicitly set.
Possible values: "nulls_are_small", "nulls_are_large", "nulls_are_last"
Whether "X ON EMPTY" should come before "X ON ERROR" (for dialects like T-SQL, MySQL, Oracle).
Whether alias reference expansion is disabled for this dialect.
Some dialects like Oracle do NOT support referencing aliases in projections or WHERE clauses. The original expression must be repeated instead.
For example, in Oracle: SELECT y.foo AS bar, bar * 2 AS baz FROM y -- INVALID SELECT y.foo AS bar, y.foo * 2 AS baz FROM y -- VALID
Whether INTERSECT binds more tightly than UNION and EXCEPT when a set operation chain isn't parenthesized. If False, all set operations have equal precedence and associate left to right.
Specifies the strategy according to which identifiers should be normalized.
Associates this dialect's time formats with their equivalent Python strftime formats.
Columns that are auto-generated by the engine corresponding to this dialect.
For example, such columns may be excluded from SELECT * queries.
58 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 59 # Disable quoting for pseudocolumns as it may break queries e.g 60 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 61 return ( 62 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 63 ) and super().can_quote(identifier, identify=identify)
Checks if an identifier can be quoted
Arguments:
- identifier: The identifier to check.
- identify:
True: Always returnsTrueexcept for certain cases."safe": Only returnsTrueif the identifier is case-insensitive."unsafe": Only returnsTrueif the identifier is case-sensitive.
Returns:
Whether the given text can be identified.
Whether string literals support escape sequences (e.g. \n). Set by the metaclass based on the tokenizer's STRING_ESCAPES.
Whether byte string literals support escape sequences. Set by the metaclass based on the tokenizer's BYTE_STRING_ESCAPES.
Mapping of an identifier escape char (\) to its escaped version (\\). Set by the metaclass based on the tokenizer's IDENTIFIER_ESCAPES.
65 class Tokenizer(tokens.Tokenizer): 66 VAR_SINGLE_TOKENS = {"@", "$", "#"} 67 68 UNICODE_STRINGS = [ 69 (prefix + q, q) 70 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 71 for prefix in ("U", "u") 72 ] 73 74 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html 75 # Alternative quoting, e.g. q'[it's]': the delimiters are part of the quote, so the 76 # text in between is taken as is, without escapes 77 RAW_STRINGS = [ 78 (f"{prefix}'{start}", f"{end}'") 79 for prefix in ("q", "Q") 80 for start, end in ( 81 ("[", "]"), 82 ("{", "}"), 83 ("(", ")"), 84 ("<", ">"), 85 *( 86 (c, c) 87 for c in string.ascii_letters + string.digits + string.punctuation 88 if c not in "[{(<" 89 ), 90 ) 91 ] 92 NATIONAL_RAW_STRINGS = [ 93 (prefix + start, end) 94 for start, end in t.cast(list[tuple[str, str]], RAW_STRINGS) 95 for prefix in ("n", "N") 96 ] 97 STRING_ESCAPES_ALLOWED_IN_RAW_STRINGS = False 98 99 NESTED_COMMENTS = False 100 101 KEYWORDS = { 102 **tokens.Tokenizer.KEYWORDS, 103 "(+)": TokenType.JOIN_MARKER, 104 # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Comparison-Conditions.html 105 "^=": TokenType.NEQ, 106 "BINARY_DOUBLE": TokenType.DOUBLE, 107 "BINARY_FLOAT": TokenType.FLOAT, 108 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 109 "COLUMNS": TokenType.COLUMN, 110 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 111 "MINUS": TokenType.EXCEPT, 112 "NVARCHAR2": TokenType.NVARCHAR, 113 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 114 "SAMPLE": TokenType.TABLE_SAMPLE, 115 "START": TokenType.BEGIN, 116 "SUBPARTITION": TokenType.PARTITION, 117 "VARCHAR2": TokenType.VARCHAR, 118 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 119 }
Inherited Members
- sqlglot.tokens.Tokenizer
- Tokenizer
- SINGLE_TOKENS
- BIT_STRINGS
- BYTE_STRINGS
- HEX_STRINGS
- HEREDOC_STRINGS
- UNICODE_IDENTIFIERS
- IDENTIFIERS
- QUOTES
- STRING_ESCAPES
- IDENTIFIER_ESCAPES
- HEREDOC_TAG_IS_IDENTIFIER
- HEREDOC_STRING_ALTERNATIVE
- NUMERIC_ESCAPES
- DROP_UNKNOWN_ESCAPES
- NUMERIC_ESCAPES_ARE_BYTES
- LONE_SURROGATE_REPLACEMENT
- DASH_COMMENT_REQUIRES_BOUNDARY
- COMMENTS_TERMINATE_AT_NEWLINE_ONLY
- UNQUOTED_PATH_TERMINATORS
- HINT_START
- TOKENS_PRECEDING_HINT
- COMMANDS
- COMMAND_PREFIX_TOKENS
- NUMERIC_LITERALS
- NUMBERS_CAN_HAVE_DECIMALS
- COMMENTS
- dialect
- tokenize
- sql
- size
- tokens