:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): close three HogQL dialect gaps in SQL generation prompts (#77340)
posthog[bot]a41707c9c4a9
diff --git a/ee/hogai/chat_agent/sql/prompts.py b/ee/hogai/chat_agent/sql/prompts.pyindex fd1bc89f2c21..d3cc985156e6 100644--- a/ee/hogai/chat_agent/sql/prompts.py+++ b/ee/hogai/chat_agent/sql/prompts.py@@ -19,8 +19,10 @@ CRITICAL - Function name casing:Important HogQL differences versus other SQL dialects:- JSON properties are accessed using `properties.foo.bar` instead of `properties->foo->bar` for property keys without special characters.- JSON properties can also be accessed using `properties.foo['bar']` if there's any special character (note the single quotes).-- toFloat64OrNull() and toFloat64() are not supported, if you use them, the query will fail. Use toFloat() instead.+- Width-suffixed conversion functions (toInt64, toInt32, toFloat64, toUInt8, etc., including their OrNull/OrZero variants like toInt32OrNull, toFloat64OrZero) are not supported, if you use them, the query will fail. Use the unsuffixed form instead: toInt(), toFloat(), toUInt() and their OrNull/OrZero variants (toIntOrNull, toFloatOrZero, toUIntOrNull, ...).- Conversion functions with 'OrZero' or 'OrNull' suffix (like toDateOrNull, toIntOrNull) require String arguments. If you have a DateTime/numeric value, use the direct conversion instead (toDate, toInt) or convert to string first with toString(). Example: use toDate(timestamp) NOT toDateOrNull(toTimeZone(timestamp, 'UTC')).+- toDate() takes exactly one argument. It does not accept a timezone argument like ClickHouse's toDate(value, timezone). Convert to the desired timezone first with toTimeZone(), then wrap in toDate(). Example: toDate(toTimeZone(timestamp, 'US/Pacific')) NOT toDate(timestamp, 'US/Pacific').+- The ClickHouse `SETTINGS` clause is not supported in HogQL and will always cause an "Unsupported: SelectStmt.settingsClause()" error. Do not append `SETTINGS ...` to a query, even to tune things like `max_execution_time` or `join_algorithm`.- LAG()/LEAD() are not supported. Instead, use lagInFrame()/leadInFrame().Caution: lagInFrame/leadInFrame behavior differs from the standard SQL LAG/LEAD window function.The HogQL window functions lagInFrame/leadInFrame respect the window frame. To get behavior identical to LAG/LEAD, use `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`.diff --git a/ee/hogai/tools/execute_sql/prompts.py b/ee/hogai/tools/execute_sql/prompts.pyindex f352457da0fb..72204b03b025 100644--- a/ee/hogai/tools/execute_sql/prompts.py+++ b/ee/hogai/tools/execute_sql/prompts.py@@ -4,8 +4,10 @@ Use this tool to generate a HogQL query, which is PostHog's variant of SQL that# Important HogQL differences versus other SQL dialects- JSON properties are accessed using `properties.foo.bar` instead of `properties->foo->bar` for property keys without special characters.- JSON properties can also be accessed using `properties.foo['bar']` if there's any special character (note the single quotes).-- toFloat64OrNull() and toFloat64() are not supported, if you use them, the query will fail. Use toFloat() instead.+- Width-suffixed conversion functions (toInt64, toInt32, toFloat64, toUInt8, etc., including their OrNull/OrZero variants like toInt32OrNull, toFloat64OrZero) are not supported, if you use them, the query will fail. Use the unsuffixed form instead: toInt(), toFloat(), toUInt() and their OrNull/OrZero variants (toIntOrNull, toFloatOrZero, toUIntOrNull, ...).- Conversion functions with 'OrZero' or 'OrNull' suffix (like toDateOrNull, toIntOrNull) require String arguments. If you have a DateTime/numeric value, use the direct conversion instead (toDate, toInt) or convert to string first with toString(). Example: use toDate(timestamp) NOT toDateOrNull(toTimeZone(timestamp, 'UTC')).+- toDate() takes exactly one argument. It does not accept a timezone argument like ClickHouse's toDate(value, timezone). Convert to the desired timezone first with toTimeZone(), then wrap in toDate(). Example: toDate(toTimeZone(timestamp, 'US/Pacific')) NOT toDate(timestamp, 'US/Pacific').+- The ClickHouse `SETTINGS` clause is not supported in HogQL and will always cause an "Unsupported: SelectStmt.settingsClause()" error. Do not append `SETTINGS ...` to a query, even to tune things like `max_execution_time` or `join_algorithm`.- LAG()/LEAD() are not supported. Instead, use lagInFrame()/leadInFrame().Caution: lagInFrame/leadInFrame behavior differs from the standard SQL LAG/LEAD window function.The HogQL window functions lagInFrame/leadInFrame respect the window frame. To get behavior identical to LAG/LEAD, use `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`.diff --git a/services/mcp/src/templates/execute-sql-prompt.md b/services/mcp/src/templates/execute-sql-prompt.mdindex 28f03534d44f..242be0d5677a 100644--- a/services/mcp/src/templates/execute-sql-prompt.md+++ b/services/mcp/src/templates/execute-sql-prompt.md@@ -19,6 +19,9 @@ If a `query-*` tool fits, use it. Default to `query-*`; SQL is the escape hatch,### Common pitfalls- **For `system.*` entities, filter `information_schema` by the fully-qualified `table_name`:** use `'system.insights'`, not `'insights'`; the bare name (or a `table_schema = 'system'` split) silently returns zero rows.+- **HogQL rejects the ClickHouse `SETTINGS` clause outright** — appending `SETTINGS ...` (e.g. to tune `max_execution_time` or `join_algorithm`) always fails with `Unsupported: SelectStmt.settingsClause()`. Don't include it.+- **`toDate()` takes exactly one argument** — it does not accept ClickHouse's `toDate(value, timezone)` form. Convert timezone first with `toTimeZone()`, then wrap in `toDate()`: `toDate(toTimeZone(timestamp, 'US/Pacific'))`, not `toDate(timestamp, 'US/Pacific')`.+- **Width-suffixed conversion functions aren't supported** — `toInt64`, `toInt32`, `toFloat64`, `toUInt8`, etc. (and their `OrNull`/`OrZero` variants) always fail. Use the unsuffixed form instead: `toInt()`, `toFloat()`, `toUInt()`, `toIntOrNull()`, `toFloatOrZero()`, and so on.### Format SQL for readability