Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat: add functional unique index (#11307)

Naman Verma4fe311cb0efc
diff --git a/ee/sqlschema/postgressqlschema/provider.go b/ee/sqlschema/postgressqlschema/provider.go
index f39c02fac3..d43b0606f0 100644
--- a/ee/sqlschema/postgressqlschema/provider.go
+++ b/ee/sqlschema/postgressqlschema/provider.go
@@ -3,6 +3,7 @@ package postgressqlschema
import (
"context"
"database/sql"
+ "log/slog"
"github.com/uptrace/bun"
@@ -214,6 +215,20 @@ WHERE
}
func (provider *provider) GetIndices(ctx context.Context, name sqlschema.TableName) ([]sqlschema.Index, error) {
+ // A key is one entry an index is built on: a column (plain) or an expression
+ // like lower(x) (functional). Returns one row per key of each non-constraint-
+ // backed index on the table, ordered by position. Keys are enumerated with
+ // generate_series over indnkeyatts (count of key columns, excluding non-key
+ // INCLUDE columns), so expression keys are covered too. Reconstruction uses:
+ //
+ // index_name | index the key belongs to
+ // is_expression | true if the key is an expression, not a plain column
+ // key_def | rendered key text, e.g. org_id or lower(key)
+ // column_name | column name for plain keys; NULL for expression keys
+ // predicate | partial-index WHERE; NULL if not partial (same every row)
+ //
+ // table_name/unique/primary/column_position are carried over from the prior
+ // query (unique gates the loop, column_position orders the keys).
rows, err := provider.
sqlstore.
BunDB().
@@ -224,19 +239,21 @@ SELECT
i.indisunique AS unique,
i.indisprimary AS primary,
a.attname AS column_name,
- array_position(i.indkey, a.attnum) AS column_position,
+ n AS column_position,
+ (i.indkey[n - 1] = 0) AS is_expression,
+ pg_get_indexdef(i.indexrelid, n, true) AS key_def,
pg_get_expr(i.indpred, i.indrelid) AS predicate
FROM
pg_index i
LEFT JOIN pg_class ct ON ct.oid = i.indrelid
LEFT JOIN pg_class ci ON ci.oid = i.indexrelid
- LEFT JOIN pg_attribute a ON a.attrelid = ct.oid
LEFT JOIN pg_constraint con ON con.conindid = i.indexrelid
+ CROSS JOIN generate_series(1, i.indnkeyatts) AS n
+ LEFT JOIN pg_attribute a ON a.attrelid = ct.oid AND a.attnum = i.indkey[n - 1]
WHERE
- a.attnum = ANY(i.indkey)
- AND con.oid IS NULL
+ ct.relname = ?
AND ct.relkind = 'r'
- AND ct.relname = ?
+ AND con.oid IS NULL
ORDER BY index_name, column_position`, string(name))
if err != nil {
return nil, provider.sqlstore.WrapNotFoundErrf(err, errors.CodeNotFound, "no indices for table (%s) found", name)
@@ -251,6 +268,10 @@ ORDER BY index_name, column_position`, string(name))
type indexEntry struct {
columns []sqlschema.ColumnName
predicate *string
+ // keyDefs holds every key rendered by pg_get_indexdef, in key order; used
+ // for functional indexes where a key may be an expression like lower(x).
+ keyDefs []string
+ hasExpression bool
}
uniqueIndicesMap := make(map[string]*indexEntry)
@@ -260,30 +281,45 @@ ORDER BY index_name, column_position`, string(name))
indexName string
unique bool
primary bool
- columnName string
- // starts from 0 and is unused in this function, this is to ensure that the column names are in the correct order
+ columnName *string
+ // starts from 1 and is unused in this function, this is to ensure that the column names are in the correct order
columnPosition int
+ isExpression bool
+ keyDef string
predicate *string
)
- if err := rows.Scan(&tableName, &indexName, &unique, &primary, &columnName, &columnPosition, &predicate); err != nil {
+ if err := rows.Scan(&tableName, &indexName, &unique, &primary, &columnName, &columnPosition, &isExpression, &keyDef, &predicate); err != nil {
return nil, err
}
- if unique {
- if _, ok := uniqueIndicesMap[indexName]; !ok {
- uniqueIndicesMap[indexName] = &indexEntry{
- columns: []sqlschema.ColumnName{sqlschema.ColumnName(columnName)},
- predicate: predicate,
- }
- } else {
- uniqueIndicesMap[indexName].columns = append(uniqueIndicesMap[indexName].columns, sqlschema.ColumnName(columnName))
- }
+ if !unique {
+ continue
+ }
+
+ entry, ok := uniqueIndicesMap[indexName]
+ if !ok {
+ entry = &indexEntry{predicate: predicate}
+ uniqueIndicesMap[indexName] = entry
+ }
+
+ entry.keyDefs = append(entry.keyDefs, keyDef)
+ if isExpression {
+ entry.hasExpression = true
+ } else if columnName != nil {
+ entry.columns = append(entry.columns, sqlschema.ColumnName(*columnName))
}
}
indices := make([]sqlschema.Index, 0)
for indexName, entry := range uniqueIndicesMap {
+ // functional partial indexes aren't representable (PartialUniqueIndex has no
+ // expressions); skip rather than misrepresent.
+ if entry.hasExpression && entry.predicate != nil {
+ provider.settings.Logger().WarnContext(ctx, "skipping functional partial unique index; not representable by sqlschema", slog.String("index", indexName), slog.String("table", string(name)))
+ continue
+ }
+
if entry.predicate != nil {
index := &sqlschema.PartialUniqueIndex{
TableName: name,
@@ -291,6 +327,17 @@ ORDER BY index_name, column_position`, string(name))
Where: *entry.predicate,
}
+ if index.Name() == indexName {
+ indices = append(indices, index)
+ } else {
+ indices = append(indices, index.Named(indexName))
+ }
+ } else if entry.hasExpression {
+ index := &sqlschema.UniqueIndexWithExpressions{
+ TableName: name,
+ Expressions: entry.keyDefs,
+ }
+
if index.Name() == indexName {
indices = append(indices, index)
} else {
diff --git a/ee/sqlschema/postgressqlschema/provider_test.go b/ee/sqlschema/postgressqlschema/provider_test.go
new file mode 100644
index 0000000000..416ddd7b62
--- /dev/null
+++ b/ee/sqlschema/postgressqlschema/provider_test.go
@@ -0,0 +1,116 @@
+package postgressqlschema
+
+import (
+ "context"
+ "database/sql/driver"
+ "testing"
+
+ "github.com/DATA-DOG/go-sqlmock"
+ "github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest"
+ "github.com/SigNoz/signoz/pkg/sqlschema"
+ "github.com/SigNoz/signoz/pkg/sqlstore"
+ "github.com/SigNoz/signoz/pkg/sqlstore/sqlstoretest"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// TestGetIndices feeds GetIndices the exact rows the catalog query returns for a
+// real table (captured from devenv postgres) and checks each unique-index shape
+// is reconstructed: expression keys, partial (predicate), and plain columns.
+func TestGetIndices(t *testing.T) {
+ // column order must match the SELECT in GetIndices.
+ columns := []string{"table_name", "index_name", "unique", "primary", "column_name", "column_position", "is_expression", "key_def", "predicate"}
+
+ testCases := []struct {
+ name string
+ table sqlschema.TableName
+ rows [][]driver.Value
+ want []sqlschema.Index
+ }{
+ {
+ name: "UniqueIndexWithExpressions",
+ table: "tag",
+ rows: [][]driver.Value{
+ {"tag", "uq_tag_a36c51df", true, false, "org_id", 1, false, "org_id", nil},
+ {"tag", "uq_tag_a36c51df", true, false, "kind", 2, false, "kind", nil},
+ {"tag", "uq_tag_a36c51df", true, false, nil, 3, true, "lower(key)", nil},
+ {"tag", "uq_tag_a36c51df", true, false, nil, 4, true, "lower(value)", nil},
+ },
+ want: []sqlschema.Index{
+ &sqlschema.UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "kind", "lower(key)", "lower(value)"},
+ },
+ },
+ },
+ {
+ name: "PartialUniqueIndexes",
+ table: "service_account",
+ rows: [][]driver.Value{
+ {"service_account", "puq_service_account_email_org_id_471d6134", true, false, "email", 1, false, "email", "(status <> 'deleted'::text)"},
+ {"service_account", "puq_service_account_email_org_id_471d6134", true, false, "org_id", 2, false, "org_id", "(status <> 'deleted'::text)"},
+ {"service_account", "puq_service_account_name_org_id_471d6134", true, false, "name", 1, false, "name", "(status <> 'deleted'::text)"},
+ {"service_account", "puq_service_account_name_org_id_471d6134", true, false, "org_id", 2, false, "org_id", "(status <> 'deleted'::text)"},
+ },
+ want: []sqlschema.Index{
+ (&sqlschema.PartialUniqueIndex{
+ TableName: "service_account",
+ ColumnNames: []sqlschema.ColumnName{"email", "org_id"},
+ Where: "(status <> 'deleted'::text)",
+ }).Named("puq_service_account_email_org_id_471d6134"),
+ (&sqlschema.PartialUniqueIndex{
+ TableName: "service_account",
+ ColumnNames: []sqlschema.ColumnName{"name", "org_id"},
+ Where: "(status <> 'deleted'::text)",
+ }).Named("puq_service_account_name_org_id_471d6134"),
+ },
+ },
+ {
+ name: "PlainUniqueIndex",
+ table: "tag_relation",
+ rows: [][]driver.Value{
+ {"tag_relation", "uq_tag_relation_kind_resource_id_tag_id", true, false, "kind", 1, false, "kind", nil},
+ {"tag_relation", "uq_tag_relation_kind_resource_id_tag_id", true, false, "resource_id", 2, false, "resource_id", nil},
+ {"tag_relation", "uq_tag_relation_kind_resource_id_tag_id", true, false, "tag_id", 3, false, "tag_id", nil},
+ },
+ want: []sqlschema.Index{
+ &sqlschema.UniqueIndex{
+ TableName: "tag_relation",
+ ColumnNames: []sqlschema.ColumnName{"kind", "resource_id", "tag_id"},
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ store := sqlstoretest.New(sqlstore.Config{Provider: "postgres"}, sqlmock.QueryMatcherRegexp)
+ rows := sqlmock.NewRows(columns)
+ for _, row := range testCase.rows {
+ rows.AddRow(row...)
+ }
+ store.Mock().ExpectQuery("pg_index").WillReturnRows(rows)
+
+ schema, err := New(context.Background(), instrumentationtest.New().ToProviderSettings(), sqlschema.Config{}, store)
+ require.NoError(t, err)
+
+ indices, err := schema.GetIndices(context.Background(), testCase.table)
+ require.NoError(t, err)
+ assert.Len(t, indices, len(testCase.want))
+
+ // GetIndices iterates a map, so match by name rather than slice order.
+ got := make(map[string]sqlschema.Index, len(indices))
+ for _, index := range indices {
+ got[index.Name()] = index
+ }
+ for _, want := range testCase.want {
+ actual, ok := got[want.Name()]
+ if !assert.True(t, ok, "expected index %s not returned", want.Name()) {
+ // so that other items in the for loop can also be checked.
+ continue
+ }
+ assert.True(t, want.Equals(actual), "index %s reconstructed differently", want.Name())
+ }
+ })
+ }
+}
diff --git a/pkg/sqlschema/index.go b/pkg/sqlschema/index.go
index a64aec7be7..78cd483a4f 100644
--- a/pkg/sqlschema/index.go
+++ b/pkg/sqlschema/index.go
@@ -1,6 +1,8 @@
package sqlschema
import (
+ "fmt"
+ "hash/fnv"
"slices"
"strings"
@@ -136,6 +138,121 @@ func (index *UniqueIndex) ToDropSQL(fmter SQLFormatter) []byte {
return sql
}
+// UniqueIndexWithExpressions is a functional unique index: each key is a SQL
+// expression (e.g. LOWER(col)) emitted verbatim, so the caller owns its
+// well-formedness; plain columns go in as bare identifiers. The auto-generated
+// name uses a hash suffix (uq_t_<hash>) since expressions aren't valid
+// identifier fragments.
+//
+// Use this only when at least one key is a real expression: an all-identifier
+// key list reconstructs as a plain UniqueIndex on read-back and won't compare
+// equal — use UniqueIndex instead.
+type UniqueIndexWithExpressions struct {
+ TableName TableName
+ Expressions []string
+ name string
+}
+
+func (index *UniqueIndexWithExpressions) Name() string {
+ if index.name != "" {
+ return index.name
+ }
+
+ var b strings.Builder
+ b.WriteString(IndexTypeUnique.String())
+ b.WriteString("_")
+ b.WriteString(string(index.TableName))
+ b.WriteString("_")
+ hasher := fnv.New32a()
+ _, _ = hasher.Write([]byte(strings.Join(normalizeExpressions(index.Expressions), "\x00")))
+ fmt.Fprintf(&b, "%08x", hasher.Sum32())
+ return b.String()
+}
+
+func (index *UniqueIndexWithExpressions) Named(name string) Index {
+ copyOfExpressions := make([]string, len(index.Expressions))
+ copy(copyOfExpressions, index.Expressions)
+
+ return &UniqueIndexWithExpressions{
+ TableName: index.TableName,
+ Expressions: copyOfExpressions,
+ name: name,
+ }
+}
+
+func (index *UniqueIndexWithExpressions) IsNamed() bool {
+ return index.name != ""
+}
+
+func (*UniqueIndexWithExpressions) Type() IndexType {
+ return IndexTypeUnique
+}
+
+// Columns is nil: a functional index exposes no plain key columns.
+func (*UniqueIndexWithExpressions) Columns() []ColumnName {
+ return nil
+}
+
+func (index *UniqueIndexWithExpressions) Equals(other Index) bool {
+ otherIndex, ok := other.(*UniqueIndexWithExpressions)
+ if !ok {
+ return false
+ }
+
+ // Expressions are compared normalized so a declared `LOWER(x)` matches the
+ // `lower(x)` a backend renders on read-back.
+ return index.Name() == other.Name() && slices.Equal(normalizeExpressions(index.Expressions), normalizeExpressions(otherIndex.Expressions))
+}
+
+// normalizeExpressions canonicalizes each Expressions entry (case, whitespace,
+// redundant parens, identifier quoting) so a declared LOWER(x) matches the
+// lower(x) a backend renders on read-back. Reuses expressionNormalizer: an index key
+// is the same SQL grammar as a WHERE predicate. It does not handle opclass,
+// COLLATE, or ASC/DESC/NULLS suffixes: postgres strips them on read-back
+// (pg_get_indexdef), while sqlite keeps them verbatim, so an index declared with
+// one round-trips on sqlite but not on postgres.
+func normalizeExpressions(expressions []string) []string {
+ if len(expressions) == 0 {
+ return nil
+ }
+
+ normalized := make([]string, len(expressions))
+ for i, expression := range expressions {
+ normalized[i] = (&expressionNormalizer{input: expression, foldCase: true}).normalize()
+ }
+ return normalized
+}
+
+func (index *UniqueIndexWithExpressions) ToCreateSQL(fmter SQLFormatter) []byte {
+ sql := []byte{}
+
+ sql = append(sql, "CREATE UNIQUE INDEX IF NOT EXISTS "...)
+ sql = fmter.AppendIdent(sql, index.Name())
+ sql = append(sql, " ON "...)
+ sql = fmter.AppendIdent(sql, string(index.TableName))
+ sql = append(sql, " ("...)
+
+ for i, expr := range index.Expressions {
+ if i > 0 {
+ sql = append(sql, ", "...)
+ }
+ sql = append(sql, expr...)
+ }
+
+ sql = append(sql, ")"...)
+
+ return sql
+}
+
+func (index *UniqueIndexWithExpressions) ToDropSQL(fmter SQLFormatter) []byte {
+ sql := []byte{}
+
+ sql = append(sql, "DROP INDEX IF EXISTS "...)
+ sql = fmter.AppendIdent(sql, index.Name())
+
+ return sql
+}
+
type PartialUniqueIndex struct {
TableName TableName
ColumnNames []ColumnName
@@ -160,7 +277,7 @@ func (index *PartialUniqueIndex) Name() string {
b.WriteString(string(column))
}
b.WriteString("_")
- b.WriteString((&whereNormalizer{input: index.Where}).hash())
+ b.WriteString((&expressionNormalizer{input: index.Where}).hash())
return b.String()
}
@@ -198,7 +315,7 @@ func (index *PartialUniqueIndex) Equals(other Index) bool {
return false
}
- return index.Name() == other.Name() && slices.Equal(index.Columns(), other.Columns()) && (&whereNormalizer{input: index.Where}).normalize() == (&whereNormalizer{input: otherPartial.Where}).normalize()
+ return index.Name() == other.Name() && slices.Equal(index.Columns(), other.Columns()) && (&expressionNormalizer{input: index.Where}).normalize() == (&expressionNormalizer{input: otherPartial.Where}).normalize()
}
func (index *PartialUniqueIndex) ToCreateSQL(fmter SQLFormatter) []byte {
diff --git a/pkg/sqlschema/index_test.go b/pkg/sqlschema/index_test.go
index e6d1f870a1..2f8d31c702 100644
--- a/pkg/sqlschema/index_test.go
+++ b/pkg/sqlschema/index_test.go
@@ -38,6 +38,39 @@ func TestIndexToCreateSQL(t *testing.T) {
},
sql: `CREATE UNIQUE INDEX IF NOT EXISTS "my_index" ON "users" ("id", "name", "email")`,
},
+ {
+ name: "Unique_Functional_SingleExpression",
+ index: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "uq_users_72852951" ON "users" (LOWER(email))`,
+ },
+ {
+ name: "Unique_Functional_MixedColumnsAndExpressions",
+ index: &UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "kind", "LOWER(key)", "LOWER(value)"},
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "uq_tag_a36c51df" ON "tag" (org_id, kind, LOWER(key), LOWER(value))`,
+ },
+ {
+ name: "Unique_Functional_ComplexExpression",
+ index: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(TRIM(first_name) || ' ' || TRIM(last_name))"},
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "uq_users_37e845f3" ON "users" (LOWER(TRIM(first_name) || ' ' || TRIM(last_name)))`,
+ },
+ {
+ name: "Unique_Functional_Named",
+ index: &UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "kind", "LOWER(key)", "LOWER(value)"},
+ name: "uq_tag_org_kind_lower_key_lower_value",
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "uq_tag_org_kind_lower_key_lower_value" ON "tag" (org_id, kind, LOWER(key), LOWER(value))`,
+ },
{
name: "PartialUnique_1Column",
index: &PartialUniqueIndex{
@@ -229,6 +262,90 @@ func TestIndexEquals(t *testing.T) {
},
equals: false,
},
+ {
+ name: "Unique_Functional_Same",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ equals: true,
+ },
+ {
+ name: "Unique_Functional_CaseInsensitiveEqual",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"lower(email)"},
+ },
+ equals: true,
+ },
+ {
+ name: "Unique_Functional_QuotedSimpleIdentifierEqualsUnquoted",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{`LOWER("email")`},
+ },
+ equals: true,
+ },
+ {
+ name: "Unique_Functional_UnquotedMixedCaseEqualsLower",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(Email)"},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ equals: true,
+ },
+ {
+ name: "Unique_Functional_QuotedMixedCaseNotEqualUnquoted",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{`LOWER("Email")`},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ equals: false,
+ },
+ {
+ name: "Unique_Functional_DifferentExpressions",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ b: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"UPPER(email)"},
+ },
+ equals: false,
+ },
+ {
+ name: "Unique_Functional_NotEqualToPlainSameColumns",
+ a: &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ },
+ b: &UniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ },
+ equals: false,
+ },
}
for _, testCase := range testCases {
@@ -238,6 +355,138 @@ func TestIndexEquals(t *testing.T) {
}
}
+func TestUniqueIndexFunctionalName(t *testing.T) {
+ t.Run("autogen uses uq_<table>_<hash>", func(t *testing.T) {
+ idx := &UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "kind", "LOWER(key)", "LOWER(value)"},
+ }
+ assert.Equal(t, "uq_tag_a36c51df", idx.Name())
+ })
+
+ t.Run("same expressions produce the same name", func(t *testing.T) {
+ a := &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ }
+ b := &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ }
+ assert.Equal(t, a.Name(), b.Name())
+ })
+
+ t.Run("different expressions produce different names", func(t *testing.T) {
+ a := &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ }
+ b := &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"UPPER(email)"},
+ }
+ assert.NotEqual(t, a.Name(), b.Name())
+ })
+
+ t.Run("expressions in different order produce different names", func(t *testing.T) {
+ a := &UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "LOWER(key)"},
+ }
+ b := &UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"LOWER(key)", "org_id"},
+ }
+ assert.NotEqual(t, a.Name(), b.Name())
+ })
+
+ t.Run("functional autogen differs from plain autogen for same columns", func(t *testing.T) {
+ plain := &UniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ }
+ functional := &UniqueIndexWithExpressions{
+ TableName: "users",
+ Expressions: []string{"LOWER(email)"},
+ }
+ assert.Equal(t, "uq_users_email", plain.Name())
+ assert.NotEqual(t, plain.Name(), functional.Name())
+ })
+
+ t.Run("Named() override wins over hash", func(t *testing.T) {
+ idx := (&UniqueIndexWithExpressions{
+ TableName: "tag",
+ Expressions: []string{"org_id", "LOWER(key)"},
+ }).Named("my_functional_index")
+ assert.Equal(t, "my_functional_index", idx.Name())
+ })
+}
+
+func TestNormalizeExpressions(t *testing.T) {
+ testCases := []struct {
+ name string
+ expressions []string
+ output []string
+ }{
+ {
+ name: "Empty",
+ expressions: nil,
+ output: nil,
+ },
+ {
+ name: "PlainColumnsUnchanged",
+ expressions: []string{"org_id", "kind"},
+ output: []string{"org_id", "kind"},
+ },
+ {
+ name: "FunctionNameCaseFolded",
+ expressions: []string{"LOWER(key)", "LOWER(value)"},
+ output: []string{"lower(key)", "lower(value)"},
+ },
+ {
+ name: "PostgresRenderedMatchesDeclared",
+ expressions: []string{"lower(key)"},
+ output: []string{"lower(key)"},
+ },
+ {
+ name: "WhitespaceCollapsedPerExpression",
+ expressions: []string{"LOWER( key )", "org_id"},
+ output: []string{"lower( key )", "org_id"},
+ },
+ {
+ name: "RedundantOuterParenthesesStripped",
+ expressions: []string{"(LOWER(key))"},
+ output: []string{"lower(key)"},
+ },
+ {
+ name: "QuotedSimpleIdentifierUnquoted",
+ expressions: []string{`LOWER("email")`},
+ output: []string{"lower(email)"},
+ },
+ {
+ name: "QuotedIdentifierCaseByIdentifierPreserved",
+ expressions: []string{`LOWER("Key")`},
+ output: []string{`lower("Key")`},
+ },
+ {
+ name: "StringLiteralCasePreserved",
+ expressions: []string{"COALESCE(status, 'Deleted')"},
+ output: []string{"coalesce(status, 'Deleted')"},
+ },
+ {
+ name: "NormalizedIndependentlyPerElement",
+ expressions: []string{"UPPER(a)", "b", "LOWER(c)"},
+ output: []string{"upper(a)", "b", "lower(c)"},
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.output, normalizeExpressions(testCase.expressions))
+ })
+ }
+}
+
func TestPartialUniqueIndexName(t *testing.T) {
a := &PartialUniqueIndex{
TableName: "users",
diff --git a/pkg/sqlschema/normalizer.go b/pkg/sqlschema/normalizer.go
index e52ed23b21..9da70c1447 100644
--- a/pkg/sqlschema/normalizer.go
+++ b/pkg/sqlschema/normalizer.go
@@ -6,25 +6,26 @@ import (
"strings"
)
-type whereNormalizer struct {
- input string
+type expressionNormalizer struct {
+ input string
+ foldCase bool
}
-func (n *whereNormalizer) hash() string {
+func (n *expressionNormalizer) hash() string {
hasher := fnv.New32a()
_, _ = hasher.Write([]byte(n.normalize()))
return fmt.Sprintf("%08x", hasher.Sum32())
}
-func (n *whereNormalizer) normalize() string {
- where := strings.TrimSpace(n.input)
- where = n.stripOuterParentheses(where)
+func (n *expressionNormalizer) normalize() string {
+ expr := strings.TrimSpace(n.input)
+ expr = n.stripOuterParentheses(expr)
var output strings.Builder
- output.Grow(len(where))
+ output.Grow(len(expr))
- for i := 0; i < len(where); i++ {
- switch where[i] {
+ for i := 0; i < len(expr); i++ {
+ switch expr[i] {
case ' ', '\t', '\n', '\r':
if output.Len() > 0 {
last := output.String()[output.Len()-1]
@@ -33,21 +34,25 @@ func (n *whereNormalizer) normalize() string {
}
}
case '\'':
- end := n.consumeSingleQuotedLiteral(where, i, &output)
+ end := n.consumeSingleQuotedLiteral(expr, i, &output)
i = end
case '"':
- token, end := n.consumeDoubleQuotedToken(where, i)
+ token, end := n.consumeDoubleQuotedToken(expr, i)
output.WriteString(token)
i = end
default:
- output.WriteByte(where[i])
+ c := expr[i]
+ if n.foldCase && c >= 'A' && c <= 'Z' {
+ c += 'a' - 'A'
+ }
+ output.WriteByte(c)
}
}
return strings.TrimSpace(output.String())
}
-func (n *whereNormalizer) stripOuterParentheses(s string) string {
+func (n *expressionNormalizer) stripOuterParentheses(s string) string {
for {
s = strings.TrimSpace(s)
if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' || !n.hasWrappingParentheses(s) {
@@ -57,7 +62,7 @@ func (n *whereNormalizer) stripOuterParentheses(s string) string {
}
}
-func (n *whereNormalizer) hasWrappingParentheses(s string) bool {
+func (n *expressionNormalizer) hasWrappingParentheses(s string) bool {
depth := 0
inSingleQuotedLiteral := false
inDoubleQuotedToken := false
@@ -101,7 +106,7 @@ func (n *whereNormalizer) hasWrappingParentheses(s string) bool {
return depth == 0
}
-func (n *whereNormalizer) consumeSingleQuotedLiteral(s string, start int, output *strings.Builder) int {
+func (n *expressionNormalizer) consumeSingleQuotedLiteral(s string, start int, output *strings.Builder) int {
output.WriteByte(s[start])
for i := start + 1; i < len(s); i++ {
output.WriteByte(s[i])
@@ -118,7 +123,7 @@ func (n *whereNormalizer) consumeSingleQuotedLiteral(s string, start int, output
return len(s) - 1
}
-func (n *whereNormalizer) consumeDoubleQuotedToken(s string, start int) (string, int) {
+func (n *expressionNormalizer) consumeDoubleQuotedToken(s string, start int) (string, int) {
var ident strings.Builder
for i := start + 1; i < len(s); i++ {
@@ -142,7 +147,7 @@ func (n *whereNormalizer) consumeDoubleQuotedToken(s string, start int) (string,
return s[start:], len(s) - 1
}
-func (n *whereNormalizer) isSimpleUnquotedIdentifier(s string) bool {
+func (n *expressionNormalizer) isSimpleUnquotedIdentifier(s string) bool {
if s == "" || strings.ToLower(s) != s {
return false
}
diff --git a/pkg/sqlschema/normalizer_test.go b/pkg/sqlschema/normalizer_test.go
index d35cb7fa4f..5e92e2fe7b 100644
--- a/pkg/sqlschema/normalizer_test.go
+++ b/pkg/sqlschema/normalizer_test.go
@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestWhereNormalizerNormalize(t *testing.T) {
+func TestExpressionNormalizerNormalize(t *testing.T) {
testCases := []struct {
name string
input string
@@ -47,11 +47,16 @@ func TestWhereNormalizerNormalize(t *testing.T) {
input: `NOT ("deleted_at" IS NOT NULL AND ("active" = false OR "status" = 'archived'))`,
output: `NOT (deleted_at IS NOT NULL AND (active = false OR status = 'archived'))`,
},
+ {
+ name: "CaseNotFoldedWithoutFlag",
+ input: `LOWER("email") IS NOT NULL`,
+ output: `LOWER(email) IS NOT NULL`,
+ },
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
- assert.Equal(t, testCase.output, (&whereNormalizer{input: testCase.input}).normalize())
+ assert.Equal(t, testCase.output, (&expressionNormalizer{input: testCase.input}).normalize())
})
}
}
diff --git a/pkg/sqlschema/sqlitesqlschema/provider.go b/pkg/sqlschema/sqlitesqlschema/provider.go
index c70b442f9d..a72ad58906 100644
--- a/pkg/sqlschema/sqlitesqlschema/provider.go
+++ b/pkg/sqlschema/sqlitesqlschema/provider.go
@@ -2,6 +2,7 @@ package sqlitesqlschema
import (
"context"
+ "log/slog"
"strconv"
"strings"
@@ -92,7 +93,6 @@ func (provider *provider) GetIndices(ctx context.Context, tableName sqlschema.Ta
unique bool
origin string
partial bool
- columns []sqlschema.ColumnName
)
if err := rows.Scan(&seq, &name, &unique, &origin, &partial); err != nil {
return nil, err
@@ -108,53 +108,103 @@ func (provider *provider) GetIndices(ctx context.Context, tableName sqlschema.Ta
continue
}
- if err := provider.
- sqlstore.
- BunDB().
- NewRaw("SELECT name FROM PRAGMA_index_info(?)", string(name)).
- Scan(ctx, &columns); err != nil {
+ if !unique {
+ continue
+ }
+
+ columns, hasExpression, err := provider.indexKeys(ctx, name)
+ if err != nil {
return nil, err
}
- if unique && partial {
- var indexSQL string
+ // SQLite exposes expression keys and the predicate only in the DDL.
+ var ddl string
+ if hasExpression || partial {
if err := provider.
sqlstore.
BunDB().
NewRaw("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?", name).
- Scan(ctx, &indexSQL); err != nil {
+ Scan(ctx, &ddl); err != nil {
return nil, err
}
+ }
- where := extractWhereClause(indexSQL)
- index := &sqlschema.PartialUniqueIndex{
+ // functional partial indexes aren't representable (PartialUniqueIndex has no
+ // expressions); skip rather than misrepresent.
+ if hasExpression && partial {
+ provider.settings.Logger().WarnContext(ctx, "skipping functional partial unique index; not representable by sqlschema", slog.String("index", name), slog.String("table", string(tableName)))
+ continue
+ }
+
+ var index sqlschema.Index
+ if partial {
+ index = &sqlschema.PartialUniqueIndex{
TableName: tableName,
ColumnNames: columns,
- Where: where,
+ Where: extractWhereClause(ddl),
}
-
- if index.Name() == name {
- indices = append(indices, index)
- } else {
- indices = append(indices, index.Named(name))
+ } else if hasExpression {
+ index = &sqlschema.UniqueIndexWithExpressions{
+ TableName: tableName,
+ Expressions: extractIndexColumns(ddl),
}
- } else if unique {
- index := &sqlschema.UniqueIndex{
+ } else {
+ index = &sqlschema.UniqueIndex{
TableName: tableName,
ColumnNames: columns,
}
+ }
- if index.Name() == name {
- indices = append(indices, index)
- } else {
- indices = append(indices, index.Named(name))
- }
+ if index.Name() != name {
+ index = index.Named(name)
}
+ indices = append(indices, index)
}
return indices, nil
}
+// indexKeys returns an index's plain key columns and whether any key is an
+// expression (SQLite marks expression keys with cid = -2 and a NULL name).
+func (provider *provider) indexKeys(ctx context.Context, name string) ([]sqlschema.ColumnName, bool, error) {
+ rows, err := provider.
+ sqlstore.
+ BunDB().
+ QueryContext(ctx, "SELECT cid, name FROM PRAGMA_index_info(?)", name)
+ if err != nil {
+ return nil, false, err
+ }
+ defer func() {
+ if err := rows.Close(); err != nil {
+ provider.settings.Logger().ErrorContext(ctx, "error closing rows", errors.Attr(err))
+ }
+ }()
+
+ var (
+ columns []sqlschema.ColumnName
+ hasExpression bool
+ )
+ for rows.Next() {
+ var (
+ cid int
+ columnName *string
+ )
+ if err := rows.Scan(&cid, &columnName); err != nil {
+ return nil, false, err
+ }
+
+ if cid == -2 {
+ hasExpression = true
+ continue
+ }
+ if columnName != nil {
+ columns = append(columns, sqlschema.ColumnName(*columnName))
+ }
+ }
+
+ return columns, hasExpression, nil
+}
+
func (provider *provider) ToggleFKEnforcement(ctx context.Context, db bun.IDB, on bool) error {
_, err := db.ExecContext(ctx, "PRAGMA foreign_keys = ?", on)
if err != nil {
@@ -173,6 +223,92 @@ func (provider *provider) ToggleFKEnforcement(ctx context.Context, db bun.IDB, o
return errors.NewInternalf(errors.CodeInternal, "foreign_keys(actual: %s, expected: %s), maybe a transaction is in progress?", strconv.FormatBool(val), strconv.FormatBool(on))
}
+// extractIndexColumns returns an index's key entries: the comma-separated items
+// in the first top-level parenthesised list after ON, each verbatim (trimmed).
+// Quotes and nested parens are respected so their commas don't split keys.
+// SQLite reports expression keys with a NULL name in PRAGMA_index_info, so their
+// text is only recoverable from the DDL.
+func extractIndexColumns(sql string) []string {
+ var (
+ inSingleQuotedLiteral bool
+ inDoubleQuotedIdentifier bool
+ inBacktickQuotedIdentifier bool
+ inBracketQuotedIdentifier bool
+ depth int
+ started bool
+ keyStart int
+ columns []string
+ )
+
+ for i := 0; i < len(sql); i++ {
+ switch sql[i] {
+ case '\'':
+ if inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ break
+ }
+ if inSingleQuotedLiteral && i+1 < len(sql) && sql[i+1] == '\'' {
+ i++
+ continue
+ }
+ inSingleQuotedLiteral = !inSingleQuotedLiteral
+ continue
+ case '"':
+ if inSingleQuotedLiteral || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ break
+ }
+ if inDoubleQuotedIdentifier && i+1 < len(sql) && sql[i+1] == '"' {
+ i++
+ continue
+ }
+ inDoubleQuotedIdentifier = !inDoubleQuotedIdentifier
+ continue
+ case '`':
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBracketQuotedIdentifier {
+ break
+ }
+ inBacktickQuotedIdentifier = !inBacktickQuotedIdentifier
+ continue
+ case '[':
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ break
+ }
+ inBracketQuotedIdentifier = true
+ continue
+ case ']':
+ if inBracketQuotedIdentifier {
+ inBracketQuotedIdentifier = false
+ }
+ continue
+ }
+
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+
+ switch sql[i] {
+ case '(':
+ depth++
+ if depth == 1 && !started {
+ started = true
+ keyStart = i + 1
+ }
+ case ')':
+ depth--
+ if depth == 0 && started {
+ columns = append(columns, strings.TrimSpace(sql[keyStart:i]))
+ return columns
+ }
+ case ',':
+ if depth == 1 {
+ columns = append(columns, strings.TrimSpace(sql[keyStart:i]))
+ keyStart = i + 1
+ }
+ }
+ }
+
+ return columns
+}
+
func extractWhereClause(sql string) string {
lastWhere := -1
inSingleQuotedLiteral := false
diff --git a/pkg/sqlschema/sqlitesqlschema/provider_test.go b/pkg/sqlschema/sqlitesqlschema/provider_test.go
index d95df855a5..bf9f346e72 100644
--- a/pkg/sqlschema/sqlitesqlschema/provider_test.go
+++ b/pkg/sqlschema/sqlitesqlschema/provider_test.go
@@ -50,3 +50,93 @@ func TestExtractWhereClause(t *testing.T) {
})
}
}
+
+func TestExtractIndexColumns(t *testing.T) {
+ testCases := []struct {
+ name string
+ sql string
+ columns []string
+ }{
+ {
+ name: "SingleColumn",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" ("email")`,
+ columns: []string{`"email"`},
+ },
+ {
+ name: "SingleExpression",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" (LOWER(email))`,
+ columns: []string{"LOWER(email)"},
+ },
+ {
+ name: "MixedColumnsAndExpressions",
+ sql: `CREATE UNIQUE INDEX "idx" ON "tag" (org_id, kind, LOWER(key), LOWER(value))`,
+ columns: []string{"org_id", "kind", "LOWER(key)", "LOWER(value)"},
+ },
+ {
+ name: "CommaInsideStringLiteralNotSplit",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" (LOWER(TRIM(first_name) || ', ' || last_name))`,
+ columns: []string{"LOWER(TRIM(first_name) || ', ' || last_name)"},
+ },
+ {
+ name: "CommaInsideQuotedIdentifierNotSplit",
+ sql: `CREATE UNIQUE INDEX "idx" ON "t" ("weird,col", LOWER(x))`,
+ columns: []string{`"weird,col"`, "LOWER(x)"},
+ },
+ {
+ name: "WhereClauseAfterKeyListIgnored",
+ sql: `CREATE UNIQUE INDEX "idx" ON "tag" (LOWER(key)) WHERE deleted = 0`,
+ columns: []string{"LOWER(key)"},
+ },
+ {
+ name: "WhereClauseWithParensAfterKeyListIgnored",
+ sql: `CREATE UNIQUE INDEX "idx" ON "tag" (LOWER(key)) WHERE (deleted = 0 AND kind = 'a')`,
+ columns: []string{"LOWER(key)"},
+ },
+ {
+ name: "CommaInsideBacktickIdentifierNotSplit",
+ sql: "CREATE UNIQUE INDEX `idx` ON `t` (`weird,col`, x)",
+ columns: []string{"`weird,col`", "x"},
+ },
+ {
+ name: "CommaInsideBracketIdentifierNotSplit",
+ sql: `CREATE UNIQUE INDEX [idx] ON [t] ([weird,col], x)`,
+ columns: []string{"[weird,col]", "x"},
+ },
+ {
+ name: "EscapedDoubleQuoteInIdentifier",
+ sql: `CREATE UNIQUE INDEX "idx" ON "t" ("a""b", x)`,
+ columns: []string{`"a""b"`, "x"},
+ },
+ {
+ name: "EscapedSingleQuoteInLiteral",
+ sql: `CREATE UNIQUE INDEX "idx" ON "t" (COALESCE(x, 'it''s, ok'))`,
+ columns: []string{"COALESCE(x, 'it''s, ok')"},
+ },
+ {
+ name: "NestedFunctionCallCommasNotSplit",
+ sql: `CREATE UNIQUE INDEX "idx" ON "t" (COALESCE(a, b, c), d)`,
+ columns: []string{"COALESCE(a, b, c)", "d"},
+ },
+ {
+ name: "NewlinesAndTabsTrimmed",
+ sql: "CREATE UNIQUE INDEX \"idx\" ON \"t\" (\n\tLOWER(key),\n\torg_id\n)",
+ columns: []string{"LOWER(key)", "org_id"},
+ },
+ {
+ name: "CollateAndOrderSuffixKeptVerbatim",
+ sql: `CREATE UNIQUE INDEX "idx" ON "t" (LOWER(key) COLLATE NOCASE, value DESC)`,
+ columns: []string{"LOWER(key) COLLATE NOCASE", "value DESC"},
+ },
+ {
+ name: "NoKeyListReturnsNil",
+ sql: `CREATE INDEX "idx" ON "t"`,
+ columns: nil,
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.columns, extractIndexColumns(testCase.sql))
+ })
+ }
+}