mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
s3vectors: match json_extract's escaping of control characters in filter strings (#12817)
* s3vectors: render filter string control characters as serde_json does
json_extract serializes the stored value with serde_json: \b, \f, \n,
\r, \t, \u00XX for the rest of U+0000-U+001F, and U+007F-U+009F raw.
The scalar comparison text used Rust debug escapes (\0, \u{1}, \u{7f}),
so $eq/$in on such strings missed scalar values and $ne/$nin kept them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
* test(s3vectors): filter on every control character, quote and backslash
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e109f91c6f
commit
4bbc03747f
3 files changed
+47
-5
No files matched your search
@@ -266,6 +266,25 @@ def test_filter_arrays(vectors, index):
|
||||
assert matches({"$nin": [1, "2.5"]}) == ["flag", "scalar", "space"]
|
||||
|
||||
|
||||
def test_filter_control_characters(vectors, index):
|
||||
chars = [chr(c) for c in [*range(0x20), *range(0x7F, 0xA0)]] + ['"', "\\"]
|
||||
rows = {f"c{ord(ch):02x}": f"a{ch}b" for ch in chars}
|
||||
vectors.put_vectors(**index, vectors=[{"key": key, "data": {"float32": [1, 0, 0, 0]}, "metadata": {"s": value}}
|
||||
for key, value in rows.items()])
|
||||
|
||||
def matches(condition):
|
||||
hits = vectors.query_vectors(**index, queryVector={"float32": [1, 0, 0, 0]}, topK=100,
|
||||
filter={"s": condition})["vectors"]
|
||||
return sorted(hit["key"] for hit in hits)
|
||||
|
||||
for key, value in rows.items():
|
||||
others = sorted(k for k in rows if k != key)
|
||||
assert matches({"$eq": value}) == [key], repr(value)
|
||||
assert matches({"$in": [value, "other"]}) == [key], repr(value)
|
||||
assert matches({"$ne": value}) == others, repr(value)
|
||||
assert matches({"$nin": [value, "other"]}) == others, repr(value)
|
||||
|
||||
|
||||
def test_euclidean_distance(vectors, index):
|
||||
vectors.put_vectors(**index, vectors=[{"key": "p", "data": {"float32": [3, 4, 0, 0]}}])
|
||||
result = vectors.query_vectors(**index, queryVector={"float32": [0, 0, 0, 0]}, topK=1, returnDistance=True)
|
||||
|
||||
@@ -2111,7 +2111,7 @@ func elementMatch(field string, operand interface{}) (string, error) {
|
||||
}
|
||||
|
||||
// jsonbString renders s as the JSON text json_extract returns for a stored
|
||||
// string: quoted, with the same escapes the jsonb serializer emits.
|
||||
// string: quoted, with the escapes serde_json emits.
|
||||
func jsonbString(s string) string {
|
||||
var b strings.Builder
|
||||
b.WriteByte('"')
|
||||
@@ -2127,11 +2127,13 @@ func jsonbString(s string) string {
|
||||
b.WriteString(`\r`)
|
||||
case '\t':
|
||||
b.WriteString(`\t`)
|
||||
case 0:
|
||||
b.WriteString(`\0`)
|
||||
case '\b':
|
||||
b.WriteString(`\b`)
|
||||
case '\f':
|
||||
b.WriteString(`\f`)
|
||||
default:
|
||||
if r < 0x20 || (r >= 0x7f && r <= 0x9f) {
|
||||
fmt.Fprintf(&b, `\u{%x}`, r)
|
||||
if r < 0x20 {
|
||||
fmt.Fprintf(&b, `\u%04x`, r)
|
||||
} else {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
|
||||
@@ -399,6 +399,9 @@ func TestTranslateFilter(t *testing.T) {
|
||||
"(COALESCE(json_extract(`metadata`, '$[\"a''b\"]') = '\"v''1\"', false) OR COALESCE(json_array_contains(`metadata`, '$[\"a''b\"]', '\"v''1\"'), false))"},
|
||||
{"array value escapes", map[string]interface{}{"k": `<a&"b">`},
|
||||
"(COALESCE(" + str + ` = '"<a&\"b\">"', false) OR ` + elem(`"<a&\"b\">"`) + ")"},
|
||||
{"control characters", map[string]interface{}{"k": map[string]interface{}{"$ne": "\x00\x01\b\f\x1f\x7f\u0085\u009f"}},
|
||||
"NOT (COALESCE(" + str + " = '\"\\u0000\\u0001\\b\\f\\u001f\x7f\u0085\u009f\"', false) OR " +
|
||||
elem(`"\u0000\u0001\b\f\u001f`+"\x7f\u0085\u009f\"") + ")"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got, err := translateFilter(tc.filter)
|
||||
@@ -434,6 +437,24 @@ func TestTranslateFilter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// json_extract serializes a stored string with serde_json, which escapes the
|
||||
// same characters as encoding/json below U+00A0.
|
||||
func TestJsonbStringControlCharacters(t *testing.T) {
|
||||
for r := rune(0); r < 0xa0; r++ {
|
||||
s := "a" + string(r) + "b"
|
||||
var b strings.Builder
|
||||
enc := json.NewEncoder(&b)
|
||||
enc.SetEscapeHTML(false)
|
||||
if err := enc.Encode(s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := strings.TrimSuffix(b.String(), "\n")
|
||||
if got := jsonbString(s); got != want {
|
||||
t.Errorf("U+%04X: got %q, want %q", r, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The catalog stores nonFilterableMetadataKeys as a JSON string; an older
|
||||
// record may still carry the array form. Both must round-trip into the index.
|
||||
func TestIndexFromRecordNonFilterableKeys(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user