Files
triggerdotdev--trigger.dev/internal-packages/tsql
Eric Allam 02cf9c81ad fix(tsql): make JSON functions work on the output and error columns (#4221)
## Summary

A Query page (TRQL) query that pulls fields out of a run's `output` with
JSON functions (`JSONExtractString`, `JSONExtractInt`, `JSONHas`, and
the rest of the family) failed with "The first argument of function ...
should be a string containing JSON, illegal type: JSON". Those queries
now work.

## Root cause and fix

`output` is a native ClickHouse `JSON` column, but `JSONExtract*`,
`JSONHas`, `JSONLength`, and `JSONType` all expect a String containing
JSON text. The compiler already swaps in the column's String companion
(`output_text`) when a JSON column is selected or compared, but not
inside function-call arguments, so it emitted `JSONExtractInt(output,
'x')` against the native column.

The fix prints the companion column for the first argument of these
functions when it resolves to a bare JSON field, keeping the table alias
when qualified (so it works in JOINs):

JSONExtractInt(output, 'x') -> JSONExtractInt(output_text, 'x')
JSONExtractArrayRaw(assumeNotNull(output), 'y') ->
JSONExtractArrayRaw(assumeNotNull(output_text), 'y')

It also reaches through value-preserving passthrough wrappers like
`assumeNotNull(...)`, while leaving value-changing wrappers like
`toJSONString(output)` on the native column (that argument is already a
String). The swap is also semantically correct, not just a type fix:
`output_text` is the unwrapped data JSON that the TRQL `output` model
already represents, so field paths line up.

Covered by printer unit tests and a ClickHouse integration test that
runs the whole family (plus the wrapped and `toJSONString` cases)
against a real native-JSON column. Both new cases fail with the exact
"illegal type: JSON" error without the fix.
2026-07-10 12:44:52 +01:00
..
2026-01-09 11:39:36 +00:00
2026-01-09 11:39:36 +00:00
2026-01-09 11:39:36 +00:00
2026-01-09 11:39:36 +00:00
2026-01-09 11:39:36 +00:00

TSQL (TriggerSQL)

TriggerSQL is a DSL that is safely converted into ClickHouse SQL queries with protection against SQL injection and it's tenant-safe (users can only query their own data).

Attribution

This package is derived from PostHog's HogQL (MIT License). See NOTICE.md for the full copyright notice.

ANTLR Grammar

The ANTLR grammar is heavily inspired by PostHog's HogQL.

These are found in ./src/grammar and are the .g4 files.

Generating the source code

pnpm run grammar:build