Skip to content

Commit 3876216

Browse files
fix: ensure non-ASCII entity key prefix compatibility
Fix critical bug where serialize_entity_key_prefix and serialize_entity_key produce incompatible results for non-ASCII characters, breaking prefix scans for existing online store data. ## Problem The optimization changed serialize_entity_key to write UTF-8 byte lengths (len(k_encoded)) while serialize_entity_key_prefix still wrote character counts (len(k)). For non-ASCII keys like "用户ID": - Character length: 4 - UTF-8 byte length: 8 This inconsistency breaks prefix scans and could cause data lookup failures for existing non-ASCII entity keys after upgrade. ## Solution - Update serialize_entity_key_prefix to write UTF-8 byte lengths consistently - Add comprehensive test coverage for non-ASCII key compatibility - Verify both ASCII and non-ASCII keys work correctly - Test multi-key scenarios with mixed character types ## Tests Added - test_non_ascii_prefix_compatibility: Tests Chinese, Korean, Cyrillic, Arabic - test_ascii_prefix_compatibility: Ensures ASCII keys still work - test_multi_key_non_ascii_prefix_compatibility: Mixed ASCII/non-ASCII keys All tests verify that prefix serialization produces byte-identical prefixes to the corresponding portions of full entity key serialization. Fixes #5981 Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
1 parent fb16437 commit 3876216

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎sdk/python/feast/infra/key_encoding_utils.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ def serialize_entity_key_prefix(
6868
for k in sorted_keys:
6969
output.append(struct.pack("<I", ValueType.STRING))
7070
if entity_key_serialization_version > 2:
71-
output.append(struct.pack("<I", len(k)))
72-
output.append(k.encode("utf8"))
71+
k_encoded = k.encode("utf8")
72+
output.append(struct.pack("<I", len(k_encoded)))
73+
else:
74+
k_encoded = k.encode("utf8")
75+
output.append(k_encoded)
7376
return b"".join(output)
7477

7578

‎sdk/python/tests/unit/infra/test_key_encoding_utils.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
deserialize_entity_key,
55
reserialize_entity_v2_key_to_v3,
66
serialize_entity_key,
7+
serialize_entity_key_prefix,
78
)
89
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
910
from feast.protos.feast.types.Value_pb2 import Value as ValueProto
@@ -260,3 +261,78 @@ def test_performance_bounds_single_entity():
260261
# 1000 operations should complete in < 20ms each for serialization and deserialization
261262
assert serialize_time < 0.02, f"Serialization too slow: {serialize_time:.4f}s"
262263
assert deserialize_time < 0.02, f"Deserialization too slow: {deserialize_time:.4f}s"
264+
265+
266+
def test_non_ascii_prefix_compatibility():
267+
"""Critical test: ensure prefix serialization matches full entity key serialization for non-ASCII keys."""
268+
# Test with non-ASCII characters that have different byte vs character lengths
269+
non_ascii_keys = ["用户ID", "사용자ID", "идентификатор", "مُعرِّف"]
270+
271+
for key in non_ascii_keys:
272+
# Test single key prefix
273+
prefix_result = serialize_entity_key_prefix(
274+
[key], entity_key_serialization_version=3
275+
)
276+
277+
# Create full entity key and serialize it
278+
entity_key = EntityKeyProto(
279+
join_keys=[key], entity_values=[ValueProto(string_val="test_value")]
280+
)
281+
full_result = serialize_entity_key(
282+
entity_key, entity_key_serialization_version=3
283+
)
284+
285+
# The prefix should match the beginning of the full serialization
286+
# Extract just the key portion (skip entity count, but include key metadata)
287+
prefix_len = len(prefix_result)
288+
assert full_result[:prefix_len] == prefix_result, (
289+
f"Prefix mismatch for non-ASCII key '{key}': "
290+
f"Character length: {len(key)}, "
291+
f"UTF-8 byte length: {len(key.encode('utf8'))}"
292+
)
293+
294+
295+
def test_ascii_prefix_compatibility():
296+
"""Verify prefix compatibility still works for ASCII keys."""
297+
ascii_keys = ["user_id", "session_id", "device_id"]
298+
299+
for key in ascii_keys:
300+
prefix_result = serialize_entity_key_prefix(
301+
[key], entity_key_serialization_version=3
302+
)
303+
304+
entity_key = EntityKeyProto(
305+
join_keys=[key], entity_values=[ValueProto(string_val="test_value")]
306+
)
307+
full_result = serialize_entity_key(
308+
entity_key, entity_key_serialization_version=3
309+
)
310+
311+
prefix_len = len(prefix_result)
312+
assert full_result[:prefix_len] == prefix_result, (
313+
f"Prefix mismatch for ASCII key '{key}'"
314+
)
315+
316+
317+
def test_multi_key_non_ascii_prefix_compatibility():
318+
"""Test multi-key prefix compatibility with non-ASCII characters."""
319+
mixed_keys = ["user_id", "用户会话", "session_id"] # Mix ASCII and non-ASCII
320+
321+
prefix_result = serialize_entity_key_prefix(
322+
mixed_keys, entity_key_serialization_version=3
323+
)
324+
325+
entity_key = EntityKeyProto(
326+
join_keys=mixed_keys,
327+
entity_values=[
328+
ValueProto(string_val="test1"),
329+
ValueProto(string_val="test2"),
330+
ValueProto(string_val="test3"),
331+
],
332+
)
333+
full_result = serialize_entity_key(entity_key, entity_key_serialization_version=3)
334+
335+
prefix_len = len(prefix_result)
336+
assert full_result[:prefix_len] == prefix_result, (
337+
"Multi-key prefix mismatch with non-ASCII"
338+
)

0 commit comments

Comments
 (0)