Skip to content

Commit

Permalink
[SPARK-36980][SQL] Insert support query with CTE
Browse files Browse the repository at this point in the history
  • Loading branch information
AngersZhuuuu committed Oct 12, 2021
1 parent 6e8cd3b commit 8a1b4e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ resource
;

dmlStatementNoWith
: insertInto queryTerm queryOrganization #singleInsertQuery
: insertInto query #singleInsertQuery
| fromClause multiInsertQueryBody+ #multiInsertQuery
| DELETE FROM multipartIdentifier tableAlias whereClause? #deleteFromTable
| UPDATE multipartIdentifier tableAlias setClause whereClause? #updateTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
*/
override def visitSingleInsertQuery(
ctx: SingleInsertQueryContext): LogicalPlan = withOrigin(ctx) {
withInsertInto(
ctx.insertInto(),
plan(ctx.queryTerm).optionalMap(ctx.queryOrganization)(withQueryResultClauses))
withInsertInto(ctx.insertInto(), visitQuery(ctx.query))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,14 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
}
}
}

test("SPARK-36980: Insert support query with CTE") {
withTable("t") {
sql("CREATE TABLE t(i int, part1 int, part2 int) using parquet")
sql("INSERT INTO t WITH v1(c1) as (values (1)) select 1, 2,3 from v1")
checkAnswer(spark.table("t"), Row(1, 2, 3))
}
}
}

class FileExistingTestFileSystem extends RawLocalFileSystem {
Expand Down

0 comments on commit 8a1b4e4

Please sign in to comment.