Skip to content

Commit 903d48d

Browse files
sohailshkntkathole
authored andcommitted
fix: Read SQLite online features in batches below the bound-variable limit
online_read bound one variable per entity key in a single IN (...) list, so a request with more distinct entity keys than SQLITE_LIMIT_VARIABLE_NUMBER (32766 by default since SQLite 3.32.0, 999 before; builds can set it differently) failed with "too many SQL variables". Query the distinct keys in batches sized to the connection's limit. Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
1 parent 326b149 commit 903d48d

2 files changed

Lines changed: 84 additions & 11 deletions

File tree

‎sdk/python/feast/infra/online_stores/sqlite.py‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,25 @@ def online_read(
393393
)
394394
for entity_key in entity_keys
395395
]
396-
# Fetch all entities in one go
397-
cur.execute(
398-
f"SELECT entity_key, feature_name, value, event_ts "
399-
f"FROM {_quote_id(_table_id(config.project, table, config.registry.enable_online_feature_view_versioning))} "
400-
f"WHERE entity_key IN ({','.join('?' * len(entity_keys))}) "
401-
f"ORDER BY entity_key",
402-
serialized_entity_keys,
396+
table_name = _table_id(
397+
config.project, table, config.registry.enable_online_feature_view_versioning
403398
)
404-
rows = cur.fetchall()
405-
rows = {
406-
k: list(group) for k, group in itertools.groupby(rows, key=lambda r: r[0])
407-
}
399+
# Every key is a bound variable, and SQLite rejects a statement with more
400+
# than SQLITE_LIMIT_VARIABLE_NUMBER of them, so look keys up in batches.
401+
batch_size = max(1, conn.getlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER))
402+
unique_keys = list(dict.fromkeys(serialized_entity_keys))
403+
rows: Dict[bytes, List[Any]] = {}
404+
for start in range(0, len(unique_keys), batch_size):
405+
batch = unique_keys[start : start + batch_size]
406+
cur.execute(
407+
f"SELECT entity_key, feature_name, value, event_ts "
408+
f"FROM {_quote_id(table_name)} "
409+
f"WHERE entity_key IN ({','.join('?' * len(batch))}) "
410+
f"ORDER BY entity_key",
411+
batch,
412+
)
413+
for k, group in itertools.groupby(cur.fetchall(), key=lambda r: r[0]):
414+
rows[k] = list(group)
408415
for entity_key_bin in serialized_entity_keys:
409416
res = {}
410417
res_ts = None
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sqlite3
2+
from datetime import datetime, timedelta, timezone
3+
4+
from feast import Entity, FeatureView, Field, FileSource, RepoConfig
5+
from feast.infra.online_stores.sqlite import SqliteOnlineStore, SqliteOnlineStoreConfig
6+
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
7+
from feast.protos.feast.types.Value_pb2 import Value as ValueProto
8+
from feast.types import Int64
9+
10+
11+
def _entity_key(driver_id: int) -> EntityKeyProto:
12+
return EntityKeyProto(
13+
join_keys=["driver_id"], entity_values=[ValueProto(int64_val=driver_id)]
14+
)
15+
16+
17+
def test_online_read_with_more_keys_than_sqlite_variable_limit(tmp_path):
18+
config = RepoConfig(
19+
project="read_limit",
20+
registry=str(tmp_path / "registry.db"),
21+
provider="local",
22+
online_store=SqliteOnlineStoreConfig(path=str(tmp_path / "online.db")),
23+
offline_store={"type": "dask"},
24+
entity_key_serialization_version=3,
25+
)
26+
driver = Entity(name="driver_id", join_keys=["driver_id"])
27+
view = FeatureView(
28+
name="driver_stats",
29+
entities=[driver],
30+
ttl=timedelta(days=1),
31+
schema=[Field(name="trips", dtype=Int64)],
32+
source=FileSource(path=str(tmp_path / "unused.parquet"), timestamp_field="ts"),
33+
)
34+
store = SqliteOnlineStore()
35+
store.update(config, [], [view], [], [driver], partial=False)
36+
37+
now = datetime.now(tz=timezone.utc)
38+
num_keys = 120
39+
store.online_write_batch(
40+
config,
41+
view,
42+
[
43+
(_entity_key(i), {"trips": ValueProto(int64_val=i * 10)}, now, None)
44+
for i in range(num_keys)
45+
],
46+
None,
47+
)
48+
49+
# Lower the per-statement variable limit so that one IN (...) list over all
50+
# keys no longer fits, whatever limit this SQLite build was compiled with.
51+
store._get_conn(config).setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 50)
52+
# An id that was never written, and a repeated id, ride along.
53+
keys = [_entity_key(i) for i in range(num_keys)] + [
54+
_entity_key(10_000),
55+
_entity_key(3),
56+
]
57+
result = store.online_read(config, view, keys)
58+
59+
assert len(result) == num_keys + 2
60+
for i in range(num_keys):
61+
ts, features = result[i]
62+
assert ts is not None and features is not None
63+
assert features["trips"].int64_val == i * 10
64+
assert result[num_keys] == (None, None)
65+
assert result[num_keys + 1][1]["trips"].int64_val == 30
66+
assert store.online_read(config, view, []) == []

0 commit comments

Comments
 (0)