Skip to content

: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.

fix(data-catalog): show and block known query errors on new metric (#127408)

Sinan Kucukkoselera893f020be31
diff --git a/frontend/src/lib/monaco/CodeEditorImpl.tsx b/frontend/src/lib/monaco/CodeEditorImpl.tsx
index bbfbb119d707..5cfca6f8866f 100644
--- a/frontend/src/lib/monaco/CodeEditorImpl.tsx
+++ b/frontend/src/lib/monaco/CodeEditorImpl.tsx
@@ -49,6 +49,10 @@ export interface CodeEditorProps extends Omit<EditorProps, 'loading' | 'theme'>
onMetadataLoading?: (loading: boolean) => void
onFixWithAI?: (prompt: string) => void
onError?: (error: string | null) => void
+ /** How long to wait after an edit before validating. Set it higher where an error interrupts the user. */
+ metadataDebounceMs?: number
+ /** Validate against freshly fetched sources. Set it where the result blocks an action, because it costs a fetch per validation. */
+ metadataFreshSources?: boolean
/** Override the query sent for metadata validation (e.g. active query in multi-query mode) */
metadataQuery?: string
/** Character offset of metadataQuery within the full editor text, for correct marker positioning */
@@ -166,6 +170,8 @@ export function CodeEditor({
onMetadata,
onMetadataLoading,
onFixWithAI,
+ metadataDebounceMs,
+ metadataFreshSources,
metadataQuery,
metadataQueryOffset,
originalValue,
@@ -212,6 +218,8 @@ export function CodeEditor({
onMetadata,
onMetadataLoading,
onFixWithAI,
+ metadataDebounceMs,
+ metadataFreshSources,
metadataFilters: sourceQuery?.kind === NodeKind.HogQLQuery ? sourceQuery.filters : undefined,
})
useMountedLogic(builtCodeEditorLogic)
diff --git a/frontend/src/lib/monaco/codeEditorLogic.tsx b/frontend/src/lib/monaco/codeEditorLogic.tsx
index f86932702d4e..0861597eef07 100644
--- a/frontend/src/lib/monaco/codeEditorLogic.tsx
+++ b/frontend/src/lib/monaco/codeEditorLogic.tsx
@@ -58,6 +58,10 @@ export interface CodeEditorLogicProps {
onMetadata?: (metadata: HogQLMetadataResponse | null) => void
onMetadataLoading?: (loading: boolean) => void
onFixWithAI?: (prompt: string) => void
+ /** How long to wait after an edit before validating. Set it higher where an error interrupts the user. */
+ metadataDebounceMs?: number
+ /** Validate against freshly fetched sources. Set it where the result blocks an action, because it costs a fetch per validation. */
+ metadataFreshSources?: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -140,6 +144,8 @@ export type codeEditorLogicType = MakeLogicType<
codeEditorLogicMeta
>
+const DEFAULT_METADATA_DEBOUNCE_MS = 300
+
export const codeEditorLogic = kea<codeEditorLogicType>([
path(['lib', 'monaco', 'hogQLMetadataProvider']),
props({} as CodeEditorLogicProps),
@@ -161,7 +167,7 @@ export const codeEditorLogic = kea<codeEditorLogicType>([
props.onMetadata?.(null)
return null
}
- await breakpoint(300)
+ await breakpoint(props.metadataDebounceMs ?? DEFAULT_METADATA_DEBOUNCE_MS)
const query = props.metadataQuery ?? props.query
if (query === '') {
props.onMetadata?.(null)
@@ -190,6 +196,7 @@ export const codeEditorLogic = kea<codeEditorLogicType>([
variables,
connectionId,
indexUsage: props.indexUsage,
+ freshSources: props.metadataFreshSources,
},
{ recursion: false }
)
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index 66f618ab1a58..2bb4be8cd6fe 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -30062,6 +30062,10 @@
"$ref": "#/definitions/HogQLFilters",
"description": "Extra filters applied to query via {filters} or the column-bound {filters(expr AS key, ...)} placeholder"
},
+ "freshSources": {
+ "description": "Validate on the Python validator with freshly fetched sources, the same sources query execution uses. Adds a sources fetch on every request, so only set it where the result blocks an action.",
+ "type": "boolean"
+ },
"globals": {
"description": "Extra globals for the query",
"type": "object"
@@ -30152,6 +30156,10 @@
},
"type": "array"
},
+ "internalError": {
+ "description": "The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid.",
+ "type": "boolean"
+ },
"isUsingIndices": {
"$ref": "#/definitions/QueryIndexUsage"
},
@@ -43506,6 +43514,10 @@
},
"type": "array"
},
+ "internalError": {
+ "description": "The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid.",
+ "type": "boolean"
+ },
"isUsingIndices": {
"$ref": "#/definitions/QueryIndexUsage"
},
diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.ts
index e48869cd2905..c9667e19a619 100644
--- a/frontend/src/queries/schema/schema-general.ts
+++ b/frontend/src/queries/schema/schema-general.ts
@@ -980,6 +980,8 @@ export interface HogQLMetadataColumn {
export interface HogQLMetadataResponse {
query?: string
isValid?: boolean
+ /** The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid. */
+ internalError?: boolean
isUsingIndices?: QueryIndexUsage
/** One entry per property filter, in query order. */
index_usage?: PredicateIndexUsage[]
@@ -1094,6 +1096,8 @@ export interface HogQLMetadata extends DataNode<HogQLMetadataResponse> {
debug?: boolean
/** Analyze how each property filter reads its data. Costs a second type-resolution pass, so only editors that render the result should ask for it. */
indexUsage?: boolean
+ /** Validate on the Python validator with freshly fetched sources, the same sources query execution uses. Adds a sources fetch on every request, so only set it where the result blocks an action. */
+ freshSources?: boolean
/** Infer output column names and types without executing the query. Adds a type-resolution pass, so callers must opt in. */
includeOutputTypes?: boolean
}
diff --git a/posthog/hogql/metadata.py b/posthog/hogql/metadata.py
index 6ba1ed5845c1..8cb0d0b4020d 100644
--- a/posthog/hogql/metadata.py
+++ b/posthog/hogql/metadata.py
@@ -115,6 +115,7 @@ def get_hogql_metadata(
)
query_modifiers = create_default_modifiers_for_team(team, query.modifiers)
+ use_cached_sources = not query.freshSources
source = get_direct_connection_source(team, query.connectionId, user=user)
if query.connectionId and source is None:
response.isValid = False
@@ -129,7 +130,7 @@ def get_hogql_metadata(
modifiers=query_modifiers,
connection_id=str(source.id),
# Editor-assist only: query execution never reads cached sources.
- use_cached_sources=True,
+ use_cached_sources=use_cached_sources,
)
heuristic_warnings: list[HogQLNotice] = []
@@ -145,7 +146,7 @@ def get_hogql_metadata(
modifiers=query_modifiers,
enable_select_queries=True,
# Editor-assist only: query execution never reads cached sources.
- use_cached_sources=True,
+ use_cached_sources=use_cached_sources,
# A resolved direct-connection source prints with its engine dialect (below), so the
# context must be marked direct — otherwise the ClickHouse printer's direct-table guard
# fires and metadata/autocomplete reports a false "can only be queried through its direct
@@ -179,7 +180,7 @@ def get_hogql_metadata(
team=team,
user=user,
modifiers=query_modifiers,
- use_cached_sources=True,
+ use_cached_sources=use_cached_sources,
trigger="metadata",
)
context.database = database
@@ -255,6 +256,8 @@ def get_hogql_metadata(
response.errors.append(HogQLNotice(message=f"Unexpected {e.__class__.__name__}: {str(e)}"))
else:
response.errors.append(HogQLNotice(message=f"Unexpected {e.__class__.__name__}"))
+ if not isinstance(e, ExposedHogQLError):
+ response.internalError = True
finally:
if context is not None:
response.warnings = [*context.warnings, *heuristic_warnings]
diff --git a/posthog/hogql/test/test_metadata.py b/posthog/hogql/test/test_metadata.py
index a26eb0fdfd43..be8068c75094 100644
--- a/posthog/hogql/test/test_metadata.py
+++ b/posthog/hogql/test/test_metadata.py
@@ -4,7 +4,7 @@ from posthog.test.base import APIBaseTest, ClickhouseTestMixin
from unittest.mock import patch
from django.conf import settings
-from django.db import DatabaseError, connection
+from django.db import DatabaseError, OperationalError, connection
from django.test import override_settings
from django.test.utils import CaptureQueriesContext
@@ -22,6 +22,7 @@ from posthog.schema import (
SessionTableVersion,
)
+from posthog.hogql.database.database import Database
from posthog.hogql.direct_connection import INVALID_CONNECTION_ID_ERROR
from posthog.hogql.flag_called_warnings import (
FLAG_CALLED_FROM_BOTH_TABLES_NOTICE,
@@ -91,6 +92,38 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):
team=self.team,
)
+ def test_fresh_sources_resolve_a_view_that_cached_sources_miss(self) -> None:
+ query = "SELECT a FROM just_created_view"
+ self.assertEqual(len(self._select(query).errors), 1)
+
+ DataWarehouseSavedQuery.objects.create(
+ team=self.team,
+ name="just_created_view",
+ query={"query": "SELECT 1 AS a"},
+ columns={"a": "Int64"},
+ )
+
+ self.assertEqual(len(self._select(query).errors), 1)
+ fresh = get_hogql_metadata(
+ query=HogQLMetadata(kind="HogQLMetadata", language=HogLanguage.HOG_QL, query=query, freshSources=True),
+ team=self.team,
+ )
+ self.assertEqual(fresh.errors, [])
+
+ def test_internal_error_separates_a_validator_failure_from_a_query_error(self) -> None:
+ self.assertFalse(self._select("SELECT missing_column FROM events").internalError)
+
+ with patch.object(Database, "_fetch_sources", side_effect=OperationalError("connection reset")):
+ failed = get_hogql_metadata(
+ query=HogQLMetadata(
+ kind="HogQLMetadata", language=HogLanguage.HOG_QL, query="SELECT 1", freshSources=True
+ ),
+ team=self.team,
+ )
+
+ self.assertFalse(failed.isValid)
+ self.assertTrue(failed.internalError)
+
def _select_with_variables(
self, query: str, variables: Optional[dict[str, dict]] = None, globals: Optional[dict] = None
) -> HogQLMetadataResponse:
diff --git a/posthog/schema.py b/posthog/schema.py
index a4b3121dc3ba..11380bac1b91 100644
--- a/posthog/schema.py
+++ b/posthog/schema.py
@@ -21949,6 +21949,13 @@ class QueryResponseAlternative9(BaseModel):
index_usage: list[PredicateIndexUsage] | None = Field(
default=None, description="One entry per property filter, in query order."
)
+ internalError: bool | None = Field(
+ default=None,
+ description=(
+ "The validator raised an internal error, such as a database failure, so the"
+ " errors do not prove that the query is invalid."
+ ),
+ )
isUsingIndices: QueryIndexUsage | None = None
isValid: bool | None = None
notices: list[HogQLNotice]
@@ -29639,6 +29646,13 @@ class HogQLMetadataResponse(BaseModel):
index_usage: list[PredicateIndexUsage] | None = Field(
default=None, description="One entry per property filter, in query order."
)
+ internalError: bool | None = Field(
+ default=None,
+ description=(
+ "The validator raised an internal error, such as a database failure, so the"
+ " errors do not prove that the query is invalid."
+ ),
+ )
isUsingIndices: QueryIndexUsage | None = None
isValid: bool | None = None
notices: list[HogQLNotice]
@@ -34836,6 +34850,14 @@ class HogQLMetadata(BaseModel):
"Extra filters applied to query via {filters} or the column-bound {filters(expr AS key, ...)} placeholder"
),
)
+ freshSources: bool | None = Field(
+ default=None,
+ description=(
+ "Validate on the Python validator with freshly fetched sources, the same"
+ " sources query execution uses. Adds a sources fetch on every request, so"
+ " only set it where the result blocks an action."
+ ),
+ )
globals: dict[str, Any] | None = Field(default=None, description="Extra globals for the query")
includeOutputTypes: bool | None = Field(
default=None,
diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.ts
index 61b5e99ee68b..379c7cc097e1 100644
--- a/products/dashboards/frontend/generated/api.schemas.ts
+++ b/products/dashboards/frontend/generated/api.schemas.ts
@@ -5255,6 +5255,8 @@ export interface HogQLMetadataResponseApi {
errors: HogQLNoticeApi[]
/** One entry per property filter, in query order. */
index_usage?: PredicateIndexUsageApi[] | null
+ /** The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid. */
+ internalError?: boolean | null
isUsingIndices?: QueryIndexUsageApi | null
isValid?: boolean | null
notices: HogQLNoticeApi[]
diff --git a/products/data_catalog/frontend/DataCatalogNewMetricScene.tsx b/products/data_catalog/frontend/DataCatalogNewMetricScene.tsx
index d2c32b6445e3..f057eed34f0e 100644
--- a/products/data_catalog/frontend/DataCatalogNewMetricScene.tsx
+++ b/products/data_catalog/frontend/DataCatalogNewMetricScene.tsx
@@ -39,7 +39,7 @@ const DEFINITION_TYPE_HINTS: Record<MetricDefinitionType, string> = {
}
export function DataCatalogNewMetricScene(): JSX.Element {
- const { metricForm, metricFormValidationErrors, showMetricFormErrors, saving, sqlNameTaken } =
+ const { metricForm, metricFormValidationErrors, showMetricFormErrors, saving, saveDisabledReason, sqlNameTaken } =
useValues(dataCatalogNewMetricSceneLogic)
const { setMetricFormValues, submitMetric } = useActions(dataCatalogNewMetricSceneLogic)
const unitInputRef = useRef<HTMLInputElement>(null)
@@ -64,7 +64,9 @@ export function DataCatalogNewMetricScene(): JSX.Element {
type="secondary"
onClick={() => submitMetric(false)}
loading={saving === 'propose'}
- disabledReason={saving === 'approve' ? 'Saving the metric' : undefined}
+ disabledReason={
+ saving === 'approve' ? 'Saving the metric' : (saveDisabledReason ?? undefined)
+ }
data-attr="data-catalog-new-metric-save-proposed"
>
Save as proposed
@@ -74,7 +76,9 @@ export function DataCatalogNewMetricScene(): JSX.Element {
htmlType="submit"
onClick={() => submitMetric(true)}
loading={saving === 'approve'}
- disabledReason={saving === 'propose' ? 'Saving the metric' : undefined}
+ disabledReason={
+ saving === 'propose' ? 'Saving the metric' : (saveDisabledReason ?? undefined)
+ }
data-attr="data-catalog-create-metric-submit"
>
Create and approve
diff --git a/products/data_catalog/frontend/components/NewMetricSqlDefinition.tsx b/products/data_catalog/frontend/components/NewMetricSqlDefinition.tsx
index 184647617b1a..a4ec9670997a 100644
--- a/products/data_catalog/frontend/components/NewMetricSqlDefinition.tsx
+++ b/products/data_catalog/frontend/components/NewMetricSqlDefinition.tsx
@@ -10,6 +10,9 @@ import { SqlTestResult } from './SqlTestResult'
const NEW_METRIC_SQL_QUERY_KEY = 'data-catalog-new-metric'
+// The banner below the editor moves the page, so wait for a longer pause in typing than the editor default.
+const SQL_VALIDATION_DEBOUNCE_MS = 600
+
const SQL_PLACEHOLDER = '-- e.g. SELECT count(DISTINCT person_id) FROM events WHERE timestamp > now() - INTERVAL 30 DAY'
export function NewMetricSqlDefinition({ onAddUnit }: { onAddUnit: () => void }): JSX.Element {
@@ -20,13 +23,14 @@ export function NewMetricSqlDefinition({ onAddUnit }: { onAddUnit: () => void })
sqlPreviewLoading,
sqlPreviewError,
sqlPreviewStale,
- sqlEditorError,
+ sqlValidationMessage,
} = useValues(dataCatalogNewMetricSceneLogic)
- const { testSqlQuery, setSqlEditorError, openInSqlEditor } = useActions(dataCatalogNewMetricSceneLogic)
+ const { testSqlQuery, setSqlEditorError, setSqlMetadata, setSqlMetadataLoading, openInSqlEditor } =
+ useActions(dataCatalogNewMetricSceneLogic)
const testDisabledReason = !metricForm.sql.trim()
? 'Write a query before testing it'
- : (sqlEditorError ?? undefined)
+ : (sqlValidationMessage ?? undefined)
return (
<div className="flex flex-col gap-2">
@@ -39,6 +43,12 @@ export function NewMetricSqlDefinition({ onAddUnit }: { onAddUnit: () => void })
queryKey={NEW_METRIC_SQL_QUERY_KEY}
sourceQuery={sqlSourceQuery}
onError={setSqlEditorError}
+ onMetadata={setSqlMetadata}
+ onMetadataLoading={setSqlMetadataLoading}
+ metadataDebounceMs={SQL_VALIDATION_DEBOUNCE_MS}
+ // This result disables saving, and save validates against fresh sources. Cached
+ // sources can miss a new table and would block a query that save accepts.
+ metadataFreshSources
onPressCmdEnter={testSqlQuery}
options={{ placeholder: SQL_PLACEHOLDER }}
minHeight="10rem"
@@ -67,7 +77,8 @@ export function NewMetricSqlDefinition({ onAddUnit }: { onAddUnit: () => void })
Open in SQL editor
</LemonButton>
</div>
- {sqlPreviewError ? (
+ {sqlValidationMessage && <LemonBanner type="error">{sqlValidationMessage}</LemonBanner>}
+ {sqlPreviewError && !sqlValidationMessage ? (
<LemonBanner type="error">{sqlPreviewError}</LemonBanner>
) : sqlPreview ? (
<SqlTestResult
diff --git a/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.test.ts b/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.test.ts
index 3fe7a4f28ff2..549db9aefc3c 100644
--- a/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.test.ts
+++ b/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.test.ts
@@ -102,6 +102,17 @@ describe('dataCatalogNewMetricSceneLogic', () => {
logic.unmount()
})
+ function showInvalidQuery(form: Partial<NewMetricFormValues> = {}): void {
+ logic.actions.setMetricFormValues({ ...SQL_FORM, ...form })
+ logic.actions.setSqlEditorError('Error on line 10, column 5')
+ logic.actions.setSqlMetadata({
+ query: SQL_FORM.sql,
+ errors: [{ message: 'Unable to resolve field: foo' }],
+ warnings: [],
+ notices: [],
+ })
+ }
+
it.each([
['Create and approve', true, 1, 'Metric created and approved'],
['Save as proposed', false, 0, 'Metric saved as proposed'],
@@ -136,6 +147,29 @@ describe('dataCatalogNewMetricSceneLogic', () => {
expect(logic.values.metricFormValidationErrors.sql).toBeTruthy()
})
+ it.each([
+ ['sql', true],
+ ['insight', false],
+ ['markdown', false],
+ ] as const)('known query error with a %s definition disables saving: %s', (definitionType, disabled) => {
+ showInvalidQuery({ definitionType })
+
+ expect(!!logic.values.saveDisabledReason).toBe(disabled)
+ })
+
+ it.each([
+ ['the edited query is still validating', true, 'Error on line 10, column 5: Unable to resolve field: foo'],
+ ['validation of the edited query failed', false, null],
+ ])('after an edit when %s', (_case, loading, message) => {
+ showInvalidQuery()
+
+ logic.actions.setMetricFormValues({ sql: 'SELECT 1' })
+ logic.actions.setSqlMetadataLoading(loading)
+
+ expect(logic.values.sqlValidationMessage).toEqual(message)
+ expect(!!logic.values.saveDisabledReason).toBe(!!message)
+ })
+
it('never approves a confirmed overwrite', async () => {
;(dataCatalogMetricsCreate as jest.Mock).mockRejectedValueOnce(takenNameError())
logic.actions.setMetricFormValues(SQL_FORM)
@@ -166,6 +200,32 @@ describe('dataCatalogNewMetricSceneLogic', () => {
expect(router.values.location.pathname).toContain(urls.dataCatalogMetric('monthly_active_users'))
})
+ it('shows the validation message until the query is valid', () => {
+ showInvalidQuery()
+
+ expect(logic.values.sqlValidationMessage).toEqual('Error on line 10, column 5: Unable to resolve field: foo')
+
+ logic.actions.setSqlEditorError(null)
+ logic.actions.setSqlMetadata({ query: SQL_FORM.sql, errors: [], warnings: [], notices: [] })
+
+ expect(logic.values.sqlValidationMessage).toBeNull()
+ })
+
+ it('does not treat a validator failure as a known query error', () => {
+ logic.actions.setMetricFormValues(SQL_FORM)
+ logic.actions.setSqlEditorError('Error on line 1, column 1')
+ logic.actions.setSqlMetadata({
+ query: SQL_FORM.sql,
+ internalError: true,
+ errors: [{ message: 'Unexpected OperationalError' }],
+ warnings: [],
+ notices: [],
+ })
+
+ expect(logic.values.sqlValidationMessage).toBeNull()
+ expect(logic.values.saveDisabledReason).toBeNull()
+ })
+
it('runs one test query at a time', () => {
;(performQuery as jest.Mock).mockReturnValue(new Promise(() => {}))
logic.actions.setMetricFormValues(SQL_FORM)
diff --git a/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.ts b/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.ts
index d6599404c9e0..c8f0883aad7f 100644
--- a/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.ts
+++ b/products/data_catalog/frontend/dataCatalogNewMetricSceneLogic.ts
@@ -11,7 +11,7 @@ import { QUERY_TYPES_METADATA } from 'scenes/saved-insights/insightTypesMetadata
import { urls } from 'scenes/urls'
import { performQuery } from '~/queries/query'
-import { HogQLQuery, NodeKind, ProductKey } from '~/queries/schema/schema-general'
+import { HogQLMetadataResponse, HogQLQuery, NodeKind, ProductKey } from '~/queries/schema/schema-general'
import { Breadcrumb } from '~/types'
import {
@@ -67,6 +67,8 @@ export interface SqlPreview {
const SQL_PREVIEW_QUERY_TAGS = { productKey: ProductKey.DATA_CATALOG, scene: 'DataCatalogNewMetric' }
+const SQL_INVALID_SAVE_MESSAGE = 'Fix the error in the query before saving'
+
const SQL_PREVIEW_FAILED_MESSAGE = "Couldn't run the query. Try again, or open it in the SQL editor to see more."
function insightTypeName(query: unknown): string | null {
@@ -121,6 +123,7 @@ export interface dataCatalogNewMetricSceneLogicValues {
metricFormTouches: Record<string, boolean>
metricFormValidationErrors: DeepPartialMap<NewMetricFormValues, ValidationErrorType>
metricsUsingSelectedInsight: string[]
+ saveDisabledReason: string | null
savedInsights: SavedInsightOption[]
savedInsightsFailed: boolean
savedInsightsLoading: boolean
@@ -128,12 +131,16 @@ export interface dataCatalogNewMetricSceneLogicValues {
selectedInsight: SavedInsightOption | null
showMetricFormErrors: boolean
sqlEditorError: string | null
+ sqlMetadataError: string | null
+ sqlMetadataLoading: boolean
sqlNameTaken: boolean
sqlPreview: SqlPreview | null
sqlPreviewError: string | null
sqlPreviewLoading: boolean
sqlPreviewStale: boolean
sqlSourceQuery: HogQLQuery
+ sqlValidatedQuery: string | null
+ sqlValidationMessage: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -235,6 +242,12 @@ export interface dataCatalogNewMetricSceneLogicActions {
setSqlEditorError: (error: string | null) => {
error: string | null
}
+ setSqlMetadata: (metadata: HogQLMetadataResponse | null) => {
+ metadata: HogQLMetadataResponse | null
+ }
+ setSqlMetadataLoading: (loading: boolean) => {
+ loading: boolean
+ }
submitMetric: (approve: boolean) => {
approve: boolean
}
@@ -266,6 +279,14 @@ export interface dataCatalogNewMetricSceneLogicActions {
export interface dataCatalogNewMetricSceneLogicMeta {
__keaTypeGenInternalSelectorTypes: {
sqlSourceQuery: (metricForm: NewMetricFormValues) => HogQLQuery
+ sqlValidationMessage: (
+ sqlEditorError: string | null,
+ sqlMetadataError: string | null,
+ sqlValidatedQuery: string | null,
+ sqlMetadataLoading: boolean,
+ metricForm: NewMetricFormValues
+ ) => string | null
+ saveDisabledReason: (metricForm: NewMetricFormValues, sqlValidationMessage: string | null) => string | null
sqlPreviewStale: (sqlPreview: SqlPreview | null, metricForm: NewMetricFormValues) => boolean
sqlNameTaken: (allMetrics: DataCatalogMetricApi[], metricForm: NewMetricFormValues) => boolean
selectedInsight: (
@@ -299,6 +320,8 @@ export const dataCatalogNewMetricSceneLogic = kea<dataCatalogNewMetricSceneLogic
setSaving: (saving: MetricSaveAction | null) => ({ saving }),
setInsightSearch: (search: string) => ({ search }),
setSqlEditorError: (error: string | null) => ({ error }),
+ setSqlMetadata: (metadata: HogQLMetadataResponse | null) => ({ metadata }),
+ setSqlMetadataLoading: (loading: boolean) => ({ loading }),
testSqlQuery: true,
openInSqlEditor: true,
setLeaving: true,
@@ -391,6 +414,26 @@ export const dataCatalogNewMetricSceneLogic = kea<dataCatalogNewMetricSceneLogic
setSqlEditorError: (_, { error }) => error,
},
],
+ sqlMetadataError: [
+ null as string | null,
+ {
+ // An internal error says nothing about the query text. Save validates the query again on its own.
+ setSqlMetadata: (_, { metadata }) =>
+ metadata?.internalError ? null : (metadata?.errors?.[0]?.message ?? null),
+ },
+ ],
+ sqlValidatedQuery: [
+ null as string | null,
+ {
+ setSqlMetadata: (_, { metadata }) => metadata?.query ?? null,
+ },
+ ],
+ sqlMetadataLoading: [
+ false,
+ {
+ setSqlMetadataLoading: (_, { loading }) => loading,
+ },
+ ],
leaving: [
false,
{
@@ -428,6 +471,28 @@ export const dataCatalogNewMetricSceneLogic = kea<dataCatalogNewMetricSceneLogic
(s) => [s.metricForm],
(form: NewMetricFormValues): HogQLQuery => ({ kind: NodeKind.HogQLQuery, query: form.sql }),
],
+ sqlValidationMessage: [
+ (s) => [s.sqlEditorError, s.sqlMetadataError, s.sqlValidatedQuery, s.sqlMetadataLoading, s.metricForm],
+ (
+ sqlEditorError: string | null,
+ sqlMetadataError: string | null,
+ sqlValidatedQuery: string | null,
+ sqlMetadataLoading: boolean,
+ form: NewMetricFormValues
+ ): string | null => {
+ // Keep the error during a pending validation so that the banner does not flicker while typing.
+ // A failed validation reports nothing, so an error for other text must not outlive it.
+ if (!sqlMetadataError || (!sqlMetadataLoading && sqlValidatedQuery !== form.sql)) {
+ return null
+ }
+ return [sqlEditorError, sqlMetadataError].filter(Boolean).join(': ')
+ },
+ ],
+ saveDisabledReason: [
+ (s) => [s.metricForm, s.sqlValidationMessage],
+ (form: NewMetricFormValues, sqlValidationMessage: string | null): string | null =>
+ form.definitionType === 'sql' && sqlValidationMessage ? SQL_INVALID_SAVE_MESSAGE : null,
+ ],
sqlPreviewStale: [
(s) => [s.sqlPreview, s.metricForm],
(sqlPreview: SqlPreview | null, form: NewMetricFormValues): boolean =>
diff --git a/products/desktop/packages/api-client/src/generated.ts b/products/desktop/packages/api-client/src/generated.ts
index c75032e18696..898dbc6ef340 100644
--- a/products/desktop/packages/api-client/src/generated.ts
+++ b/products/desktop/packages/api-client/src/generated.ts
@@ -4351,6 +4351,7 @@ export namespace Schemas {
ch_table_names?: (Array<string> | null) | undefined;
errors: Array<HogQLNotice>;
index_usage?: (Array<PredicateIndexUsage> | null) | undefined;
+ internalError?: (boolean | null) | undefined;
isUsingIndices?: (QueryIndexUsage | null) | undefined;
isValid?: (boolean | null) | undefined;
notices: Array<HogQLNotice>;
diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.ts
index c34f6e96d1d7..e98f4eede05f 100644
--- a/products/product_analytics/frontend/generated/api.schemas.ts
+++ b/products/product_analytics/frontend/generated/api.schemas.ts
@@ -4092,6 +4092,8 @@ export interface HogQLMetadataResponseApi {
errors: HogQLNoticeApi[]
/** One entry per property filter, in query order. */
index_usage?: PredicateIndexUsageApi[] | null
+ /** The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid. */
+ internalError?: boolean | null
isUsingIndices?: QueryIndexUsageApi | null
isValid?: boolean | null
notices: HogQLNoticeApi[]
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index acff2c78dfe9..efc73ac978a8 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -8487,6 +8487,8 @@ export namespace Schemas {
errors: HogQLNotice[];
/** One entry per property filter, in query order. */
index_usage?: PredicateIndexUsage[] | null;
+ /** The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid. */
+ internalError?: boolean | null;
isUsingIndices?: QueryIndexUsage | null;
isValid?: boolean | null;
notices: HogQLNotice[];
@@ -57320,6 +57322,8 @@ export namespace Schemas {
debug?: boolean | null;
/** Extra filters applied to query via {filters} or the column-bound {filters(expr AS key, ...)} placeholder */
filters?: HogQLFilters | null;
+ /** Validate on the Python validator with freshly fetched sources, the same sources query execution uses. Adds a sources fetch on every request, so only set it where the result blocks an action. */
+ freshSources?: boolean | null;
/** Extra globals for the query */
globals?: HogQLMetadataGlobals;
/** Infer output column names and types without executing the query. Adds a type-resolution pass, so callers must opt in. */
@@ -95436,6 +95440,8 @@ export namespace Schemas {
errors: HogQLNotice[];
/** One entry per property filter, in query order. */
index_usage?: PredicateIndexUsage[] | null;
+ /** The validator raised an internal error, such as a database failure, so the errors do not prove that the query is invalid. */
+ internalError?: boolean | null;
isUsingIndices?: QueryIndexUsage | null;
isValid?: boolean | null;
notices: HogQLNotice[];