-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-33095][SQL] Support ALTER TABLE in JDBC v2 Table Catalog: add, update type and nullability of columns (MySQL dialect) #30025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4854728
[SPARK-33095] Support ALTER TABLE in JDBC v2 Table Catalog: add, upda…
ScrapCodes dee8534
Rebased to latest, and refactored as per the code changes from pr 29972.
ScrapCodes cbce070
fix formatting
ScrapCodes ac3600e
review feedback: quote strings and optimize imports.
ScrapCodes 230fed8
Pass type information to alter table for nullability change.
ScrapCodes 3fc4132
Revert "Pass type information to alter table for nullability change."
ScrapCodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...integration-tests/src/test/scala/org/apache/spark/sql/jdbc/v2/MySQLIntegrationSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.jdbc.v2 | ||
|
|
||
| import java.sql.Connection | ||
|
|
||
| import org.scalatest.time.SpanSugar._ | ||
|
|
||
| import org.apache.spark.SparkConf | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog | ||
| import org.apache.spark.sql.jdbc.{DatabaseOnDocker, DockerJDBCIntegrationSuite} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.tags.DockerTest | ||
|
|
||
| /** | ||
| * | ||
| * To run this test suite for a specific version (e.g., mysql:5.7.31): | ||
| * {{{ | ||
| * MYSQL_DOCKER_IMAGE_NAME=mysql:5.7.31 | ||
| * ./build/sbt -Pdocker-integration-tests "testOnly *v2*MySQLIntegrationSuite" | ||
| * | ||
| * }}} | ||
| * | ||
| */ | ||
| @DockerTest | ||
| class MySQLIntegrationSuite extends DockerJDBCIntegrationSuite with V2JDBCTest { | ||
| override val catalogName: String = "mysql" | ||
| override val db = new DatabaseOnDocker { | ||
| override val imageName = sys.env.getOrElse("MYSQL_DOCKER_IMAGE_NAME", "mysql:5.7.31") | ||
| override val env = Map( | ||
| "MYSQL_ROOT_PASSWORD" -> "rootpass" | ||
| ) | ||
| override val usesIpc = false | ||
| override val jdbcPort: Int = 3306 | ||
|
|
||
| override def getJdbcUrl(ip: String, port: Int): String = | ||
| s"jdbc:mysql://$ip:$port/mysql?user=root&password=rootpass" | ||
| } | ||
|
|
||
| override def sparkConf: SparkConf = super.sparkConf | ||
| .set("spark.sql.catalog.mysql", classOf[JDBCTableCatalog].getName) | ||
| .set("spark.sql.catalog.mysql.url", db.getJdbcUrl(dockerIp, externalPort)) | ||
|
|
||
| override val connectionTimeout = timeout(7.minutes) | ||
|
|
||
| override def dataPreparation(conn: Connection): Unit = {} | ||
|
|
||
| override def testUpdateColumnType(tbl: String): Unit = { | ||
| sql(s"CREATE TABLE $tbl (ID INTEGER) USING _") | ||
| var t = spark.table(tbl) | ||
| var expectedSchema = new StructType().add("ID", IntegerType) | ||
| assert(t.schema === expectedSchema) | ||
| sql(s"ALTER TABLE $tbl ALTER COLUMN id TYPE STRING") | ||
| t = spark.table(tbl) | ||
| expectedSchema = new StructType().add("ID", StringType) | ||
| assert(t.schema === expectedSchema) | ||
| // Update column type from STRING to INTEGER | ||
| val msg1 = intercept[AnalysisException] { | ||
| sql(s"ALTER TABLE $tbl ALTER COLUMN id TYPE INTEGER") | ||
| }.getMessage | ||
| assert(msg1.contains("Cannot update alt_table field ID: string cannot be cast to int")) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah now I get what you mean. The
AlterTablelogical plan does have the table schema, but the catalog API doesn't pass it in. It's not possible to change the catalog API at this point, and it's also not worthy to add an extra table lookup here just to support update nullability in MySQL.I think the first version is fine. Sorry for the back and forth!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks will do so ! 👍