Skip to content

: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.

fix(mssql): treat conversion failures as non-retryable (#64440)

Tom Owers5df5892230cf
diff --git a/posthog/temporal/data_imports/sources/mssql/source.py b/posthog/temporal/data_imports/sources/mssql/source.py
index 2b4012b92472..a52eacb8ddbd 100644
--- a/posthog/temporal/data_imports/sources/mssql/source.py
+++ b/posthog/temporal/data_imports/sources/mssql/source.py
@@ -70,6 +70,13 @@ class MSSQLSource(SQLSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatabase
# whose definition selects a column that's no longer present. Fixed source-data shape,
# so retrying won't help.
"Invalid column name": "One of the columns being synced no longer exists in your SQL Server. A column was likely dropped or renamed, or a view's definition references a column that's no longer present. Fix the column or view definition at the source, then re-enable the sync.",
+ # SQL Server error 245 — an implicit type conversion fails on a specific row's value
+ # (e.g. converting the varchar 'SFDR' to int). Our SELECT does no casts and the
+ # incremental predicate only ever compares like types, so this conversion lives in the
+ # view body or a computed column we're reading from. It's fixed by the source data +
+ # view definition, so retrying replays the identical 245. Match the stable error text,
+ # not the volatile value / data type that follow it.
+ "Conversion failed when converting": "A value in one of the tables or views you're syncing can't be converted to the type its query expects (SQL Server error 245) — for example a text value where a number is required. This usually comes from a view definition or computed column that casts or compares mismatched types. Fix the conversion at the source (correct the data or the view), then re-enable the sync.",
# Raised by the `sshtunnel` library (via the shared `open_ssh_tunnel` helper) when the
# SSH tunnel can't be brought up — the bastion host is unreachable, the host/port is
# wrong, the SSH key/credentials are rejected, or a firewall blocks PostHog's IPs. This
diff --git a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
index b23e3e9ddaae..844ea2975595 100644
--- a/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/posthog/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -589,3 +589,19 @@ class TestMSSQLSourceNonRetryableErrors:
def test_invalid_column_name_is_non_retryable(self, error_msg):
non_retryable = MSSQLSource().get_non_retryable_errors()
assert any(pattern in error_msg for pattern in non_retryable.keys()), error_msg
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Real pymssql MSSQLDatabaseException for SQL Server error 245 raised mid-fetch when a
+ # view body implicitly converts a varchar value to int.
+ "SQL Server message 245, severity 16, state 1, procedure b'@\\x88[\\xd4\\xfe\\xff', line 1:\n"
+ "b\"Conversion failed when converting the varchar value 'SFDR' to data type int."
+ 'DB-Lib error message 20018, severity 16:\\nGeneral SQL Server error: Check messages from the SQL Server\\n"',
+ # Different value / target type must still match the stable substring.
+ "Conversion failed when converting the nvarchar value 'N/A' to data type bigint.",
+ ],
+ )
+ def test_conversion_failed_is_non_retryable(self, error_msg):
+ non_retryable = MSSQLSource().get_non_retryable_errors()
+ assert any(pattern in error_msg for pattern in non_retryable.keys()), error_msg