Skip to content

Meta attribute macro for Crosstab pivots#133

Merged
jbrestel merged 8 commits intomasterfrom
meta-attribute-macro
Jan 28, 2026
Merged

Meta attribute macro for Crosstab pivots#133
jbrestel merged 8 commits intomasterfrom
meta-attribute-macro

Conversation

@jbrestel
Copy link
Member

Add macro whichis populated by running of an attribute meta query. This allows us to specify columns returned by a "pivot" (crosstab function in postgres).

NOTE: The crosstab function is postgres specific. No attempt was made to name things generically.

Related to :
VEuPathDB/EbrcModelCommon#78
VEuPathDB/ApiCommonModel#139

jbrestel and others added 5 commits January 23, 2026 22:52
When a sqlQuery has an attributeMetaQueryRef, automatically populate
a macro with the ordered column definitions (name + type) from the
meta query results. This enables use of crosstab pivots where the
AS clause needs to reference the dynamically defined columns.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move META_ATTRIBUTE_COLUMNS_FOR_CROSSTAB macro population to occur
before super.resolveReferences() call. The macro validation happens
during super.resolveReferences(), so the macro must be populated in
_sqlMacroMap before that point to avoid "macro not defined" errors.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
….jbrestel/project_home/WDK into meta-attribute-macro
Add getPostgresType() method to ColumnType to map WDK types to
PostgreSQL type names (e.g., "string" -> "text", "number" -> "integer").
Update SqlQuery to use getPostgresType() when generating the crosstab
macro so the AS clause uses valid PostgreSQL types.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jbrestel
Copy link
Member Author

Tested on my ToxoDB dev site for phenotype and RNASeq (see related PRs)

@jbrestel
Copy link
Member Author

jbrestel commented Jan 27, 2026

Hmm.. Actually something may not be write with the colTypes. The query being run looks like this:

SELECT ta.source_id, ta.project_id, ta.gene_source_id, p.*
FROM apidbtuning.TranscriptAttributes ta
LEFT OUTER JOIN (
SELECT * FROM public.crosstab(
'SELECT genes.string_value as gene_id, av.attribute_stable_id, av.number_value
FROM eda.attributevalue_sdfae3769c3_gnPhntyD av,
(SELECT gnPhntyD_stable_id, string_value
FROM eda.attributevalue_sdfae3769c3_gnPhntyD
WHERE attribute_stable_id = ''VEUPATHDB_GENE_ID'') genes
WHERE av.gnPhntyD_stable_id = genes.gnPhntyD_stable_id
AND av.attribute_stable_id != ''VEUPATHDB_GENE_ID''
ORDER BY 1, 2',
'SELECT stable_id
FROM eda.attributegraph_sdfae3769c3_gnPhntyD
WHERE has_values = 1
AND stable_id != ''VEUPATHDB_GENE_ID''
AND data_type = ''number''
AND (hidden IS NULL OR NOT (hidden::jsonb @> ''["variableTree"]''::jsonb))
ORDER BY stable_id'
) AS ct(gene_id text, VAR_0699dcd5 text, VAR_32e9589b text, VAR_41fe31ad text, VAR_45083826 text, VAR_4b1f735e text, VAR_69d23b9f text, VAR_8e5c07ee text, VAR_90b02f16 text, VAR_92eac45c text, VAR_c067524e text, VAR_c10f5a2e text, VAR_c33fe1b0 text, VAR_c494aa69 text, VAR_ccdd8654 text, VAR_d016448c text, VAR_d23519d9 text, VAR_e06c02ce text, VAR_e2db12e5 text, VAR_f3418d2f text)
) p ON ta.gene_source_id = p.gene_id

The column being returned "av.number_value" is numeric but the WDK is casting them all to "text" when populating the macro.

NOTE: Somehow it seems to be getting this right on the front end because i can see histograms and the sorting by numeric seems to be working. Not sure what is going on there.

@jbrestel
Copy link
Member Author

Ok. the text vs. numeric issue has been resolved. It is behaving correctly now

jbrestel and others added 2 commits January 27, 2026 11:16
When attributeMetaQueryRef returns zero rows, still define the macro
with a placeholder value (_no_columns_defined text) to prevent
"macro not defined" validation errors. This ensures the model loads
even when a meta query has no results.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jbrestel jbrestel merged commit b93f65d into master Jan 28, 2026
1 check passed
@jbrestel jbrestel deleted the meta-attribute-macro branch January 28, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants