sqlglot.dialects.oracle
1from __future__ import annotations 2 3import typing as t 4 5from sqlglot import exp, tokens 6from sqlglot.dialects.dialect import ( 7 Dialect, 8 NormalizationStrategy, 9) 10from sqlglot.generators.oracle import OracleGenerator 11from sqlglot.parsers.oracle import OracleParser 12from sqlglot.tokens import TokenType 13 14 15class Oracle(Dialect): 16 ALIAS_POST_TABLESAMPLE = True 17 LOCKING_READS_SUPPORTED = True 18 TABLESAMPLE_SIZE_IS_PERCENT = True 19 NULL_ORDERING = "nulls_are_large" 20 ON_CONDITION_EMPTY_BEFORE_ERROR = False 21 ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False 22 DISABLES_ALIAS_REF_EXPANSION = True 23 24 # See section 8: https://docs.oracle.com/cd/A97630_01/server.920/a96540/sql_elements9a.htm 25 NORMALIZATION_STRATEGY = NormalizationStrategy.UPPERCASE 26 27 # https://docs.oracle.com/database/121/SQLRF/sql_elements004.htm#SQLRF00212 28 # https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes 29 TIME_MAPPING = { 30 "D": "%u", # Day of week (1-7) 31 "DAY": "%A", # name of day 32 "DD": "%d", # day of month (1-31) 33 "DDD": "%j", # day of year (1-366) 34 "DY": "%a", # abbreviated name of day 35 "HH": "%I", # Hour of day (1-12) 36 "HH12": "%I", # alias for HH 37 "HH24": "%H", # Hour of day (0-23) 38 "IW": "%V", # Calendar week of year (1-52 or 1-53), as defined by the ISO 8601 standard 39 "MI": "%M", # Minute (0-59) 40 "MM": "%m", # Month (01-12; January = 01) 41 "MON": "%b", # Abbreviated name of month 42 "MONTH": "%B", # Name of month 43 "SS": "%S", # Second (0-59) 44 "WW": "%W", # Week of year (1-53) 45 "YY": "%y", # 15 46 "YYYY": "%Y", # 2015 47 "FF6": "%f", # only 6 digits are supported in python formats 48 } 49 50 PSEUDOCOLUMNS = {"ROWNUM", "ROWID", "OBJECT_ID", "OBJECT_VALUE", "LEVEL"} 51 52 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 53 # Disable quoting for pseudocolumns as it may break queries e.g 54 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 55 return ( 56 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 57 ) and super().can_quote(identifier, identify=identify) 58 59 class Tokenizer(tokens.Tokenizer): 60 VAR_SINGLE_TOKENS = {"@", "$", "#"} 61 62 UNICODE_STRINGS = [ 63 (prefix + q, q) 64 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 65 for prefix in ("U", "u") 66 ] 67 68 NESTED_COMMENTS = False 69 70 KEYWORDS = { 71 **tokens.Tokenizer.KEYWORDS, 72 "(+)": TokenType.JOIN_MARKER, 73 "BINARY_DOUBLE": TokenType.DOUBLE, 74 "BINARY_FLOAT": TokenType.FLOAT, 75 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 76 "COLUMNS": TokenType.COLUMN, 77 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 78 "MINUS": TokenType.EXCEPT, 79 "NVARCHAR2": TokenType.NVARCHAR, 80 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 81 "SAMPLE": TokenType.TABLE_SAMPLE, 82 "START": TokenType.BEGIN, 83 "TOP": TokenType.TOP, 84 "VARCHAR2": TokenType.VARCHAR, 85 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 86 } 87 88 Parser = OracleParser 89 90 Generator = OracleGenerator
16class Oracle(Dialect): 17 ALIAS_POST_TABLESAMPLE = True 18 LOCKING_READS_SUPPORTED = True 19 TABLESAMPLE_SIZE_IS_PERCENT = True 20 NULL_ORDERING = "nulls_are_large" 21 ON_CONDITION_EMPTY_BEFORE_ERROR = False 22 ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False 23 DISABLES_ALIAS_REF_EXPANSION = True 24 25 # See section 8: https://docs.oracle.com/cd/A97630_01/server.920/a96540/sql_elements9a.htm 26 NORMALIZATION_STRATEGY = NormalizationStrategy.UPPERCASE 27 28 # https://docs.oracle.com/database/121/SQLRF/sql_elements004.htm#SQLRF00212 29 # https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes 30 TIME_MAPPING = { 31 "D": "%u", # Day of week (1-7) 32 "DAY": "%A", # name of day 33 "DD": "%d", # day of month (1-31) 34 "DDD": "%j", # day of year (1-366) 35 "DY": "%a", # abbreviated name of day 36 "HH": "%I", # Hour of day (1-12) 37 "HH12": "%I", # alias for HH 38 "HH24": "%H", # Hour of day (0-23) 39 "IW": "%V", # Calendar week of year (1-52 or 1-53), as defined by the ISO 8601 standard 40 "MI": "%M", # Minute (0-59) 41 "MM": "%m", # Month (01-12; January = 01) 42 "MON": "%b", # Abbreviated name of month 43 "MONTH": "%B", # Name of month 44 "SS": "%S", # Second (0-59) 45 "WW": "%W", # Week of year (1-53) 46 "YY": "%y", # 15 47 "YYYY": "%Y", # 2015 48 "FF6": "%f", # only 6 digits are supported in python formats 49 } 50 51 PSEUDOCOLUMNS = {"ROWNUM", "ROWID", "OBJECT_ID", "OBJECT_VALUE", "LEVEL"} 52 53 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 54 # Disable quoting for pseudocolumns as it may break queries e.g 55 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 56 return ( 57 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 58 ) and super().can_quote(identifier, identify=identify) 59 60 class Tokenizer(tokens.Tokenizer): 61 VAR_SINGLE_TOKENS = {"@", "$", "#"} 62 63 UNICODE_STRINGS = [ 64 (prefix + q, q) 65 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 66 for prefix in ("U", "u") 67 ] 68 69 NESTED_COMMENTS = False 70 71 KEYWORDS = { 72 **tokens.Tokenizer.KEYWORDS, 73 "(+)": TokenType.JOIN_MARKER, 74 "BINARY_DOUBLE": TokenType.DOUBLE, 75 "BINARY_FLOAT": TokenType.FLOAT, 76 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 77 "COLUMNS": TokenType.COLUMN, 78 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 79 "MINUS": TokenType.EXCEPT, 80 "NVARCHAR2": TokenType.NVARCHAR, 81 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 82 "SAMPLE": TokenType.TABLE_SAMPLE, 83 "START": TokenType.BEGIN, 84 "TOP": TokenType.TOP, 85 "VARCHAR2": TokenType.VARCHAR, 86 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 87 } 88 89 Parser = OracleParser 90 91 Generator = OracleGenerator
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
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.
53 def can_quote(self, identifier: exp.Identifier, identify: str | bool = "safe") -> bool: 54 # Disable quoting for pseudocolumns as it may break queries e.g 55 # `WHERE "ROWNUM" = ...` does not work but `WHERE ROWNUM = ...` does 56 return ( 57 identifier.quoted or not isinstance(identifier.parent, exp.Pseudocolumn) 58 ) 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.
60 class Tokenizer(tokens.Tokenizer): 61 VAR_SINGLE_TOKENS = {"@", "$", "#"} 62 63 UNICODE_STRINGS = [ 64 (prefix + q, q) 65 for q in t.cast(list[str], tokens.Tokenizer.QUOTES) 66 for prefix in ("U", "u") 67 ] 68 69 NESTED_COMMENTS = False 70 71 KEYWORDS = { 72 **tokens.Tokenizer.KEYWORDS, 73 "(+)": TokenType.JOIN_MARKER, 74 "BINARY_DOUBLE": TokenType.DOUBLE, 75 "BINARY_FLOAT": TokenType.FLOAT, 76 "BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO, 77 "COLUMNS": TokenType.COLUMN, 78 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 79 "MINUS": TokenType.EXCEPT, 80 "NVARCHAR2": TokenType.NVARCHAR, 81 "ORDER SIBLINGS BY": TokenType.ORDER_SIBLINGS_BY, 82 "SAMPLE": TokenType.TABLE_SAMPLE, 83 "START": TokenType.BEGIN, 84 "TOP": TokenType.TOP, 85 "VARCHAR2": TokenType.VARCHAR, 86 "SYSTIMESTAMP": TokenType.SYSTIMESTAMP, 87 }
Inherited Members
- sqlglot.tokens.Tokenizer
- Tokenizer
- SINGLE_TOKENS
- BIT_STRINGS
- BYTE_STRINGS
- HEX_STRINGS
- RAW_STRINGS
- HEREDOC_STRINGS
- IDENTIFIERS
- QUOTES
- STRING_ESCAPES
- ESCAPE_FOLLOW_CHARS
- IDENTIFIER_ESCAPES
- HEREDOC_TAG_IS_IDENTIFIER
- HEREDOC_STRING_ALTERNATIVE
- STRING_ESCAPES_ALLOWED_IN_RAW_STRINGS
- HINT_START
- TOKENS_PRECEDING_HINT
- COMMANDS
- COMMAND_PREFIX_TOKENS
- NUMERIC_LITERALS
- NUMBERS_CAN_HAVE_DECIMALS
- COMMENTS
- dialect
- tokenize
- sql
- size
- tokens