[v2 Bug] Static analysis: Redshift `HLL_CREATE_SKETCH` not recognized as an aggregate → false `UnaggregatedColumn (dbt0213)`
Is this a new bug in dbt v2.x compared to the latest version of dbt 1.x?
- I believe this is a new bug in dbt v2.x
- I have searched the existing issues and could not find a duplicate
Current Behavior
Under --static-analysis strict, a model that uses Amazon Redshift's HyperLogLog aggregate
HLL_CREATE_SKETCH(<col>) in a GROUP BY query fails with:
[error] [UnaggregatedColumn (dbt0213)]: Un-aggregated columns in aggregation context:
s.visitor_id must be aggregated or appear in a GROUP BY clauseHLL_CREATE_SKETCH is an aggregate function in Redshift (it returns one HLLSKETCH per group and
ignores NULLs — AWS docs below), so its argument does not need to appear in GROUP BY. Fusion's
static analyzer does not recognize HLL_CREATE_SKETCH as an aggregate and therefore treats the
argument column as un-aggregated. The SQL compiles and runs correctly in dbt Core and directly in
Redshift.
This is the aggregate-recognition analogue of #15299 (there the analyzer mishandles types in a GROUP BY context; here it mishandles which functions aggregate).
Expected Behavior
HLL_CREATE_SKETCH(col) (and the related Redshift HLL aggregates) should be recognized as aggregate
functions, so their arguments are not required in GROUP BY and no dbt0213 is raised. No exemption
should be necessary.
Steps To Reproduce
Reproduces only when Fusion can probe a real Redshift table schema (source or materialized ref).
An upstream table
sessionswith columnsvisit_date,visitor_id:-- models/sessions.sql (materialized as table so its schema exists in the warehouse) select '2024-05-01'::date as visit_date, 1 as visitor_idA downstream model that builds an HLL sketch per day:
-- models/sessions_daily.sql select visit_date, hll_create_sketch(visitor_id) as unique_visitors_sketch from {{ ref('sessions') }} group by visit_dateCompile with strict static analysis:
dbt compile --static-analysis strict --select sessions_dailyObserve
[error] [UnaggregatedColumn (dbt0213)]: ... visitor_id must be aggregated or appear in a GROUP BY clause, pointing at thehll_create_sketch(visitor_id)argument.
Controls (all verified):
- Replacing
hll_create_sketch(visitor_id)with a recognized aggregate, e.g.count(distinct visitor_id), compiles clean — confirming the issue is aggregate recognition, not the GROUP BY itself. - The model runs successfully in Redshift and under dbt Core (which does not statically analyze).
- Wrapping the argument in a recognized aggregate (
hll_create_sketch(max(visitor_id))) clears the error but changes the result, so it is not a valid workaround.
Relevant log output
Reproduced on dbt-fusion 2.0.5 (Redshift). Minimal repro:
select
account_id,
hll_create_sketch(contract_id) as unique_visitors_sketch
from {{ ref('sessions') }}
group by account_id
dbt compile --static-analysis strict output:
dbt 2.0.5
Loading profiles.yml
Failed model dev_dataengineering.sessions_daily (view) [1 of 1 in 1.99s]
=================== Errors and Warnings ====================
[error] [UnaggregatedColumn (dbt0213)]: Un-aggregated columns in aggregation context: edwh.ods_oitc_finance.finance_oitc_intermediate_invoice_items.contract_id must be aggregated or appear in a GROUP BY clause
--> models/sessions_daily.sql:4:5 (target/compiled/spike/models/sessions_daily.sql:4:5)
==================== Execution Summary =====================
Finished 'compile' with 1 error for target 'prod' [2.6s]
Processed: 1 model
Summary: 1 total | 1 error
Control — same query with a recognized aggregate (count(distinct contract_id)) compiles clean, confirming the cause is aggregate recognition, not the GROUP BY:
==================== Execution Summary =====================
Finished 'compile' successfully for target 'prod' [2.0s]
Summary: 1 total | 1 successEnvironment
- dbt: dbt-fusion 2.0.5
- Adapter: Redshift
- Affected models in our project: 2 (both use `hll_create_sketch(visitor_id)` for unique-visitor sketches)Which database adapter are you using?
redshift
Is this a discrepancy vs. dbt 1.x?
- Yes — this works in dbt 1.x but not in dbt v2.x
Additional Context
No response
Source: dbt-labs/dbt-core