Skip to content

Aggregate inside expression (e.g. coalesce(count(...), 0)) throws QueryCompilationError: Unknown expression type: agg #720

@evanheckert

Description

@evanheckert

Summary

Wrapping an aggregate in another expression (e.g., coalesce(count(item.property), 0)) throws during compilation. Using the plain aggregate (count(item.property)) succeeds. Oddly, the wrapped form does work inside a live query, so behavior is inconsistent.

Repro

Building the subquery outside of the hook threw the error:

const vehicleDispatchCountSubquery = new Query()
  .from({ vehicleDispatch: vehicleDispatchCollection })
  .groupBy(({ vehicleDispatch }) => vehicleDispatch.dispatchId)
  .select(({ vehicleDispatch }) => ({
    dispatchId: vehicleDispatch.dispatchId,
    vehicleDispatchCount: coalesce(count(vehicleDispatch.id), 0),
  }))

However, just using count(vehicleDispatch.id) without coalesce no longer throws the error.

Alternatively, within a useLiveQuery() nesting works without error:

export const useDispatchSidebarData = (date: string) =>
  useLiveQuery(
    q => {
      const vdCountSubquery = q
        .from({ vehicleDispatch: vehicleDispatchCollection })
        .groupBy(({ vehicleDispatch }) => vehicleDispatch.dispatchId)
        .select(({ vehicleDispatch }) => ({
          dispatchId: vehicleDispatch.dispatchId,
          vehicleDispatchCount: coalesce(count(vehicleDispatch.id)),
        }))
        .distinct()
      return q
        .from({ dispatch: dispatchCollection })
        .where(({ dispatch }) => eq(dispatch.date, date))
        .leftJoin({ vdCount: vdCountSubquery }, ({ dispatch, vdCount }) => eq(dispatch.id, vdCount.dispatchId))
        .select(({ dispatch, vdCount }) => ({
          ...dispatch,
          vehicleDispatchCount: vdCount?.vehicleDispatchCount,
        }))
    },
    [date]
  )

Error

QueryCompilationError: Unknown expression type: agg
    at compileExpressionInternal (…/chunk-TT3G3ME3.js:703:13)
    at …/chunk-TT3G3ME3.js:751:14
    at Array.map (<anonymous>)
    at compileFunction (…/chunk-TT3G3ME3.js:750:34)

Environment

Running in docker -> node:20-alpine -> node v20.19.5

{
    "@electric-sql/client": "^1.1.0",
    "@electric-sql/react": "^1.0.15",
    ...
    "@tanstack/electric-db-collection": "^0.1.38",
    "@tanstack/query-core": "^5.90.5",
    "@tanstack/query-db-collection": "^0.2.36",
    "@tanstack/react-db": "^0.1.36",
    ...
    "zod": "^4.1.12"
    "react": "^19.2.0",
    "react-dom": "^19.2.0",
    "vite": "^7.1.12",
    ...
    "babel-plugin-react-compiler": "^1.0.0",
}

Possible Cause

@samwillis in discord link

We may not yet support an aggregate expression wrapped by other expressions. Do open an issue, we should fix that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions