huaxingao commented on a change in pull request #29324:
URL: https://github.com/apache/spark/pull/29324#discussion_r464021800



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala
##########
@@ -184,15 +189,56 @@ abstract class JdbcDialect extends Serializable {
   /**
    * Rename an existing table.
    *
-   * TODO (SPARK-32382): Override this method in the dialects that don't 
support such syntax.
-   *
    * @param oldTable The existing table.
    * @param newTable New name of the table.
    * @return The SQL statement to use for renaming the table.
    */
   def renameTable(oldTable: String, newTable: String): String = {
     s"ALTER TABLE $oldTable RENAME TO $newTable"
   }
+
+  /**
+   * Alter an existing table.
+   *
+   * @param tableName The name of the table to be altered.
+   * @param changes Changes to apply to the table.
+   * @return The SQL statement to use for altering the table.
+   */
+  def alterTable(tableName: String, changes: Seq[TableChange]): Array[String] 
= {
+    val updateClause = mutable.ArrayBuilder.make[String]
+    for (change <- changes) {
+      change match {
+        case add: AddColumn =>
+          add.fieldNames match {
+            case Array(name) =>
+              val dataType = JdbcUtils.getJdbcType(add.dataType(), 
this).databaseTypeDefinition
+              updateClause += s"ALTER TABLE $tableName ADD COLUMN $name 
$dataType"
+            case _ =>
+              throw new IllegalArgumentException(s"Unsupported TableChange 
fieldNames" +
+                s" ${add.fieldNames}")
+          }
+        case rename: RenameColumn =>
+          rename.fieldNames match {
+            case Array(name) =>
+              updateClause += s"ALTER TABLE $tableName RENAME COLUMN $name TO 
${rename.newName}"
+            case _ =>
+              throw new IllegalArgumentException(s"Unsupported TableChange 
fieldNames" +
+                s" ${rename.fieldNames}")
+          }
+        case delete: DeleteColumn =>
+          delete.fieldNames match {
+            case Array(name) =>
+              updateClause += s"ALTER TABLE $tableName DROP COLUMN $name"
+            case _ =>
+              throw new IllegalArgumentException(s"Unsupported TableChange 
fieldNames" +
+                s" ${delete.fieldNames}")
+          }
+        case _ => throw new IllegalArgumentException(s"JDBC alterTable has 
Unsupported" +

Review comment:
       I added ```UpdateColumnType``` and ```UpdateColumnNullability```. I 
didn't add ```UpdateColumnComment``` and ```UpdateColumnPosition```. Seems most 
of the databases don't support these two, so I didn't implement these two.
   
   I added ```UpdateColumnNullability```, but didn't add a test for it yet, 
because the test failed at the check here
   ```
                 case update: UpdateColumnNullability =>
                   val field = findField("update", update.fieldNames)
                   val fieldName = update.fieldNames.quoted
                   if (!update.nullable && field.nullable) {
                     alter.failAnalysis(
                       s"Cannot change nullable column to non-nullable: 
$fieldName")
                   }
   ```
   I am confused. I thought the whole point of ```UpdateColumnNullability``` is 
to change nullable column to non-nullable?
   
   
   




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to