drizzle-team / drizzle-team/drizzle-orm
[BUG]: [1.0.0-rc.1] Relational query: extras with same name as a column gets the column's codec applied to its value
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
1.0.0-rc.1
### What version of `drizzle-kit` are you using?
1.0.0-rc.1
### Other packages
_No response_
### Describe the Bug
## Summary
When a relational query selects a table column **and** an `extras` SQL aliased to the same name (e.g. wrapping a PostGIS column with `ST_AsGeoJSON(...)::jsonb`), Postgres returns the second
value (the GeoJSON), but the JIT relational query mapper still runs the **original column's codec** (`geometry:tuple` / `parseEWKB`) on it, crashing with `RangeError: Offset is outside the
bounds of the DataView`.
## Versions
- `drizzle-orm@1.0.0-rc.1`
- Postgres + PostGIS, `pg@8.20.0`
## Repro
```ts
// schema
export const VehicleTelemetry = pgTable("vehicle_telemetries", {
id: uuid().primaryKey(),
location: geometry("location", { srid: 4326 }), // tuple mode (default)
// ...
});
// query — note: NO `columns:` filter, so `location` is selected from the table
db.query.Vehicle.findMany({
with: {
telemetry: {
extras: {
location: (t) =>
sql`ST_AsGeoJSON(${t.location})::jsonb`.as("location"),
},
},
},
});
```
Generated SQL selects two columns aliased `location`:
```sql
select "d0"."location" as "location",
(ST_AsGeoJSON("d0"."location")::jsonb) as "location",
...
```
## Expected
Either:
- `extras` aliasing an existing column name should **shadow** the column (and bypass the column's codec), or
- the dialect should refuse to emit duplicate aliases.
## Actual
```
RangeError: Offset is outside the bounds of the DataView
at DataView.prototype.getUint32 ()
at parseGeometryTuple (drizzle-orm/pg-core/codecs.ts:284)
at Object.eval (drizzle:jit-relational-query-mapper:71)
```
The driver hands the GeoJSON object (from the second `location`) into `parseEWKB`, which expects an EWKB hex string.
## Workaround
Whitelist/exclude the underlying column:
```ts
telemetry: {
columns: { location: false },
extras: { location: (t) => sql`ST_AsGeoJSON(${t.location})::jsonb`.as("location") },
}
```
Worked fine on `1.0.0-beta.*` because the codec wasn't applied.
Contributor guide
Assessment
This issue has not been assessed yet.