Skip to content

Commit 575ee26

Browse files
authored
Support multiple targets for SELECT INTO (apache#2360)
1 parent 42840f0 commit 575ee26

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/ast/query.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,8 +3715,11 @@ pub struct SelectInto {
37153715
pub unlogged: bool,
37163716
/// `TABLE` keyword present.
37173717
pub table: bool,
3718-
/// Name of the target table.
3719-
pub name: ObjectName,
3718+
/// Target(s) of the `INTO` clause.
3719+
///
3720+
/// [Postgres]: https://www.postgresql.org/docs/current/sql-selectinto.html
3721+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.7/en/select-into.html
3722+
pub targets: Vec<Expr>,
37203723
}
37213724

37223725
impl fmt::Display for SelectInto {
@@ -3725,7 +3728,14 @@ impl fmt::Display for SelectInto {
37253728
let unlogged = if self.unlogged { " UNLOGGED" } else { "" };
37263729
let table = if self.table { " TABLE" } else { "" };
37273730

3728-
write!(f, "INTO{}{}{} {}", temporary, unlogged, table, self.name)
3731+
write!(
3732+
f,
3733+
"INTO{}{}{} {}",
3734+
temporary,
3735+
unlogged,
3736+
table,
3737+
display_comma_separated(&self.targets)
3738+
)
37293739
}
37303740
}
37313741

src/ast/spans.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,10 +2392,10 @@ impl Spanned for SelectInto {
23922392
temporary: _, // bool
23932393
unlogged: _, // bool
23942394
table: _, // bool
2395-
name,
2395+
targets,
23962396
} = self;
23972397

2398-
name.span()
2398+
union_spans(targets.iter().map(|t| t.span()))
23992399
}
24002400
}
24012401

src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19627,13 +19627,13 @@ impl<'a> Parser<'a> {
1962719627
.is_some();
1962819628
let unlogged = self.parse_keyword(Keyword::UNLOGGED);
1962919629
let table = self.parse_keyword(Keyword::TABLE);
19630-
let name = self.parse_object_name(false)?;
19630+
let targets = self.parse_comma_separated(Parser::parse_expr)?;
1963119631

1963219632
Ok(SelectInto {
1963319633
temporary,
1963419634
unlogged,
1963519635
table,
19636-
name,
19636+
targets,
1963719637
})
1963819638
}
1963919639

tests/sqlparser_common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ fn parse_select_into() {
11181118
temporary: false,
11191119
unlogged: false,
11201120
table: false,
1121-
name: ObjectName::from(vec![Ident::new("table0")]),
1121+
targets: vec![Expr::Identifier(Ident::new("table0"))],
11221122
},
11231123
only(&select.into)
11241124
);
@@ -1129,6 +1129,10 @@ fn parse_select_into() {
11291129
"SELECT * INTO TEMPORARY UNLOGGED TABLE table0 FROM table1",
11301130
);
11311131

1132+
verified_only_select("SELECT a, b INTO foo.bar, bar.baz FROM t");
1133+
verified_stmt("SELECT a, b, c INTO p, q, r FROM t");
1134+
verified_stmt("SELECT a, b INTO :h1, :h2 FROM t");
1135+
11321136
// Do not allow aliases here
11331137
let sql = "SELECT * INTO table0 asdf FROM table1";
11341138
let result = parse_sql_statements(sql);

0 commit comments

Comments
 (0)