: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(hogql): defer snowflake.connector off the hogql.query import path (#69937)
Paul D'Ambrae295ecf76d28
diff --git a/posthog/hogql/direct_sql/snowflake_adapter.py b/posthog/hogql/direct_sql/snowflake_adapter.pyindex 41620ee69b1c..14136ea53dd3 100644--- a/posthog/hogql/direct_sql/snowflake_adapter.py+++ b/posthog/hogql/direct_sql/snowflake_adapter.py@@ -1,10 +1,10 @@+from __future__ import annotations+import refrom typing import TYPE_CHECKING, castimport sqlparse-import snowflake.connectorfrom opentelemetry import trace-from snowflake.connector.constants import FIELD_ID_TO_NAMEfrom sqlparse import tokens as sqlparse_tokensfrom posthog.hogql.constants import HogQLDialect@@ -15,6 +15,9 @@ from posthog.hogql.errors import ExposedHogQLErrorfrom posthog.hogql.snowflake_connection_cache import cached_snowflake_connectionif TYPE_CHECKING:+ import snowflake.connector+ import snowflake.connector.cursor+from posthog.models.team import Teamfrom products.warehouse_sources.backend.facade.models import ExternalDataSource@@ -81,6 +84,10 @@ def snowflake_field_type_to_clickhouse_type(type_code: object | None, scale: obj# every column as String.if not isinstance(type_code, int):return "String"+ from snowflake.connector.constants import (+ FIELD_ID_TO_NAME, # noqa: PLC0415 — keeps the heavy dep off the import path+ )+name = FIELD_ID_TO_NAME.get(type_code, "")if name == "FIXED":# NUMBER/DECIMAL/INT all report as FIXED; scale 0 (or absent) is an integer.@@ -89,6 +96,8 @@ def snowflake_field_type_to_clickhouse_type(type_code: object | None, scale: objdef snowflake_error_to_message(error: Exception) -> str:+ import snowflake.connector # noqa: PLC0415 — keeps the heavy dep off the import path+if isinstance(error, snowflake.connector.errors.Error):args = error.argsif len(args) >= 2 and isinstance(args[1], str) and args[1].strip():@@ -148,8 +157,8 @@ class SnowflakeAdapter:dialect: HogQLDialect | None = "snowflake"def validate_source_config(- self, source: "ExternalDataSource", team: "Team"- ) -> tuple["SnowflakeImplementation", "SnowflakeSourceConfig"]:+ self, source: ExternalDataSource, team: Team+ ) -> tuple[SnowflakeImplementation, SnowflakeSourceConfig]:from products.warehouse_sources.backend.facade.source_management import SnowflakeSource, SourceRegistryfrom products.warehouse_sources.backend.facade.types import ExternalDataSourceType@@ -179,6 +188,8 @@ class SnowflakeAdapter:MULTI_STATEMENT_COUNT=1 so the server refuses any stacked statement. A least-privilegeSELECT-only role is recommended as defense in depth, but is not the boundary we rely on."""+ import snowflake.connector # noqa: PLC0415 — keeps the heavy dep off the import path+source = request.sourcesnowflake_implementation, source_config = self.validate_source_config(source, request.team)settings = request.settingsdiff --git a/posthog/hogql/snowflake_connection_cache.py b/posthog/hogql/snowflake_connection_cache.pyindex a9629439ddf9..82bb574fbe2a 100644--- a/posthog/hogql/snowflake_connection_cache.py+++ b/posthog/hogql/snowflake_connection_cache.py@@ -26,11 +26,11 @@ from dataclasses import dataclassfrom time import monotonicfrom typing import TYPE_CHECKING-import snowflake.connector-from posthog.hogql.direct_query_metrics import SNOWFLAKE_CONNECTION_CACHE_TOTALif TYPE_CHECKING:+ import snowflake.connector+from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs import SnowflakeSourceConfigfrom products.warehouse_sources.backend.temporal.data_imports.sources.snowflake.snowflake import (SnowflakeImplementation,@@ -43,12 +43,16 @@ SNOWFLAKE_CONNECTION_CACHE_TTL_SECONDS = 1800# can't accumulate open sessions without limit.SNOWFLAKE_CONNECTION_CACHE_MAX_PER_THREAD = 8+# Transport/connection-level failures mean the cached session is suspect; SQL-level# errors (ProgrammingError, IntegrityError) leave the connection healthy.-_CONNECTION_LEVEL_ERRORS = (- snowflake.connector.errors.OperationalError,- snowflake.connector.errors.InterfaceError,-)+def _connection_level_errors() -> tuple[type[Exception], ...]:+ import snowflake.connector # noqa: PLC0415 — keeps the heavy dep off the import path++ return (+ snowflake.connector.errors.OperationalError,+ snowflake.connector.errors.InterfaceError,+ )@dataclass@@ -156,7 +160,7 @@ def cached_snowflake_connection(try:yield connection- except _CONNECTION_LEVEL_ERRORS:+ except _connection_level_errors():_evict(key)raise