diff --git a/docs/sql-ref-syntax-dml-insert-overwrite-directory.md b/docs/sql-ref-syntax-dml-insert-overwrite-directory.md index 60ac345121b05..3ef32bdb5e4ce 100644 --- a/docs/sql-ref-syntax-dml-insert-overwrite-directory.md +++ b/docs/sql-ref-syntax-dml-insert-overwrite-directory.md @@ -77,8 +77,8 @@ USING file_format [ OPTIONS ( key = val [ , ... ] ) ] * **query** A query that produces the rows to be inserted. It can be in one of following formats: - * a `SELECT` statement - * a `TABLE` statement + * a [SELECT](sql-ref-syntax-qry-select.html) statement + * a [Inline Table](sql-ref-syntax-qry-select-inline-table.html) statement * a `FROM` statement ### Examples diff --git a/docs/sql-ref-syntax-dml-insert-table.md b/docs/sql-ref-syntax-dml-insert-table.md index eb88c56e0bb92..504ced437047b 100644 --- a/docs/sql-ref-syntax-dml-insert-table.md +++ b/docs/sql-ref-syntax-dml-insert-table.md @@ -61,8 +61,8 @@ INSERT [ INTO | OVERWRITE ] [ TABLE ] table_identifier [ partition_spec ] [ ( co * **query** A query that produces the rows to be inserted. It can be in one of following formats: - * a `SELECT` statement - * a `TABLE` statement + * a [SELECT](sql-ref-syntax-qry-select.html) statement + * a [Inline Table](sql-ref-syntax-qry-select-inline-table.html) statement * a `FROM` statement ### Examples diff --git a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 index 8c492574a90a8..8aca8eda190ee 100644 --- a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 +++ b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 @@ -445,7 +445,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 diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala index b4232fb38a782..82c8891730ef5 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala @@ -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)) } /** diff --git a/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala index 73fdf2804b2c6..85d325ecd2e85 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala @@ -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 {