← All runs and checks

Live ClickHouse latency check — 2026-09-29

DB latency checkNot a benchmark run: no questions were asked and nothing was graded. Direct, read-only timings of the SQL behind three open cbioportal-mcp PRs, on the current public database.

None of these PRs is merged or deployed. #151, #154 and #160 are open. All numbers come from the current public ClickHouse database (cbioportal_public_librechat_blue), with each PR's SQL run inline, next to what is deployed today. They describe the database side only, not what users would see end to end.

#160 · IN-subquery rewrite

0.24–0.60 s

saved per eligible view call; median 0.29 s over 8 gene × alteration cases. Results identical in all 8.

#154 · precomputed lookups

0.08–1.35 s

live fallback SQL today, vs an estimated ~3–4 ms keyed lookup once the tables exist.

#151 · schema cache

1.4–2.6 ms

server time of each SHOW TABLES / DESCRIBE a cache hit would avoid.

DB share of answer latency

not measured

the 09-23 run artifacts record tool names and timings but not the SQL each call ran, so no answer could be replayed.

Method · All queries · #160 · #154 · #151 · Limitations · Data · Parameterized views

Summary

For scale: the 2026-09-23 run

ModelAnswersSQL tool callsMedian answer sp90 answer sMedian LLM calls
Haiku14670829.5774.859
Sonnet14675971.93157.2810

Method and safety

Every measured query

Medians of three warm runs. Times in ms. The exact SQL of each is below the table and in queries.json.

QueryServer msRows readBytes readLaptop round trip ms
Schema and metadata
metadata-show-tables2.58825,543568.46
metadata-describe-cancer_study1.72243,736566.20
metadata-describe-genomic_event_derived1.38192,563548.27
metadata-describe-sample_to_gene_panel_derived1.375459539.48
metadata-describe-clinical_data_derived1.3871,586541.05
metadata-describe-sample_derived1.5512969590.97
metadata-describe-gene1.374307543.38
metadata-describe-genetic_profile1.4012963555.84
metadata-version-timezone1.9011546.55
metadata-settings42.751,609603,095712.86
inventory3.49827,019556.85
Keyed-lookup estimates
lookup-estimate-gene4.0044,896510,560547.76
lookup-estimate-study3.0654813,834552.48
#160 before/after
160-before-TP53-mutation991.7930,599,882567,050,0141,704.56
160-after-TP53-mutation394.8330,599,882567,050,014992.66
160-before-TP53-amplification875.8743,829,849681,771,0921,420.19
160-after-TP53-amplification489.1643,829,849681,771,0921,058.41
160-before-TP53-deep_deletion753.2743,829,849702,416,4281,444.88
160-after-TP53-deep_deletion468.6743,829,849702,416,4281,017.18
160-before-TP53-structural_variant485.2914,117,465294,297,6671,107.88
160-after-TP53-structural_variant211.0814,117,465294,297,6671,033.98
160-before-ERBB2-mutation744.8630,599,882538,585,9581,340.93
160-after-ERBB2-mutation361.5830,599,882538,585,9581,035.03
160-before-ERBB2-amplification765.8543,829,849701,038,0721,394.65
160-after-ERBB2-amplification496.4943,829,849701,038,0721,087.27
160-before-ERBB2-deep_deletion732.7743,829,849680,061,8201,295.10
160-after-ERBB2-deep_deletion487.9743,829,849680,061,8201,093.79
160-before-ERBB2-structural_variant572.3714,117,465290,145,2401,216.84
160-after-ERBB2-structural_variant270.0514,117,465290,145,240859.12
#154 live fallback
154-frequency-msk_chord_2024186.836,162,61522,762,255875.77
154-top-mutation-msk_chord_2024282.0110,281,78146,600,333838.16
154-profiled-msk_chord_2024138.482,916,22832,829,525781.63
154-frequency-brca_tcga_pan_can_atlas_201882.096,432,95116,190,499648.44
154-top-mutation-brca_tcga_pan_can_atlas_2018310.1510,019,63732,958,606889.98
154-profiled-brca_tcga_pan_can_atlas_201878.312,850,6929,002,888641.21
154-cancer-type-TP531,348.4630,599,882567,050,0141,902.63
154-cancer-type-TP53-pan-cancer-tcga852.7430,599,882567,050,0141,408.48
#154 table-build SELECTs (one study each)
154-build-study_gene_alteration_counts-msk_chord_2024558.5513,498,04084,986,8761,231.98
154-build-study_gene_alteration_counts-brca_tcga_pan_can_atlas_2018547.7814,038,71287,346,2961,753.96
154-build-cancer_type_gene_alteration_counts-msk_chord_2024997.1313,718,83091,693,4821,895.59
154-build-cancer_type_gene_alteration_counts-brca_tcga_pan_can_atlas_20181,036.7014,161,19893,593,0182,821.80
154-build-study_profiled_counts-msk_chord_202469.332,916,22832,829,525698.10
154-build-study_profiled_counts-brca_tcga_pan_can_atlas_201835.992,850,6929,002,888686.03
SQL for all 43 queries
metadata-show-tables
SHOW TABLES
metadata-describe-cancer_study
DESCRIBE TABLE cancer_study
metadata-describe-genomic_event_derived
DESCRIBE TABLE genomic_event_derived
metadata-describe-sample_to_gene_panel_derived
DESCRIBE TABLE sample_to_gene_panel_derived
metadata-describe-clinical_data_derived
DESCRIBE TABLE clinical_data_derived
metadata-describe-sample_derived
DESCRIBE TABLE sample_derived
metadata-describe-gene
DESCRIBE TABLE gene
metadata-describe-genetic_profile
DESCRIBE TABLE genetic_profile
metadata-version-timezone
SELECT version(), timezone()
metadata-settings
SELECT name, value FROM system.settings LIMIT 1000
inventory
SELECT name, total_rows, total_bytes FROM system.tables WHERE database=currentDatabase() ORDER BY name
lookup-estimate-gene
SELECT hugo_gene_symbol, entrez_gene_id FROM gene WHERE hugo_gene_symbol='TP53'
lookup-estimate-study
SELECT cancer_study_identifier FROM cancer_study WHERE cancer_study_identifier='msk_chord_2024'
160-before-TP53-mutation
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='TP53', alteration='mutation')
160-after-TP53-mutation
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'TP53'
      AND ged.off_panel = 0
      AND (
        ('mutation' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('mutation' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('mutation' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('mutation' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'TP53' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'mutation' = 'mutation',           'MUTATION_EXTENDED',
          'mutation' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'mutation' = 'mutation',           'MUTATION_EXTENDED',
          'mutation' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-TP53-amplification
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='TP53', alteration='amplification')
160-after-TP53-amplification
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'TP53'
      AND ged.off_panel = 0
      AND (
        ('amplification' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('amplification' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('amplification' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('amplification' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'TP53' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'amplification' = 'mutation',           'MUTATION_EXTENDED',
          'amplification' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'amplification' = 'mutation',           'MUTATION_EXTENDED',
          'amplification' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-TP53-deep_deletion
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='TP53', alteration='deep_deletion')
160-after-TP53-deep_deletion
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'TP53'
      AND ged.off_panel = 0
      AND (
        ('deep_deletion' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('deep_deletion' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('deep_deletion' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('deep_deletion' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'TP53' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'deep_deletion' = 'mutation',           'MUTATION_EXTENDED',
          'deep_deletion' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'deep_deletion' = 'mutation',           'MUTATION_EXTENDED',
          'deep_deletion' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-TP53-structural_variant
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='TP53', alteration='structural_variant')
160-after-TP53-structural_variant
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'TP53'
      AND ged.off_panel = 0
      AND (
        ('structural_variant' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('structural_variant' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('structural_variant' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('structural_variant' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'TP53' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'structural_variant' = 'mutation',           'MUTATION_EXTENDED',
          'structural_variant' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'structural_variant' = 'mutation',           'MUTATION_EXTENDED',
          'structural_variant' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-ERBB2-mutation
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='ERBB2', alteration='mutation')
160-after-ERBB2-mutation
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'ERBB2'
      AND ged.off_panel = 0
      AND (
        ('mutation' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('mutation' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('mutation' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('mutation' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'ERBB2' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'mutation' = 'mutation',           'MUTATION_EXTENDED',
          'mutation' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'mutation' = 'mutation',           'MUTATION_EXTENDED',
          'mutation' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'mutation' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-ERBB2-amplification
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='ERBB2', alteration='amplification')
160-after-ERBB2-amplification
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'ERBB2'
      AND ged.off_panel = 0
      AND (
        ('amplification' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('amplification' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('amplification' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('amplification' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'ERBB2' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'amplification' = 'mutation',           'MUTATION_EXTENDED',
          'amplification' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'amplification' = 'mutation',           'MUTATION_EXTENDED',
          'amplification' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'amplification' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-ERBB2-deep_deletion
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='ERBB2', alteration='deep_deletion')
160-after-ERBB2-deep_deletion
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'ERBB2'
      AND ged.off_panel = 0
      AND (
        ('deep_deletion' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('deep_deletion' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('deep_deletion' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('deep_deletion' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'ERBB2' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'deep_deletion' = 'mutation',           'MUTATION_EXTENDED',
          'deep_deletion' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'deep_deletion' = 'mutation',           'MUTATION_EXTENDED',
          'deep_deletion' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'deep_deletion' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
160-before-ERBB2-structural_variant
SELECT * FROM gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', gene='ERBB2', alteration='structural_variant')
160-after-ERBB2-structural_variant
WITH cohort AS (
    SELECT cancer_study_identifier
    FROM cancer_study_query_preferences
    WHERE preference_name = 'all_studies_non_redundant'
),
sample_cancer_type AS (
    SELECT cd.sample_unique_id, cd.attribute_value AS cancer_type
    FROM clinical_data_derived cd
    JOIN cohort c USING (cancer_study_identifier)
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
altered AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT ged.sample_unique_id) AS altered_samples
    FROM genomic_event_derived ged
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    WHERE ged.hugo_gene_symbol = 'ERBB2'
      AND ged.off_panel = 0
      AND (
        ('structural_variant' = 'mutation'
            AND ged.variant_type = 'mutation'
            AND ged.mutation_status != 'UNCALLED')
        OR ('structural_variant' = 'amplification'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = 2)
        OR ('structural_variant' = 'deep_deletion'
            AND ged.variant_type = 'cna'
            AND ged.cna_alteration = -2)
        OR ('structural_variant' = 'structural_variant'
            AND ged.variant_type = 'structural_variant')
      )
    GROUP BY sct.cancer_type
),
profiled_samples_for_gene AS (
    -- Map the user-facing alteration token to the alteration_type stored
    -- on sample_to_gene_panel_derived. Same gene-in-panel-or-WES branch
    -- as the mutation view, but with the matching alteration_type filter.
    SELECT stgp.sample_unique_id, stgp.cancer_study_identifier
    FROM sample_to_gene_panel_derived stgp
    JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    -- IN-subquery instead of JOIN gene: resolves the symbol to its entrez
    -- id(s) once, rather than joining every panel row against the gene
    -- table before filtering. COUNT(DISTINCT) downstream makes the result
    -- identical even for symbols with several (or duplicated) gene rows.
    -- IS NOT NULL keeps NULL keys unmatched (as the equality JOIN did) even
    -- under transform_null_in=1.
    WHERE gpl.gene_id IN (
        SELECT entrez_gene_id FROM gene
        WHERE hugo_gene_symbol = 'ERBB2' AND entrez_gene_id IS NOT NULL)
      AND stgp.alteration_type = multiIf(
          'structural_variant' = 'mutation',           'MUTATION_EXTENDED',
          'structural_variant' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
    UNION ALL
    SELECT sample_unique_id, cancer_study_identifier
    FROM sample_to_gene_panel_derived
    WHERE gene_panel_id = 'WES'
      AND alteration_type = multiIf(
          'structural_variant' = 'mutation',           'MUTATION_EXTENDED',
          'structural_variant' = 'amplification',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'deep_deletion',      'COPY_NUMBER_ALTERATION',
          'structural_variant' = 'structural_variant', 'STRUCTURAL_VARIANT',
          '')
),
profiled AS (
    SELECT sct.cancer_type,
           COUNT(DISTINCT p.sample_unique_id) AS profiled_samples
    FROM profiled_samples_for_gene p
    JOIN cohort c USING (cancer_study_identifier)
    JOIN sample_cancer_type sct USING (sample_unique_id)
    GROUP BY sct.cancer_type
)
SELECT a.cancer_type,
       a.altered_samples,
       p.profiled_samples,
       ROUND(a.altered_samples * 100.0 / NULLIF(p.profiled_samples, 0), 1) AS frequency_pct
FROM altered a
JOIN profiled p USING (cancer_type)
WHERE p.profiled_samples >= 50
154-frequency-msk_chord_2024
WITH
        events AS (
            SELECT sample_unique_id,
                   (variant_type = 'mutation' AND mutation_status != 'UNCALLED') AS is_mutation,
                   (variant_type = 'cna' AND cna_alteration = 2) AS is_amplification,
                   (variant_type = 'cna' AND cna_alteration = -2) AS is_deep_deletion,
                   (variant_type = 'structural_variant' AND mutation_status != 'UNCALLED') AS is_structural_variant
            FROM genomic_event_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND hugo_gene_symbol IN ('TP53')
              AND off_panel = 0
        ),
        profiled AS (
            SELECT stgp.sample_unique_id AS sample_unique_id,
                   stgp.alteration_type AS alteration_type
            FROM sample_to_gene_panel_derived stgp
            JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
            JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
            JOIN gene ge ON gpl.gene_id = ge.entrez_gene_id
            WHERE stgp.cancer_study_identifier IN ('msk_chord_2024')
              AND ge.hugo_gene_symbol IN ('TP53')
              AND (stgp.alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (stgp.alteration_type = 'COPY_NUMBER_ALTERATION' AND stgp.genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
            UNION ALL
            SELECT sample_unique_id, alteration_type
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND gene_panel_id = 'WES'
              AND (alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
        )
        SELECT *
        FROM (
            SELECT groupUniqArray(hugo_gene_symbol) AS matched_genes
            FROM gene WHERE hugo_gene_symbol IN ('TP53')
        ) AS gene_match
        CROSS JOIN (
            SELECT groupUniqArray(cancer_study_identifier) AS matched_studies
            FROM cancer_study WHERE cancer_study_identifier IN ('msk_chord_2024')
        ) AS study_match
        CROSS JOIN (
            SELECT uniqExactIf(sample_unique_id, is_mutation) AS altered_mutation,
                   uniqExactIf(sample_unique_id, is_amplification) AS altered_amplification,
                   uniqExactIf(sample_unique_id, is_deep_deletion) AS altered_deep_deletion,
                   uniqExactIf(sample_unique_id, is_structural_variant)
                       AS altered_structural_variant,
                   uniqExactIf(sample_unique_id, is_mutation OR is_amplification
                       OR is_deep_deletion OR is_structural_variant) AS altered_any
            FROM events
        ) AS altered
        CROSS JOIN (
            SELECT uniqExactIf(sample_unique_id, alteration_type = 'MUTATION_EXTENDED')
                       AS profiled_MUTATION_EXTENDED,
                   uniqExactIf(sample_unique_id, alteration_type = 'COPY_NUMBER_ALTERATION')
                       AS profiled_COPY_NUMBER_ALTERATION,
                   uniqExactIf(sample_unique_id, alteration_type = 'STRUCTURAL_VARIANT')
                       AS profiled_STRUCTURAL_VARIANT,
                   uniqExact(sample_unique_id) AS profiled_ANY
            FROM profiled
        ) AS profiled_counts
154-top-mutation-msk_chord_2024
WITH
        altered AS (
            SELECT hugo_gene_symbol, COUNT(DISTINCT sample_unique_id) AS altered_samples
            FROM genomic_event_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND off_panel = 0
              AND (variant_type = 'mutation' AND mutation_status != 'UNCALLED')
            GROUP BY hugo_gene_symbol
            ORDER BY altered_samples DESC, hugo_gene_symbol ASC
            LIMIT 20
        ),
        wes_samples AS (
            SELECT DISTINCT sample_unique_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND gene_panel_id = 'WES'
              AND (alteration_type IN ('MUTATION_EXTENDED'))
        ),
        panel_profiled AS (
            SELECT ge.hugo_gene_symbol AS hugo_gene_symbol,
                   COUNT(DISTINCT stgp.sample_unique_id) AS n
            FROM sample_to_gene_panel_derived stgp
            JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
            JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
            JOIN gene ge ON gpl.gene_id = ge.entrez_gene_id
            WHERE stgp.cancer_study_identifier IN ('msk_chord_2024')
              AND (stgp.alteration_type IN ('MUTATION_EXTENDED'))
              AND stgp.sample_unique_id NOT IN (SELECT sample_unique_id FROM wes_samples)
              AND ge.hugo_gene_symbol IN (SELECT hugo_gene_symbol FROM altered)
            GROUP BY ge.hugo_gene_symbol
        )
        SELECT a.hugo_gene_symbol AS hugo_gene_symbol,
               a.altered_samples AS altered_samples,
               (SELECT count() FROM wes_samples) + COALESCE(p.n, 0) AS profiled_samples
        FROM altered a
        LEFT JOIN panel_profiled p ON a.hugo_gene_symbol = p.hugo_gene_symbol
        ORDER BY altered_samples DESC, hugo_gene_symbol ASC
154-profiled-msk_chord_2024
WITH
        profiled AS (
            SELECT cancer_study_identifier, alteration_type AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
            UNION ALL
            SELECT cancer_study_identifier, 'COPY_NUMBER_ALTERATION_DISCRETE' AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND ((alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
            UNION ALL
            SELECT cancer_study_identifier, 'ANY_MUT_CNA_SV' AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
              AND (alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
        ),
        sample_patient AS (
            SELECT sample_unique_id, patient_unique_id
            FROM sample_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
        )
        SELECT * FROM (
            SELECT cancer_study_identifier, 'ALL_SAMPLES' AS profile_type,
                   COUNT(DISTINCT sample_unique_id) AS samples,
                   COUNT(DISTINCT patient_unique_id) AS patients,
                   toUInt64(0) AS wes_samples
            FROM sample_derived
            WHERE cancer_study_identifier IN ('msk_chord_2024')
            GROUP BY cancer_study_identifier
            UNION ALL
            SELECT p.cancer_study_identifier, p.profile_type,
                   COUNT(DISTINCT p.sample_unique_id),
                   COUNT(DISTINCT nullIf(sp.patient_unique_id, '')),
                   COUNT(DISTINCT if(p.gene_panel_id = 'WES', p.sample_unique_id, NULL))
            FROM profiled p
            LEFT JOIN sample_patient sp ON p.sample_unique_id = sp.sample_unique_id
            GROUP BY p.cancer_study_identifier, p.profile_type
        )
        ORDER BY cancer_study_identifier, profile_type = 'ALL_SAMPLES' DESC,
                 profile_type = 'ANY_MUT_CNA_SV' DESC, samples DESC, profile_type
154-frequency-brca_tcga_pan_can_atlas_2018
WITH
        events AS (
            SELECT sample_unique_id,
                   (variant_type = 'mutation' AND mutation_status != 'UNCALLED') AS is_mutation,
                   (variant_type = 'cna' AND cna_alteration = 2) AS is_amplification,
                   (variant_type = 'cna' AND cna_alteration = -2) AS is_deep_deletion,
                   (variant_type = 'structural_variant' AND mutation_status != 'UNCALLED') AS is_structural_variant
            FROM genomic_event_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND hugo_gene_symbol IN ('TP53')
              AND off_panel = 0
        ),
        profiled AS (
            SELECT stgp.sample_unique_id AS sample_unique_id,
                   stgp.alteration_type AS alteration_type
            FROM sample_to_gene_panel_derived stgp
            JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
            JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
            JOIN gene ge ON gpl.gene_id = ge.entrez_gene_id
            WHERE stgp.cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND ge.hugo_gene_symbol IN ('TP53')
              AND (stgp.alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (stgp.alteration_type = 'COPY_NUMBER_ALTERATION' AND stgp.genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
            UNION ALL
            SELECT sample_unique_id, alteration_type
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND gene_panel_id = 'WES'
              AND (alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
        )
        SELECT *
        FROM (
            SELECT groupUniqArray(hugo_gene_symbol) AS matched_genes
            FROM gene WHERE hugo_gene_symbol IN ('TP53')
        ) AS gene_match
        CROSS JOIN (
            SELECT groupUniqArray(cancer_study_identifier) AS matched_studies
            FROM cancer_study WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
        ) AS study_match
        CROSS JOIN (
            SELECT uniqExactIf(sample_unique_id, is_mutation) AS altered_mutation,
                   uniqExactIf(sample_unique_id, is_amplification) AS altered_amplification,
                   uniqExactIf(sample_unique_id, is_deep_deletion) AS altered_deep_deletion,
                   uniqExactIf(sample_unique_id, is_structural_variant)
                       AS altered_structural_variant,
                   uniqExactIf(sample_unique_id, is_mutation OR is_amplification
                       OR is_deep_deletion OR is_structural_variant) AS altered_any
            FROM events
        ) AS altered
        CROSS JOIN (
            SELECT uniqExactIf(sample_unique_id, alteration_type = 'MUTATION_EXTENDED')
                       AS profiled_MUTATION_EXTENDED,
                   uniqExactIf(sample_unique_id, alteration_type = 'COPY_NUMBER_ALTERATION')
                       AS profiled_COPY_NUMBER_ALTERATION,
                   uniqExactIf(sample_unique_id, alteration_type = 'STRUCTURAL_VARIANT')
                       AS profiled_STRUCTURAL_VARIANT,
                   uniqExact(sample_unique_id) AS profiled_ANY
            FROM profiled
        ) AS profiled_counts
154-top-mutation-brca_tcga_pan_can_atlas_2018
WITH
        altered AS (
            SELECT hugo_gene_symbol, COUNT(DISTINCT sample_unique_id) AS altered_samples
            FROM genomic_event_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND off_panel = 0
              AND (variant_type = 'mutation' AND mutation_status != 'UNCALLED')
            GROUP BY hugo_gene_symbol
            ORDER BY altered_samples DESC, hugo_gene_symbol ASC
            LIMIT 20
        ),
        wes_samples AS (
            SELECT DISTINCT sample_unique_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND gene_panel_id = 'WES'
              AND (alteration_type IN ('MUTATION_EXTENDED'))
        ),
        panel_profiled AS (
            SELECT ge.hugo_gene_symbol AS hugo_gene_symbol,
                   COUNT(DISTINCT stgp.sample_unique_id) AS n
            FROM sample_to_gene_panel_derived stgp
            JOIN gene_panel gp ON stgp.gene_panel_id = gp.stable_id
            JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
            JOIN gene ge ON gpl.gene_id = ge.entrez_gene_id
            WHERE stgp.cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND (stgp.alteration_type IN ('MUTATION_EXTENDED'))
              AND stgp.sample_unique_id NOT IN (SELECT sample_unique_id FROM wes_samples)
              AND ge.hugo_gene_symbol IN (SELECT hugo_gene_symbol FROM altered)
            GROUP BY ge.hugo_gene_symbol
        )
        SELECT a.hugo_gene_symbol AS hugo_gene_symbol,
               a.altered_samples AS altered_samples,
               (SELECT count() FROM wes_samples) + COALESCE(p.n, 0) AS profiled_samples
        FROM altered a
        LEFT JOIN panel_profiled p ON a.hugo_gene_symbol = p.hugo_gene_symbol
        ORDER BY altered_samples DESC, hugo_gene_symbol ASC
154-profiled-brca_tcga_pan_can_atlas_2018
WITH
        profiled AS (
            SELECT cancer_study_identifier, alteration_type AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
            UNION ALL
            SELECT cancer_study_identifier, 'COPY_NUMBER_ALTERATION_DISCRETE' AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND ((alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
            UNION ALL
            SELECT cancer_study_identifier, 'ANY_MUT_CNA_SV' AS profile_type,
                   sample_unique_id, gene_panel_id
            FROM sample_to_gene_panel_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
              AND (alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT') OR (alteration_type = 'COPY_NUMBER_ALTERATION' AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE')))
        ),
        sample_patient AS (
            SELECT sample_unique_id, patient_unique_id
            FROM sample_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
        )
        SELECT * FROM (
            SELECT cancer_study_identifier, 'ALL_SAMPLES' AS profile_type,
                   COUNT(DISTINCT sample_unique_id) AS samples,
                   COUNT(DISTINCT patient_unique_id) AS patients,
                   toUInt64(0) AS wes_samples
            FROM sample_derived
            WHERE cancer_study_identifier IN ('brca_tcga_pan_can_atlas_2018')
            GROUP BY cancer_study_identifier
            UNION ALL
            SELECT p.cancer_study_identifier, p.profile_type,
                   COUNT(DISTINCT p.sample_unique_id),
                   COUNT(DISTINCT nullIf(sp.patient_unique_id, '')),
                   COUNT(DISTINCT if(p.gene_panel_id = 'WES', p.sample_unique_id, NULL))
            FROM profiled p
            LEFT JOIN sample_patient sp ON p.sample_unique_id = sp.sample_unique_id
            GROUP BY p.cancer_study_identifier, p.profile_type
        )
        ORDER BY cancer_study_identifier, profile_type = 'ALL_SAMPLES' DESC,
                 profile_type = 'ANY_MUT_CNA_SV' DESC, samples DESC, profile_type
154-cancer-type-TP53
SELECT * FROM (
            SELECT cancer_type, 'TP53' AS hugo_gene_symbol,
                   altered_samples, profiled_samples
            FROM gene_alteration_frequency_by_cancer_type(
                preference='all_studies_non_redundant', gene='TP53',
                alteration='mutation')
            )
        ORDER BY altered_samples / profiled_samples DESC, altered_samples DESC, cancer_type ASC
        LIMIT 20
154-cancer-type-TP53-pan-cancer-tcga
SELECT * FROM (
            SELECT cancer_type, 'TP53' AS hugo_gene_symbol,
                   altered_samples, profiled_samples
            FROM gene_alteration_frequency_by_cancer_type(
                preference='pan_cancer_tcga', gene='TP53',
                alteration='mutation')
            )
        ORDER BY altered_samples / profiled_samples DESC, altered_samples DESC, cancer_type ASC
        LIMIT 20
154-build-study_gene_alteration_counts-msk_chord_2024
WITH
alteration_to_profile AS (
    SELECT t.1 AS alteration_type, t.2 AS profile_type
    FROM (
        SELECT arrayJoin([
            ('mutation',           'MUTATION_EXTENDED'),
            ('amplification',      'COPY_NUMBER_ALTERATION'),
            ('deep_deletion',      'COPY_NUMBER_ALTERATION'),
            ('structural_variant', 'STRUCTURAL_VARIANT'),
            ('any',                'ANY')
        ]) AS t
    )
),
events AS (
    SELECT cancer_study_identifier,
           hugo_gene_symbol,
           sample_unique_id,
           multiIf(
               variant_type = 'mutation' AND mutation_status != 'UNCALLED', 'mutation',
               variant_type = 'cna' AND cna_alteration = 2,                  'amplification',
               variant_type = 'cna' AND cna_alteration = -2,                 'deep_deletion',
               variant_type = 'structural_variant' AND mutation_status != 'UNCALLED',
                                                                             'structural_variant',
               '') AS event_type
    FROM (SELECT * FROM genomic_event_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE off_panel = 0
),
altered AS (
    -- event_type, not alteration_type: in ClickHouse a SELECT alias shadows
    -- the source column in WHERE, so `'any' AS alteration_type` below would
    -- turn a WHERE on alteration_type into a constant.
    SELECT cancer_study_identifier, hugo_gene_symbol, event_type AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples,
           COUNT(*) AS altered_events
    FROM events
    WHERE event_type != ''
    GROUP BY cancer_study_identifier, hugo_gene_symbol, event_type
    UNION ALL
    SELECT cancer_study_identifier, hugo_gene_symbol, 'any' AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples,
           COUNT(*) AS altered_events
    FROM events
    WHERE event_type != ''
    GROUP BY cancer_study_identifier, hugo_gene_symbol
),
typed_profile_rows AS (
    -- Same profile filters as the {mutation,cna,sv}_*_coverage views:
    -- CNA rows only from DISCRETE profiles.
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT')
       OR (alteration_type = 'COPY_NUMBER_ALTERATION'
           AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE'))
),
profile_rows AS (
    SELECT cancer_study_identifier, profile_type, sample_unique_id, gene_panel_id
    FROM typed_profile_rows
    UNION ALL
    SELECT cancer_study_identifier, 'ANY' AS profile_type, sample_unique_id, gene_panel_id
    FROM typed_profile_rows
),
sample_buckets AS (
    -- One row per sample per profile type: its panel-set signature.
    SELECT cancer_study_identifier, profile_type, sample_unique_id,
           arraySort(groupUniqArray(gene_panel_id)) AS panels
    FROM profile_rows
    GROUP BY cancer_study_identifier, profile_type, sample_unique_id
),
wes_profiled AS (
    SELECT cancer_study_identifier, profile_type, COUNT(*) AS n
    FROM sample_buckets
    WHERE has(panels, 'WES')
    GROUP BY cancer_study_identifier, profile_type
),
signature_counts AS (
    SELECT cancer_study_identifier, profile_type, panels, COUNT(*) AS n
    FROM sample_buckets
    WHERE NOT has(panels, 'WES')
    GROUP BY cancer_study_identifier, profile_type, panels
),
panel_genes AS (
    -- Same panel -> gene chain as mutation_panel_gene_coverage.
    SELECT gp.stable_id AS gene_panel_id, g.hugo_gene_symbol AS hugo_gene_symbol
    FROM gene_panel gp
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    JOIN gene g ON gpl.gene_id = g.entrez_gene_id
),
panel_profiled AS (
    SELECT cancer_study_identifier, profile_type, hugo_gene_symbol, SUM(n) AS n
    FROM (
        -- DISTINCT: a gene listed by two panels of one signature counts once.
        SELECT DISTINCT s.cancer_study_identifier, s.profile_type, s.panels, s.n, pg.hugo_gene_symbol
        FROM (
            SELECT cancer_study_identifier, profile_type, panels, n,
                   arrayJoin(panels) AS gene_panel_id
            FROM signature_counts
        ) s
        JOIN panel_genes pg ON s.gene_panel_id = pg.gene_panel_id
    )
    GROUP BY cancer_study_identifier, profile_type, hugo_gene_symbol
)
SELECT a.cancer_study_identifier,
       a.hugo_gene_symbol,
       a.alteration_type,
       a.altered_samples,
       COALESCE(w.n, 0) + COALESCE(p.n, 0) AS profiled_samples,
       a.altered_events,
       now() AS built_at
FROM altered a
JOIN alteration_to_profile m ON a.alteration_type = m.alteration_type
LEFT JOIN wes_profiled w
    ON w.cancer_study_identifier = a.cancer_study_identifier
   AND w.profile_type = m.profile_type
LEFT JOIN panel_profiled p
    ON p.cancer_study_identifier = a.cancer_study_identifier
   AND p.profile_type = m.profile_type
   AND p.hugo_gene_symbol = a.hugo_gene_symbol
154-build-study_gene_alteration_counts-brca_tcga_pan_can_atlas_2018
WITH
alteration_to_profile AS (
    SELECT t.1 AS alteration_type, t.2 AS profile_type
    FROM (
        SELECT arrayJoin([
            ('mutation',           'MUTATION_EXTENDED'),
            ('amplification',      'COPY_NUMBER_ALTERATION'),
            ('deep_deletion',      'COPY_NUMBER_ALTERATION'),
            ('structural_variant', 'STRUCTURAL_VARIANT'),
            ('any',                'ANY')
        ]) AS t
    )
),
events AS (
    SELECT cancer_study_identifier,
           hugo_gene_symbol,
           sample_unique_id,
           multiIf(
               variant_type = 'mutation' AND mutation_status != 'UNCALLED', 'mutation',
               variant_type = 'cna' AND cna_alteration = 2,                  'amplification',
               variant_type = 'cna' AND cna_alteration = -2,                 'deep_deletion',
               variant_type = 'structural_variant' AND mutation_status != 'UNCALLED',
                                                                             'structural_variant',
               '') AS event_type
    FROM (SELECT * FROM genomic_event_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE off_panel = 0
),
altered AS (
    -- event_type, not alteration_type: in ClickHouse a SELECT alias shadows
    -- the source column in WHERE, so `'any' AS alteration_type` below would
    -- turn a WHERE on alteration_type into a constant.
    SELECT cancer_study_identifier, hugo_gene_symbol, event_type AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples,
           COUNT(*) AS altered_events
    FROM events
    WHERE event_type != ''
    GROUP BY cancer_study_identifier, hugo_gene_symbol, event_type
    UNION ALL
    SELECT cancer_study_identifier, hugo_gene_symbol, 'any' AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples,
           COUNT(*) AS altered_events
    FROM events
    WHERE event_type != ''
    GROUP BY cancer_study_identifier, hugo_gene_symbol
),
typed_profile_rows AS (
    -- Same profile filters as the {mutation,cna,sv}_*_coverage views:
    -- CNA rows only from DISCRETE profiles.
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT')
       OR (alteration_type = 'COPY_NUMBER_ALTERATION'
           AND genetic_profile_id IN (SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE'))
),
profile_rows AS (
    SELECT cancer_study_identifier, profile_type, sample_unique_id, gene_panel_id
    FROM typed_profile_rows
    UNION ALL
    SELECT cancer_study_identifier, 'ANY' AS profile_type, sample_unique_id, gene_panel_id
    FROM typed_profile_rows
),
sample_buckets AS (
    -- One row per sample per profile type: its panel-set signature.
    SELECT cancer_study_identifier, profile_type, sample_unique_id,
           arraySort(groupUniqArray(gene_panel_id)) AS panels
    FROM profile_rows
    GROUP BY cancer_study_identifier, profile_type, sample_unique_id
),
wes_profiled AS (
    SELECT cancer_study_identifier, profile_type, COUNT(*) AS n
    FROM sample_buckets
    WHERE has(panels, 'WES')
    GROUP BY cancer_study_identifier, profile_type
),
signature_counts AS (
    SELECT cancer_study_identifier, profile_type, panels, COUNT(*) AS n
    FROM sample_buckets
    WHERE NOT has(panels, 'WES')
    GROUP BY cancer_study_identifier, profile_type, panels
),
panel_genes AS (
    -- Same panel -> gene chain as mutation_panel_gene_coverage.
    SELECT gp.stable_id AS gene_panel_id, g.hugo_gene_symbol AS hugo_gene_symbol
    FROM gene_panel gp
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    JOIN gene g ON gpl.gene_id = g.entrez_gene_id
),
panel_profiled AS (
    SELECT cancer_study_identifier, profile_type, hugo_gene_symbol, SUM(n) AS n
    FROM (
        -- DISTINCT: a gene listed by two panels of one signature counts once.
        SELECT DISTINCT s.cancer_study_identifier, s.profile_type, s.panels, s.n, pg.hugo_gene_symbol
        FROM (
            SELECT cancer_study_identifier, profile_type, panels, n,
                   arrayJoin(panels) AS gene_panel_id
            FROM signature_counts
        ) s
        JOIN panel_genes pg ON s.gene_panel_id = pg.gene_panel_id
    )
    GROUP BY cancer_study_identifier, profile_type, hugo_gene_symbol
)
SELECT a.cancer_study_identifier,
       a.hugo_gene_symbol,
       a.alteration_type,
       a.altered_samples,
       COALESCE(w.n, 0) + COALESCE(p.n, 0) AS profiled_samples,
       a.altered_events,
       now() AS built_at
FROM altered a
JOIN alteration_to_profile m ON a.alteration_type = m.alteration_type
LEFT JOIN wes_profiled w
    ON w.cancer_study_identifier = a.cancer_study_identifier
   AND w.profile_type = m.profile_type
LEFT JOIN panel_profiled p
    ON p.cancer_study_identifier = a.cancer_study_identifier
   AND p.profile_type = m.profile_type
   AND p.hugo_gene_symbol = a.hugo_gene_symbol
154-build-cancer_type_gene_alteration_counts-msk_chord_2024
WITH
alteration_to_profile AS (
    SELECT t.1 AS alteration_type, t.2 AS profile_type
    FROM (
        SELECT arrayJoin([
            ('mutation',           'MUTATION_EXTENDED'),
            ('amplification',      'COPY_NUMBER_ALTERATION'),
            ('deep_deletion',      'COPY_NUMBER_ALTERATION'),
            ('structural_variant', 'STRUCTURAL_VARIANT'),
            ('any',                'ANY')
        ]) AS t
    )
),
sample_cancer_type AS (
    SELECT q.preference_name AS preference_name,
           cd.cancer_study_identifier AS cancer_study_identifier,
           cd.sample_unique_id AS sample_unique_id,
           cd.attribute_value AS cancer_type
    FROM (SELECT * FROM clinical_data_derived WHERE cancer_study_identifier='msk_chord_2024') cd
    JOIN cancer_study_query_preferences q ON cd.cancer_study_identifier = q.cancer_study_identifier
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
events AS (
    SELECT cancer_study_identifier,
           hugo_gene_symbol,
           sample_unique_id,
           multiIf(
               variant_type = 'mutation' AND mutation_status != 'UNCALLED', 'mutation',
               variant_type = 'cna' AND cna_alteration = 2,                  'amplification',
               variant_type = 'cna' AND cna_alteration = -2,                 'deep_deletion',
               variant_type = 'structural_variant' AND mutation_status != 'UNCALLED',
                                                                             'structural_variant',
               '') AS event_type
    FROM (SELECT * FROM genomic_event_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE off_panel = 0
      AND cancer_study_identifier IN (SELECT cancer_study_identifier FROM cancer_study_query_preferences)
),
typed_events AS (
    SELECT sct.preference_name AS preference_name, sct.cancer_type AS cancer_type,
           e.hugo_gene_symbol AS hugo_gene_symbol, e.event_type AS event_type,
           e.sample_unique_id AS sample_unique_id
    FROM events e
    JOIN sample_cancer_type sct
        ON e.cancer_study_identifier = sct.cancer_study_identifier
       AND e.sample_unique_id = sct.sample_unique_id
    WHERE e.event_type != ''
),
altered AS (
    SELECT preference_name, cancer_type, hugo_gene_symbol, event_type AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples
    FROM typed_events
    GROUP BY preference_name, cancer_type, hugo_gene_symbol, event_type
    UNION ALL
    SELECT preference_name, cancer_type, hugo_gene_symbol, 'any' AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples
    FROM typed_events
    GROUP BY preference_name, cancer_type, hugo_gene_symbol
),
profile_rows AS (
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'COPY_NUMBER_ALTERATION', 'STRUCTURAL_VARIANT')
    UNION ALL
    SELECT cancer_study_identifier, 'ANY' AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'COPY_NUMBER_ALTERATION', 'STRUCTURAL_VARIANT')
),
sample_buckets AS (
    SELECT sct.preference_name AS preference_name, sct.cancer_type AS cancer_type,
           pr.profile_type AS profile_type, pr.sample_unique_id AS sample_unique_id,
           arraySort(groupUniqArray(pr.gene_panel_id)) AS panels
    FROM profile_rows pr
    JOIN sample_cancer_type sct
        ON pr.cancer_study_identifier = sct.cancer_study_identifier
       AND pr.sample_unique_id = sct.sample_unique_id
    GROUP BY preference_name, cancer_type, profile_type, sample_unique_id
),
wes_profiled AS (
    SELECT preference_name, cancer_type, profile_type, COUNT(*) AS n
    FROM sample_buckets
    WHERE has(panels, 'WES')
    GROUP BY preference_name, cancer_type, profile_type
),
signature_counts AS (
    SELECT preference_name, cancer_type, profile_type, panels, COUNT(*) AS n
    FROM sample_buckets
    WHERE NOT has(panels, 'WES')
    GROUP BY preference_name, cancer_type, profile_type, panels
),
panel_genes AS (
    SELECT gp.stable_id AS gene_panel_id, g.hugo_gene_symbol AS hugo_gene_symbol
    FROM gene_panel gp
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    JOIN gene g ON gpl.gene_id = g.entrez_gene_id
),
panel_profiled AS (
    SELECT preference_name, cancer_type, profile_type, hugo_gene_symbol, SUM(n) AS n
    FROM (
        SELECT DISTINCT s.preference_name, s.cancer_type, s.profile_type, s.panels, s.n,
                        pg.hugo_gene_symbol
        FROM (
            SELECT preference_name, cancer_type, profile_type, panels, n,
                   arrayJoin(panels) AS gene_panel_id
            FROM signature_counts
        ) s
        JOIN panel_genes pg ON s.gene_panel_id = pg.gene_panel_id
    )
    GROUP BY preference_name, cancer_type, profile_type, hugo_gene_symbol
)
SELECT a.preference_name,
       a.cancer_type,
       a.hugo_gene_symbol,
       a.alteration_type,
       a.altered_samples,
       COALESCE(w.n, 0) + COALESCE(p.n, 0) AS profiled_samples,
       now() AS built_at
FROM altered a
JOIN alteration_to_profile m ON a.alteration_type = m.alteration_type
LEFT JOIN wes_profiled w
    ON w.preference_name = a.preference_name
   AND w.cancer_type = a.cancer_type
   AND w.profile_type = m.profile_type
LEFT JOIN panel_profiled p
    ON p.preference_name = a.preference_name
   AND p.cancer_type = a.cancer_type
   AND p.profile_type = m.profile_type
   AND p.hugo_gene_symbol = a.hugo_gene_symbol
154-build-cancer_type_gene_alteration_counts-brca_tcga_pan_can_atlas_2018
WITH
alteration_to_profile AS (
    SELECT t.1 AS alteration_type, t.2 AS profile_type
    FROM (
        SELECT arrayJoin([
            ('mutation',           'MUTATION_EXTENDED'),
            ('amplification',      'COPY_NUMBER_ALTERATION'),
            ('deep_deletion',      'COPY_NUMBER_ALTERATION'),
            ('structural_variant', 'STRUCTURAL_VARIANT'),
            ('any',                'ANY')
        ]) AS t
    )
),
sample_cancer_type AS (
    SELECT q.preference_name AS preference_name,
           cd.cancer_study_identifier AS cancer_study_identifier,
           cd.sample_unique_id AS sample_unique_id,
           cd.attribute_value AS cancer_type
    FROM (SELECT * FROM clinical_data_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018') cd
    JOIN cancer_study_query_preferences q ON cd.cancer_study_identifier = q.cancer_study_identifier
    WHERE cd.attribute_name = 'CANCER_TYPE'
),
events AS (
    SELECT cancer_study_identifier,
           hugo_gene_symbol,
           sample_unique_id,
           multiIf(
               variant_type = 'mutation' AND mutation_status != 'UNCALLED', 'mutation',
               variant_type = 'cna' AND cna_alteration = 2,                  'amplification',
               variant_type = 'cna' AND cna_alteration = -2,                 'deep_deletion',
               variant_type = 'structural_variant' AND mutation_status != 'UNCALLED',
                                                                             'structural_variant',
               '') AS event_type
    FROM (SELECT * FROM genomic_event_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE off_panel = 0
      AND cancer_study_identifier IN (SELECT cancer_study_identifier FROM cancer_study_query_preferences)
),
typed_events AS (
    SELECT sct.preference_name AS preference_name, sct.cancer_type AS cancer_type,
           e.hugo_gene_symbol AS hugo_gene_symbol, e.event_type AS event_type,
           e.sample_unique_id AS sample_unique_id
    FROM events e
    JOIN sample_cancer_type sct
        ON e.cancer_study_identifier = sct.cancer_study_identifier
       AND e.sample_unique_id = sct.sample_unique_id
    WHERE e.event_type != ''
),
altered AS (
    SELECT preference_name, cancer_type, hugo_gene_symbol, event_type AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples
    FROM typed_events
    GROUP BY preference_name, cancer_type, hugo_gene_symbol, event_type
    UNION ALL
    SELECT preference_name, cancer_type, hugo_gene_symbol, 'any' AS alteration_type,
           COUNT(DISTINCT sample_unique_id) AS altered_samples
    FROM typed_events
    GROUP BY preference_name, cancer_type, hugo_gene_symbol
),
profile_rows AS (
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'COPY_NUMBER_ALTERATION', 'STRUCTURAL_VARIANT')
    UNION ALL
    SELECT cancer_study_identifier, 'ANY' AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'COPY_NUMBER_ALTERATION', 'STRUCTURAL_VARIANT')
),
sample_buckets AS (
    SELECT sct.preference_name AS preference_name, sct.cancer_type AS cancer_type,
           pr.profile_type AS profile_type, pr.sample_unique_id AS sample_unique_id,
           arraySort(groupUniqArray(pr.gene_panel_id)) AS panels
    FROM profile_rows pr
    JOIN sample_cancer_type sct
        ON pr.cancer_study_identifier = sct.cancer_study_identifier
       AND pr.sample_unique_id = sct.sample_unique_id
    GROUP BY preference_name, cancer_type, profile_type, sample_unique_id
),
wes_profiled AS (
    SELECT preference_name, cancer_type, profile_type, COUNT(*) AS n
    FROM sample_buckets
    WHERE has(panels, 'WES')
    GROUP BY preference_name, cancer_type, profile_type
),
signature_counts AS (
    SELECT preference_name, cancer_type, profile_type, panels, COUNT(*) AS n
    FROM sample_buckets
    WHERE NOT has(panels, 'WES')
    GROUP BY preference_name, cancer_type, profile_type, panels
),
panel_genes AS (
    SELECT gp.stable_id AS gene_panel_id, g.hugo_gene_symbol AS hugo_gene_symbol
    FROM gene_panel gp
    JOIN gene_panel_list gpl ON gp.internal_id = gpl.internal_id
    JOIN gene g ON gpl.gene_id = g.entrez_gene_id
),
panel_profiled AS (
    SELECT preference_name, cancer_type, profile_type, hugo_gene_symbol, SUM(n) AS n
    FROM (
        SELECT DISTINCT s.preference_name, s.cancer_type, s.profile_type, s.panels, s.n,
                        pg.hugo_gene_symbol
        FROM (
            SELECT preference_name, cancer_type, profile_type, panels, n,
                   arrayJoin(panels) AS gene_panel_id
            FROM signature_counts
        ) s
        JOIN panel_genes pg ON s.gene_panel_id = pg.gene_panel_id
    )
    GROUP BY preference_name, cancer_type, profile_type, hugo_gene_symbol
)
SELECT a.preference_name,
       a.cancer_type,
       a.hugo_gene_symbol,
       a.alteration_type,
       a.altered_samples,
       COALESCE(w.n, 0) + COALESCE(p.n, 0) AS profiled_samples,
       now() AS built_at
FROM altered a
JOIN alteration_to_profile m ON a.alteration_type = m.alteration_type
LEFT JOIN wes_profiled w
    ON w.preference_name = a.preference_name
   AND w.cancer_type = a.cancer_type
   AND w.profile_type = m.profile_type
LEFT JOIN panel_profiled p
    ON p.preference_name = a.preference_name
   AND p.cancer_type = a.cancer_type
   AND p.profile_type = m.profile_type
   AND p.hugo_gene_symbol = a.hugo_gene_symbol
154-build-study_profiled_counts-msk_chord_2024
WITH
sample_patient AS (
    SELECT sample_unique_id, patient_unique_id
    FROM (SELECT * FROM sample_derived WHERE cancer_study_identifier='msk_chord_2024')
),
discrete_cna_profiles AS (
    SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE'
),
profiled AS (
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    UNION ALL
    SELECT cancer_study_identifier, 'COPY_NUMBER_ALTERATION_DISCRETE' AS profile_type,
           sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE alteration_type = 'COPY_NUMBER_ALTERATION'
      AND genetic_profile_id IN (SELECT stable_id FROM discrete_cna_profiles)
    UNION ALL
    SELECT cancer_study_identifier, 'ANY_MUT_CNA_SV' AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='msk_chord_2024')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT')
       OR (alteration_type = 'COPY_NUMBER_ALTERATION'
           AND genetic_profile_id IN (SELECT stable_id FROM discrete_cna_profiles))
)
SELECT cancer_study_identifier,
       'ALL_SAMPLES' AS profile_type,
       COUNT(DISTINCT sample_unique_id) AS samples,
       COUNT(DISTINCT patient_unique_id) AS patients,
       toUInt64(0) AS wes_samples,
       now() AS built_at
FROM (SELECT * FROM sample_derived WHERE cancer_study_identifier='msk_chord_2024')
GROUP BY cancer_study_identifier
UNION ALL
SELECT p.cancer_study_identifier,
       p.profile_type,
       COUNT(DISTINCT p.sample_unique_id) AS samples,
       COUNT(DISTINCT nullIf(sp.patient_unique_id, '')) AS patients,
       COUNT(DISTINCT if(p.gene_panel_id = 'WES', p.sample_unique_id, NULL)) AS wes_samples,
       now() AS built_at
FROM profiled p
LEFT JOIN sample_patient sp ON p.sample_unique_id = sp.sample_unique_id
GROUP BY p.cancer_study_identifier, p.profile_type
154-build-study_profiled_counts-brca_tcga_pan_can_atlas_2018
WITH
sample_patient AS (
    SELECT sample_unique_id, patient_unique_id
    FROM (SELECT * FROM sample_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
),
discrete_cna_profiles AS (
    SELECT stable_id FROM genetic_profile WHERE datatype = 'DISCRETE'
),
profiled AS (
    SELECT cancer_study_identifier, alteration_type AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    UNION ALL
    SELECT cancer_study_identifier, 'COPY_NUMBER_ALTERATION_DISCRETE' AS profile_type,
           sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE alteration_type = 'COPY_NUMBER_ALTERATION'
      AND genetic_profile_id IN (SELECT stable_id FROM discrete_cna_profiles)
    UNION ALL
    SELECT cancer_study_identifier, 'ANY_MUT_CNA_SV' AS profile_type, sample_unique_id, gene_panel_id
    FROM (SELECT * FROM sample_to_gene_panel_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
    WHERE alteration_type IN ('MUTATION_EXTENDED', 'STRUCTURAL_VARIANT')
       OR (alteration_type = 'COPY_NUMBER_ALTERATION'
           AND genetic_profile_id IN (SELECT stable_id FROM discrete_cna_profiles))
)
SELECT cancer_study_identifier,
       'ALL_SAMPLES' AS profile_type,
       COUNT(DISTINCT sample_unique_id) AS samples,
       COUNT(DISTINCT patient_unique_id) AS patients,
       toUInt64(0) AS wes_samples,
       now() AS built_at
FROM (SELECT * FROM sample_derived WHERE cancer_study_identifier='brca_tcga_pan_can_atlas_2018')
GROUP BY cancer_study_identifier
UNION ALL
SELECT p.cancer_study_identifier,
       p.profile_type,
       COUNT(DISTINCT p.sample_unique_id) AS samples,
       COUNT(DISTINCT nullIf(sp.patient_unique_id, '')) AS patients,
       COUNT(DISTINCT if(p.gene_panel_id = 'WES', p.sample_unique_id, NULL)) AS wes_samples,
       now() AS built_at
FROM profiled p
LEFT JOIN sample_patient sp ON p.sample_unique_id = sp.sample_unique_id
GROUP BY p.cancer_study_identifier, p.profile_type

#160 — gene-frequency views through an IN subquery

cBioPortal/cbioportal-mcp#160 · open, not deployed · measured at PR head 93f255d

“Before” is the currently deployed parameterized view gene_alteration_frequency_by_cancer_type(preference='all_studies_non_redundant', …). “After” is the PR's SELECT body inlined with the same parameters substituted. CNA is covered by the two supported values, amplification and deep_deletion; there is no generic cna parameter.

Gene · alterationBefore msAfter msSaving msRows read (both)Same result
TP53 · mutation991.79394.83596.9630,599,882yes
TP53 · amplification875.87489.16386.7043,829,849yes
TP53 · deep deletion753.27468.67284.6043,829,849yes
TP53 · structural variant485.29211.08274.2114,117,465yes
ERBB2 · mutation744.86361.58383.2830,599,882yes
ERBB2 · amplification765.85496.49269.3643,829,849yes
ERBB2 · deep deletion732.77487.97244.8043,829,849yes
ERBB2 · structural variant572.37270.05302.3214,117,465yes

Saving 0.245–0.597 s per call, median 0.293 s. Rows and bytes read are unchanged: the rewrite cuts join/filter work, not the data read. The per-answer gain is this saving times the number of such calls in an answer, which needs the historical SQL to establish.

#154 — domain tools on precomputed aggregates

cBioPortal/cbioportal-mcp#154 · open, not deployed · measured at PR head 0fb4393

The PR's tools read small precomputed tables and fall back to live SQL when those are missing. None of the three tables exists on the public database, so this measures (1) the exact fallback SQL from the PR's query builders, and (2) the table-build SELECTs, each restricted to one study.

One fallback call replaced by one lookup

Fallback queryFallback sEst. saving s
154-frequency-msk_chord_20240.1870.183
154-top-mutation-msk_chord_20240.2820.278
154-profiled-msk_chord_20240.1380.134
154-frequency-brca_tcga_pan_can_atlas_20180.0820.078
154-top-mutation-brca_tcga_pan_can_atlas_20180.3100.306
154-profiled-brca_tcga_pan_can_atlas_20180.0780.074
154-cancer-type-TP531.3481.344
154-cancer-type-TP53-pan-cancer-tcga0.8530.849

Estimated saving = fallback time − 4.00 ms, the upper end of the 3–4 ms lookup estimate. Database side only. In the 09-23 questions, a text screen finds 54 gene-frequency answers (median 5 SQL calls), 26 top-genes answers (median 4) and 6 profiled-count answers (median 2) that such tools could serve (template-turns.json; categories overlap). If a tool replaces N such calls with one lookup it saves roughly N × fallback − lookup, but that's an assumption, not a measured benchmark saving, and SQL calls are not the same as sequential model turns.

Table-build SELECTs (computation only)

Build SELECTServer msRows read
154-build-study_gene_alteration_counts-msk_chord_2024558.5513,498,040
154-build-study_gene_alteration_counts-brca_tcga_pan_can_atlas_2018547.7814,038,712
154-build-cancer_type_gene_alteration_counts-msk_chord_2024997.1313,718,830
154-build-cancer_type_gene_alteration_counts-brca_tcga_pan_can_atlas_20181,036.7014,161,198
154-build-study_profiled_counts-msk_chord_202469.332,916,228
154-build-study_profiled_counts-brca_tcga_pan_can_atlas_201835.992,850,692

Three build SELECTs back the four tools: study_gene_alteration_counts (frequency, top genes), cancer_type_gene_alteration_counts (by cancer type) and study_profiled_counts (profiled counts). Each source table was replaced by a study-filtered subquery, with all other joins, filters and grouping kept. Timing covers the SELECT only, not inserting into MergeTree; per-study costs can't safely be multiplied into a full-build estimate.

#151 — schema and guide cache

cBioPortal/cbioportal-mcp#151 · open, not deployed

#151 caches SHOW TABLES, DESCRIBE TABLE and the dynamic study guides, one hour by default. The SHOW/DESCRIBE timings are the database work a cache hit skips.

Cacheable callServer ms saved per hitLaptop round trip ms (not in-cluster)
metadata-show-tables2.58568.46
metadata-describe-cancer_study1.72566.20
metadata-describe-genomic_event_derived1.38548.27
metadata-describe-sample_to_gene_panel_derived1.37539.48
metadata-describe-clinical_data_derived1.38541.05
metadata-describe-sample_derived1.55590.97
metadata-describe-gene1.37543.38
metadata-describe-genetic_profile1.40555.84

The 09-23 run made 101 list-tables and 327 describe-columns tool calls; the cache hit rate is unknown. SELECT version(), timezone() and system.settings were measured too (main table) but #151 doesn't cache them. Study-guide savings weren't measured. A cache hit still costs the model round trip that asked for the tool.

Limitations

Data and links