Skip to content
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

[Spark] Python DeltaTableBuilder API for Identity Columns #3404

Merged
merged 6 commits into from
Aug 8, 2024

Conversation

zhipengmao-db
Copy link
Contributor

@zhipengmao-db zhipengmao-db commented Jul 22, 2024

Which Delta project/connector is this regarding?

  • Spark
  • Standalone
  • Flink
  • Kernel
  • Other (fill in here)

Description

This PR is part of #1959

In this PR, we extend the addColumn interface in DeltaTableBuilder to allow for Identity Columns creation.

Resolves #1072

How was this patch tested?

New tests.

Does this PR introduce any user-facing changes?

We update the arguments of addColumn method:

  • Support a new data type for parameter generatedAlwaysAs. Users can specify generatedAlwaysAs as IdentityGenerator to add an identity column that is GENERATED ALWAYS.

  • Add a new parameter generatedByDefaultAs. Users can specify generatedByDefaultAs as IdentityGenerator to add an identity column that is GENERATED BY DEFAULT.

  • Users can optionally pass in start (default = 1) and step (default = 1) values to construct IdentityGenerator object, which specify the start and step value to generate the identity column.

Interface

 def addColumn(
        self,
        colName: str,
        dataType: Union[str, DataType],
        nullable: bool = True,
        generatedAlwaysAs: Optional[Union[str, IdentityGenerator]] = None,
        generatedByDefaultAs: Optional[IdentityGenerator] = None,
        comment: Optional[str] = None,
) -> "DeltaTableBuilder"

Example Usage

 DeltaTable.create()
    .tableName("tableName")
    .addColumn("id", dataType=LongType(), generatedAlwaysAs=IdentityGenerator())
    .execute()

 DeltaTable.create()
    .tableName("tableName")
    .addColumn("id", dataType=LongType(), generatedAlwaysAs=IdentityGenerator(start=1, step=1))
    .execute()

 DeltaTable.create()
    .tableName("tableName")
    .addColumn("id", dataType=LongType(), generatedByDefaultAs=IdentityGenerator())
    .execute()

 DeltaTable.create()
    .tableName("tableName")
    .addColumn("id", dataType=LongType(), generatedByDefaultAs=IdentityGenerator(start=1, step=1))
    .execute()

@zhipengmao-db zhipengmao-db changed the title Python DeltaTableBuilder API [Spark] Python DeltaTableBuilder API for Identity Columns Jul 22, 2024
@zhipengmao-db zhipengmao-db force-pushed the identity-column-python branch 2 times, most recently from cda45ad to 934b82a Compare July 23, 2024 15:14
python/delta/tables.py Outdated Show resolved Hide resolved
python/run-tests.py Outdated Show resolved Hide resolved
python/delta/tests/test_deltatable.py Outdated Show resolved Hide resolved
@@ -1164,7 +1178,8 @@ def addColumn(
colName: str,
dataType: Union[str, DataType],
nullable: bool = True,
generatedAlwaysAs: Optional[str] = None,
generatedAlwaysAs: Optional[Union[str, IdentityGenerator]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

API looks nice like this BTW!

Update tests

Update test
Copy link
Contributor

@c27kwan c27kwan left a comment

Choose a reason for hiding this comment

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

Nice!

python/delta/tests/test_deltatable.py Show resolved Hide resolved
Copy link
Contributor

@c27kwan c27kwan left a comment

Choose a reason for hiding this comment

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

lgtm! 🎉

@scottsand-db scottsand-db merged commit 78cdeb0 into delta-io:master Aug 8, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for GENERATED ALWAYS AS IDENTITY in DeltaTableBuilder
4 participants