:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(data-warehouse): resume mssql full refreshes and incremental imports (#112714)
Tom Owers04fb4dbbdf04
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.pyindex 1af9e08bb457..1c1c59699f49 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/mssql.py@@ -10,6 +10,9 @@ just holds an instance and validates credentials.from __future__ import annotationsimport time+import uuid+import datetime+import functoolsimport collectionsfrom collections.abc import Callable, Iteratorfrom contextlib import ExitStack, contextmanager@@ -41,6 +44,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.dearun_with_deadline,)from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import open_ssh_tunnel+from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.sql import (BracketIdentifierQuoter,Column,@@ -61,17 +65,19 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.sqlfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.incremental import (IncrementalFieldFilter,)+from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.keyset import checked_keyset_keyfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.location import (normalize_namespace,resolve_source_location,)from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs, SourceResponsefrom products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.mssql import MSSQLSourceConfig-from products.warehouse_sources.backend.types import IncrementalFieldType, PartitionSettings+from products.warehouse_sources.backend.types import ExternalDataSchemaSyncType, IncrementalFieldType, PartitionSettings__all__ = ["MSSQLColumn","MSSQLImplementation",+ "MSSQLResumeState","filter_mssql_incremental_fields",]@@ -278,6 +284,199 @@ def filter_mssql_incremental_fields(return results+# Key column types a keyset walk can seek on, mapped to the type the seek value is cast to. The+# server compares and orders each of them itself, and each value survives the trip through JSON to+# the next worker without loss. The cast keeps the comparison in the column's own type, so the seek+# stays an index range.+#+# Date and time types with a fraction are left out: pymssql sends a datetime value with millisecond+# precision, so the seek value can be lower than the key it came from. Text is left out because a+# string parameter arrives as nvarchar, which makes the server convert a varchar key column for each+# comparison.+_KEYSET_KEY_TYPES = {+ "tinyint": "tinyint",+ "smallint": "smallint",+ "int": "int",+ "bigint": "bigint",+ "date": "date",+ "uniqueidentifier": "uniqueidentifier",+}+++@frozen+class MSSQLResumeState:+ """Where an interrupted read continues from.++ A full refresh stores the key of the last row it handed over, with the columns that key belongs+ to. An incremental read stores the incremental value of that row.+ """++ key_columns: list[str] | None = None+ last_key: list[Any] | None = None+ incremental_value: Any = None+++@frozen+class MSSQLUniqueIndex:+ """A unique index from the catalog. Each column is `(name, base type, nullable)`."""++ name: str+ is_clustered: bool+ is_primary_key: bool+ columns: list[tuple[str, str, bool]]+++@frozen+class MSSQLKeyset:+ """The unique key a full refresh pages on, or the reason it cannot.++ `reason` is a stable token, so the share of tables that stay on the single query is countable+ from logs.+ """++ columns: list[str] | None = None+ cast_types: list[str] | None = None+ reason: str | None = None+++def _keyset_index_reason(index: MSSQLUniqueIndex, readable_columns: set[str]) -> str | None:+ for name, data_type, nullable in index.columns:+ if nullable:+ # `key > last` never matches a NULL, so a row with a NULL key would never be read.+ return "nullable_key"+ if data_type not in _KEYSET_KEY_TYPES:+ return f"non_orderable_type:{data_type}"+ if name not in readable_columns:+ return "key_not_projected"+ return None+++def resolve_mssql_keyset(+ *,+ unique_indexes: list[MSSQLUniqueIndex],+ readable_columns: set[str],+ should_use_incremental_field: bool,+) -> MSSQLKeyset:+ """Pick the unique index a full refresh pages on.++ The clustered index comes first, because a walk in its order reads the table in storage order.+ The primary key comes next, then the index with the fewest columns.+ """+ if should_use_incremental_field:+ return MSSQLKeyset(reason="incremental_sync")+ if not unique_indexes:+ return MSSQLKeyset(reason="no_unique_index")++ first_reason: str | None = None+ for index in sorted(+ unique_indexes, key=lambda i: (not i.is_clustered, not i.is_primary_key, len(i.columns), i.name)+ ):+ reason = _keyset_index_reason(index, readable_columns)+ if reason is None:+ return MSSQLKeyset(+ columns=[name for name, _, _ in index.columns],+ cast_types=[_KEYSET_KEY_TYPES[data_type] for _, data_type, _ in index.columns],+ )+ first_reason = first_reason or reason+ return MSSQLKeyset(reason=first_reason)+++def _keyset_key_value(value: Any) -> Any:+ """The form of a key value that goes into the checkpoint and into the next seek.++ One form for both uses, so a resumed walk sends the server the same value as an uninterrupted+ one.+ """+ if isinstance(value, uuid.UUID):+ return str(value)+ if isinstance(value, datetime.date):+ return value.isoformat()+ return value+++def _keyset_resume_key(state: MSSQLResumeState | None, keyset_columns: list[str]) -> tuple[Any, ...] | None:+ """The key to seek past, or None to start from the first row.++ A checkpoint for other columns means the table's indexes changed after it was written. A seek+ with it would compare the wrong columns, so the walk starts again.+ """+ if state is None or not state.last_key or state.key_columns != keyset_columns:+ return None+ return tuple(state.last_key)+++def _incremental_resume_value(+ state: MSSQLResumeState | None, incremental_field_type: IncrementalFieldType | None+) -> Any | None:+ """The stored incremental value, in the type the query binds."""+ value = state.incremental_value if state is not None else None+ if not isinstance(value, str):+ return value+ if incremental_field_type == IncrementalFieldType.Date:+ return datetime.date.fromisoformat(value)+ if incremental_field_type in (IncrementalFieldType.DateTime, IncrementalFieldType.Timestamp):+ return datetime.datetime.fromisoformat(value)+ return value+++class _TrackedFetch:+ """`cursor.fetchmany` that records when the result set has no more rows."""++ def __init__(self, cursor: pymssql.Cursor) -> None:+ self._cursor = cursor+ self.exhausted = False++ def __call__(self, size: int) -> list[Any]:+ rows = self._cursor.fetchmany(size) or []+ self.exhausted = not rows+ return rows+++def _keyset_seek_condition(quoted_columns: list[str], placeholders: list[str]) -> str:+ """Rows after the last key, in the order of the key.++ SQL Server has no row-value comparison. This form keeps a range on the first column, which the+ server can seek on. A plain OR of one branch per column often makes it scan the index.+ """+ column, placeholder = quoted_columns[0], placeholders[0]+ if len(quoted_columns) == 1:+ return f"{column} > {placeholder}"+ rest = _keyset_seek_condition(quoted_columns[1:], placeholders[1:])+ return f"{column} >= {placeholder} AND ({column} > {placeholder} OR ({rest}))"+++def _build_keyset_query(+ schema: str,+ table_name: str,+ keyset: MSSQLKeyset,+ after: tuple[Any, ...] | None,+ limit: int,+ enabled_columns: list[str] | None = None,+ primary_keys: list[str] | None = None,+ row_filters: list[ValidatedRowFilter] | None = None,+) -> tuple[str, dict[str, Any]]:+ """One page of a full refresh: the next `limit` rows after `after`, in key order."""+ if not keyset.columns or not keyset.cast_types:+ raise ValueError("A keyset query needs key columns")++ qualified_table = _IDENTIFIER_QUOTER.quote_qualified(schema, table_name)+ projected = compute_projected_columns(enabled_columns, primary_keys, None)+ select_clause = format_projected_select_clause(projected, _IDENTIFIER_QUOTER)+ quoted_keys = [_IDENTIFIER_QUOTER.quote(column) for column in keyset.columns]++ conditions, params = render_named_conditions(row_filters or [], _IDENTIFIER_QUOTER)+ if after is not None:+ placeholders = [f"CAST(%(keyset_{index})s AS {cast_type})" for index, cast_type in enumerate(keyset.cast_types)]+ params.update({f"keyset_{index}": value for index, value in enumerate(after)})+ conditions.insert(0, f"({_keyset_seek_condition(quoted_keys, placeholders)})")++ query = f"SELECT TOP ({int(limit)}) {select_clause} FROM {qualified_table}"+ if conditions:+ query = f"{query} WHERE {' AND '.join(conditions)}"+ order_by = ", ".join(f"{column} ASC" for column in quoted_keys)+ return f"{query} ORDER BY {order_by}", params++def _build_query(schema: str,table_name: str,@@ -289,6 +488,7 @@ def _build_query(enabled_columns: list[str] | None = None,primary_keys: list[str] | None = None,row_filters: list[ValidatedRowFilter] | None = None,+ include_last_value: bool = False,) -> tuple[str, dict[str, Any]]:# Every identifier interpolated below is validated by the bracket# quoter — bad input (`;`, `]`, whitespace, etc.) raises before any@@ -312,7 +512,9 @@ def _build_query(if db_incremental_field_last_value is None:db_incremental_field_last_value = incremental_type_to_initial_value(incremental_field_type)- operator = incremental_type_to_operator(incremental_field_type)+ # A resumed read stopped at a batch boundary, and rows with the boundary value can sit on both+ # sides of it. It reads that value again, and the merge on the primary key drops the repeats.+ operator = ">=" if include_last_value else incremental_type_to_operator(incremental_field_type)quoted_incremental = _IDENTIFIER_QUOTER.quote(incremental_field)conditions = [f"{quoted_incremental} {operator} %(incremental_value)s", *filter_conditions]query = f"{base_query} WHERE {' AND '.join(conditions)}"@@ -403,6 +605,7 @@ class MSSQLTableSetup:inner_query_args: dict[str, Any]chunk_size: intpartition_settings: PartitionSettings | None+ keyset: MSSQLKeysetclass MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Connection, pymssql.Cursor]): # ty: ignore[invalid-type-arguments]@@ -717,6 +920,54 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Conreturn [row[0] for row in rows]+ def get_unique_indexes_for_table(+ self, cursor: pymssql.Cursor, schema: str, table_name: str, logger: FilteringBoundLogger+ ) -> list[MSSQLUniqueIndex]:+ """Return the unique indexes a keyset walk could page on.++ A filtered index is unique only for the rows it covers, and the server does not use a+ disabled one. The type is the base type, so a key of an alias type keeps its real order.+ A failure returns no indexes, and the read then takes the single query.+ """+ query = """+ SELECT i.name, i.type, i.is_primary_key, c.name, ty.name, c.is_nullable+ FROM sys.indexes i+ JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id+ JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id+ JOIN sys.types ty ON c.system_type_id = ty.user_type_id+ JOIN sys.tables t ON i.object_id = t.object_id+ JOIN sys.schemas s ON t.schema_id = s.schema_id+ WHERE i.is_unique = 1+ AND i.has_filter = 0+ AND i.is_disabled = 0+ AND i.is_hypothetical = 0+ AND ic.is_included_column = 0+ AND ic.key_ordinal > 0+ AND s.name = %(schema)s+ AND t.name = %(table_name)s+ ORDER BY i.index_id, ic.key_ordinal"""+ try:+ cursor.execute(query, {"schema": schema, "table_name": table_name})+ rows = cursor.fetchall() or []+ except Exception as e:+ logger.debug(f"Could not read the unique indexes: {e}", exc_info=e)+ return []++ columns_by_index: dict[str, list[tuple[str, str, bool]]] = collections.defaultdict(list)+ flags_by_index: dict[str, tuple[bool, bool]] = {}+ for index_name, index_type, is_primary_key, column_name, type_name, is_nullable in rows:+ columns_by_index[index_name].append((column_name, type_name, bool(is_nullable)))+ flags_by_index[index_name] = (index_type == 1, bool(is_primary_key))+ return [+ MSSQLUniqueIndex(+ name=name,+ is_clustered=flags_by_index[name][0],+ is_primary_key=flags_by_index[name][1],+ columns=columns,+ )+ for name, columns in columns_by_index.items()+ ]+def get_table_metadata(self, cursor: pymssql.Cursor, schema: str, table_name: str) -> Table[MSSQLColumn]:"""Return rich column metadata for building a PyArrow schema."""query = """@@ -957,7 +1208,12 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Con# Pipeline build — the dlt `SourceResponse` for a single table# ------------------------------------------------------------------- def build_pipeline(self, config: MSSQLSourceConfig, inputs: SourceInputs) -> SourceResponse:+ def build_pipeline(+ self,+ config: MSSQLSourceConfig,+ inputs: SourceInputs,+ resumable_source_manager: ResumableSourceManager[MSSQLResumeState] | None = None,+ ) -> SourceResponse:# Resolve the per-row namespace + table from `schema_metadata` (multi-schema) or the# config namespace (legacy single-schema). `response_name` keeps the legacy Delta path.# No fallback namespace: every real source either has a config schema or carries@@ -978,6 +1234,7 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Condb_incremental_field_last_value = inputs.db_incremental_field_last_valueenabled_columns = inputs.enabled_columnsrow_filters = inputs.row_filters+ manager = resumable_source_managerdef _resolve_projection(full_table: Table[MSSQLColumn], primary_keys: list[str] | None@@ -1014,9 +1271,19 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Conrow_filters=row_filters,)+ if manager is None or should_use_incremental_field:+ unique_indexes: list[MSSQLUniqueIndex] = []+ else:+ unique_indexes = self.get_unique_indexes_for_table(cursor, schema, table_name, logger)+return MSSQLTableSetup(primary_keys=primary_keys,projection=projection,+ keyset=resolve_mssql_keyset(+ unique_indexes=unique_indexes,+ readable_columns={column.name for column in projection.table.columns},+ should_use_incremental_field=should_use_incremental_field,+ ),inner_query=inner_query,inner_query_args=inner_query_args,chunk_size=self.get_chunk_size(@@ -1072,8 +1339,41 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Conreturn setup_projectionreturn _resolve_projection(fresh_table, primary_keys)+ def _execute(cursor: pymssql.Cursor, query: str, args: dict[str, Any]) -> list[str]:+ logger.debug(f"MS SQL query: {query} with args: {args}")+ retry_on_deadlock(lambda: cursor.execute(query, args), logger=logger)+ return [column[0] for column in cursor.description or []]++ keyset = setup.keyset+ if manager is not None and keyset.reason is not None and not should_use_incremental_field:+ logger.info(f"MSSQL keyset resume unavailable: reason={keyset.reason}")+ if manager is not None and keyset.columns is not None and manager.can_resume():+ state = manager.load_state()+ if state is None or not state.last_key or state.key_columns != keyset.columns:+ logger.info("MSSQL keyset checkpoint does not match the selected key; restarting the read")+ manager.clear_state()++ # A resumed incremental read repeats the rows at its checkpoint value. Only a merge on a+ # primary key drops them, so an append sync and a table without a key restart from the+ # stored watermark as before.+ resumes_incremental = (+ manager is not None+ and should_use_incremental_field+ and incremental_field is not None+ and inputs.sync_type == ExternalDataSchemaSyncType.INCREMENTAL+ and bool(primary_keys)+ )+def get_rows() -> Iterator[Any]:binary_reporter = BinaryColumnReporter(logger)+ last_value = db_incremental_field_last_value+ resumed = False+ if manager is not None and resumes_incremental and manager.can_resume():+ resume_value = _incremental_resume_value(manager.load_state(), incremental_field_type)+ if resume_value is not None:+ logger.debug(f"MSSQL incremental resume: {incremental_field} >= {resume_value}")+ last_value, resumed = resume_value, True+with self.connect(config, team_id=inputs.team_id, lock_timeout_ms=None) as streaming_connection:projection = _refreshed_projection(streaming_connection)arrow_schema = projection.table.to_arrow_schema()@@ -1084,37 +1384,121 @@ class MSSQLImplementation(SQLSourceImplementation[MSSQLSourceConfig, pymssql.Conshould_use_incremental_field,incremental_field,incremental_field_type,- db_incremental_field_last_value,+ last_value,enabled_columns=projection.enabled_columns,primary_keys=primary_keys,row_filters=row_filters,+ include_last_value=resumed,)if incremental_field:query = f"{query} ORDER BY {_IDENTIFIER_QUOTER.quote(incremental_field)} ASC"- logger.debug(f"MS SQL query: {query} with args: {args}")-- retry_on_deadlock(lambda: cursor.execute(query, args), logger=logger)-- column_names = [column[0] for column in cursor.description or []]+ column_names = _execute(cursor, query, args)# The read can still return fewer columns than the catalog listed, so restrict# the schema to what came back instead of failing the Arrow build.read_schema = restrict_schema_to_columns(arrow_schema, column_names)+ incremental_index = (+ column_names.index(incremental_field) if resumes_incremental and incremental_field else None+ )for rows in fetch_row_batches(cursor.fetchmany, max_rows=setup.chunk_size):- yield table_from_iterator(+ table = table_from_iterator((dict(zip(column_names, row)) for row in rows),read_schema,primary_keys=primary_keys,binary_reporter=binary_reporter,)+ # The rows arrive in ascending order, so the last row holds the highest value.+ # See `get_rows_by_keyset` for why the stage comes directly before the yield.+ if manager is not None and incremental_index is not None:+ reached = rows[-1][incremental_index]+ if reached is not None:+ manager.save_state(MSSQLResumeState(incremental_value=reached))+ yield table++ # Only a finished read gets here. A read that stops early keeps its checkpoint.+ if manager is not None and resumes_incremental:+ manager.clear_state()++ def get_rows_by_keyset(+ manager: ResumableSourceManager[MSSQLResumeState], key_columns: list[str]+ ) -> Iterator[Any]:+ binary_reporter = BinaryColumnReporter(logger)+ last_key = _keyset_resume_key(manager.load_state() if manager.can_resume() else None, key_columns)+ if last_key is not None:+ logger.debug(f"MSSQL keyset resume: {key_columns} > {last_key}")++ with self.connect(config, team_id=inputs.team_id, lock_timeout_ms=None) as connection:+ # Each page is its own transaction, so the walk holds no transaction open on the+ # source between pages.+ connection.autocommit(True)+ projection = _refreshed_projection(connection)+ arrow_schema = projection.table.to_arrow_schema()+ page_rows = setup.chunk_size+ while True:+ query, args = _build_keyset_query(+ schema,+ table_name,+ keyset,+ last_key,+ page_rows,+ enabled_columns=projection.enabled_columns,+ primary_keys=primary_keys,+ row_filters=row_filters,+ )+ rows_in_page = 0+ with connection.cursor() as cursor:+ column_names = _execute(cursor, query, args)+ key_indexes = [column_names.index(column) for column in key_columns]+ read_schema = restrict_schema_to_columns(arrow_schema, column_names)++ fetch = _TrackedFetch(cursor)+ for rows in fetch_row_batches(fetch, max_rows=page_rows):+ rows_in_page += len(rows)+ # A hand-off after the only batch of a table has nothing left to read.+ # With a checkpoint, the next attempt would read no rows, and the+ # loader does not finalize a run that sends it no batch. With no+ # checkpoint, the next attempt is a new read that replaces the table.+ is_only_batch = last_key is None and fetch.exhausted and rows_in_page < page_rows+ batch_key = checked_keyset_key(+ tuple(_keyset_key_value(rows[-1][index]) for index in key_indexes), key_columns+ )+ table = table_from_iterator(+ (dict(zip(column_names, row)) for row in rows),+ read_schema,+ primary_keys=primary_keys,+ binary_reporter=binary_reporter,+ )+ # The rows arrive in key order, so each batch ends at a key the walk can+ # continue from. The pipeline commits the staged key only after it writes+ # the batch below. A stage after the yield would leave the committed key+ # one batch behind, and a full refresh appends, so a resumed read would+ # write that batch twice. Nothing that can fail sits between the two.+ last_key = batch_key+ if not is_only_batch:+ manager.save_state(MSSQLResumeState(key_columns=key_columns, last_key=list(batch_key)))+ yield table++ if rows_in_page < page_rows:+ break++ # Only a finished walk gets here. A walk that stops early keeps its checkpoint.+ manager.clear_state()++ items: Callable[[], Iterator[Any]] = get_rows+ takes_keyset_path = manager is not None and keyset.columns is not None+ if manager is not None and keyset.columns is not None:+ items = functools.partial(get_rows_by_keyset, manager, keyset.columns)return SourceResponse(name=location.response_name,- items=get_rows,+ items=items,primary_keys=primary_keys,partition_count=setup.partition_settings.partition_count if setup.partition_settings else None,partition_size=setup.partition_settings.partition_size if setup.partition_settings else None,rows_to_sync=rows_to_sync,+ # `supports_resume` defaults to True. A read on the single query has no position to+ # hand to another worker, so it must say so.+ supports_resume=takes_keyset_path or resumes_incremental,)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.pyindex aa29054b595b..f90605ddb4cd 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py@@ -11,7 +11,7 @@ from products.warehouse_sources.backend.facade.source_config import (SourceFieldInputConfigType,SourceFieldSSHTunnelConfig,)-from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType, ResumableSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import (HostNotAllowedError,SSHTunnelMixin,@@ -19,8 +19,10 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixValidateDatabaseHostMixin,)from products.warehouse_sources.backend.temporal.data_imports.sources.common.registry import SourceRegistry+from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import SourceSchemafrom products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.base import SQLSource+from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs, SourceResponsefrom products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.mssql import MSSQLSourceConfigfrom products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssql import (_SSH_HANDSHAKE_EOF_ERROR,@@ -29,6 +31,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssqMSSQL_SCHEMA_DISCOVERY_DEADLINE_SECONDS,MSSQLImplementation,MSSQLMetadataTimeoutError,+ MSSQLResumeState,retry_on_transient_connection_error,run_metadata_with_deadline,)@@ -70,11 +73,30 @@ _MSSQL_IMPLEMENTATION = MSSQLImplementation()@SourceRegistry.register-class MSSQLSource(SQLSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatabaseHostMixin):+class MSSQLSource(+ SQLSource[MSSQLSourceConfig],+ ResumableSource[MSSQLSourceConfig, MSSQLResumeState],+ SSHTunnelMixin,+ ValidateDatabaseHostMixin,+):@propertydef get_implementation(self) -> MSSQLImplementation:return _MSSQL_IMPLEMENTATION+ def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MSSQLResumeState]:+ return ResumableSourceManager[MSSQLResumeState](inputs, MSSQLResumeState)++ def source_for_pipeline( # type: ignore[override]+ self,+ config: MSSQLSourceConfig,+ resumable_source_manager: ResumableSourceManager[MSSQLResumeState],+ inputs: SourceInputs,+ ) -> SourceResponse:+ # A reset must not continue from a checkpoint. The read starts from the first row.+ if inputs.reset_pipeline:+ resumable_source_manager.clear_state()+ return self.get_implementation.build_pipeline(config, inputs, resumable_source_manager=resumable_source_manager)+@propertydef source_type(self) -> ExternalDataSourceType:return ExternalDataSourceType.MSSQLdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql_resume.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql_resume.pynew file mode 100644index 000000000000..fb694e09609b--- /dev/null+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql_resume.py@@ -0,0 +1,476 @@+import re+import uuid+import datetime+from collections.abc import Iterator+from contextlib import contextmanager+from typing import Any++import pytest+from unittest.mock import MagicMock++from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager+from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql import Table+from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs, SourceResponse+from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.mssql import MSSQLSourceConfig+from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssql import (+ MSSQLColumn,+ MSSQLImplementation,+ MSSQLKeyset,+ MSSQLResumeState,+ MSSQLUniqueIndex,+ _build_keyset_query,+ resolve_mssql_keyset,+)+from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.source import MSSQLSource+from products.warehouse_sources.backend.types import ExternalDataSchemaSyncType, IncrementalFieldType++_MSSQL_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssql"+_CHUNK_ROWS = 3+++def _make_config() -> MSSQLSourceConfig:+ return MSSQLSourceConfig.from_dict(+ {"host": "localhost", "port": 1433, "database": "d", "user": "u", "password": "p", "schema": "dbo"}+ )+++def _make_inputs(**overrides: Any) -> SourceInputs:+ defaults: dict[str, Any] = {+ "schema_name": "orders",+ "schema_id": "schema-id",+ "source_id": "source-id",+ "team_id": 1,+ "should_use_incremental_field": False,+ "db_incremental_field_last_value": None,+ "db_incremental_field_earliest_value": None,+ "incremental_field": None,+ "incremental_field_type": None,+ "job_id": "job-id",+ "logger": MagicMock(),+ "reset_pipeline": False,+ }+ defaults.update(overrides)+ return SourceInputs(**defaults)+++class _FakeRedis:+ def __init__(self, store: dict[str, str]) -> None:+ self._store = store++ def set(self, key: str, value: str, ex: int | None = None) -> None:+ self._store[key] = value++ def get(self, key: str) -> str | None:+ return self._store.get(key)++ def exists(self, key: str) -> int:+ return int(key in self._store)++ def delete(self, key: str) -> None:+ self._store.pop(key, None)+++class _MemoryResumeManager(ResumableSourceManager[MSSQLResumeState]):+ def __init__(self, store: dict[str, str]) -> None:+ super().__init__(_make_inputs(), MSSQLResumeState)+ self.store = store++ @contextmanager+ def _get_redis(self) -> Iterator[_FakeRedis]:+ yield _FakeRedis(self.store)+++def _comparable(value: Any) -> Any:+ if isinstance(value, uuid.UUID):+ return str(value)+ if isinstance(value, datetime.date):+ return value.isoformat()+ return value+++class _FakeCursor:+ def __init__(self, server: "_FakeSQLServer") -> None:+ self._server = server+ self._pending: list[tuple[Any, ...]] = []+ self.description: list[tuple[str]] = []++ def __enter__(self) -> "_FakeCursor":+ return self++ def __exit__(self, *args: Any) -> None:+ return None++ def execute(self, query: str, args: dict[str, Any] | None = None) -> None:+ args = args or {}+ self._server.queries.append((query, args))+ columns = self._server.columns+ order_columns = [columns.index(name) for name in re.findall(r"\[(\w+)\] ASC", query.partition("ORDER BY")[2])]++ def order(row: tuple[Any, ...]) -> tuple[Any, ...]:+ return tuple(_comparable(row[index]) for index in order_columns)++ rows = sorted(self._server.rows, key=order)+ if "keyset_0" in args:+ after = tuple(_comparable(args[f"keyset_{index}"]) for index in range(len(order_columns)))+ rows = [row for row in rows if order(row) > after]+ if "incremental_value" in args:+ match = re.search(r"\[(\w+)\] (>=|>) %\(incremental_value\)s", query)+ assert match is not None+ position, floor = columns.index(match.group(1)), _comparable(args["incremental_value"])+ inclusive = match.group(2) == ">="+ rows = [+ row+ for row in rows+ if row[position] is not None+ and (_comparable(row[position]) > floor or (inclusive and _comparable(row[position]) == floor))+ ]+ top = re.search(r"TOP \((\d+)\)", query)+ if top is not None:+ rows = rows[: int(top.group(1))]+ self._pending = rows+ self.description = [(name,) for name in columns]++ def fetchmany(self, size: int) -> list[tuple[Any, ...]]:+ page, self._pending = self._pending[:size], self._pending[size:]+ return page+++class _FakeSQLServer:+ def __init__(self, columns: list[str], rows: list[tuple[Any, ...]]) -> None:+ self.columns = columns+ self.rows = rows+ self.queries: list[tuple[str, dict[str, Any]]] = []++ def __enter__(self) -> "_FakeSQLServer":+ return self++ def __exit__(self, *args: Any) -> None:+ return None++ def autocommit(self, status: bool) -> None:+ return None++ def cursor(self, **kwargs: Any) -> _FakeCursor:+ return _FakeCursor(self)+++def _index(columns: list[tuple[str, str, bool]], *, clustered: bool = False, primary: bool = False) -> MSSQLUniqueIndex:+ return MSSQLUniqueIndex(+ name="ix_" + "_".join(name for name, _, _ in columns),+ is_clustered=clustered,+ is_primary_key=primary,+ columns=columns,+ )+++_UUIDS = [uuid.UUID(int=(index * 7919 + 13) << 64 | index) for index in range(10)]+_DAYS = [datetime.date(2001, 1, 1) + datetime.timedelta(days=index * 40) for index in range(10)]++# name -> (columns as (name, data type), primary key, unique indexes, rows)+_TABLES: dict[str, tuple[list[tuple[str, str]], list[str] | None, list[MSSQLUniqueIndex], list[tuple[Any, ...]]]] = {+ "bigint_key": (+ [("id", "bigint"), ("note", "nvarchar")],+ ["id"],+ [_index([("id", "bigint", False)], clustered=True, primary=True)],+ [(index * 3_000_000_000 - 9, f"note {index}" if index % 3 else None) for index in range(10)],+ ),+ "uuid_key": (+ [("id", "uniqueidentifier"), ("amount", "int")],+ ["id"],+ [_index([("id", "uniqueidentifier", False)], primary=True)],+ [(key, index) for index, key in enumerate(reversed(_UUIDS))],+ ),+ "date_key": (+ [("day", "date"), ("amount", "int")],+ ["day"],+ [_index([("day", "date", False)], clustered=True, primary=True)],+ [(day, index) for index, day in enumerate(_DAYS)],+ ),+ "composite_key": (+ [("tenant", "int"), ("id", "uniqueidentifier"), ("amount", "int")],+ ["tenant", "id"],+ [_index([("tenant", "int", False), ("id", "uniqueidentifier", False)], clustered=True, primary=True)],+ [(index % 3, key, index) for index, key in enumerate(_UUIDS)],+ ),+ "unique_index_without_primary_key": (+ [("code", "int"), ("amount", "int")],+ None,+ [_index([("code", "int", False)])],+ [(100 - index * 7, None if index % 4 == 0 else index) for index in range(10)],+ ),+ "nullable_unique_index": (+ [("code", "int"), ("amount", "int")],+ None,+ [_index([("code", "int", True)])],+ [(None if index == 4 else index, index) for index in range(10)],+ ),+ "no_key": (+ [("id", "int"), ("amount", "int")],+ None,+ [],+ [(None if index % 4 == 0 else index, index) for index in range(10)],+ ),+ "text_key": (+ [("id", "varchar"), ("amount", "int")],+ ["id"],+ [_index([("id", "varchar", False)], clustered=True, primary=True)],+ [(f"key-{index}", index) for index in range(10)],+ ),+}++_KEYSET_TABLES = ["bigint_key", "uuid_key", "date_key", "composite_key", "unique_index_without_primary_key"]+_SINGLE_QUERY_TABLES = ["nullable_unique_index", "no_key", "text_key"]++_INCREMENTAL_COLUMNS = [("id", "int"), ("updated_at", "datetime2")]+_INCREMENTAL_START = datetime.datetime(2020, 1, 1)+# Two rows share each value, so a batch of three rows always ends between rows of one value.+_INCREMENTAL_ROWS = [+ (index, _INCREMENTAL_START + datetime.timedelta(seconds=index // 2, microseconds=250_000)) for index in range(10)+]+_INCREMENTAL_INPUTS: dict[str, Any] = {+ "should_use_incremental_field": True,+ "incremental_field": "updated_at",+ "incremental_field_type": IncrementalFieldType.DateTime,+ "db_incremental_field_last_value": _INCREMENTAL_START,+ "sync_type": ExternalDataSchemaSyncType.INCREMENTAL,+}+++def serve(mocker):+ def _serve(+ columns: list[tuple[str, str]],+ primary_keys: list[str] | None,+ unique_indexes: list[MSSQLUniqueIndex],+ rows: list[tuple[Any, ...]],+ ) -> _FakeSQLServer:+ table = Table(+ name="orders",+ parents=("dbo",),+ columns=[MSSQLColumn(name=name, data_type=data_type, nullable=True) for name, data_type in columns],+ )+ mocker.patch.object(MSSQLImplementation, "get_table_metadata", return_value=table)+ mocker.patch.object(MSSQLImplementation, "get_primary_keys_for_table", return_value=primary_keys)+ mocker.patch.object(MSSQLImplementation, "get_unique_indexes_for_table", return_value=unique_indexes)+ mocker.patch.object(MSSQLImplementation, "get_rows_to_sync", return_value=0)+ mocker.patch.object(MSSQLImplementation, "get_chunk_size", return_value=_CHUNK_ROWS)+ mocker.patch.object(MSSQLImplementation, "get_partition_settings", return_value=None)+ server = _FakeSQLServer([name for name, _ in columns], rows)+ mocker.patch(f"{_MSSQL_MODULE}.pymssql.connect", return_value=server)+ return server++ return _serve+++def _build(store: dict[str, str], **input_overrides: Any) -> tuple[SourceResponse, _MemoryResumeManager]:+ manager = _MemoryResumeManager(store)+ source = MSSQLImplementation().build_pipeline(+ _make_config(), _make_inputs(**input_overrides), resumable_source_manager=manager+ )+ return source, manager+++def _read(+ store: dict[str, str], *, stop_after: int | None = None, write_last: bool = True, **input_overrides: Any+) -> list[dict[str, Any]]:+ # Mirrors the pipeline: it writes each batch it receives, then commits the staged checkpoint.+ source, manager = _build(store, **input_overrides)+ written: list[dict[str, Any]] = []+ items = source.items()+ for index, table in enumerate(items, start=1): # type: ignore[arg-type]+ if index == stop_after and not write_last:+ break+ written.extend(table.to_pylist())+ manager.commit()+ if index == stop_after:+ break+ items.close() # type: ignore[union-attr]+ return written+++class TestKeysetFullRefresh:+ @pytest.mark.parametrize("write_last", [True, False])+ @pytest.mark.parametrize("stop_after", [1, 2, 3])+ @pytest.mark.parametrize("table", _KEYSET_TABLES)+ def test_interrupted_read_resumes_to_the_rows_of_an_uninterrupted_read(self, serve, table, stop_after, write_last):+ server = serve(*_TABLES[table])+ uninterrupted = _read({})+ assert len(uninterrupted) == len(server.rows)++ store: dict[str, str] = {}+ first = _read(store, stop_after=stop_after, write_last=write_last)+ resumed = _read(store)++ assert first + resumed == uninterrupted+ assert store == {}++ @pytest.mark.parametrize("table", _KEYSET_TABLES)+ def test_run_reports_that_it_can_resume(self, serve, table):+ serve(*_TABLES[table])+ source, _ = _build({})+ assert source.supports_resume is True++ @pytest.mark.parametrize("table", _SINGLE_QUERY_TABLES)+ def test_table_without_a_usable_key_reads_on_the_single_query(self, serve, table):+ server = serve(*_TABLES[table])+ source, _ = _build({})+ store: dict[str, str] = {}++ rows = _read(store)++ assert source.supports_resume is False+ assert len(rows) == len(server.rows)+ assert store == {}+ assert all("TOP" not in query and "ORDER BY" not in query for query, _ in server.queries)++ def test_table_that_fits_in_one_batch_stores_no_checkpoint(self, serve):+ columns, primary_keys, unique_indexes, rows = _TABLES["bigint_key"]+ serve(columns, primary_keys, unique_indexes, rows[: _CHUNK_ROWS - 1])+ store: dict[str, str] = {}++ _read(store, stop_after=1)++ assert store == {}++ def test_checkpoint_for_other_key_columns_restarts_the_read(self, serve):+ server = serve(*_TABLES["bigint_key"])+ store: dict[str, str] = {}+ manager = _MemoryResumeManager(store)+ manager.save_state(MSSQLResumeState(key_columns=["legacy_id"], last_key=[10**12]))+ manager.commit()++ source, manager = _build(store)++ # The pipeline checks the checkpoint after build_pipeline returns. Clearing an incompatible+ # checkpoint here makes it replace the partial table instead of treating a fresh read as a resume.+ assert manager.can_resume() is False+ assert sum(table.num_rows for table in source.items()) == len(server.rows) # type: ignore[union-attr]++ def test_reset_ignores_the_checkpoint(self, serve):+ server = serve(*_TABLES["bigint_key"])+ store: dict[str, str] = {}+ _read(store, stop_after=2)+ manager = _MemoryResumeManager(store)++ source = MSSQLSource().source_for_pipeline(_make_config(), manager, _make_inputs(reset_pipeline=True))++ assert sum(table.num_rows for table in source.items()) == len(server.rows) # type: ignore[union-attr]+++class TestResolveKeyset:+ @pytest.mark.parametrize(+ "indexes,readable,incremental,expected_columns,expected_reason",+ [+ ([_index([("id", "int", False)], primary=True)], {"id"}, False, ["id"], None),+ ([_index([("id", "int", False)], primary=True)], {"id"}, True, None, "incremental_sync"),+ ([], {"id"}, False, None, "no_unique_index"),+ ([_index([("id", "int", True)])], {"id"}, False, None, "nullable_key"),+ ([_index([("id", "nvarchar", False)], primary=True)], {"id"}, False, None, "non_orderable_type:nvarchar"),+ ([_index([("id", "datetime2", False)], primary=True)], {"id"}, False, None, "non_orderable_type:datetime2"),+ ([_index([("code", "int", False)])], {"id"}, False, None, "key_not_projected"),+ (+ [+ _index([("id", "uniqueidentifier", False)], primary=True),+ _index([("seq", "bigint", False)], clustered=True),+ ],+ {"id", "seq"},+ False,+ ["seq"],+ None,+ ),+ (+ [_index([("a", "int", False), ("b", "int", False)]), _index([("id", "int", False)], primary=True)],+ {"a", "b", "id"},+ False,+ ["id"],+ None,+ ),+ (+ [_index([("id", "varchar", False)], clustered=True, primary=True), _index([("code", "int", False)])],+ {"id", "code"},+ False,+ ["code"],+ None,+ ),+ (+ [_index([("a", "int", False), ("b", "date", True)], primary=True)],+ {"a", "b"},+ False,+ None,+ "nullable_key",+ ),+ ],+ )+ def test_picks_the_key_or_names_the_reason(self, indexes, readable, incremental, expected_columns, expected_reason):+ keyset = resolve_mssql_keyset(+ unique_indexes=indexes, readable_columns=readable, should_use_incremental_field=incremental+ )+ assert (keyset.columns, keyset.reason) == (expected_columns, expected_reason)+++class TestBuildKeysetQuery:+ @pytest.mark.parametrize(+ "columns,cast_types,after,expected_sql,expected_params",+ [+ (["id"], ["bigint"], None, "SELECT TOP (500) * FROM [dbo].[orders] ORDER BY [id] ASC", {}),+ (+ ["id"],+ ["uniqueidentifier"],+ ("0a",),+ "SELECT TOP (500) * FROM [dbo].[orders] WHERE ([id] > CAST(%(keyset_0)s AS uniqueidentifier))"+ " ORDER BY [id] ASC",+ {"keyset_0": "0a"},+ ),+ (+ ["tenant", "day", "id"],+ ["int", "date", "bigint"],+ (4, "2020-01-05", 9),+ "SELECT TOP (500) * FROM [dbo].[orders] WHERE ([tenant] >= CAST(%(keyset_0)s AS int)"+ " AND ([tenant] > CAST(%(keyset_0)s AS int) OR ([day] >= CAST(%(keyset_1)s AS date)"+ " AND ([day] > CAST(%(keyset_1)s AS date) OR ([id] > CAST(%(keyset_2)s AS bigint))))))"+ " ORDER BY [tenant] ASC, [day] ASC, [id] ASC",+ {"keyset_0": 4, "keyset_1": "2020-01-05", "keyset_2": 9},+ ),+ ],+ )+ def test_seeks_past_the_last_key_in_key_order(self, columns, cast_types, after, expected_sql, expected_params):+ query, params = _build_keyset_query(+ "dbo", "orders", MSSQLKeyset(columns=columns, cast_types=cast_types), after, 500+ )+ assert (query, params) == (expected_sql, expected_params)+++class TestIncrementalResume:+ @pytest.mark.parametrize("write_last", [True, False])+ @pytest.mark.parametrize("stop_after", [1, 2, 3])+ def test_interrupted_read_resumes_from_the_last_written_value(self, serve, stop_after, write_last):+ server = serve(_INCREMENTAL_COLUMNS, ["id"], [], _INCREMENTAL_ROWS)+ uninterrupted = _read({}, **_INCREMENTAL_INPUTS)++ store: dict[str, str] = {}+ first = _read(store, stop_after=stop_after, write_last=write_last, **_INCREMENTAL_INPUTS)+ server.queries.clear()+ resumed = _read(store, **_INCREMENTAL_INPUTS)++ merged = {row["id"]: row for row in first + resumed}+ assert sorted(merged.values(), key=lambda row: row["id"]) == sorted(uninterrupted, key=lambda row: row["id"])+ assert store == {}+ # Rows at the checkpoint value are read again. Rows below it are not.+ written_batches = stop_after if write_last else stop_after - 1+ if written_batches:+ checkpoint = first[-1]["updated_at"]+ assert server.queries[-1][1]["incremental_value"] == checkpoint+ assert min(row["updated_at"] for row in resumed) == checkpoint++ @pytest.mark.parametrize(+ "primary_keys,sync_type,expected",+ [+ (["id"], ExternalDataSchemaSyncType.INCREMENTAL, True),+ (["id"], ExternalDataSchemaSyncType.APPEND, False),+ (None, ExternalDataSchemaSyncType.INCREMENTAL, False),+ ],+ )+ def test_only_a_merge_on_a_primary_key_resumes(self, serve, primary_keys, sync_type, expected):+ serve([("code", "int"), ("updated_at", "datetime2")], primary_keys and ["code"], [], [])+ source, _ = _build({}, **{**_INCREMENTAL_INPUTS, "sync_type": sync_type})+ assert source.supports_resume is expected