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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]