Skip to content

Commit

Permalink
feat(oracle): Add support for BULK COLLECT INTO
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoisen committed Oct 1, 2024
1 parent 39b928d commit 4c262c3
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 32 deletions.
1 change: 0 additions & 1 deletion sqlglot/dialects/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ class Generator(generator.Generator):
TABLESAMPLE_KEYWORDS = "SAMPLE"
LAST_DAY_SUPPORTS_DATE_PART = False
SUPPORTS_SELECT_INTO = True
SUPPORTS_SELECT_BULK_COLLECT_INTO = True
TZ_TO_WITH_TIME_ZONE = True

TYPE_MAPPING = {
Expand Down
6 changes: 1 addition & 5 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,11 +2106,7 @@ class PrimaryKey(Expression):
# https://www.postgresql.org/docs/9.1/sql-selectinto.html
# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples
class Into(Expression):
arg_types = {"this": True, "temporary": False, "unlogged": False}


class BulkCollectInto(Expression):
arg_types = {"this": True}
arg_types = {"this": True, "temporary": False, "unlogged": False, "bulk_collect_into": False}


class From(Expression):
Expand Down
14 changes: 2 additions & 12 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,6 @@ class Generator(metaclass=_Generator):
# Whether the SELECT .. INTO syntax is used instead of CTAS
SUPPORTS_SELECT_INTO = False

# Whether the SELECT .. BULK COLLECT INTO syntax is supported
SUPPORTS_SELECT_BULK_COLLECT_INTO = False

# Whether UNLOGGED tables can be created
SUPPORTS_UNLOGGED_TABLES = False

Expand Down Expand Up @@ -2044,10 +2041,8 @@ def var_sql(self, expression: exp.Var) -> str:
def into_sql(self, expression: exp.Into) -> str:
temporary = " TEMPORARY" if expression.args.get("temporary") else ""
unlogged = " UNLOGGED" if expression.args.get("unlogged") else ""
return f"{self.seg('INTO')}{temporary or unlogged} {self.sql(expression, 'this')}"

def bulkcollectinto_sql(self, expression: exp.BulkCollectInto) -> str:
return f"{self.seg('BULK COLLECT INTO')} {self.sql(expression, 'this')}"
into = "INTO" if not expression.args.get("bulk_collect_into") else "BULK COLLECT INTO"
return f"{self.seg(into)}{temporary or unlogged} {self.sql(expression, 'this')}"

def from_sql(self, expression: exp.From) -> str:
return f"{self.seg('FROM')} {self.sql(expression, 'this')}"
Expand Down Expand Up @@ -2470,10 +2465,6 @@ def select_sql(self, expression: exp.Select) -> str:
if not self.SUPPORTS_SELECT_INTO and into:
into.pop()

bulk_collect_into = expression.args.get("bulk_collect_into")
if not self.SUPPORTS_SELECT_BULK_COLLECT_INTO and bulk_collect_into:
bulk_collect_into.pop()

hint = self.sql(expression, "hint")
distinct = self.sql(expression, "distinct")
distinct = f" {distinct}" if distinct else ""
Expand Down Expand Up @@ -2517,7 +2508,6 @@ def select_sql(self, expression: exp.Select) -> str:
expression,
f"SELECT{top_distinct}{kind}{expressions}",
self.sql(expression, "into", comment=False),
self.sql(expression, "bulk_collect_into", comment=False),
self.sql(expression, "from", comment=False),
)

Expand Down
21 changes: 9 additions & 12 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2969,10 +2969,6 @@ def _parse_select(
if into:
this.set("into", into)

bulk_collect_into = self._parse_bulk_collect_into()
if bulk_collect_into:
this.set("bulk_collect_into", bulk_collect_into)

if not from_:
from_ = self._parse_from()

Expand Down Expand Up @@ -3188,23 +3184,24 @@ def _parse_hint(self) -> t.Optional[exp.Hint]:
return None

def _parse_into(self) -> t.Optional[exp.Into]:
if not self._match(TokenType.INTO):
# https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/SELECT-INTO-statement.html
bulk_collect_into = self._match_text_seq("BULK", "COLLECT", "INTO")

if not bulk_collect_into and not self._match(TokenType.INTO):
return None

temp = self._match(TokenType.TEMPORARY)
unlogged = self._match_text_seq("UNLOGGED")
self._match(TokenType.TABLE)

return self.expression(
exp.Into, this=self._parse_table(schema=True), temporary=temp, unlogged=unlogged
exp.Into,
this=self._parse_table(schema=True),
temporary=temp,
unlogged=unlogged,
bulk_collect_into=bulk_collect_into,
)

def _parse_bulk_collect_into(self) -> t.Optional[exp.BulkCollectInto]:
if not self._match(TokenType.BULK_COLLECT_INTO):
return None

return self.expression(exp.BulkCollectInto, this=self._parse_table(schema=True))

def _parse_from(
self, joins: bool = False, skip_from_token: bool = False
) -> t.Optional[exp.From]:
Expand Down
2 changes: 0 additions & 2 deletions sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class TokenType(AutoName):
AUTO_INCREMENT = auto()
BEGIN = auto()
BETWEEN = auto()
BULK_COLLECT_INTO = auto()
CACHE = auto()
CASE = auto()
CHARACTER_SET = auto()
Expand Down Expand Up @@ -662,7 +661,6 @@ class Tokenizer(metaclass=_Tokenizer):
"AUTO_INCREMENT": TokenType.AUTO_INCREMENT,
"BEGIN": TokenType.BEGIN,
"BETWEEN": TokenType.BETWEEN,
"BULK COLLECT INTO": TokenType.BULK_COLLECT_INTO,
"CACHE": TokenType.CACHE,
"UNCACHE": TokenType.UNCACHE,
"CASE": TokenType.CASE,
Expand Down

0 comments on commit 4c262c3

Please sign in to comment.