Files
seaweedfs/weed/filer/postgres/postgres_sql_gen_test.go
T
Chris Lu a5ccefb661 filer: escape LIKE wildcards in SQL list prefixes (#11757)
* filer: escape LIKE wildcards in SQL list prefixes

ListDirectoryPrefixedEntries bound prefix+"%" straight into a LIKE
clause, so '_' and '%' in object names (e.g. Langfuse keys like
call_03_...) acted as wildcards: wrong results and a ~40x slower
range scan because (dirhash, name) can no longer be narrowed.

Escape '!'/'%'/'_' in the prefix and declare ESCAPE '!' in the
generated list SQL so every abstract_sql backend (mysql, mysql2,
postgres, postgres2, sqlite) matches literally with identical
semantics. '!' is used instead of '\' because SQLite shares
SqlGenMysql but would reject the MySQL-style '\\' literal, and
'\' would still need escaping itself.

* filer: escape LIKE wildcards in YDB list prefixes

Same wildcard-in-prefix bug as the abstract_sql backends: YQL LIKE
treated '_' and '%' in the prefix as wildcards. Apply the shared
LikePattern helper and declare ESCAPE '!' (YQL allows any escape
char except '%', '_' and '\').

* test: cover literal '_'/'%'/'!' prefixes in sqlite filer listing

End-to-end regression test through SqliteStore against a real
database; runs in CI under the sqlite build tag.
2026-10-15 16:51:15 -07:00

104 lines
3.2 KiB
Go

package postgres
import (
"strings"
"testing"
)
func TestDefaultUpsertQueryIsConflictSafe(t *testing.T) {
gen := &SqlGenPostgres{UpsertQueryTemplate: DefaultUpsertQuery}
got := gen.GetSqlInsert("filemeta")
if !strings.Contains(got, "ON CONFLICT") {
t.Fatalf("expected ON CONFLICT in default upsert, got: %s", got)
}
if !strings.Contains(got, `"filemeta"`) {
t.Fatalf("expected quoted table name, got: %s", got)
}
}
func TestDefaultCreateTableQueryRendersValidSql(t *testing.T) {
gen := &SqlGenPostgres{CreateTableSqlTemplate: DefaultCreateTableQuery}
got := gen.GetSqlCreateTable("filemeta")
if strings.Contains(got, "%!") {
t.Fatalf("template rendered to malformed SQL, got: %s", got)
}
if !strings.Contains(got, `"filemeta"`) {
t.Fatalf("expected quoted table name, got: %s", got)
}
}
func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
gen := &SqlGenPostgres{}
got := gen.GetSqlInsert("filemeta")
if strings.Contains(got, "ON CONFLICT") {
t.Fatalf("plain INSERT path should not contain ON CONFLICT, got: %s", got)
}
}
func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
gen := &SqlGenPostgres{}
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
if strings.Contains(got, "COLLATE") {
t.Fatalf("default list query should not force a collation, got: %s", got)
}
if !strings.Contains(got, "ORDER BY name ASC") {
t.Fatalf("expected plain name ordering, got: %s", got)
}
}
}
func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
gen := &SqlGenPostgres{ForceBinaryCollation: true}
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
if !strings.Contains(got, `ORDER BY name COLLATE "C" ASC`) {
t.Fatalf(`expected COLLATE "C" ordering, got: %s`, got)
}
if !strings.Contains(got, `name COLLATE "C" like $4`) {
t.Fatalf(`expected COLLATE "C" prefix filter, got: %s`, got)
}
if strings.Contains(got, "AND name>$2") || strings.Contains(got, "AND name>=$2") {
t.Fatalf("pagination comparison must also be byte-ordered, got: %s", got)
}
}
}
func TestListSqlEscapesLikeWildcards(t *testing.T) {
gen := &SqlGenPostgres{}
for _, got := range []string{gen.GetSqlListExclusive("filemeta"), gen.GetSqlListInclusive("filemeta")} {
if !strings.Contains(got, "like $4 ESCAPE '!'") {
t.Fatalf("list query must declare the escape char matching LikePattern, got: %s", got)
}
}
}
func TestIsByteOrderedCollation(t *testing.T) {
byteOrdered := []string{"C", "POSIX", "c", "C.UTF-8", "C.utf8"}
for _, c := range byteOrdered {
if !isByteOrderedCollation(c) {
t.Fatalf("expected %q to be treated as byte-ordered", c)
}
}
locale := []string{"en_US.UTF-8", "en_US.utf8", "en-US-x-icu", "und-x-icu", ""}
for _, c := range locale {
if isByteOrderedCollation(c) {
t.Fatalf("expected %q to be treated as locale-aware", c)
}
}
}
func TestResolveCreateTableQuery(t *testing.T) {
cases := []struct {
in, want string
}{
{"", ""},
{"true", DefaultCreateTableQuery},
{"false", ""},
{"CREATE TABLE custom", "CREATE TABLE custom"},
}
for _, c := range cases {
if got := ResolveCreateTableQuery(c.in); got != c.want {
t.Fatalf("ResolveCreateTableQuery(%q) = %q, want %q", c.in, got, c.want)
}
}
}