Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

INSERT column sorting is broken #41

@benpaxton-hf

Description

@benpaxton-hf

395bcf3 adds sorting to the builder.Insert function, but sorts incorrectly, resulting in mangled column/value mapping (e.g. an Insert(builder.Eq{"y":0, "x":1}) may result in INSERT INTO foo (x, y) VALUES (0, 1) instead of the correct (1, 0)).

builder/builder.go

Lines 278 to 281 in ef80bec

sort.Slice(b.insertVals, func(i, j int) bool {
return b.insertCols[i] < b.insertCols[j]
})
sort.Strings(b.insertCols)

Due to how the "sort" package works, Swap() and Less() are called interleaved - using sort.Slice() where the Less() function uses the original order of the slice won't work - it needs to use the current order.

See https://play.golang.org/p/ITcqKVBvdbW for an example of how this goes wrong - both keys and values are in the same order to start with, but due to Less() using the original order, the sorting puts the values slice in a semi-random order dependent on the original order of the keys slice. Uncommenting line 26, i.e. keeping the keys and values slices in the same order as the sorting algorithm progresses, fixes the sort (and makes the sort.Ints() call redundant as a bonus!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions