Skip to content

Write temporal primary keys as canonical text and parse them in check (#107) - #116

Open
RodolfoMeloDev wants to merge 2 commits into
CrunchyData:mainfrom
RodolfoMeloDev:fix/107-temporal-pk-json-and-check-filter
Open

RodolfoMeloDev wants to merge 2 commits into
CrunchyData:mainfrom
RodolfoMeloDev:fix/107-temporal-pk-json-and-check-filter

Conversation

@RodolfoMeloDev

Copy link
Copy Markdown

Fixes #107.

Problem

check reads the pk JSON of a row back and binds it to the key filter on both the source and the target. SQL_REPO_SELECT_OUTOFSYNC_ROWS picks one pk per pk_hash from either side. For a date/datetime/timestamp/time key, the JSON applied the platform's replace() to the raw column, and the filter bound that text to col = ?. The issue describes SQL Server and PostgreSQL; checking every platform found that the problem reaches all of them:

Platform pk JSON on main Effect
PostgreSQL function replace(timestamp ...) does not exist the compare query aborts, the run continues with an empty side, and every row is reported missing
SQL Server style 0, Nov 5 2026 2:03PM, or nov 5 2026 under SET LANGUAGE Portuguese seconds and fractions are lost, and the text depends on the session language
Oracle NLS_DATE_FORMAT, 05-NOV-26, or 05/11/26 with a pt-BR JVM the time and the century are lost (year 9999 becomes 99)
MySQL, MariaDB 2026-11-05 14:03:27 in session time, while the pk_hash uses UTC
Db2 implicit text not bindable on the other side

The check filter then fails on PostgreSQL (timestamp = varchar) and resolves through session settings elsewhere. A row whose key lost its seconds is found on neither side and, since #103, is cleared as converged. Real drift is reported as in sync.

Change

New TemporalKeyUtils, used for primary-key columns of the types in TIMESTAMP_TYPES, except MySQL year and SQL Server rowversion (timestamp):

  • keyText() renders YYYY-MM-DD HH24:MI:SS[.fraction], or HH24:MI:SS for time, with explicit formats only. It keeps each platform's full precision (up to 12 digits on Db2) and normalizes to UTC exactly where castTimestamp() does for the pk_hash.
  • keyParameter() parses the bind parameter with explicit formats or ISO input. It trims the fraction to what the platform accepts (7 on SQL Server, 9 on Oracle and Snowflake, 12 on Db2). Where a wider comparison would miss the row, it converts to the column's own type: a SQL Server datetime compared with an uncast datetime2 misses .003/.997.
  • createColumnFilterClause() now receives the platform and reads data_type, which is added to SQL_REPO_DCTABLECOLUMNMAP_BYORIGINALIAS.

Non-temporal keys and the pk_hash are unchanged.

Platform Key text (timestamp) Filter parameter (timestamp)
postgres to_char(col,'YYYY-MM-DD HH24:MI:SS.US'); tz: at time zone 'UTC' cast(? as timestamp); tz: (cast(? as timestamp) at time zone 'UTC')
mssql convert(varchar(27), col, 121); datetimeoffset via switchoffset(col,'+00:00') cast(convert(datetime2(7), left(?, 27), 121) as <datetime|smalldatetime|date>)
oracle to_char(col,'YYYY-MM-DD HH24:MI:SS.FF9'); DATE without fraction to_timestamp(substr(?,1,29),'YYYY-MM-DD HH24:MI:SS.FF9'); DATE: to_date(substr(?,1,19),...)
mysql date_format(convert_tz(col,@@session.time_zone,'UTC'),'%Y-%m-%d %H:%i:%s.%f') convert_tz(cast(? as datetime(6)),'UTC',@@session.time_zone)
mariadb date_format(col,'%Y-%m-%d %H:%i:%s.%f') cast(? as datetime(6))
db2 to_char(col,'YYYY-MM-DD HH24:MI:SS.FF12') cast(cast(? as varchar(40)) as timestamp(12))
snowflake to_char(col,'YYYY-MM-DD HH24:MI:SS.FF9'); ltz/tz via convert_timezone('UTC', col) to_timestamp_ntz(substr(?,1,29))

Tests

  • New TemporalKeyUtilsTest, 78 cases: golden cases for every platform and type, plus the call sites (pk JSON and filter, temporal and non-temporal).
  • mvn test: 111 tests, 0 failures.
  • SQL level, on PostgreSQL 16, SQL Server 2022, MySQL 8.4, MariaDB 11.4, Oracle XE 21/23 and Db2 12.1:
    • Every row is found by its own key text, including the range limits (0001-01-01, 1753-01-01, 9999-12-31 23:59:59.997/.9999999/.999999999999), datetime2(7), datetimeoffset with offsets that change the UTC date, Oracle TIMESTAMP(9) and WITH TIME ZONE, and Db2 TIMESTAMP(12).
    • Each parser accepts the text written by every other platform: 0 to 12 fractional digits, and date-only text.
    • The results hold under DateStyle SQL, DMY, SET LANGUAGE Portuguese plus SET DATEFORMAT dmy, Oracle NLS for Brazil, and session time zone America/Sao_Paulo.
    • The filter keeps the primary-key index: Clustered Index Seek on SQL Server, INDEX UNIQUE SCAN on Oracle, const on MySQL.
  • End to end: see tables e2e_keys and e2e_dkeys below. On main they fail on all six platforms. With the fixes, every row is equal with identical data, and every change is reported by compare and confirmed by check. That includes the row present only on the target, whose key comes from PostgreSQL and is bound on the source platform.

Notes

  • Snowflake is implemented from the documentation and was not run; there is no local instance. Please validate it, or tell me how you would like it covered.
  • This PR and the one for check truncates numeric primary-key values to 32 bits and reports real drift as in sync #111 change adjacent lines in checkRows(). Whichever is merged second needs a trivial rebase.
  • Not changed here, possible follow-ups:
    • SQLFixGenerationService (fix=true) still writes the pk JSON values as SQL literals. A temporal literal with 6 fractional digits is rejected by a SQL Server datetime target.
    • Oracle renders a decimal pk value between -1 and 1 without the leading zero (.5) in the pk JSON.

End-to-end evidence

The same scenario ran with each platform below as the source and PostgreSQL 16 as
target and repository, once with main (db72a0c) and once with the four fix
branches merged together (739d1be: #106, #107, #111 and #112), from a fresh repository
and target each time. pgCompare ran with default settings and a pt-BR JVM locale.

Source Image
PostgreSQL 16 postgres:16
MySQL 8.4 mysql:8.4
MariaDB 11.4 mariadb:11.4
SQL Server 2022 mcr.microsoft.com/mssql/server:2022-latest
Oracle XE 21 gvenzl/oracle-xe:21-slim-faststart
Db2 12.1 icr.io/db2_community/db2:latest
Snowflake not run: no local instance

Three tables with identical rows on both sides:

Table Primary key Exercises
e2e_keys (id bigint, dt timestamp(3)); datetime on SQL Server, TIMESTAMP(3) on Oracle and Db2 #107, #111 (keys 2147483648, 123456789012...), #112 (a, b, 11/11/2026, xml)
e2e_dkeys (id int, d date); DATE on Oracle #107
e2e_num id int; amount decimal(24,10) with 0.19, -0.5, 0.0165, 1e10, 12345678901, 1e-10... #106

Steps: compare with identical data (all rows must be equal), then these changes on the target, then compare and check (both must report exactly them):

-- e2e_keys: expected Not Equal 2, Missing Source 1, Missing Target 1
UPDATE hr.e2e_keys SET note = 'a, c' WHERE id = 123456789012;          -- only a text column with ', ' differs (#112)
UPDATE hr.e2e_keys SET amount = -0.4 WHERE id = 123456789013;          -- value below 1 (#106)
DELETE FROM hr.e2e_keys WHERE id = 123456789020;                      -- key above 2^31 (#111) + timestamp with ms (#107)
INSERT INTO hr.e2e_keys VALUES (123456789021, '2026-11-05 10:00:01.457', 2, 'extra, only target');
-- e2e_dkeys: expected Missing Target 1, Not Equal 1
DELETE FROM hr.e2e_dkeys WHERE id = 3;
UPDATE hr.e2e_dkeys SET note = 'y, changed' WHERE id = 2;
-- e2e_num: expected Not Equal 2
UPDATE hr.e2e_num SET amount = 12345678902 WHERE id = 2;              -- exponent e+10 (#106 mysql/mariadb)
UPDATE hr.e2e_num SET amount = 0.1901 WHERE id = 4;                   -- exponent e-01 (#106 mssql/db2)

Result: with the fixes, all 18 table runs (6 platforms x 3 tables) match the expectation in the three steps, with no SEVERE line in the log. On main:

Source Identical data check after the changes
PostgreSQL temporal-key tables abort (function replace(timestamp ...) does not exist) no result
MySQL, MariaDB temporal-key tables all Missing Target; e2e_num 4 of 9 Not Equal (e+10, e-10 decades) e2e_keys: 4 rows reported equal, 3 real changes lost
SQL Server temporal-key tables all Missing Target; e2e_num 4 of 9 Not Equal (0 < abs(v) < 1) e2e_keys: 5 rows reported equal, 4 real changes lost
Oracle temporal-key tables all Missing Target e2e_keys: 5 rows reported equal, 4 real changes lost
Db2 temporal-key tables all Missing Target e2e_keys: 4 rows reported equal, 3 real changes lost
Full result per platform, build and table (E = equal, NE = not equal, MS/MT = missing source/target)
postgres  main   e2e_keys   identical:ERR [E0 NE0 MS0 MT0]  drift-compare:ERR [E0 NE0 MS0 MT0]  drift-check:ERR [(no result)]  severe=8
postgres  main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT0]  drift-compare:ERR [E0 NE0 MS0 MT0]  drift-check:ERR [(no result)]  severe=8
postgres  main   e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=8
postgres  fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
postgres  fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
postgres  fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0
mysql     main   e2e_keys   identical:ERR [E0 NE0 MS0 MT5]  drift-compare:ERR [E0 NE0 MS0 MT5]  drift-check:ERR [E4 NE0 MS0 MT1]  severe=18
mysql     main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT4]  drift-compare:ERR [E0 NE0 MS0 MT4]  drift-check:ERR [E0 NE0 MS0 MT4]  severe=18
mysql     main   e2e_num    identical:ERR [E5 NE4 MS0 MT0]  drift-compare:ERR [E4 NE5 MS0 MT0]  drift-check:ERR [E0 NE5 MS0 MT0]  severe=18
mysql     fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
mysql     fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
mysql     fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0
mariadb   main   e2e_keys   identical:ERR [E0 NE0 MS0 MT5]  drift-compare:ERR [E0 NE0 MS0 MT5]  drift-check:ERR [E4 NE0 MS0 MT1]  severe=18
mariadb   main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT4]  drift-compare:ERR [E0 NE0 MS0 MT4]  drift-check:ERR [E0 NE0 MS0 MT4]  severe=18
mariadb   main   e2e_num    identical:ERR [E5 NE4 MS0 MT0]  drift-compare:ERR [E4 NE5 MS0 MT0]  drift-check:ERR [E0 NE5 MS0 MT0]  severe=18
mariadb   fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
mariadb   fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
mariadb   fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0
mssql     main   e2e_keys   identical:ERR [E0 NE0 MS0 MT5]  drift-compare:ERR [E0 NE0 MS0 MT5]  drift-check:ERR [E5 NE0 MS0 MT0]  severe=18
mssql     main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT4]  drift-compare:ERR [E0 NE0 MS0 MT4]  drift-check:ERR [E0 NE0 MS0 MT4]  severe=18
mssql     main   e2e_num    identical:ERR [E5 NE4 MS0 MT0]  drift-compare:ERR [E4 NE5 MS0 MT0]  drift-check:ERR [E0 NE5 MS0 MT0]  severe=18
mssql     fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
mssql     fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
mssql     fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0
oracle    main   e2e_keys   identical:ERR [E0 NE0 MS0 MT5]  drift-compare:ERR [E0 NE0 MS0 MT5]  drift-check:ERR [E5 NE0 MS0 MT0]  severe=15
oracle    main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT4]  drift-compare:ERR [E0 NE0 MS0 MT4]  drift-check:ERR [E2 NE0 MS0 MT2]  severe=15
oracle    main   e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=15
oracle    fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
oracle    fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
oracle    fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0
db2       main   e2e_keys   identical:ERR [E0 NE0 MS0 MT5]  drift-compare:ERR [E0 NE0 MS0 MT5]  drift-check:ERR [E4 NE0 MS0 MT1]  severe=15
db2       main   e2e_dkeys  identical:ERR [E0 NE0 MS0 MT4]  drift-compare:ERR [E0 NE0 MS0 MT4]  drift-check:ERR [E0 NE0 MS0 MT4]  severe=15
db2       main   e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=15
db2       fixed  e2e_keys   identical:OK  [E5 NE0 MS0 MT0]  drift-compare:OK  [E2 NE2 MS1 MT1]  drift-check:OK  [E0 NE2 MS1 MT1]  severe=0
db2       fixed  e2e_dkeys  identical:OK  [E4 NE0 MS0 MT0]  drift-compare:OK  [E2 NE1 MS0 MT1]  drift-check:OK  [E0 NE1 MS0 MT1]  severe=0
db2       fixed  e2e_num    identical:OK  [E9 NE0 MS0 MT0]  drift-compare:OK  [E7 NE2 MS0 MT0]  drift-check:OK  [E0 NE2 MS0 MT0]  severe=0

Unit tests (mvn test) per branch
main                                          db72a0c  Tests run: 33, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS
fix/111-check-pk-numeric-bind                 b0fc3b3  Tests run: 47, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS
fix/112-check-column-diff-json                05b0c00  Tests run: 60, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS
fix/106-notation-exponent                     7f3c8b1  Tests run: 34, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS
fix/107-temporal-pk-json-and-check-filter     6baa271  Tests run: 111, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS
integration/local                             739d1be  Tests run: 153, Failures: 0, Errors: 0, Skipped: 0  BUILD SUCCESS

Before the end-to-end runs, the expressions were also run directly on each engine,
including under non-default session settings (PostgreSQL DateStyle SQL, DMY; SQL Server
SET LANGUAGE Portuguese and SET DATEFORMAT dmy; Oracle NLS for Brazil; MySQL and Oracle
session time zone America/Sao_Paulo). The scripts, data and full pgCompare logs of every run
are available on request.

🤖 Generated with Claude Code

RodolfoMeloDev and others added 2 commits November 12, 2026 11:59
…CrunchyData#107)

The pk JSON of a row is read back by check and bound to the key filter on
both the source and the target, since the repository picks one pk per
pk_hash from either side. For a date/datetime/timestamp/time key the JSON
applied the platform's replace() to the raw column:

- PostgreSQL rejects it (function replace(timestamp ...) does not exist):
  the compare query aborts, the run continues with an empty side and every
  row is reported missing.
- SQL Server converts with style 0 ('Nov  5 2026  2:03PM'): seconds and
  fractions are lost and the text follows SET LANGUAGE.
- Oracle converts with NLS_DATE_FORMAT ('05-NOV-26'): the time and century
  are lost.
- MySQL and MariaDB keep the value, but in session time while the pk_hash
  uses UTC.

The check filter then bound that text to col = ?, which PostgreSQL rejects
for temporal columns and the other platforms resolve with session
settings. A row whose key lost its seconds is not found on either side and,
since CrunchyData#103, is cleared as converged: real drift reported as in sync.

Add TemporalKeyUtils, used for primary-key columns of the types in
TIMESTAMP_TYPES (except MySQL year and SQL Server rowversion):

- keyText() renders YYYY-MM-DD HH24:MI:SS[.fraction] (HH24:MI:SS for time)
  with explicit formats only, keeping each platform's full precision and
  normalizing to UTC exactly where castTimestamp() does for the pk_hash.
- keyParameter() parses the bind parameter back with explicit formats or
  ISO input, trimming the fraction to what the platform accepts, and
  converts to the column's own type where a wider comparison would miss
  the row (SQL Server datetime against datetime2).

createColumnFilterClause() now reads data_type from dc_table_column_map
(added to SQL_REPO_DCTABLECOLUMNMAP_BYORIGINALIAS) and the destination
platform to build the filter. Non-temporal keys are unchanged, and the
pk_hash is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…runchyData#107)

Checked on Db2 12.1:

- to_char(...,'FF9') cut a timestamp(12) key to 9 digits, and timestamp()
  parses to precision 6, so such a row was not found by its own key.
  Render FF12 and parse with cast(... as timestamp(12)).
- Db2 TIME has no fraction and time() rejects '14:03:27.123456'
  (SQL0180N), which other platforms write for their time keys. Parse only
  the first 8 characters.
- Snowflake parses at most 9 fractional digits; trim the parameter so a
  12-digit Db2 key does not fail there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Temporal primary key columns break the PK JSON expression (PostgreSQL query aborts, SQL Server loses seconds) and the check filter

1 participant