Skip to content

gosqlx: ExtractMetadata and Extract* return tables/columns/functions in random map order #619

Closed

Description

@ajitpratap0

Summary

gosqlx.ExtractMetadata returns Tables, Columns, Functions and their qualified variants in Go map iteration order, so repeated calls on the same AST return differently ordered slices. ExtractTables, ExtractTablesQualified, ExtractColumns, ExtractColumnsQualified and ExtractFunctions have the same problem. This makes their output unsuitable for snapshot tests, caching keys or stable CLI/JSON output.

Reproduction

tree, _ := gosqlx.Parse("SELECT a, b, c, d, e, f, g, h FROM t1 JOIN t2 ON t1.x = t2.y JOIN t3 ON t3.z = t1.x")
for i := 0; i < 20; i++ {
    m := gosqlx.ExtractMetadata(tree)
    fmt.Println(strings.Join(m.Columns, ","), strings.Join(m.Tables, ","))
}

Actual (main 61ca6c8): Columns came back in 20 distinct orders across 20 calls, for example c,h,x,a,b,d,e,f,g,y,z and then x,y,z,a,d,e,g,h,b,c,f. Tables came back as t1,t2,t3 15 times, t2,t3,t1 4 times and t3,t1,t2 once.

Expected: the same order on every call, either first-appearance order or sorted.

Notes

  • pkg/gosqlx/extract.go:557 (stringSetToSlice) and pkg/gosqlx/extract.go:568 (qualifiedSetToSlice) range over the dedup maps and never sort the result. All the Extract* functions and ExtractMetadata (extract.go:700-704) go through these helpers.
  • Simplest fix: slices.Sort the result in both helpers, with a total order on QualifiedName (Schema, Table, Name). Keeping first-appearance order would need an ordered slice next to each dedup map in metadataCollector.
  • Document the order guarantee on Metadata.

Found during review of #588, #585.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions