fix(parsers): honour HL7 v2 MSH delimiters, repetitions and escape sequences - #382
Merged
Merged
Conversation
…quences The HL7 v2 parser hard-coded "|" and "^", so it ignored the delimiters a message declares in MSH-1/MSH-2. It never split "~" repetitions and never decoded escape sequences. As a result PID-3 "12345~98765" came out as the patient id, "\S\" / "\T\" stayed in text values, and a message using "#" as its field separator parsed to zero messages. Each MSH segment now sets the delimiters for its message: the field separator comes from seg[3] and the component, repetition, escape and subcomponent characters from MSH-2. Any that are missing default to ^~\&. Those delimiters stay in force until the next MSH. Segment ids are detected with seg[:3], and a recognised segment must be followed by the current field separator. Single-valued columns (ids, names, codes, dates, units, status) take the first repetition. OBX-5 is a repeating field, so the observation value keeps every repetition, joined with "~". Fields are split before escapes are decoded, so an escaped delimiter such as \R\ never splits a value. \F\ \S\ \T\ \R\ \E\ are decoded using the message's own delimiters, and other escapes (\H\, \N\, \Xhh\, \.br\) are kept verbatim. Whole-field values are re-emitted with the standard ^ / & / ~ separators. Standard |^~\& messages without escapes, and without repetitions outside OBX-5, produce the same frames, metadata and warnings as before. Closes #261
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
FreshData benchmark report —
|
| fixture | n_rows | n_cols | p50 s | p95 s | peak MB | repair % | false-repair % | preserve % | trust | monotonic | export % |
|---|
Authored-code reduction (Metric 6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The HL7 v2 ER7 parser hard-coded
|and^, so it ignored the delimiters a message declares in MSH-1/MSH-2. It never split~repetitions and never decoded escape sequences. PID-312345~98765became the patient id,\S\/\T\stayed in values, and a#-delimited message parsed to 0 messages.seg[3], component/repetition/escape/subcomponent from MSH-2 (missing ones default to^~\&). They apply until the next MSH.seg[:3], and a known segment must be followed by the current field separator. Unknown segments are still reported by name.~. A custom repetition character is re-emitted as~.\R\never splits a value.\F\ \S\ \T\ \R\ \E\are decoded using the message's delimiters. Other escapes (\H\,\N\,\Xhh\,\.br\) are kept verbatim.message_typeuse the standard^/&/~separators whatever the message declared.|^~\&messages without escapes, and without repetitions outside OBX-5, give the same frames, metadata and warnings as before.Most of the
hl7v2.pyline count isruff formaton a file that was not formatted on main.Tests
New
tests/parsers/test_hl7v2_delimiters.py:#field separator)MSH#$*@!delimiters, and a partial MSH-2 that falls back to defaults\R\decoded after the split rather than split onPIDXprefix treated as unknownVerification
ruff check .passes; changed files areruff format-cleanmypy src/freshdata: no issues in 202 source filespytest -m "not online and not large": py3.12 4483 passed / 6 skipped; py3.9 4479 passed / 10 skippedCloses #261