You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "allisonwang-db (via GitHub)" <gi...@apache.org> on 2023/12/05 20:19:58 UTC
[PR] [SPARK-46272][SQL][TESTS] Support CTAS using DSv2 sources [spark]
allisonwang-db opened a new pull request, #44190:
URL: https://github.com/apache/spark/pull/44190
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
https://github.com/apache/spark/pull/43949 supports CREATE TABLE using DSv2 sources. This PR supports CREATE TABLE AS SELECT (CTAS) using DSv2 sources. It turns out that we don't need additional code changes. This PR simply adds more test cases for CTAS queries.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
To add tests for CTAS for DSv2 sources.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
New tests
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1425084776
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2SessionCatalog.scala:
##########
@@ -232,7 +233,19 @@ class V2SessionCatalog(catalog: SessionCatalog)
throw QueryCompilationErrors.tableAlreadyExistsError(ident)
}
- loadTable(ident)
+ val table = loadTable(ident)
+
+ // Check if the schema of the created table matches the given schema.
+ if (schema.nonEmpty) {
+ val tableSchema = CharVarcharUtils.replaceCharVarcharWithStringInSchema(
+ table.columns().asSchema)
+ if (!DataType.equalsIgnoreNullability(tableSchema, schema)) {
Review Comment:
can we check it in `loadTable`? In case the data source is a bit random and only return wrong schema at second time.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1424656357
##########
common/utils/src/main/resources/error/error-classes.json:
##########
@@ -887,6 +887,11 @@
],
"sqlState" : "42K02"
},
+ "DATA_SOURCE_TABLE_SCHEMA_MISMATCH" : {
+ "message" : [
+ "The schema of the data source table <tableSchema> does not match the actual schema <actualSchema>. If you are using the DataFrameReader.schema API or creating a table, avoid specifying the schema."
+ ]
Review Comment:
sqlState is required now. maybe `42K02` is fine
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #44190: [SPARK-46272][SQL] Support CTAS using DSv2 sources
URL: https://github.com/apache/spark/pull/44190
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1422053024
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -723,6 +724,158 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
}
+
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(0, 1), Row(1, 2)))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ }
+ }
+
+ test("SPARK-46272: create table as select - error cases") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ // CTAS with too many columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1, 2), (1, 2, 3)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS",
Review Comment:
This is actually tricky, and behavior depends on the implementation of the data source.
In `createTable`, we store the query's schema (col1, col2, col3) in HMS, but this data source's `getTable` method does not take into account the input `schema`.
So, when the DSv2 relation is being created, it uses `table.columns.asSchema` which is the default implementation of the schema (i, j).
https://github.com/apache/spark/blob/bacdb3b5fec9783f46042764eeee80eb2a0f5702/sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala#L210-L211
However, if the data source utilize user-defined schema in `getTable`, it won't throw this exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #44190:
URL: https://github.com/apache/spark/pull/44190#issuecomment-1856326748
LGTM if all tests pass
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1416326853
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala:
##########
@@ -914,8 +914,8 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
unsupportedTableOperationError(table.name(), "either micro-batch or continuous scan")
}
- def unsupportedAppendInBatchModeError(table: Table): Throwable = {
- unsupportedTableOperationError(table.name(), "append in batch mode")
+ def unsupportedAppendInBatchModeError(name: String): Throwable = {
+ unsupportedTableOperationError(name, "append in batch mode")
Review Comment:
I see! Thanks for letting me know!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1424657051
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala:
##########
@@ -85,6 +86,11 @@ case class CreateTableAsSelectExec(
val table = catalog.createTable(
ident, getV2Columns(query.schema, catalog.useNullableQuerySchema),
partitioning.toArray, properties.asJava)
+ // Check if the table schema matches the schema of the query.
Review Comment:
shall we do this check in a lower level? where we instantiate the v2 Table.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1424951174
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala:
##########
@@ -85,6 +86,11 @@ case class CreateTableAsSelectExec(
val table = catalog.createTable(
ident, getV2Columns(query.schema, catalog.useNullableQuerySchema),
partitioning.toArray, properties.asJava)
+ // Check if the table schema matches the schema of the query.
Review Comment:
Make sense
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #44190:
URL: https://github.com/apache/spark/pull/44190#issuecomment-1864044746
thanks, merging to master!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1422053024
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -723,6 +724,158 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
}
+
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(0, 1), Row(1, 2)))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ }
+ }
+
+ test("SPARK-46272: create table as select - error cases") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ // CTAS with too many columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1, 2), (1, 2, 3)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS",
Review Comment:
This is actually tricky, and the behavior depends on the implementation of the data source.
In `createTable`, we store the query's schema (col1, col2, col3) in HMS, but this data source's `getTable` method does not take into account the input `schema`.
So, when the DSv2 relation is being created, it uses `table.columns.asSchema` which is the default implementation of the schema (i, j).
https://github.com/apache/spark/blob/bacdb3b5fec9783f46042764eeee80eb2a0f5702/sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala#L210-L211
However, if the data source utilize user-defined schema in `getTable`, it won't throw this exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1425326906
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2SessionCatalog.scala:
##########
@@ -232,7 +233,19 @@ class V2SessionCatalog(catalog: SessionCatalog)
throw QueryCompilationErrors.tableAlreadyExistsError(ident)
}
- loadTable(ident)
+ val table = loadTable(ident)
+
+ // Check if the schema of the created table matches the given schema.
+ if (schema.nonEmpty) {
+ val tableSchema = CharVarcharUtils.replaceCharVarcharWithStringInSchema(
+ table.columns().asSchema)
+ if (!DataType.equalsIgnoreNullability(tableSchema, schema)) {
Review Comment:
I tried it but looks like `loadTable` is used in many other places, such as in `tableExists`. If we check the schema there, it will fail commands other than create table (e.g `DROP TABLE t` will fail because the schema in catalogTable does not match the table schema)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on PR #44190:
URL: https://github.com/apache/spark/pull/44190#issuecomment-1841896006
cc @cloud-fan
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1424659710
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -725,8 +725,127 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[WritableDataSourceSupportsExternalMetadata]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2) t(i, j)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq((0, 1), (1, 2)).toDF("i", "j"))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5) t(i, j)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq((2, 3), (4, 5)).toDF("i", "j"))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq((2, 3), (4, 5)).toDF("i", "j"))
+ }
+ }
+
+ test("SPARK-46272: create table as select - schema name mismatch") {
Review Comment:
can we have create table test (no as select) for schema mismatch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1418295715
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -723,6 +724,158 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
}
+
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(0, 1), Row(1, 2)))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ }
+ }
+
+ test("SPARK-46272: create table as select - error cases") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ // CTAS with too many columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1, 2), (1, 2, 3)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS",
Review Comment:
I'm a bit confused here. So for this data source, we probably don't check anything when creating the table. But when load the table, and get the `Table` instance in `V2SessionCatalog`, shall we check if `Table#schema` match what we stored in HMS?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on PR #44190:
URL: https://github.com/apache/spark/pull/44190#issuecomment-1858655801
@cloud-fan the test failure seems unrelated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1418289369
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -723,6 +724,158 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
}
+
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(0, 1), Row(1, 2)))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ }
+ }
+
+ test("SPARK-46272: create table as select - error cases") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ // CTAS with too many columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1, 2), (1, 2, 3)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS",
+ parameters = Map(
+ "tableName" -> "`spark_catalog`.`default`.`test`",
+ "tableColumns" -> "`i`, `j`",
+ "dataColumns" -> "`col1`, `col2`, `col3`"))
+ }
+ // CTAS with not enough columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0), (1)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.NOT_ENOUGH_DATA_COLUMNS",
+ parameters = Map(
+ "tableName" -> "`spark_catalog`.`default`.`test`",
+ "tableColumns" -> "`i`, `j`",
+ "dataColumns" -> "`col1`"))
+ }
+ // CTAS with type mismatch
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES ('a', 'b'), ('c', 'd')
+ |""".stripMargin)
+ },
+ errorClass = "INCOMPATIBLE_DATA_FOR_TABLE.CANNOT_SAFELY_CAST",
+ parameters = Map(
+ "tableName" -> "`spark_catalog`.`default`.`test`",
+ "colName" -> "`i`", "srcType" -> "\"STRING\"", "targetType" -> "\"INT\""))
+ }
+ // CTAS with schema specified
Review Comment:
This is not related to data source and we probably don't need it here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL][TESTS] Support CTAS using DSv2 sources [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1416261141
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala:
##########
@@ -914,8 +914,8 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
unsupportedTableOperationError(table.name(), "either micro-batch or continuous scan")
}
- def unsupportedAppendInBatchModeError(table: Table): Throwable = {
- unsupportedTableOperationError(table.name(), "append in batch mode")
+ def unsupportedAppendInBatchModeError(name: String): Throwable = {
+ unsupportedTableOperationError(name, "append in batch mode")
Review Comment:
When we change `src/main`, you don't need to use `[TESTS]`, @allisonwang-db .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44190:
URL: https://github.com/apache/spark/pull/44190#discussion_r1423115936
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2Suite.scala:
##########
@@ -723,6 +724,158 @@ class DataSourceV2Suite extends QueryTest with SharedSparkSession with AdaptiveS
}
}
}
+
+ test("SPARK-46272: create table as select") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ withTable("test") {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1), (1, 2)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(0, 1), Row(1, 2)))
+ sql(
+ s"""
+ |CREATE OR REPLACE TABLE test USING ${cls.getName}
+ |AS VALUES (2, 3), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ sql(
+ s"""
+ |CREATE TABLE IF NOT EXISTS test USING ${cls.getName}
+ |AS VALUES (3, 4), (4, 5)
+ |""".stripMargin)
+ checkAnswer(sql("SELECT * FROM test"), Seq(Row(2, 3), Row(4, 5)))
+ }
+ }
+
+ test("SPARK-46272: create table as select - error cases") {
+ val cls = classOf[SupportsExternalMetadataDataSource]
+ // CTAS with too many columns
+ withTable("test") {
+ checkError(
+ exception = intercept[AnalysisException] {
+ sql(
+ s"""
+ |CREATE TABLE test USING ${cls.getName}
+ |AS VALUES (0, 1, 2), (1, 2, 3)
+ |""".stripMargin)
+ },
+ errorClass = "INSERT_COLUMN_ARITY_MISMATCH.TOO_MANY_DATA_COLUMNS",
Review Comment:
I think we should enforce something here. When calling `TableProvider.getTable` with user-specified schema, we should make sure the returned table reports the same schema as the user-specified schema (probably ignore nullability). It's actually a documented requirement
```
* Return a {@link Table} instance with the specified table schema, partitioning and properties
* to do read/write. The returned table should report the same schema and partitioning with the
* specified ones, or Spark may fail the operation.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-46272][SQL] Support CTAS using DSv2 sources [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #44190:
URL: https://github.com/apache/spark/pull/44190#issuecomment-1858658174
Can you retrigger the GC jobs?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org