You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vitaliili-db (via GitHub)" <gi...@apache.org> on 2024/01/09 23:00:14 UTC

[PR] [SPARK-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

vitaliili-db opened a new pull request, #44644:
URL: https://github.com/apache/spark/pull/44644

   <!--
   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.
   -->
   
   This change adds logic to generate correct DDL for nested fields. In particular instead of generating list of fields with data type names it will add `NOT NULL` qualifier when necessary and nested field comment when present.
   
   For a table:
   ```
   CREATE TABLE t(field STRUCT<one: STRING NOT NULL, two: DOUBLE NOT NULL COMMENT 'comment'>);
   ```
   
   Before:
   ```
   SHOW CREATE TABLE t;
   -- output => CREATE TABLE t(field STRUCT<one: STRING, two: DOUBLE>)
   ```
   
   After
   ```
   SHOW CREATE TABLE t;
   -- output => CREATE TABLE t(field STRUCT<one: STRING NOT NULL, two: DOUBLE NOT NULL COMMENT 'comment'>)
   ```
   
   ### 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.
   -->
   
   Generate correct DDL.
   
   ### 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, we do not document behavior of this command for struct case.
   
   ### 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 unit test.
   
   
   ### 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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #44644:
URL: https://github.com/apache/spark/pull/44644#issuecomment-1898585515

   +1, LGTM. Merging to master.
   Thank you, @vitaliili-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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #44644:
URL: https://github.com/apache/spark/pull/44644#issuecomment-1891509611

   BTW, here is a similar PR https://github.com/apache/spark/pull/41016


-- 
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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1448536657


##########
sql/api/src/main/scala/org/apache/spark/sql/types/StructField.scala:
##########
@@ -148,6 +148,24 @@ case class StructField(
     .map(" COMMENT '" + _ + "'")
     .getOrElse("")
 
+  /**
+   * Returns a string containing a DDL statement for data type. It handles a special
+   * case for StructType in order to capture `NOT NULL` field qualifier as well as field comment.
+   * This method is called recursively for nested struct(s).
+   */
+  private def getDataTypeDDL(dataType: DataType): String = {
+    dataType match {
+      case s: StructType =>
+        val fieldsDDL = s.fields.map { f =>
+          val nullString = if (f.nullable) "" else " NOT NULL"
+          s"${QuotingUtils.quoteIfNeeded(f.name)}: " +
+          s"${getDataTypeDDL(f.dataType)}$nullString${f.getDDLComment}"
+        }
+        s"STRUCT<${fieldsDDL.mkString(", ")}>"

Review Comment:
   Can't we incapsulate the logic inside of the method `StructType.sql()`?



-- 
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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1451273056


##########
connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufSerdeSuite.scala:
##########
@@ -126,14 +122,6 @@ class ProtobufSerdeSuite extends SharedSparkSession with ProtobufTestBase {
       params = Map(
         "protobufType" -> "FieldMissingInProto",
         "toType" -> toSQLType(CATALYST_STRUCT)))
-
-    assertFailedConversionMessage(protoFile,

Review Comment:
   Why did you remove this?



-- 
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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "vitaliili-db (via GitHub)" <gi...@apache.org>.
vitaliili-db commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1454047848


##########
connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufSerdeSuite.scala:
##########
@@ -126,14 +122,6 @@ class ProtobufSerdeSuite extends SharedSparkSession with ProtobufTestBase {
       params = Map(
         "protobufType" -> "FieldMissingInProto",
         "toType" -> toSQLType(CATALYST_STRUCT)))
-
-    assertFailedConversionMessage(protoFile,

Review Comment:
   Reverted and fixed.



-- 
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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #44644:
URL: https://github.com/apache/spark/pull/44644#issuecomment-1891511061

   @vitaliili-db Could you change PR's title, it is too much specific.


-- 
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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "vitaliili-db (via GitHub)" <gi...@apache.org>.
vitaliili-db commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1454047848


##########
connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufSerdeSuite.scala:
##########
@@ -126,14 +122,6 @@ class ProtobufSerdeSuite extends SharedSparkSession with ProtobufTestBase {
       params = Map(
         "protobufType" -> "FieldMissingInProto",
         "toType" -> toSQLType(CATALYST_STRUCT)))
-
-    assertFailedConversionMessage(protoFile,

Review Comment:
   This test variation does not make sense since `nonnullCatalyst.toSql` produces different result from `CATALYST_STRUCT.toSql`. Non-nullable fields are tested in "Fail to convert with missing Catalyst fields"



-- 
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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "vitaliili-db (via GitHub)" <gi...@apache.org>.
vitaliili-db commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1449249197


##########
sql/api/src/main/scala/org/apache/spark/sql/types/StructField.scala:
##########
@@ -148,6 +148,24 @@ case class StructField(
     .map(" COMMENT '" + _ + "'")
     .getOrElse("")
 
+  /**
+   * Returns a string containing a DDL statement for data type. It handles a special
+   * case for StructType in order to capture `NOT NULL` field qualifier as well as field comment.
+   * This method is called recursively for nested struct(s).
+   */
+  private def getDataTypeDDL(dataType: DataType): String = {
+    dataType match {
+      case s: StructType =>
+        val fieldsDDL = s.fields.map { f =>
+          val nullString = if (f.nullable) "" else " NOT NULL"
+          s"${QuotingUtils.quoteIfNeeded(f.name)}: " +
+          s"${getDataTypeDDL(f.dataType)}$nullString${f.getDDLComment}"
+        }
+        s"STRUCT<${fieldsDDL.mkString(", ")}>"

Review Comment:
   great idea, done!



-- 
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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk closed pull request #44644: [SPARK-46629] Fix for STRUCT type DDL not picking up nullability and comment
URL: https://github.com/apache/spark/pull/44644


-- 
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-46629] Fix for STRUCT type in SHOW CREATE TABLE command [spark]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #44644:
URL: https://github.com/apache/spark/pull/44644#issuecomment-1884017511

   cc @MaxGekk 


-- 
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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "vitaliili-db (via GitHub)" <gi...@apache.org>.
vitaliili-db commented on PR #44644:
URL: https://github.com/apache/spark/pull/44644#issuecomment-1894530476

   @MaxGekk changed the title and added test. The #44644 seems to be similar to my initial PR, I leave it for you to decide which one we should merge.


-- 
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-46629] Fix for STRUCT type DDL not picking up nullability and comment [spark]

Posted by "vitaliili-db (via GitHub)" <gi...@apache.org>.
vitaliili-db commented on code in PR #44644:
URL: https://github.com/apache/spark/pull/44644#discussion_r1454047848


##########
connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufSerdeSuite.scala:
##########
@@ -126,14 +122,6 @@ class ProtobufSerdeSuite extends SharedSparkSession with ProtobufTestBase {
       params = Map(
         "protobufType" -> "FieldMissingInProto",
         "toType" -> toSQLType(CATALYST_STRUCT)))
-
-    assertFailedConversionMessage(protoFile,

Review Comment:
   This test variation does not make sense since `nonnullCatalyst.toSql` produces different result from `CATALYST_STRUCT.toSql`. Missing non-nullable field is tested in "Fail to convert with missing Catalyst fields"



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