-
Notifications
You must be signed in to change notification settings - Fork 16.6k
fix(csv-import): add primary key when required by MySQL #37547
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
Open
Arunodoy18
wants to merge
1
commit into
apache:master
Choose a base branch
from
Arunodoy18:fix-csv-import-primary-key
base: master
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
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 hidden or 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
236 changes: 236 additions & 0 deletions
236
tests/integration_tests/databases/commands/upload_mysql_pk_test.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| Tests for CSV upload with MySQL databases requiring primary keys. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, Mock, patch | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from superset.commands.database.uploaders.base import UploadCommand | ||
| from superset.commands.database.uploaders.csv_reader import CSVReader | ||
| from superset.db_engine_specs.mysql import MySQLEngineSpec | ||
| from superset.models.core import Database | ||
| from superset.sql.parse import Table | ||
|
|
||
|
|
||
| def test_mysql_requires_primary_key_detection(): | ||
| """Test that _requires_primary_key correctly detects MySQL setting""" | ||
| # Create a mock engine | ||
| mock_engine = Mock() | ||
| mock_conn = Mock() | ||
| mock_result = Mock() | ||
|
|
||
| # Test case: sql_require_primary_key is ON (1) | ||
| mock_result.scalar.return_value = 1 | ||
| mock_conn.execute.return_value = mock_result | ||
| mock_conn.__enter__ = Mock(return_value=mock_conn) | ||
| mock_conn.__exit__ = Mock(return_value=False) | ||
| mock_engine.connect.return_value = mock_conn | ||
|
|
||
| assert MySQLEngineSpec._requires_primary_key(mock_engine) is True | ||
|
|
||
| # Test case: sql_require_primary_key is OFF (0) | ||
| mock_result.scalar.return_value = 0 | ||
| assert MySQLEngineSpec._requires_primary_key(mock_engine) is False | ||
|
|
||
| # Test case: Query fails (e.g., older MySQL version without the setting) | ||
| mock_conn.execute.side_effect = Exception("Unknown system variable") | ||
| assert MySQLEngineSpec._requires_primary_key(mock_engine) is False | ||
|
|
||
|
|
||
| def test_mysql_df_to_sql_adds_primary_key(): | ||
| """Test that df_to_sql adds a primary key when required""" | ||
| # Create test data | ||
| df = pd.DataFrame({ | ||
| "name": ["Alice", "Bob", "Charlie"], | ||
| "age": [30, 25, 35], | ||
| "city": ["NYC", "LA", "SF"] | ||
| }) | ||
|
|
||
| # Create mock database and table | ||
| mock_database = Mock(spec=Database) | ||
| mock_table = Table(table="test_table", schema=None) | ||
|
|
||
| # Mock engine with sql_require_primary_key enabled | ||
| mock_engine = Mock() | ||
| mock_conn = Mock() | ||
| mock_result = Mock() | ||
| mock_result.scalar.return_value = 1 # Primary key required | ||
| mock_conn.execute.return_value = mock_result | ||
| mock_conn.commit = Mock() | ||
| mock_conn.__enter__ = Mock(return_value=mock_conn) | ||
| mock_conn.__exit__ = Mock(return_value=False) | ||
| mock_engine.connect.return_value = mock_conn | ||
|
|
||
| # Mock engine.begin() for transaction context | ||
| mock_transaction = Mock() | ||
| mock_transaction.__enter__ = Mock(return_value=mock_conn) | ||
| mock_transaction.__exit__ = Mock(return_value=False) | ||
| mock_engine.begin.return_value = mock_transaction | ||
|
|
||
| mock_engine.dialect.supports_multivalues_insert = True | ||
|
|
||
| # Track to_sql calls | ||
| to_sql_called = False | ||
| captured_df = None | ||
| captured_kwargs = None | ||
|
|
||
| def capture_to_sql(con, **kwargs): | ||
| nonlocal to_sql_called, captured_df, captured_kwargs | ||
| to_sql_called = True | ||
| captured_df = kwargs.get("name") # Just capture what we can | ||
| captured_kwargs = kwargs | ||
|
|
||
| # Mock df.to_sql to capture the call | ||
| with patch.object(pd.DataFrame, "to_sql", side_effect=capture_to_sql): | ||
| with patch.object( | ||
| MySQLEngineSpec, | ||
| "get_engine", | ||
| return_value=mock_engine.__enter__() | ||
| ): | ||
| to_sql_kwargs = { | ||
| "if_exists": "fail", | ||
| "index": False, | ||
| "chunksize": 1000, | ||
| } | ||
|
|
||
| MySQLEngineSpec.df_to_sql( | ||
| mock_database, | ||
| mock_table, | ||
| df, | ||
| to_sql_kwargs | ||
| ) | ||
|
|
||
| # Verify to_sql was called | ||
| assert to_sql_called | ||
| assert "test_table" in str(captured_kwargs) | ||
|
|
||
| # Verify ALTER TABLE was called to add PRIMARY KEY | ||
| execute_calls = [call for call in mock_conn.execute.call_args_list] | ||
| # First call is checking sql_require_primary_key | ||
| # Second call should be ALTER TABLE | ||
| assert len(execute_calls) >= 2 | ||
| alter_call = execute_calls[1][0][0] | ||
| assert "ALTER TABLE" in alter_call | ||
| assert "AUTO_INCREMENT" in alter_call | ||
| assert "PRIMARY KEY" in alter_call | ||
|
|
||
|
|
||
| def test_mysql_df_to_sql_skips_primary_key_when_not_required(): | ||
| """Test that df_to_sql doesn't add primary key when not required""" | ||
| df = pd.DataFrame({ | ||
| "name": ["Alice", "Bob"], | ||
| "age": [30, 25] | ||
| }) | ||
|
|
||
| mock_database = Mock(spec=Database) | ||
| mock_table = Table(table="test_table", schema=None) | ||
|
|
||
| # Mock engine with sql_require_primary_key disabled | ||
| mock_engine = Mock() | ||
| mock_conn = Mock() | ||
| mock_result = Mock() | ||
| mock_result.scalar.return_value = 0 # Primary key NOT required | ||
| mock_conn.execute.return_value = mock_result | ||
| mock_conn.__enter__ = Mock(return_value=mock_conn) | ||
| mock_conn.__exit__ = Mock(return_value=False) | ||
| mock_engine.connect.return_value = mock_conn | ||
| mock_engine.dialect.supports_multivalues_insert = True | ||
|
|
||
| # Use parent's df_to_sql | ||
| with patch.object( | ||
| MySQLEngineSpec, | ||
| "get_engine", | ||
| return_value=mock_engine.__enter__() | ||
| ): | ||
| with patch("superset.db_engine_specs.base.BaseEngineSpec.df_to_sql") as mock_parent: | ||
| to_sql_kwargs = { | ||
| "if_exists": "fail", | ||
| "index": False, | ||
| } | ||
|
|
||
| MySQLEngineSpec.df_to_sql( | ||
| mock_database, | ||
| mock_table, | ||
| df, | ||
| to_sql_kwargs | ||
| ) | ||
|
|
||
| # Verify parent's df_to_sql was called (normal path) | ||
| mock_parent.assert_called_once() | ||
| # Verify no ALTER TABLE was executed | ||
| # Only one call should be to check sql_require_primary_key | ||
| assert mock_conn.execute.call_count == 1 | ||
|
|
||
|
|
||
| def test_mysql_df_to_sql_handles_column_name_conflicts(): | ||
| """Test that primary key column name is adjusted if it conflicts""" | ||
| df = pd.DataFrame({ | ||
| "__superset_upload_id__": ["existing_data"], | ||
| "name": ["Alice"] | ||
| }) | ||
|
|
||
| mock_database = Mock(spec=Database) | ||
| mock_table = Table(table="test_table", schema=None) | ||
|
|
||
| mock_engine = Mock() | ||
| mock_conn = Mock() | ||
| mock_result = Mock() | ||
| mock_result.scalar.return_value = 1 # Primary key required | ||
| mock_conn.execute.return_value = mock_result | ||
| mock_conn.commit = Mock() | ||
| mock_conn.__enter__ = Mock(return_value=mock_conn) | ||
| mock_conn.__exit__ = Mock(return_value=False) | ||
| mock_engine.connect.return_value = mock_conn | ||
| mock_engine.dialect.supports_multivalues_insert = True | ||
|
|
||
| captured_df_columns = None | ||
|
|
||
| def capture_to_sql(con, **kwargs): | ||
| # Can't directly access df from kwargs in mock, but we can check ALTER | ||
| pass | ||
|
|
||
| with patch.object(pd.DataFrame, "to_sql", side_effect=capture_to_sql): | ||
| with patch.object( | ||
| MySQLEngineSpec, | ||
| "get_engine", | ||
| return_value=mock_engine.__enter__() | ||
| ): | ||
| to_sql_kwargs = { | ||
| "if_exists": "fail", | ||
| "index": False, | ||
| } | ||
|
|
||
| MySQLEngineSpec.df_to_sql( | ||
| mock_database, | ||
| mock_table, | ||
| df, | ||
| to_sql_kwargs | ||
| ) | ||
|
|
||
| # Verify ALTER TABLE uses a different column name (prefixed with _) | ||
| execute_calls = [call for call in mock_conn.execute.call_args_list] | ||
| if len(execute_calls) >= 2: | ||
| alter_call = execute_calls[1][0][0] | ||
| # The column name should be modified to avoid conflict | ||
| assert "__superset_upload_id__" in alter_call or "___superset_upload_id__" in alter_call | ||
|
Contributor
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. Test assertion too permissive
The assertion allows either the original or prefixed column name, but the code always prefixes when there's a conflict, so the test should verify the exact adjusted name. Code suggestionCheck the AI-generated fix before applying Code Review Run #528e4b Should Bito avoid suggestions like this for future reviews? (Manage Rules)
|
||
Oops, something went wrong.
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.
Line 456 has an unclosed opening parenthesis
(in theneeds_primary_keyassignment. The condition started on line 456 is missing a closing parenthesis before the assignment completes on line 458.Code suggestion
Code Review Run #528e4b
Should Bito avoid suggestions like this for future reviews? (Manage Rules)