fix(restheadspec,resolvespec): stop casting SqlNull-wrapped and citext columns to TEXT in filters

reflect.Type.Kind() on spectypes.SqlNull[T] wrappers (SqlInt16/32/64,
SqlFloat64, SqlBool, SqlString, and embedders like SqlTimeStamp) always
reports reflect.Struct, never the wrapped T. ValidateAndAdjustFilterForColumnType
treated those as "complex" columns and forced CAST(col AS TEXT) on eq/gt/lt
filters, e.g. CAST(atdetail.rid_parent AS TEXT) = '90446096', which can't
use the index on rid_parent.

Add spectypes.UnwrapKind to see through SqlNull wrappers to the underlying
Kind, and use it in GetColumnTypeFromModel so numeric/string SqlNull columns
are recognized correctly and compared natively.

Also stop unconditionally casting to TEXT for LIKE/ILIKE and add
reflection.IsCitextColumn: citext columns are already case-insensitive, so
casting them to TEXT flips to case-sensitive matching and defeats a citext
index.
This commit is contained in:
Hein
2026-09-15 11:18:13 +02:00
parent 1885ce016b
commit 6bd6a6f164
8 changed files with 253 additions and 21 deletions
+18
View File
@@ -3,6 +3,8 @@ package reflection
import (
"reflect"
"testing"
"github.com/bitechdev/ResolveSpec/pkg/spectypes"
)
// Test models for GORM
@@ -1047,6 +1049,22 @@ func TestGetColumnTypeFromModel(t *testing.T) {
}
}
// SqlNull-wrapped columns (e.g. nullable bigint foreign keys) must report the
// wrapped value's Kind, not reflect.Struct, so numeric eq/gt/lt filters don't
// get an unnecessary CAST(... AS TEXT) that defeats the column's index.
type SqlNullFKModel struct {
RidParent spectypes.SqlInt64 `bun:"rid_parent" json:"rid_parent"`
}
func TestGetColumnTypeFromModel_SqlNullWrapper(t *testing.T) {
model := SqlNullFKModel{RidParent: spectypes.NewSqlInt64(90446096)}
result := GetColumnTypeFromModel(model, "rid_parent")
if result != reflect.Int64 {
t.Errorf("GetColumnTypeFromModel(rid_parent) = %v, want %v (SqlInt64 must unwrap to its numeric Kind)", result, reflect.Int64)
}
}
// ============= Tests for relation functions =============
// Models for relation testing