Skip to content

Commit ee121f6

Browse files
fix: BigQuery-safe neuroblock gap check and D/C matching
Replace postgres-style interval subtraction with DATETIME_DIFF, and match CareVue D/C flags via LIKE so sqlglot can transpile the concept. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3a914fc commit ee121f6

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

mimic-iii/concepts/durations/neuroblock_dose.sql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ with drugmv as
2828
, 1 as drug
2929

3030
-- the 'stopped' column indicates if a drug has been disconnected
31-
, max(case when stopped in ('Stopped','D/C\'d') then 1 else 0 end) as drug_stopped
31+
-- match other CareVue duration scripts (avoids apostrophe that breaks sqlglot)
32+
, max(case when stopped = 'Stopped' OR stopped LIKE 'D/C%' then 1 else 0 end) as drug_stopped
3233

3334
-- we only include continuous infusions, therefore expect a rate
3435
, max(case
@@ -68,7 +69,7 @@ with drugmv as
6869
, 1 as drug
6970

7071
-- the 'stopped' column indicates if a drug has been disconnected
71-
, max(case when stopped in ('Stopped','D/C\'d') then 1 else 0 end) as drug_stopped
72+
, max(case when stopped = 'Stopped' OR stopped LIKE 'D/C%' then 1 else 0 end) as drug_stopped
7273
, max(case when valuenum <= 10 then 0 else 1 end) as drug_null
7374

7475
-- educated guess!
@@ -179,7 +180,12 @@ select
179180
)
180181
= 1 then 1
181182

182-
when (CHARTTIME - (LAG(CHARTTIME, 1) OVER (partition by icustay_id, drug order by charttime))) > (interval '8 hours') then 1
183+
-- BigQuery concepts use DATETIME_DIFF (postgres-style interval math is invalid here)
184+
when DATETIME_DIFF(
185+
CHARTTIME,
186+
LAG(CHARTTIME, 1) OVER (partition by icustay_id, drug order by charttime),
187+
HOUR
188+
) > 8 then 1
183189
else null
184190
end as drug_start
185191

mimic-iii/concepts_duckdb/durations/neuroblock_dose.sql

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ WITH drugmv AS (
88
amount AS drug_amount,
99
starttime,
1010
endtime
11-
FROM mimiciii.inputevents_mv
11+
FROM mimiciii_clinical.inputevents_mv
1212
WHERE
1313
itemid IN (222062, 221555)
1414
AND statusdescription <> 'Rewritten'
@@ -18,7 +18,7 @@ WITH drugmv AS (
1818
icustay_id,
1919
charttime,
2020
1 AS drug,
21-
MAX(CASE WHEN stopped IN ('Stopped', 'D/C''d') THEN 1 ELSE 0 END) AS drug_stopped,
21+
MAX(CASE WHEN stopped = 'Stopped' OR stopped LIKE 'D/C%' THEN 1 ELSE 0 END) AS drug_stopped,
2222
MAX(
2323
CASE
2424
WHEN itemid >= 40000 AND NOT amount IS NULL
@@ -30,7 +30,7 @@ WITH drugmv AS (
3030
) AS drug_null,
3131
MAX(CASE WHEN itemid >= 40000 THEN COALESCE(rate, amount) ELSE rate END) AS drug_rate,
3232
MAX(amount) AS drug_amount
33-
FROM mimiciii.inputevents_cv
33+
FROM mimiciii_clinical.inputevents_cv
3434
WHERE
3535
itemid IN (
3636
30114,
@@ -55,11 +55,11 @@ WITH drugmv AS (
5555
icustay_id,
5656
charttime,
5757
1 AS drug,
58-
MAX(CASE WHEN stopped IN ('Stopped', 'D/C''d') THEN 1 ELSE 0 END) AS drug_stopped,
58+
MAX(CASE WHEN stopped = 'Stopped' OR stopped LIKE 'D/C%' THEN 1 ELSE 0 END) AS drug_stopped,
5959
MAX(CASE WHEN valuenum <= 10 THEN 0 ELSE 1 END) AS drug_null,
6060
MAX(CASE WHEN valuenum <= 10 THEN valuenum ELSE NULL END) AS drug_rate,
6161
MAX(CASE WHEN valuenum > 10 THEN valuenum ELSE NULL END) AS drug_amount
62-
FROM mimiciii.chartevents
62+
FROM mimiciii_clinical.chartevents
6363
WHERE
6464
itemid IN (1856, 2164, 2548, 2285, 2290, 2670, 2546, 1098, 2390, 2511, 1028, 1858)
6565
GROUP BY
@@ -100,13 +100,11 @@ WITH drugmv AS (
100100
THEN 1
101101
WHEN LAG(drug_stopped, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST) = 1
102102
THEN 1
103-
WHEN (
104-
CHARTTIME - (
105-
LAG(CHARTTIME, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST)
106-
)
107-
) > (
108-
INTERVAL '8' HOURS
109-
)
103+
WHEN DATE_DIFF(
104+
'HOUR',
105+
LAG(CHARTTIME, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST),
106+
CHARTTIME
107+
) > 8
110108
THEN 1
111109
ELSE NULL
112110
END AS drug_start

mimic-iii/concepts_postgres/durations/neuroblock_dose.sql

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
22
DROP TABLE IF EXISTS mimiciii_derived.neuroblock_dose; CREATE TABLE mimiciii_derived.neuroblock_dose AS
3-
/* This query extracts dose+durations of neuromuscular blocking agents */ /* Note: we assume that injections will be filtered for carevue as they will have starttime = stopttime. */ /* Get drug administration data from CareVue and MetaVision */ /* metavision is simple and only requires one temporary table */
3+
/* This query extracts dose+durations of neuromuscular blocking agents */
4+
/* Note: we assume that injections will be filtered for carevue as they will have starttime = stopttime. */
5+
/* Get drug administration data from CareVue and MetaVision */
6+
/* metavision is simple and only requires one temporary table */
47
WITH drugmv AS (
58
SELECT
69
icustay_id,
@@ -9,7 +12,7 @@ WITH drugmv AS (
912
amount AS drug_amount,
1013
starttime,
1114
endtime
12-
FROM mimiciii.inputevents_mv
15+
FROM mimiciii_clinical.inputevents_mv
1316
WHERE
1417
itemid IN (
1518
222062, /* Vecuronium (664 rows, 154 infusion rows) */
@@ -21,8 +24,8 @@ WITH drugmv AS (
2124
SELECT
2225
icustay_id,
2326
charttime, /* where clause below ensures all rows are instance of the drug */
24-
1 AS drug, /* the 'stopped' column indicates if a drug has been disconnected */
25-
MAX(CASE WHEN stopped IN ('Stopped', 'D/C''d') THEN 1 ELSE 0 END) AS drug_stopped, /* we only include continuous infusions, therefore expect a rate */
27+
1 AS drug, /* the 'stopped' column indicates if a drug has been disconnected */ /* match other CareVue duration scripts (avoids apostrophe that breaks sqlglot) */
28+
MAX(CASE WHEN stopped = 'Stopped' OR stopped LIKE 'D/C%' THEN 1 ELSE 0 END) AS drug_stopped, /* we only include continuous infusions, therefore expect a rate */
2629
MAX(
2730
CASE
2831
WHEN itemid >= 40000 AND NOT amount IS NULL
@@ -34,7 +37,7 @@ WITH drugmv AS (
3437
) AS drug_null,
3538
MAX(CASE WHEN itemid >= 40000 THEN COALESCE(rate, amount) ELSE rate END) AS drug_rate,
3639
MAX(amount) AS drug_amount
37-
FROM mimiciii.inputevents_cv
40+
FROM mimiciii_clinical.inputevents_cv
3841
WHERE
3942
itemid IN (
4043
30114, /* Cisatracurium (63994 rows) */
@@ -60,11 +63,11 @@ WITH drugmv AS (
6063
icustay_id,
6164
charttime, /* where clause below ensures all rows are instance of the drug */
6265
1 AS drug, /* the 'stopped' column indicates if a drug has been disconnected */
63-
MAX(CASE WHEN stopped IN ('Stopped', 'D/C''d') THEN 1 ELSE 0 END) AS drug_stopped,
66+
MAX(CASE WHEN stopped = 'Stopped' OR stopped LIKE 'D/C%' THEN 1 ELSE 0 END) AS drug_stopped,
6467
MAX(CASE WHEN valuenum <= 10 THEN 0 ELSE 1 END) AS drug_null, /* educated guess! */
6568
MAX(CASE WHEN valuenum <= 10 THEN valuenum ELSE NULL END) AS drug_rate,
6669
MAX(CASE WHEN valuenum > 10 THEN valuenum ELSE NULL END) AS drug_amount
67-
FROM mimiciii.chartevents
70+
FROM mimiciii_clinical.chartevents
6871
WHERE
6972
itemid IN (
7073
1856, /* Vecuronium mcg/min (8 rows) */
@@ -118,13 +121,7 @@ WITH drugmv AS (
118121
THEN 1
119122
WHEN LAG(drug_stopped, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST) = 1
120123
THEN 1
121-
WHEN (
122-
CHARTTIME - (
123-
LAG(CHARTTIME, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST)
124-
)
125-
) > (
126-
INTERVAL '8 HOURS'
127-
)
124+
WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', CHARTTIME) - DATE_TRUNC('hour', LAG(CHARTTIME, 1) OVER (PARTITION BY icustay_id, drug ORDER BY charttime NULLS FIRST))) / 3600 AS BIGINT) > 8
128125
THEN 1
129126
ELSE NULL
130127
END AS drug_start
@@ -227,7 +224,8 @@ WITH drugmv AS (
227224
drug_groups_sum,
228225
drug_rate
229226
)
230-
/* now assign this data to every hour of the patient's stay */ /* drug_amount for carevue is not accurate */
227+
/* now assign this data to every hour of the patient's stay */
228+
/* drug_amount for carevue is not accurate */
231229
SELECT
232230
icustay_id,
233231
starttime,

0 commit comments

Comments
 (0)