diff --git a/data_validation/config_manager.py b/data_validation/config_manager.py index 82cc823f7..dc2360e9b 100644 --- a/data_validation/config_manager.py +++ b/data_validation/config_manager.py @@ -30,6 +30,24 @@ import ibis.expr.types.Table +def check_mssql_random_row_batch_size( + batch_size: int, + primary_keys: list, + source_client_name: str = None, + target_client_name: str = None, +) -> int: + """Cap random_row_batch_size if SQL Server is used, to avoid the 2,100 parameter limit.""" + if source_client_name == "mssql" or target_client_name == "mssql": + num_pks = len(primary_keys) or 1 + max_mssql_rows = max(1, 2000 // num_pks) + if batch_size > max_mssql_rows: + logging.warning( + f"Capping random_row_batch_size from {batch_size} to {max_mssql_rows} for SQL Server 2,100 parameter limit." + ) + return max_mssql_rows + return batch_size + + class ConfigManager(object): _config: dict = None _source_conn = None @@ -179,10 +197,23 @@ def use_random_rows(self): def random_row_batch_size(self): """Return batch size for random row filter.""" - return int( + batch_size = int( self._config.get(consts.CONFIG_RANDOM_ROW_BATCH_SIZE) or consts.DEFAULT_NUM_RANDOM_ROWS ) + if self.use_random_rows(): + source_name = getattr(self.source_client, "name", None) + target_name = getattr(self.target_client, "name", None) + capped_batch_size = check_mssql_random_row_batch_size( + batch_size, + self.primary_keys, + source_client_name=source_name, + target_client_name=target_name, + ) + if capped_batch_size != batch_size: + self._config[consts.CONFIG_RANDOM_ROW_BATCH_SIZE] = capped_batch_size + batch_size = capped_batch_size + return batch_size def get_random_row_batch_size(self): """Return number of random rows or None.""" diff --git a/docs/limitations.md b/docs/limitations.md index 1d953a23a..2ae8f3bb6 100644 --- a/docs/limitations.md +++ b/docs/limitations.md @@ -46,7 +46,7 @@ - The `text` and `ntext` data types are incompatible with the `len()` therefore the `datalength()` function ius used in it's place which will give different results for multibyte characters. - The `image` data type is not currently supported, these columns are skipped when validated. See [issue-1578](https://github.com/GoogleCloudPlatform/professional-services-data-validator/issues/1578) for details. - SQL Server's `AVG()` function can overflow and return an arithmetic overflow error when calculating the average of a max-precision decimal column (e.g. `decimal(38)`). If you encounter this, exclude the column(s) from the list of columns passed to the `--avg` option. -- SQL Server has a limit of 2,100 query parameters. This effects `--use-random-row` validation when using composite primary keys. `--random-row-batch-size` should be limited to 2,100 minus margin for other filters, divided by the number of primary key columns. +- SQL Server has a limit of 2,100 query parameters, which affects `--use-random-row` validation. When SQL Server is used as either the source or target, the tool automatically caps `--random-row-batch-size` (to `2000 // number_of_primary_keys`, with a minimum of 1) to ensure the total number of bound parameters stays within the limit. The effective batch size is logged, used during query execution, and reflected in the validation results. ## Sybase ASE diff --git a/tests/unit/test_config_manager.py b/tests/unit/test_config_manager.py index 4129a7485..32e5c35d5 100644 --- a/tests/unit/test_config_manager.py +++ b/tests/unit/test_config_manager.py @@ -747,3 +747,94 @@ def test_config_manager_lazy_state_manager(mock_state_manager, module_under_test mgr.get_source_connection() mgr.get_target_connection() assert mock_state_manager.call_count == 1 + + +@pytest.mark.parametrize( + "source_client_name,target_client_name,num_pks,requested_batch_size,expected_batch_size", + [ + ("mssql", "bigquery", 1, 5000, 2000), + ("bigquery", "mssql", 2, 5000, 1000), + ("mssql", "bigquery", 3, 3000, 666), + ("mssql", "bigquery", 4, 1000, 500), + ("mssql", "bigquery", 1, 2000, 2000), + ("mssql", "bigquery", 1, 2001, 2000), + ("mssql", "bigquery", 1, 500, 500), + ("mssql", "bigquery", 0, 5000, 2000), + ("postgres", "bigquery", 1, 5000, 5000), + ("bigquery", "postgres", 2, 5000, 5000), + ], +) +def test_check_mssql_random_row_batch_size( + module_under_test, + source_client_name, + target_client_name, + num_pks, + requested_batch_size, + expected_batch_size, +): + """Verify helper check_mssql_random_row_batch_size caps batch size appropriately.""" + pks = [ + {"source_column": f"col_{i}", "target_column": f"col_{i}"} + for i in range(num_pks) + ] + result = module_under_test.check_mssql_random_row_batch_size( + requested_batch_size, + pks, + source_client_name=source_client_name, + target_client_name=target_client_name, + ) + assert result == expected_batch_size + + +def test_config_manager_random_row_batch_size_mutates_config(module_under_test): + """Verify random_row_batch_size mutates config arg so result handlers reflect change.""" + source_client = mock.MagicMock() + source_client.name = "mssql" + target_client = mock.MagicMock() + target_client.name = "bigquery" + + config = { + consts.CONFIG_TYPE: consts.ROW_VALIDATION, + consts.CONFIG_USE_RANDOM_ROWS: True, + consts.CONFIG_RANDOM_ROW_BATCH_SIZE: 3000, + consts.CONFIG_PRIMARY_KEYS: [{"source_column": "id", "target_column": "id"}], + } + + mgr = module_under_test.ConfigManager( + config, + source_client=source_client, + target_client=target_client, + ) + + assert mgr.random_row_batch_size() == 2000 + # Verifying the underlying config was mutated as requested by maintainer + assert mgr._config[consts.CONFIG_RANDOM_ROW_BATCH_SIZE] == 2000 + # Result handler pulls from get_random_row_batch_size() + assert mgr.get_random_row_batch_size() == 2000 + + +def test_config_manager_random_row_batch_size_when_random_rows_disabled( + module_under_test, +): + """Verify random_row_batch_size does not cap or mutate when use_random_rows is False.""" + source_client = mock.MagicMock() + source_client.name = "mssql" + target_client = mock.MagicMock() + target_client.name = "bigquery" + + config = { + consts.CONFIG_TYPE: consts.ROW_VALIDATION, + consts.CONFIG_USE_RANDOM_ROWS: False, + consts.CONFIG_RANDOM_ROW_BATCH_SIZE: 5000, + consts.CONFIG_PRIMARY_KEYS: [{"source_column": "id", "target_column": "id"}], + } + + mgr = module_under_test.ConfigManager( + config, + source_client=source_client, + target_client=target_client, + ) + + assert mgr.random_row_batch_size() == 5000 + assert mgr._config[consts.CONFIG_RANDOM_ROW_BATCH_SIZE] == 5000 + assert mgr.get_random_row_batch_size() is None