orderby_nulls_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package executor
  2. import "testing"
  3. func TestOrderByNullsFirstLast(t *testing.T) {
  4. _, schema, table := newTestDB(t)
  5. e := newExec(schema, table)
  6. execMust(t, e, "CREATE TABLE t (id INTEGER PRIMARY KEY, pos INTEGER)")
  7. execMust(t, e, "INSERT INTO t VALUES (1, NULL)")
  8. execMust(t, e, "INSERT INTO t VALUES (2, 5)")
  9. execMust(t, e, "INSERT INTO t VALUES (3, 1)")
  10. execMust(t, e, "INSERT INTO t VALUES (4, NULL)")
  11. ids := func(res *Result) []int64 {
  12. out := make([]int64, len(res.Rows))
  13. for i, row := range res.Rows {
  14. out[i] = row[0].(int64)
  15. }
  16. return out
  17. }
  18. got := ids(execMust(t, e, "SELECT id FROM t ORDER BY pos ASC NULLS LAST, id ASC"))
  19. want := []int64{3, 2, 1, 4}
  20. for i := range want {
  21. if got[i] != want[i] {
  22. t.Fatalf("NULLS LAST order = %v, want %v", got, want)
  23. }
  24. }
  25. got = ids(execMust(t, e, "SELECT id FROM t ORDER BY pos ASC NULLS FIRST, id ASC"))
  26. want = []int64{1, 4, 3, 2}
  27. for i := range want {
  28. if got[i] != want[i] {
  29. t.Fatalf("NULLS FIRST order = %v, want %v", got, want)
  30. }
  31. }
  32. // Default SQLite ordering: NULLs are smallest.
  33. got = ids(execMust(t, e, "SELECT id FROM t ORDER BY pos ASC, id ASC"))
  34. want = []int64{1, 4, 3, 2}
  35. for i := range want {
  36. if got[i] != want[i] {
  37. t.Fatalf("default ASC order = %v, want %v", got, want)
  38. }
  39. }
  40. }