Closed
Description
Environment details
- OS type and version: Windows 10 Pro, macOS Ventura 13.4
- Python version: 3.8.10
- pip version: 23.1.2
sqlalchemy-bigquery
version: 1.6.1
Steps to reproduce
- Compose an SQL statement using SQLAlchemy Core or ORM which includes a GROUP BY clause with an expression
timestamp_trunc(`some_column`, hour)
- Render it (or execute it and confirm how it was rendered by examining SQLAlchemy's log)
Code example
Below is an example script to reproduce the symptom. Please refer to the first comment block in the file for detail.
"""An example script to reproduce the bug.
Executing this script outputs (partially folded for ease of reading):
sqlalchemy: 1.4.49
sqlalchemy-bigquery: 1.6.1
SQLite----------------------------------------
SELECT timestamp_trunc(sensor.time, hour) AS time,
max(sensor.value) AS max_1
FROM sensor
GROUP BY timestamp_trunc(sensor.time, hour)
BigQuery----------------------------------------
SELECT timestamp_trunc(`sensor`.`time`, hour) AS `time`,
max(`sensor`.`value`) AS `max_1`
FROM `sensor`
GROUP BY `timestamp_trunc_1`
In the final line, there is a reference to a non-existing colunn
of which name is `timestamp_trunc_1`. The line should be rendered as:
GROUP BY timestamp_trunc(`sensor`.`time`, hour)
...like the rendering result for SQLite, written above.
"""
import sqlalchemy as sa
import sqlalchemy.dialects.sqlite as sqlite
import sqlalchemy_bigquery as bigquery
print("sqlalchemy: ", sa.__version__)
print("sqlalchemy-bigquery:", bigquery.__version__)
engine = sa.create_engine("sqlite://")
metadata = sa.MetaData()
sensor_table = sa.Table(
"sensor",
metadata,
sa.Column("time", sa.TIMESTAMP),
sa.Column("value", sa.String),
)
stmt = sa.select(
sa.func.timestamp_trunc(sensor_table.c.time, sa.text("hour")).label("time"),
sa.func.max(sensor_table.c.value),
).group_by(
sa.func.timestamp_trunc(sensor_table.c.time, sa.text("hour")),
)
print("SQLite" + "-" * 40)
print(
stmt.compile(
dialect=sqlite.dialect(),
compile_kwargs={"literal_binds": True},
),
)
print("BigQuery" + "-" * 40)
print(
stmt.compile(
dialect=bigquery.dialect(),
compile_kwargs={"literal_binds": True},
),
)
Stack trace
N/A (no exception will be raised unless you send it to BigQuery)