Skip to content

Conversation

@mccanne
Copy link
Collaborator

@mccanne mccanne commented Jan 26, 2026

No description provided.

Copy link
Member

@nwt nwt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is it safe to set scope.lateral to true on these paths (rather than restoring whatever value it had before it was set to false)?

@mccanne
Copy link
Collaborator Author

mccanne commented Jan 26, 2026

Out of curiosity, why is it safe to set scope.lateral to true on these paths (rather than restoring whatever value it had before it was set to false)?

I think they should all work this way in sqlSelect. We are adjusting knobs in the scope that was created by sqlSelect. It's the expr recursion that should save and restore settings.

@mccanne mccanne merged commit ab7c028 into main Jan 26, 2026
2 checks passed
@mccanne mccanne deleted the having-scope branch January 26, 2026 22:35
@philrz
Copy link
Contributor

philrz commented Jan 26, 2026

I've confirmed that this addresses a problem with these 6 sqllogictest queries:

In a previous pass, I'd ensured the pragma pg from #6495 was present in the sqllogictest queries in which a column alias from the SELECT was referenced in the GROUP BY. However, these six queries didn't fit that criteria so were not being executed with pragma pg, but I noticed they were failing with these aggregate function ... called in non-aggregate context errors unless I added the pragma pg.

$ super -version
Version: 1ffd06a60

$ super -c "SELECT - + col2 * - AVG ( - col2 ) AS col0 FROM tab0 cor0 GROUP BY col2, col1, col2 HAVING ( - col2 + AVG ( + col0 ) ) IS NULL;"
aggregate function "avg" called in non-aggregate context at line 1, column 21:
SELECT - + col2 * - AVG ( - col2 ) AS col0 FROM tab0 cor0 GROUP BY col2, col1, col2 HAVING ( - col2 + AVG ( + col0 ) ) IS NULL;
                    ~~~~~~~~~~~~~~
aggregate function "avg" called in non-aggregate context at line 1, column 21:
SELECT - + col2 * - AVG ( - col2 ) AS col0 FROM tab0 cor0 GROUP BY col2, col1, col2 HAVING ( - col2 + AVG ( + col0 ) ) IS NULL;
                    ~~~~~~~~~~~~~~

$ super -c "SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;"
aggregate function "count" called in non-aggregate context at line 1, column 19:
SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;
                  ~~~~~~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 19:
SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;
                  ~~~~~~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 19:
SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;
                  ~~~~~~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 19:
SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;
                  ~~~~~~~~~~~~~~~~

$ super -c "SELECT DISTINCT - col1 * - - MAX ( DISTINCT + - col1 ) + + + 62 AS col0 FROM tab2 GROUP BY col1 HAVING NOT - AVG ( ALL - col0 ) IS NOT NULL;"
aggregate function "max" called in non-aggregate context at line 1, column 30:
SELECT DISTINCT - col1 * - - MAX ( DISTINCT + - col1 ) + + + 62 AS col0 FROM tab2 GROUP BY col1 HAVING NOT - AVG ( ALL - col0 ) IS NOT NULL;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~
aggregate function "max" called in non-aggregate context at line 1, column 30:
SELECT DISTINCT - col1 * - - MAX ( DISTINCT + - col1 ) + + + 62 AS col0 FROM tab2 GROUP BY col1 HAVING NOT - AVG ( ALL - col0 ) IS NOT NULL;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~

$ super -c "SELECT + COUNT ( DISTINCT + + col0 ) AS col1, col2 FROM tab2 AS cor0 GROUP BY col0, col2 HAVING NOT + COUNT ( - col1 ) + + + SUM ( DISTINCT - 33 ) + - COUNT ( * ) * - + 56 * ( col0 ) IS NOT NULL;"
aggregate function "count" called in non-aggregate context at line 1, column 10:
SELECT + COUNT ( DISTINCT + + col0 ) AS col1, col2 FROM tab2 AS cor0 GROUP BY col0, col2 HAVING NOT + COUNT ( - col1 ) + + + SUM ( DISTINCT - 33 ) + - COUNT ( * ) * - + 56 * ( col0 ) IS NOT NULL;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 10:
SELECT + COUNT ( DISTINCT + + col0 ) AS col1, col2 FROM tab2 AS cor0 GROUP BY col0, col2 HAVING NOT + COUNT ( - col1 ) + + + SUM ( DISTINCT - 33 ) + - COUNT ( * ) * - + 56 * ( col0 ) IS NOT NULL;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~

$ super -c "SELECT + COUNT ( * ) AS col1, - 0 * + - 69 * + ( + 53 ) AS col1 FROM tab0 WHERE NOT NULL IS NOT NULL GROUP BY col2 HAVING ( COUNT ( col1 ) ) IS NULL;"
aggregate function "count" called in non-aggregate context at line 1, column 10:
SELECT + COUNT ( * ) AS col1, - 0 * + - 69 * + ( + 53 ) AS col1 FROM tab0 WHERE NOT NULL IS NOT NULL GROUP BY col2 HAVING ( COUNT ( col1 ) ) IS NULL;
         ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 10:
SELECT + COUNT ( * ) AS col1, - 0 * + - 69 * + ( + 53 ) AS col1 FROM tab0 WHERE NOT NULL IS NOT NULL GROUP BY col2 HAVING ( COUNT ( col1 ) ) IS NULL;
         ~~~~~~~~~~~

$ super -c "SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;"
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~
aggregate function "count" called in non-aggregate context at line 1, column 25:
SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;
                        ~~~~~~~~~~~

Now that the changes from this PR have merged to main, these all run ok and produce the same output as Postgres, still without the need for the pragma pg.

$ super -version
Version: ab7c02895

$ super -c "SELECT - + col2 * - AVG ( - col2 ) AS col0 FROM tab0 cor0 GROUP BY col2, col1, col2 HAVING ( - col2 + AVG ( + col0 ) ) IS NULL;"
[no output]

$ super -c "SELECT DISTINCT - COUNT ( - col0 ) AS col2 FROM tab1 GROUP BY col0, col0 HAVING NOT NULL BETWEEN ( - + 94 + 83 + + - col0 - col0 * - 38 / ( 35 * - AVG ( col2 * col2 ) ) ) AND NULL;"
[no output]

$ super -c "SELECT DISTINCT - col1 * - - MAX ( DISTINCT + - col1 ) + + + 62 AS col0 FROM tab2 GROUP BY col1 HAVING NOT - AVG ( ALL - col0 ) IS NOT NULL;"
[no output]

$ super -c "SELECT + COUNT ( DISTINCT + + col0 ) AS col1, col2 FROM tab2 AS cor0 GROUP BY col0, col2 HAVING NOT + COUNT ( - col1 ) + + + SUM ( DISTINCT - 33 ) + - COUNT ( * ) * - + 56 * ( col0 ) IS NOT NULL;"
[no output]

$ super -c "SELECT + COUNT ( * ) AS col1, - 0 * + - 69 * + ( + 53 ) AS col1 FROM tab0 WHERE NOT NULL IS NOT NULL GROUP BY col2 HAVING ( COUNT ( col1 ) ) IS NULL;"
[no output]

$ super -c "SELECT + col1, col1 + - COUNT ( * ) / - + 35 AS col2 FROM tab0 GROUP BY col1, col0 HAVING NOT - col0 - + + col0 BETWEEN + NULLIF ( + COUNT ( ALL - col1 ), - col0 * - + 61 / ( + + col0 ) * MAX ( DISTINCT + ( - NULLIF ( - col2, - col2 - - col2 ) ) ) + + col0 ) AND + col0 * - + col1;"
{"+col1":81,col2:81}
{"+col1":0,col2:0}
{"+col1":0,col2:0}

Thanks @mccanne!

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.

4 participants