Skip to content

An aggregate inside CAST throws g is not defined: SELECT CAST(COUNT(*) AS INTEGER) #2764

Closed

Description

@mathiasrw

An aggregate inside CAST or CONVERT throws g is not defined. It is the most common run-time error in the sqllogictest run (44 times in the first 76 files of test/random/), reproduced on develop (06211a0), compared with SQLite 3.43.2:

Query AlaSQL SQLite
SELECT CAST(COUNT(*) AS INTEGER) AS n g is not defined 1
SELECT CAST(COUNT(*) AS INTEGER) AS n FROM t g is not defined 2
SELECT CAST(SUM(x) AS FLOAT) AS n FROM t g is not defined 3
SELECT CAST(COUNT(*) AS STRING) AS n FROM t g is not defined '2'
SELECT CONVERT(INT, COUNT(*)) AS n FROM t g is not defined
SELECT ABS(COUNT(*)) AS n FROM t 2 2
SELECT COUNT(*) + 0 AS n 1 1

(t holds two rows, x = 1 and 2.) An aggregate inside other functions and operators works; only CAST and CONVERT around it fail. g is the name of the group row in the generated code, so the generated CAST code seems to be compiled where no group row exists. #2594 counts this error among the run-time errors.

Not breaking: these queries throw today.

Activity

  1. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    MemberAuthor

    The same family gives a wrong value instead of the error when an aggregate also sits outside the CAST (sqllogictest, about 900 failures; develop 06211a0 and npm 4.20.1):

    Query AlaSQL SQLite
    SELECT COUNT(*) * CAST(COUNT(*) AS INTEGER) 0 1
    SELECT COUNT(*) * COALESCE(CAST(COUNT(*) AS INTEGER), 5) 0 1
    SELECT 2 * COALESCE(CAST(COUNT(*) AS INTEGER), 5) g is not defined 2

    Still not breaking: the value is wrong today.

  2. added this to the v4.x Focus milestone on Oct 21, 2026
  3. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  4. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #3002

  5. added a commit that references this issue on Nov 16, 2026
    b62077b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions