Skip to content
Draft
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
11 changes: 11 additions & 0 deletions evalbench/databases/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def __init__(self, db_config):
# Initialize the Redis cache client
self.cache_client = get_cache_client(db_config)

def _cache_scope(self) -> str:
"""Returns the identity of this database for the query cache key.

The engine URL is not sufficient. With a Cloud SQL connector, the URL
has no host, no user, and no database name.
"""
return "|".join(
str(part)
for part in (self.db_type, self.db_path, self.db_name, self.username)
)

def clean_tmp_creations(self) -> None:
self.drop_tmp_databases(self.tmp_dbs.copy())
self.delete_tmp_users(self.tmp_users.copy())
Expand Down
2 changes: 1 addition & 1 deletion evalbench/databases/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def execute(
return self._execute(query, eval_query, rollback)
return with_cache_execute(
query,
self.engine.url,
self._cache_scope(),
self._execute,
self.cache_client,
)
Expand Down
2 changes: 1 addition & 1 deletion evalbench/databases/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def execute(
return self._execute(query, eval_query, rollback)
return with_cache_execute(
query,
self.engine.url,
self._cache_scope(),
self._execute,
self.cache_client,
)
Expand Down
2 changes: 1 addition & 1 deletion evalbench/databases/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def execute(
return self._execute(query, eval_query, rollback)
return with_cache_execute(
query,
self.db_name,
self._cache_scope(),
self._execute,
self.cache_client,
)
Expand Down
2 changes: 1 addition & 1 deletion evalbench/databases/sqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def execute(
return self._execute(query, eval_query, rollback)
return with_cache_execute(
query,
self.engine.url,
self._cache_scope(),
self._execute,
self.cache_client,
)
Expand Down
103 changes: 103 additions & 0 deletions evalbench/test/query_cache_key_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Tests for the database identity in the query cache key."""

import os
import sqlite3
import tempfile
import unittest
from types import SimpleNamespace

from databases.mysql import MySQLDB
from databases.postgres import PGDB
from databases.sqlite import SQLiteDB
from databases.sqlserver import SQLServerDB

_QUERY = "SELECT id FROM t"


class _FakeCache:

def __init__(self):
self.store = {}

def get(self, key):
return self.store.get(key)

def set(self, key, value):
self.store[key] = value


def _connector_db(db_class, cache, url, rows, **identity):
"""Returns a database object that does not connect to a server."""
db = db_class.__new__(db_class)
db.db_type = identity.get("db_type", "postgres")
db.db_path = identity.get("db_path", "project:region:instance")
db.db_name = identity.get("db_name", "db")
db.username = identity.get("username", "user")
db.cache_client = cache
db.engine = SimpleNamespace(url=url)
db._execute = lambda query, eval_query=None, rollback=False: (
rows, None, None)
return db


class QueryCacheKeyTest(unittest.TestCase):

def test_connector_databases_do_not_share_cached_rows(self):
cases = [
(PGDB, "postgresql+pg8000://"),
(MySQLDB, "mysql+pymysql://"),
(SQLServerDB, "mssql+pytds://"),
]
differences = [
{"db_name": "other_db"},
{"db_path": "project:region:other-instance"},
{"username": "other_user"},
]
for db_class, url in cases:
for difference in differences:
with self.subTest(db_class=db_class.__name__, **difference):
cache = _FakeCache()
first = _connector_db(db_class, cache, url, [{"id": 1}])
second = _connector_db(
db_class, cache, url, [{"id": 2}], **difference)

self.assertEqual(
first.execute(_QUERY, use_cache=True)[0], [{"id": 1}])
self.assertEqual(
second.execute(_QUERY, use_cache=True)[0], [{"id": 2}])

def test_same_database_uses_the_cache(self):
cache = _FakeCache()
first = _connector_db(PGDB, cache, "postgresql+pg8000://", [{"id": 1}])
second = _connector_db(PGDB, cache, "postgresql+pg8000://", [{"id": 2}])

first.execute(_QUERY, use_cache=True)

self.assertEqual(second.execute(_QUERY, use_cache=True)[0], [{"id": 1}])

def test_sqlite_databases_with_the_same_name_do_not_share_rows(self):
cache = _FakeCache()
with tempfile.TemporaryDirectory() as tmp:
results = []
for directory, value in (("a", 1), ("b", 2)):
db_path = os.path.join(tmp, directory)
os.makedirs(db_path)
conn = sqlite3.connect(os.path.join(db_path, "shop.db"))
conn.execute("CREATE TABLE t (id INT)")
conn.execute("INSERT INTO t VALUES (?)", (value,))
conn.commit()
conn.close()
db = SQLiteDB({
"db_type": "sqlite",
"database_path": db_path,
"database_name": "shop",
"max_executions_per_minute": 1000,
})
db.cache_client = cache
results.append(db.execute(_QUERY, use_cache=True)[0])

self.assertEqual(results, [[{"id": 1}], [{"id": 2}]])


if __name__ == "__main__":
unittest.main()
Loading