This is an automated email from the ASF dual-hosted git repository.

dabla pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 776e9d2e78a Cache the dialect on PostgresHook and MsSqlHook (#75656)
776e9d2e78a is described below

commit 776e9d2e78a8c36d496cc4732c6c26297fee0aa9
Author: David Blain <[email protected]>
AuthorDate: Thu Nov 5 10:45:12 2026 +0100

    Cache the dialect on PostgresHook and MsSqlHook (#75656)
    
    Both hooks built a new dialect on every access of the ``dialect``
    property, while the dialect caches its column, target-field and
    primary-key lookups per instance (``methodtools.lru_cache``). The cache
    therefore never hit across the chunks of one ``insert_rows`` call:
    ``_generate_insert_sql`` ran the ``information_schema.columns`` query
    once per ``commit_every`` rows, about 10 ms and a round trip per chunk.
    
    ``cached_property``, as ``DbApiHook.dialect`` and ``Db2Hook.dialect``
    already are: one dialect per hook, one lookup per table.
---
 .../providers/microsoft/mssql/hooks/mssql.py       |  3 ++-
 .../tests/unit/microsoft/mssql/hooks/test_mssql.py | 22 ++++++++++++++++++++++
 .../airflow/providers/postgres/hooks/postgres.py   |  3 ++-
 .../tests/unit/postgres/hooks/test_postgres.py     | 16 ++++++++++++++++
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git 
a/providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py
 
b/providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py
index 54a79204364..5246948b5ff 100644
--- 
a/providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py
+++ 
b/providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py
@@ -19,6 +19,7 @@
 
 from __future__ import annotations
 
+from functools import cached_property
 from typing import TYPE_CHECKING, Any
 
 import pymssql
@@ -72,7 +73,7 @@ class MsSqlHook(DbApiHook):
     def dialect_name(self) -> str:
         return "mssql"
 
-    @property
+    @cached_property
     def dialect(self) -> Dialect:
         return MsSqlDialect(self)
 
diff --git 
a/providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py 
b/providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py
index 991e65551de..2db6539dab8 100644
--- a/providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py
+++ b/providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py
@@ -370,6 +370,28 @@ class TestMsSqlHook:
         hook = MsSqlHook()
         assert isinstance(hook.dialect, MsSqlDialect)
 
+    def test_dialect_is_cached_per_hook(self):
+        hook = MsSqlHook()
+        assert hook.dialect is hook.dialect
+
+    
@mock.patch("airflow.providers.microsoft.mssql.hooks.mssql.MsSqlHook.get_connection")
+    @mock.patch(
+        "airflow.providers.microsoft.mssql.hooks.mssql.MsSqlHook.inspector", 
new_callable=mock.PropertyMock
+    )
+    def test_generate_insert_sql_looks_the_columns_up_once_per_hook(self, 
inspector, get_connection):
+        get_connection.return_value = Connection(
+            conn_type="mssql", host="ip", schema="share", login="u", 
password="p"
+        )
+        inspector.return_value.get_columns.return_value = [{"name": "id"}, 
{"name": "value"}]
+        hook = MsSqlHook()
+        hook._resolve_target_fields = True
+
+        first = hook._generate_insert_sql("dbo.table", [1, "a"])
+        second = hook._generate_insert_sql("dbo.table", [2, "b"])
+
+        assert first == second
+        inspector.return_value.get_columns.assert_called_once()
+
     def test_reserved_words(self):
         hook = MsSqlHook()
         assert hook.reserved_words == 
sqlalchemy.dialects.mssql.base.RESERVED_WORDS
diff --git 
a/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py 
b/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py
index 2c6c9253e49..71913a16350 100644
--- a/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py
+++ b/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py
@@ -21,6 +21,7 @@ import os
 from collections.abc import Callable, Iterable, Mapping
 from contextlib import closing
 from copy import deepcopy
+from functools import cached_property
 from typing import TYPE_CHECKING, Any, Literal, NoReturn, Protocol, TypeAlias, 
cast, overload
 
 from more_itertools import chunked
@@ -231,7 +232,7 @@ class PostgresHook(DbApiHook):
     def dialect_name(self) -> str:
         return "postgresql"
 
-    @property
+    @cached_property
     def dialect(self) -> Dialect:
         return PostgresDialect(self)
 
diff --git a/providers/postgres/tests/unit/postgres/hooks/test_postgres.py 
b/providers/postgres/tests/unit/postgres/hooks/test_postgres.py
index 6056fcd4f16..cfdc3c0dabe 100644
--- a/providers/postgres/tests/unit/postgres/hooks/test_postgres.py
+++ b/providers/postgres/tests/unit/postgres/hooks/test_postgres.py
@@ -100,6 +100,22 @@ class TestPostgresHookConn:
         self.db_hook.get_connection = mock.Mock()
         self.db_hook.get_connection.return_value = self.connection
 
+    def test_dialect_is_cached_per_hook(self):
+        assert self.db_hook.dialect is self.db_hook.dialect
+
+    def test_generate_insert_sql_looks_the_columns_up_once_per_hook(self):
+        columns = [
+            ("id", "integer", "NO", None, "NEVER", "NO"),
+            ("value", "text", "YES", None, "NEVER", "NO"),
+        ]
+        self.db_hook._resolve_target_fields = True
+        with mock.patch.object(self.db_hook, "get_records", 
return_value=columns) as get_records:
+            first = self.db_hook._generate_insert_sql("public.table", [1, "a"])
+            second = self.db_hook._generate_insert_sql("public.table", [2, 
"b"])
+
+        assert first == second
+        get_records.assert_called_once()
+
     def test_sqlalchemy_url_with_wrong_sqlalchemy_query_value(self):
         conn = Connection(
             login="login-conn",

Reply via email to