refactor(pkg): canonicalize base types and adjust length handling

* Update base types to keep explicit modifier forms
* Modify length handling for vector types in tests
This commit is contained in:
2026-04-26 17:35:15 +02:00
parent 4ca1810d07
commit ed7130bba8
7 changed files with 18 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
package pgsql package pgsql
import "strings" import (
"sort"
"strings"
)
// TypeSpec describes PostgreSQL type capabilities used by parsers/writers. // TypeSpec describes PostgreSQL type capabilities used by parsers/writers.
type TypeSpec struct { type TypeSpec struct {
@@ -106,9 +109,9 @@ var postgresBaseTypes = map[string]TypeSpec{
"ltree": {}, "ltree": {},
"lquery": {}, "lquery": {},
"ltxtquery": {}, "ltxtquery": {},
"vector": {SupportsLength: true}, // pgvector: vector(dim) "vector": {}, // pgvector: keep explicit modifier form (vector(dim))
"halfvec": {SupportsLength: true}, // pgvector: halfvec(dim) "halfvec": {}, // pgvector: keep explicit modifier form (halfvec(dim))
"sparsevec": {SupportsLength: true}, // pgvector: sparsevec(dim) "sparsevec": {}, // pgvector: keep explicit modifier form (sparsevec(dim))
} }
var postgresTypeAliases = map[string]string{ var postgresTypeAliases = map[string]string{
@@ -148,6 +151,7 @@ func GetPostgresBaseTypes() []string {
for t := range postgresBaseTypes { for t := range postgresBaseTypes {
result = append(result, t) result = append(result, t)
} }
sort.Strings(result)
return result return result
} }

View File

@@ -53,7 +53,7 @@ func TestPostgresTypeRegistry_TypeParsingAndCapabilities(t *testing.T) {
wantBase: "vector", wantBase: "vector",
wantCanonicalBase: "vector", wantCanonicalBase: "vector",
wantKnown: true, wantKnown: true,
wantLength: true, wantLength: false,
}, },
{ {
input: "numeric(10,2)", input: "numeric(10,2)",

View File

@@ -711,6 +711,7 @@ func (r *Reader) parseTypeWithLength(typeStr string) (baseType string, length in
rawBaseType := strings.TrimSpace(matches[1]) rawBaseType := strings.TrimSpace(matches[1])
if pgsql.SupportsLength(rawBaseType) { if pgsql.SupportsLength(rawBaseType) {
if _, err := fmt.Sscanf(matches[2], "%d", &length); err == nil { if _, err := fmt.Sscanf(matches[2], "%d", &length); err == nil {
baseType = pgsql.CanonicalizeBaseType(rawBaseType)
return return
} }
} }

View File

@@ -367,9 +367,9 @@ func TestParseTypeWithLength_PreservesExplicitTypeModifiers(t *testing.T) {
wantType string wantType string
wantLength int wantLength int
}{ }{
{"varchar(255)", "varchar(255)", 255}, {"varchar(255)", "varchar", 255},
{"character varying(120)", "character varying(120)", 120}, {"character varying(120)", "character varying", 120},
{"vector(1536)", "vector(1536)", 1536}, {"vector(1536)", "vector(1536)", 0},
{"numeric(10,2)", "numeric(10,2)", 0}, {"numeric(10,2)", "numeric(10,2)", 0},
} }

View File

@@ -317,7 +317,7 @@ func TestConvertToColumn_PreservesExplicitTypeModifiers(t *testing.T) {
name: "custom vector modifier", name: "custom vector modifier",
fieldType: "vector(1536)", fieldType: "vector(1536)",
wantType: "vector(1536)", wantType: "vector(1536)",
wantLength: 1536, wantLength: 0,
}, },
} }

View File

@@ -804,6 +804,7 @@ func (r *Reader) parseTypeWithLength(typeStr string) (baseType string, length in
// This avoids converting custom modifiers like vector(1536) into Length. // This avoids converting custom modifiers like vector(1536) into Length.
if pgsql.SupportsLength(rawBaseType) && !strings.Contains(parens, ",") { if pgsql.SupportsLength(rawBaseType) && !strings.Contains(parens, ",") {
if _, err := fmt.Sscanf(parens, "%d", &length); err == nil { if _, err := fmt.Sscanf(parens, "%d", &length); err == nil {
baseType = pgsql.CanonicalizeBaseType(rawBaseType)
return return
} }
} }

View File

@@ -374,9 +374,9 @@ func TestParseTypeWithLength_PreservesExplicitTypeModifiers(t *testing.T) {
wantType string wantType string
wantLength int wantLength int
}{ }{
{"varchar(255)", "varchar(255)", 255}, {"varchar(255)", "varchar", 255},
{"character varying(120)", "character varying(120)", 120}, {"character varying(120)", "character varying", 120},
{"vector(1536)", "vector(1536)", 1536}, {"vector(1536)", "vector(1536)", 0},
{"numeric(10,2)", "numeric(10,2)", 0}, {"numeric(10,2)", "numeric(10,2)", 0},
} }