-
Notifications
You must be signed in to change notification settings - Fork 477
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
feat: add copy_grants to write_pandas #2050
Open
surpatean
wants to merge
2
commits into
snowflakedb:main
Choose a base branch
from
surpatean:feat-add-copy_grants-to-write_pandas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 |
---|---|---|
|
@@ -181,6 +181,7 @@ def write_pandas( | |
overwrite: bool = False, | ||
table_type: Literal["", "temp", "temporary", "transient"] = "", | ||
use_logical_type: bool | None = None, | ||
copy_grants: bool | None = None, | ||
**kwargs: Any, | ||
) -> tuple[ | ||
bool, | ||
|
@@ -237,14 +238,17 @@ def write_pandas( | |
the passed in DataFrame. The table will not be created if it already exists | ||
create_temp_table: (Deprecated) Will make the auto-created table as a temporary table | ||
overwrite: When true, and if auto_create_table is true, then it drops the table. Otherwise, it | ||
truncates the table. In both cases it will replace the existing contents of the table with that of the passed in | ||
Pandas DataFrame. | ||
truncates the table. In both cases it will replace the existing contents of the table with that of the | ||
passed in Pandas DataFrame. | ||
table_type: The table type of to-be-created table. The supported table types include ``temp``/``temporary`` | ||
and ``transient``. Empty means permanent table as per SQL convention. | ||
use_logical_type: Boolean that specifies whether to use Parquet logical types. With this file format option, | ||
Snowflake can interpret Parquet logical types during data loading. To enable Parquet logical types, | ||
set use_logical_type as True. Set to None to use Snowflakes default. For more information, see: | ||
https://docs.snowflake.com/en/sql-reference/sql/create-file-format | ||
copy_grants: When true and when both overwrite is true and auto_create_table is true, the grants of the table | ||
being overwritten will be copied to the new table. Otherwise, the new table will be created with only the | ||
default/future grants defined on the schema/database. | ||
|
||
|
||
Returns: | ||
|
@@ -315,6 +319,14 @@ def write_pandas( | |
else: | ||
sql_use_logical_type = " USE_LOGICAL_TYPE = FALSE" | ||
|
||
if copy_grants and (not overwrite or not auto_create_table): | ||
warnings.warn( | ||
"copy_grants is only applied when used in combination with " | ||
"overwrite and auto_create_table", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
|
||
cursor = conn.cursor() | ||
stage_location = _create_temp_stage( | ||
cursor, | ||
|
@@ -444,10 +456,15 @@ def drop_object(name: str, object_type: str) -> None: | |
name=table_name, | ||
quote_identifiers=quote_identifiers, | ||
) | ||
drop_object(original_table_location, "table") | ||
rename_table_sql = f"ALTER TABLE {target_table_location} RENAME TO {original_table_location} /* Python:snowflake.connector.pandas_tools.write_pandas() */" | ||
logger.debug(f"rename table with '{rename_table_sql}'") | ||
cursor.execute(rename_table_sql, _is_internal=True) | ||
clone_table_sql = ( | ||
f"CREATE OR REPLACE TABLE {original_table_location} " | ||
f"CLONE {target_table_location} " | ||
f"{'COPY GRANTS' if copy_grants else ''}" | ||
f" /* Python:snowflake.connector.pandas_tools.write_pandas() */ " | ||
) | ||
logger.debug(f"clone table with '{clone_table_sql}'") | ||
cursor.execute(clone_table_sql, _is_internal=True) | ||
drop_object(target_table_location, "table") | ||
Comment on lines
-447
to
+467
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be a breaking change since not all users my have create table privileges. Can you refactor the code like so
|
||
except ProgrammingError: | ||
if overwrite and auto_create_table: | ||
# drop table only if we created a new one with a random name | ||
|
This file contains 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.
please add the fact that create table privileges are required with this option.