Getting D1 Error `Expression tree is too

Getting D1 Error: Expression tree is too large (maximum depth 20)
1 Reply
Jason Hostetter
Jason HostetterOP2y ago
Here is the example query:
WITH preQuery AS (
WITH subQuery AS (
SELECT
studies.id AS study_id,
instances.id AS instance_id
FROM studies

JOIN instances on studies.id = instances.study_id

JOIN instances_metadata im0 on im0.instance_id = instances.id
WHERE
im0.metadata_id IN (SELECT id FROM metadata WHERE metadata.hex_tag = ? AND metadata.string_value = ?)

GROUP BY studies.id
LIMIT ?
OFFSET ?
)
SELECT subQuery.study_id, subQuery.instance_id, m.string_value as order_index
FROM subQuery
JOIN instances_metadata im ON im.instance_id = subQuery.instance_id
JOIN metadata m ON m.id = im.metadata_id WHERE m.hex_tag = ?
ORDER BY CAST(order_index as INTEGER) DESC
)

SELECT
preQuery.order_index,
metadata.id AS metadata_id,
metadata.hex_tag,
metadata.vr,
metadata.string_value,
metadata.blob_value,
metadata.blob_hash,
instances_metadata.sequence_item_index AS sequence_item_index,
instances.id AS instance_id,
series.id AS series_id,
studies.id AS study_id,
patients.id AS patient_id,
COUNT(DISTINCT series.id) as related_series,
COUNT(DISTINCT instances.id) as related_instances
FROM metadata
JOIN instances_metadata ON instances_metadata.metadata_id = metadata.id
JOIN instances ON instances_metadata.instance_id = instances.id
JOIN series ON instances.series_id = series.id
JOIN studies ON series.study_id = studies.id
JOIN patients ON studies.patient_id = patients.id
JOIN preQuery ON preQuery.study_id = studies.id
WHERE EXISTS (SELECT preQuery.study_id FROM preQuery WHERE preQuery.study_id = studies.id) AND substr(hex_tag, 1, 8) IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
GROUP BY studies.id, hex_tag
WITH preQuery AS (
WITH subQuery AS (
SELECT
studies.id AS study_id,
instances.id AS instance_id
FROM studies

JOIN instances on studies.id = instances.study_id

JOIN instances_metadata im0 on im0.instance_id = instances.id
WHERE
im0.metadata_id IN (SELECT id FROM metadata WHERE metadata.hex_tag = ? AND metadata.string_value = ?)

GROUP BY studies.id
LIMIT ?
OFFSET ?
)
SELECT subQuery.study_id, subQuery.instance_id, m.string_value as order_index
FROM subQuery
JOIN instances_metadata im ON im.instance_id = subQuery.instance_id
JOIN metadata m ON m.id = im.metadata_id WHERE m.hex_tag = ?
ORDER BY CAST(order_index as INTEGER) DESC
)

SELECT
preQuery.order_index,
metadata.id AS metadata_id,
metadata.hex_tag,
metadata.vr,
metadata.string_value,
metadata.blob_value,
metadata.blob_hash,
instances_metadata.sequence_item_index AS sequence_item_index,
instances.id AS instance_id,
series.id AS series_id,
studies.id AS study_id,
patients.id AS patient_id,
COUNT(DISTINCT series.id) as related_series,
COUNT(DISTINCT instances.id) as related_instances
FROM metadata
JOIN instances_metadata ON instances_metadata.metadata_id = metadata.id
JOIN instances ON instances_metadata.instance_id = instances.id
JOIN series ON instances.series_id = series.id
JOIN studies ON series.study_id = studies.id
JOIN patients ON studies.patient_id = patients.id
JOIN preQuery ON preQuery.study_id = studies.id
WHERE EXISTS (SELECT preQuery.study_id FROM preQuery WHERE preQuery.study_id = studies.id) AND substr(hex_tag, 1, 8) IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
GROUP BY studies.id, hex_tag
Want results from more Discord servers?
Add your server