Skip to content

Commit cdf0f25

Browse files
committed
Guard insert table aliases with an explicit dialect flag
1 parent 9c3697e commit cdf0f25

5 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/dialect/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,17 @@ pub trait Dialect: Debug + Any {
12381238
false
12391239
}
12401240

1241+
/// Returns true if this dialect supports `INSERT INTO t AS alias ...`.
1242+
fn supports_insert_table_explicit_alias(&self) -> bool {
1243+
false
1244+
}
1245+
1246+
/// Returns true if this dialect supports `INSERT INTO t alias ...` with
1247+
/// `alias` _not_ preceded by the "AS" keyword.
1248+
fn supports_insert_table_implicit_alias(&self) -> bool {
1249+
false
1250+
}
1251+
12411252
/// Returns true if this dialect supports `SET` statements without an explicit
12421253
/// assignment operator such as `=`. For example: `SET SHOWPLAN_XML ON`.
12431254
fn supports_set_stmt_without_operator(&self) -> bool {

src/dialect/oracle.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,11 @@ impl Dialect for OracleDialect {
110110
fn supports_comment_optimizer_hint(&self) -> bool {
111111
true
112112
}
113+
114+
/// Supports insert table aliases (but with no preceding "AS" keyword)
115+
///
116+
/// See <https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/INSERT.html#GUID-903F8043-0254-4EE9-ACC1-CB8AC0AF3423__GUID-AC239E95-0DD5-4F4B-A849-55C87840D0E6>
117+
fn supports_insert_table_implicit_alias(&self) -> bool {
118+
true
119+
}
113120
}

src/dialect/postgresql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,10 @@ impl Dialect for PostgreSqlDialect {
288288
fn supports_interval_options(&self) -> bool {
289289
true
290290
}
291+
292+
/// [Postgres] support insert table aliases with an explicit "AS" keyword.
293+
/// See: <https://www.postgresql.org/docs/17/sql-insert.html>
294+
fn supports_insert_table_explicit_alias(&self) -> bool {
295+
true
296+
}
291297
}

src/parser/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17133,19 +17133,15 @@ impl<'a> Parser<'a> {
1713317133
let table = self.parse_keyword(Keyword::TABLE);
1713417134
let table_object = self.parse_table_object()?;
1713517135

17136-
let table_alias = if dialect_of!(self is OracleDialect) {
17137-
if !self.peek_sub_query()
17138-
&& self.peek_one_of_keywords(&[Keyword::DEFAULT, Keyword::VALUES]).is_none()
17139-
{
17136+
let table_alias = if self.dialect.supports_insert_table_implicit_alias()
17137+
&& !self.peek_sub_query()
17138+
&& self.peek_one_of_keywords(&[Keyword::AS, Keyword::DEFAULT, Keyword::VALUES]).is_none() {
1714017139
self.maybe_parse(|parser| parser.parse_identifier())?
1714117140
.map(|alias| TableAliasWithoutColumns {
1714217141
explicit: false,
1714317142
alias,
1714417143
})
17145-
} else {
17146-
None
17147-
}
17148-
} else if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::AS) {
17144+
} else if self.dialect.supports_insert_table_explicit_alias() && self.parse_keyword(Keyword::AS) {
1714917145
Some(TableAliasWithoutColumns {
1715017146
explicit: true,
1715117147
alias: self.parse_identifier()?,

tests/sqlparser_oracle.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,15 @@ fn test_insert_without_alias() {
483483
))
484484
);
485485

486+
// check AS
487+
let sql = "INSERT INTO AS t default SELECT 'a' FROM dual";
488+
assert_eq!(
489+
oracle_dialect.parse_sql_statements(sql),
490+
Err(ParserError::ParserError(
491+
"Expected: SELECT, VALUES, or a subquery in the query body, found: default".into()
492+
))
493+
);
494+
486495
// check SELECT
487496
let sql = "INSERT INTO t SELECT 'a' FROM dual";
488497
let stmt = oracle_dialect.verified_stmt(sql);

0 commit comments

Comments
 (0)