From 18fb04ac9a426cc67213b9a5afb08187c11bbe10 Mon Sep 17 00:00:00 2001 From: Piotr Szul Date: Tue, 11 Jun 2024 13:39:11 +1000 Subject: [PATCH] Changing extraction of quantity from pharmacy.fill_quantity. When possible extract the numerical value and the unit. Otherwise, do not provide the quantity. Fixing: https://github.com/kind-lab/mimic-fhir/issues/106 https://github.com/kind-lab/mimic-fhir/issues/107 - Error parsing JSON: the primitive value must be a number (e.g: quantity": {"value": "3"}) - The value '60 ml' is not a valid decimal (e.g: {"value": "60ml"}) - The value 'PO' is not a valid decimal ({"value": "PO"}) --- sql/fhir_medication_dispense.sql | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sql/fhir_medication_dispense.sql b/sql/fhir_medication_dispense.sql index f691c05..8870b1d 100644 --- a/sql/fhir_medication_dispense.sql +++ b/sql/fhir_medication_dispense.sql @@ -28,6 +28,11 @@ WITH distinct_prescriptions AS ( , medu.fhir_unit AS medu_FHIR_UNIT , ph.dispensation AS ph_DISPENSATION , ph.fill_quantity AS ph_FILL_QUANTITY + -- assume the ph.fill_quantity is [#][]. The unit can be quoted. Valid units are ml, bottle, btl, g, lb + -- anything else will produce NULL ph_FILL_QUANTITY_VALUE. + -- Quantities without unit will produce NULL ph_FILL_QUANTITY_UNIT (notice the difference in the reg. expressions) + , (regexp_match(ph.fill_quantity, '^\s*#?(\d+(\.\d+)?)\s*''*(|ml|bottle|btl|g|lb)''*\s*$','i'))[1]::NUMERIC AS ph_FILL_QUANTITY_VALUE + , replace(lower((regexp_match(ph.fill_quantity, '^\s*#?(\d+(\.\d+)?)\s*''*(ml|bottle|btl|g|lb)''*\s*$','i'))[3]), 'btl', 'bottle') AS ph_FILL_QUANTITY_UNIT , TRIM(REGEXP_REPLACE(ph.medication, '\s+', ' ', 'g')) AS ph_MEDICATION , CASE WHEN ph.duration IS NULL AND ph.frequency IS NULL @@ -104,9 +109,10 @@ SELECT )) ELSE NULL END , 'quantity', - CASE WHEN ph_FILL_QUANTITY IS NOT NULL THEN + CASE WHEN ph_FILL_QUANTITY_VALUE IS NOT NULL THEN jsonb_build_object( - 'value', ph_FILL_QUANTITY + 'value', ph_FILL_QUANTITY_VALUE + , 'unit', ph_FILL_QUANTITY_UNIT ) ELSE NULL END , 'dosageInstruction', CASE WHEN dosageInstructionFlag THEN ARRAY[jsonb_build_object(