From b5761b1d48ebfa6b0d2159fa08380c700d7280d0 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Mon, 26 Oct 2026 15:56:37 +0530 Subject: [PATCH 1/3] fix: cap SQL Server random row batch size to avoid parameter limit (#1835) Signed-off-by: reginaldalfret --- data_validation/data_validation.py | 16 +++++++- tests/unit/test_data_validation.py | 62 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/data_validation/data_validation.py b/data_validation/data_validation.py index 6439f2f47..af4a5a4c7 100644 --- a/data_validation/data_validation.py +++ b/data_validation/data_validation.py @@ -140,9 +140,23 @@ def _add_random_row_filter(self): pk[consts.CONFIG_TARGET_COLUMN] for pk in self.config_manager.primary_keys ] + batch_size = self.config_manager.random_row_batch_size() + if ( + self.config_manager.source_client.name == "mssql" + or self.config_manager.target_client.name == "mssql" + ): + # SQL Server has a limit of 2,100 bound parameters per query (SQLSTATE 07002). + # Cap effective batch size so total bound parameters do not exceed 2,000. + max_mssql_rows = max(1, 2000 // len(source_pk_columns)) + 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." + ) + batch_size = max_mssql_rows + randomRowBuilder = RandomRowBuilder( source_pk_columns, - self.config_manager.random_row_batch_size(), + batch_size, ) if (self.config_manager.validation_type == consts.CUSTOM_QUERY) and ( diff --git a/tests/unit/test_data_validation.py b/tests/unit/test_data_validation.py index b7bbe9017..88e0bd95d 100644 --- a/tests/unit/test_data_validation.py +++ b/tests/unit/test_data_validation.py @@ -960,3 +960,65 @@ def test_add_random_row_filter_composite_pk(module_under_test, monkeypatch): assert source_filter.left == ["k1", "k2"] # Target client (MSSQL) has dvt_tuple_in_supported = False, falling back to disjunctive OR-of-ANDs filter (ibis.or_) assert target_filter.expr == ibis.or_ + + +@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", 1, 500, 500), + ("postgres", "bigquery", 1, 5000, 5000), + ], +) +def test_add_random_row_filter_mssql_parameter_limit( + module_under_test, + monkeypatch, + source_client_name, + target_client_name, + num_pks, + requested_batch_size, + expected_batch_size, +): + """Test that random_row_batch_size is capped to 2,000 bound parameters when SQL Server is involved.""" + source_client = mock.MagicMock() + source_client.name = source_client_name + target_client = mock.MagicMock() + target_client.name = target_client_name + + pks = [ + {consts.CONFIG_SOURCE_COLUMN: f"k{i}", consts.CONFIG_TARGET_COLUMN: f"target_k{i}"} + for i in range(num_pks) + ] + sample_df = pandas.DataFrame({f"k{i}": [1] for i in range(num_pks)}) + source_client.execute.return_value = sample_df + + config_manager = mock.MagicMock() + config_manager.primary_keys = pks + config_manager.random_row_batch_size.return_value = requested_batch_size + config_manager.validation_type = consts.ROW_VALIDATION + config_manager.custom_query_type = None + config_manager.source_client = source_client + config_manager.target_client = target_client + config_manager.get_source_raw_data_types.return_value = {} + config_manager.get_target_raw_data_types.return_value = {} + + captured_batch_size = None + + class MockRandomRowBuilder: + def __init__(self, primary_keys, batch_size): + nonlocal captured_batch_size + captured_batch_size = batch_size + + def compile(self, *args, **kwargs): + return mock.MagicMock() + + monkeypatch.setattr(module_under_test, "RandomRowBuilder", MockRandomRowBuilder) + + dv = module_under_test.DataValidation.__new__(module_under_test.DataValidation) + dv.config_manager = config_manager + dv.validation_builder = mock.MagicMock() + + dv._add_random_row_filter() + + assert captured_batch_size == expected_batch_size From 1ffbcb501f01f57db4da56716b1e655800bde296 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Mon, 26 Oct 2026 23:42:11 +0530 Subject: [PATCH 2/3] fix: move SQL Server random row batch size capping to config_manager Signed-off-by: reginaldalfret --- data_validation/config_manager.py | 33 +++++++++++++++- data_validation/data_validation.py | 16 +------- tests/unit/test_config_manager.py | 60 +++++++++++++++++++++++++++++ tests/unit/test_data_validation.py | 62 ------------------------------ 4 files changed, 93 insertions(+), 78 deletions(-) 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/data_validation/data_validation.py b/data_validation/data_validation.py index af4a5a4c7..6439f2f47 100644 --- a/data_validation/data_validation.py +++ b/data_validation/data_validation.py @@ -140,23 +140,9 @@ def _add_random_row_filter(self): pk[consts.CONFIG_TARGET_COLUMN] for pk in self.config_manager.primary_keys ] - batch_size = self.config_manager.random_row_batch_size() - if ( - self.config_manager.source_client.name == "mssql" - or self.config_manager.target_client.name == "mssql" - ): - # SQL Server has a limit of 2,100 bound parameters per query (SQLSTATE 07002). - # Cap effective batch size so total bound parameters do not exceed 2,000. - max_mssql_rows = max(1, 2000 // len(source_pk_columns)) - 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." - ) - batch_size = max_mssql_rows - randomRowBuilder = RandomRowBuilder( source_pk_columns, - batch_size, + self.config_manager.random_row_batch_size(), ) if (self.config_manager.validation_type == consts.CUSTOM_QUERY) and ( diff --git a/tests/unit/test_config_manager.py b/tests/unit/test_config_manager.py index 4129a7485..cc81dd437 100644 --- a/tests/unit/test_config_manager.py +++ b/tests/unit/test_config_manager.py @@ -747,3 +747,63 @@ 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", 1, 500, 500), + ("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 diff --git a/tests/unit/test_data_validation.py b/tests/unit/test_data_validation.py index 88e0bd95d..b7bbe9017 100644 --- a/tests/unit/test_data_validation.py +++ b/tests/unit/test_data_validation.py @@ -960,65 +960,3 @@ def test_add_random_row_filter_composite_pk(module_under_test, monkeypatch): assert source_filter.left == ["k1", "k2"] # Target client (MSSQL) has dvt_tuple_in_supported = False, falling back to disjunctive OR-of-ANDs filter (ibis.or_) assert target_filter.expr == ibis.or_ - - -@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", 1, 500, 500), - ("postgres", "bigquery", 1, 5000, 5000), - ], -) -def test_add_random_row_filter_mssql_parameter_limit( - module_under_test, - monkeypatch, - source_client_name, - target_client_name, - num_pks, - requested_batch_size, - expected_batch_size, -): - """Test that random_row_batch_size is capped to 2,000 bound parameters when SQL Server is involved.""" - source_client = mock.MagicMock() - source_client.name = source_client_name - target_client = mock.MagicMock() - target_client.name = target_client_name - - pks = [ - {consts.CONFIG_SOURCE_COLUMN: f"k{i}", consts.CONFIG_TARGET_COLUMN: f"target_k{i}"} - for i in range(num_pks) - ] - sample_df = pandas.DataFrame({f"k{i}": [1] for i in range(num_pks)}) - source_client.execute.return_value = sample_df - - config_manager = mock.MagicMock() - config_manager.primary_keys = pks - config_manager.random_row_batch_size.return_value = requested_batch_size - config_manager.validation_type = consts.ROW_VALIDATION - config_manager.custom_query_type = None - config_manager.source_client = source_client - config_manager.target_client = target_client - config_manager.get_source_raw_data_types.return_value = {} - config_manager.get_target_raw_data_types.return_value = {} - - captured_batch_size = None - - class MockRandomRowBuilder: - def __init__(self, primary_keys, batch_size): - nonlocal captured_batch_size - captured_batch_size = batch_size - - def compile(self, *args, **kwargs): - return mock.MagicMock() - - monkeypatch.setattr(module_under_test, "RandomRowBuilder", MockRandomRowBuilder) - - dv = module_under_test.DataValidation.__new__(module_under_test.DataValidation) - dv.config_manager = config_manager - dv.validation_builder = mock.MagicMock() - - dv._add_random_row_filter() - - assert captured_batch_size == expected_batch_size From e99aa9b7ca09b8da368ab6611b2a95f3b5965e93 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Thu, 29 Oct 2026 18:55:30 +0530 Subject: [PATCH 3/3] docs: update limitations.md for automatic SQL Server random row batch size capping Update SQL Server section in limitations.md to state that random row batch size is automatically capped to avoid exceeding the 2,100 query parameter limit, and that the effective batch size is reflected in validation results. Also add boundary and disabled-state unit tests for ConfigManager random row batch size. Signed-off-by: reginaldalfret --- docs/limitations.md | 2 +- tests/unit/test_config_manager.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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 cc81dd437..32e5c35d5 100644 --- a/tests/unit/test_config_manager.py +++ b/tests/unit/test_config_manager.py @@ -755,7 +755,11 @@ def test_config_manager_lazy_state_manager(mock_state_manager, module_under_test ("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), ], @@ -807,3 +811,30 @@ def test_config_manager_random_row_batch_size_mutates_config(module_under_test): 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