Skip to content

Instantly share code, notes, and snippets.

@amotl
Created March 31, 2026 07:33
Show Gist options
  • Select an option

  • Save amotl/00fe77eae558b204636d796197c5e384 to your computer and use it in GitHub Desktop.

Select an option

Save amotl/00fe77eae558b204636d796197c5e384 to your computer and use it in GitHub Desktop.
Recipe to reproduce `SQLParseException['floor((timestamp_ms / $1))' must appear in the GROUP BY clause or be used in an aggregation function.`
import sqlalchemy as sa
def workload():
"""
Install
uv pip install sqlalchemy-cratedb psycopg2-binary
Invoke
python cratedb_groupby_function.py
Problem
sqlalchemy.exc.ProgrammingError: (crate.client.exceptions.ProgrammingError) SQLParseException['floor((timestamp_ms / $1))' must appear in the GROUP BY clause or be used in an aggregation function. Perhaps you grouped by an alias that clashes with a column in the relations]
[SQL:
SELECT floor(trace_mock.timestamp_ms / CAST(? AS INTEGER)) AS time_bucket
FROM trace_mock
GROUP BY floor(trace_mock.timestamp_ms / CAST(? AS INTEGER))
ORDER BY floor(trace_mock.timestamp_ms / CAST(? AS INTEGER));
]
[parameters: ('42', '42', '42')]
"""
# Works with PostgreSQL.
# docker run --rm --publish=5433:5432 --env "POSTGRES_HOST_AUTH_METHOD=trust" postgres:18 postgres -c log_statement=all
# engine = sa.create_engine("postgresql://postgres@localhost:5433")
# Fails with CrateDB.
# docker run --rm --publish=4200:4200 --publish=5432:5432 crate/crate:nightly -Cdiscovery.type=single-node
engine = sa.create_engine("crate://")
with engine.connect() as conn:
conn.execute(
sa.text("""
CREATE TABLE IF NOT EXISTS "trace_mock" (
timestamp_ms BIGINT NOT NULL
);
""")
)
conn.execute(
sa.text("""
SELECT floor(trace_mock.timestamp_ms / CAST(:foo AS INTEGER)) AS time_bucket
FROM trace_mock
GROUP BY floor(trace_mock.timestamp_ms / CAST(:foo AS INTEGER))
ORDER BY floor(trace_mock.timestamp_ms / CAST(:foo AS INTEGER));
"""),
{"foo": "42"},
)
if __name__ == "__main__":
workload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment