Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Compilation Caching Support #24

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Multi-value fields are mapped to SQL arrays of a specific scalar type. For examp
phones = Column('PHONE_ss', ARRAY(String))
```

### SQL Compilation Caching

The dialect supports caching by leveraging SQLAlchemy SQL compilation caching capabilities, which include query caching.

### Schema

If the ORM query supplied explicitly refers to a schema, the schema would be filtered out before query execution.
Expand Down
1 change: 1 addition & 0 deletions src/sqlalchemy_solr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ class SolrDialect(default.DefaultDialect):
returns_unicode_strings = True
description_encoding = None
supports_native_boolean = True
supports_statement_cache = True

def __init__(self, **kw):
default.DefaultDialect.__init__(self, **kw)
Expand Down
2 changes: 2 additions & 0 deletions src/sqlalchemy_solr/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
class SolrDialect_http(SolrDialect): # pylint: disable=invalid-name
# pylint: disable=abstract-method,too-many-instance-attributes

supports_statement_cache = True

mf = MessageFormatter()

def __init__(self, **kw):
Expand Down
1 change: 0 additions & 1 deletion tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def prepare_orm(settings):
settings["SOLR_CONNECTION_URI"]
+ "/"
+ settings["SOLR_WORKER_COLLECTION_NAME"],
echo=True,
)
t = Table(
settings["SOLR_WORKER_COLLECTION_NAME"],
Expand Down
57 changes: 57 additions & 0 deletions tests/test_sql_compilation_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from sqlalchemy import select
from sqlalchemy.sql.expression import bindparam
from sqlalchemy.util.langhelpers import _symbol

from tests.setup import prepare_orm
from .fixtures.fixtures import SalesFixture

class TestSQLCompilationCaching:
def index_data(self, settings):
f = SalesFixture(settings)
f.truncate_collection()
f.index()

def test_sql_compilation_caching_1(self, settings):
_, t = prepare_orm(settings)

qry_1 = (select(t.c.CITY_s).select_from(t)).limit(1)
qry_2 = (select(t.c.CITY_s).select_from(t)).limit(10)

k1 = qry_1._generate_cache_key() # pylint: disable=protected-access
k2 = qry_2._generate_cache_key() # pylint: disable=protected-access

assert k1 == k2

def test_sql_compilation_caching_2(self, settings):
_, t = prepare_orm(settings)

qry_1 = (select(t.c.CITY_s).select_from(t)).limit(1).offset(1)
qry_2 = (select(t.c.CITY_s).select_from(t)).limit(1).offset(2)

k1 = qry_1._generate_cache_key() # pylint: disable=protected-access
k2 = qry_2._generate_cache_key() # pylint: disable=protected-access

assert k1 == k2

def test_sql_compilation_caching_3(self, settings):
engine, t = prepare_orm(settings)

qry = select(t).where(t.c.CITY_s == bindparam('CITY_s')).limit(10)

with engine.connect() as connection:
result_1 = connection.execute(qry, {'CITY_s': 'Singapore'})
result_2 = connection.execute(qry, {'CITY_s': 'Boras'})

assert result_1.context.cache_hit == _symbol('CACHE_MISS')
assert result_2.context.cache_hit == _symbol('CACHE_HIT')

def test_sql_compilation_caching_4(self, settings):
_, t = prepare_orm(settings)

qry_1 = select(t).where(t.c.CITY_s == bindparam('CITY_s')).limit(10)
qry_2 = select(t).where(t.c.COUNTRY_s == bindparam('COUNTRY_s')).limit(10)

k1 = qry_1._generate_cache_key() # pylint: disable=protected-access
k2 = qry_2._generate_cache_key() # pylint: disable=protected-access

assert k1 != k2
Loading