Skip to content

fix: use (path, segment_code) instead of id_path as diff JOIN key - #257

Closed
hf-mrdachner wants to merge 2 commits into
mainfrom
fix/ahb-diff-join-key
Closed

fix: use (path, segment_code) instead of id_path as diff JOIN key#257
hf-mrdachner wants to merge 2 commits into
mainfrom
fix/ahb-diff-join-key

Conversation

@hf-mrdachner

@hf-mrdachner hf-mrdachner commented Mar 29, 2026

Copy link
Copy Markdown

Summary

  • Replace id_path with (path, segment_code) as the JOIN key in v_ahb_diff
  • id_path contains positional indices that shift on insertions, producing false add/delete pairs
  • (path, segment_code) is unique per pruefidentifikator+version and stable across versions

Impact

Example: UTILMD/44001 FV2510→FV2604

Before (id_path) After (path, segment_code)
added 69 5
deleted 64 0
modified 27 1
unchanged 122 212

Test plan

  • Run existing test_ahb_diff_view.py tests — 7/7 passed
  • Verify UTILMD/44001 FV2510→FV2604 produces 5 added, 0 deleted, 1 modified
  • Verify INVOIC/31002 FV2510→FV2604 still produces 1 modified
  • Check (path, segment_code) uniqueness — unique for 97% of (version, pruefi) combinations

Known limitations

  • IFTSTA (21045): path is not unique enough due to flat segment group naming — tracked in IFTSTA: path column not unique enough for stable diff JOIN key #258
  • PARTIN/MSCONS/PRICAT: A few benign cnt=2 duplicates from repeated identical elements (e.g. two "Name des Kontoinhabers" fields) — diff result still correct because content is identical

Fixes #256

🤖 Generated with Claude Code

hf-mrdachner and others added 2 commits March 29, 2026 07:32
The id_path contains positional indices (@00005-00001-) that shift when
segments are inserted in newer format versions, causing false added/deleted
rows. (path, segment_code) is a stable, unique key across versions.

Example: UTILMD/44001 FV2510→FV2604
- Before: 69 added, 64 deleted, 27 modified (160 "changes")
- After:   5 added,  0 deleted,  1 modified (6 real changes)

Fixes #256

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…oin key

- Rename test_diff_view_no_duplicate_id_paths to test_diff_view_no_duplicate_join_keys,
  checking (path, segment_code) uniqueness instead of id_path
- Update snapshots: significantly fewer false add/delete entries due to stable join key
  (snapshot file shrinks by ~2500 lines)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@hf-kklein hf-kklein left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the problem and commented on #256 in a longer comment.

Specifically for this PR: I'd not merge it like that because we loose the only purpose that id-path has: to be unique. Instead of a single column that is unique we introduce a mechanism that requires the users of the DB to think and interpret themselves. The intention of the pre-computed ID path was to obey the "don't make me think" principle. The users of the DB shouldn't solve the uniqueness problem on their own but build upon guarantees that we as creators of the DB give and enforce.

Can we keep the id-path as a SINGLE column that is unique per ahb-line (together with formatversion and prüfi)? I like that it's a SINGLE column, not a tuple of columns. But this also means you have to dig deeper into the code and parts of the code I'm not proud of ;)

Comment on lines +79 to +80
Test that there are no duplicate (path, segment_code) pairs for the same version pair comparison.
Each (path, segment_code) combination should appear exactly once in the diff results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is why I wouldn't merge the PR as of now: if the id is not unique anymore we can drop it right away. that's the only (and a very important) purpose of the id_path column.

the id path MUST stay unique

@hf-kklein

Copy link
Copy Markdown
Contributor

removing the guarantees that id-path gives right now would also mean to rewrite parts of the ahb-tabellen codebase. ahb-tabellen is agnostic on how idpath looks like but it needs the guarantee that it's unique (and that sort_path has the correct order).

@hf-kklein

hf-kklein commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

ok, noch ein letzter kommentar: ich glaube es ist besser das "id-path ist falsch konstruiert und nicht für cross-formatversions-vergleiche zu gebrauchen" zu lösen als den vergleich symptombekämpfend anzupassen. verstehst du, was ich meine? auch wenn das schwieriger ist, wäre es glaube ich der ursächliche fix. das problem versteh ich und das ist vollkommen richtig in #256 zusammengefasst. aber die lösung sollte imho auch direkt bei der konstruktion vom id-path ansetzen

@hf-kklein

Copy link
Copy Markdown
Contributor

Hi @hf-mrdachner! An alternative fix is now available in #260 that addresses the root cause — fixing id_path construction itself in materialize_ahb_view.sql — rather than changing the diff view JOIN key.

The approach in #260 injects semantic qualifiers (like DTM+92) directly into the id_path during the recursive CTE, keeping it as a single unique column. This preserves all the downstream guarantees that @hf-kklein mentioned in his review.

Your bug report in #256 and the edge case analysis in #258 and #259 were essential foundation work for this fix — the IFTSTA, PARTIN, MSCONS, and PRICAT cases you identified are now all tested explicitly. Please keep filing issues like these; they make the codebase better! 🙏

Would you be open to reviewing #260? Your deep understanding of the data would be very valuable for the review.

@hf-mrdachner

Copy link
Copy Markdown
Author

Closing in favor of #260 which fixes the root cause (id_path construction) instead of working around it in the view JOIN. The remaining MSCONS cross-version instability is a data quality issue tracked in xml-migs-and-ahbs#80.

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.

v_ahb_diff: id_path-based JOIN produces false adds/deletes when segments are inserted

2 participants