Skip to content

Conversation

@vitaliili-db
Copy link
Contributor

@vitaliili-db vitaliili-db commented Jan 9, 2024

What changes were proposed in this pull request?

This change adds logic to generate correct DDL for nested fields in STRUCT. In particular instead of generating list of fields with data type names it will add NOT NULL qualifier when necessary and field comment when present.

For a table:

CREATE TABLE t(field STRUCT<one: STRING NOT NULL, two: DOUBLE NOT NULL COMMENT 'comment'>);
SHOW CREATE TABLE t;

Before:

CREATE TABLE t(field STRUCT<one: STRING, two: DOUBLE>)

After

CREATE TABLE t(field STRUCT<one: STRING NOT NULL, two: DOUBLE NOT NULL COMMENT 'comment'>)

Closes #41016

Why are the changes needed?

Generate correct DDL.

Does this PR introduce any user-facing change?

No, we do not document behavior of this command for struct case.

How was this patch tested?

New unit test.

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Jan 9, 2024
@HyukjinKwon
Copy link
Member

cc @MaxGekk

Comment on lines 158 to 164
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(", ")}>"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great idea, done!

@vitaliili-db vitaliili-db requested a review from MaxGekk January 11, 2024 18:30
Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitaliili-db BTW, is the output of toDDL() with NOT NULL parseable back by fromDDL()? Could you add such test to StructTypeSuite.

"protobufType" -> "FieldMissingInProto",
"toType" -> toSQLType(CATALYST_STRUCT)))

assertFailedConversionMessage(protoFile,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

Copy link
Contributor Author

@vitaliili-db vitaliili-db Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted and fixed.

@MaxGekk
Copy link
Member

MaxGekk commented Jan 15, 2024

BTW, here is a similar PR #41016

@MaxGekk
Copy link
Member

MaxGekk commented Jan 15, 2024

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

@vitaliili-db vitaliili-db requested a review from MaxGekk January 16, 2024 20:41
@vitaliili-db vitaliili-db changed the title [SPARK-46629] Fix for STRUCT type in SHOW CREATE TABLE command [SPARK-46629] Fix for STRUCT type DDL not picking up nullability and comment Jan 16, 2024
@vitaliili-db
Copy link
Contributor Author

vitaliili-db commented Jan 16, 2024

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

@MaxGekk
Copy link
Member

MaxGekk commented Jan 18, 2024

+1, LGTM. Merging to master.
Thank you, @vitaliili-db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants