2
0

rowid_precedence_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package executor
  2. import "testing"
  3. func TestRowIDAliasPrecedenceRealOidColumn(t *testing.T) {
  4. _, schema, table := newTestDB(t)
  5. e := newExec(schema, table)
  6. // id is the implicit integer PK (the rowid); oid is a real TEXT column.
  7. execMust(t, e, "CREATE TABLE lfs_object (id INTEGER PRIMARY KEY, oid TEXT, size INTEGER)")
  8. execMust(t, e, "INSERT INTO lfs_object VALUES (1, 'ef79c8f0', 1234)")
  9. // Projection: oid must be the real string, not the hidden rowid.
  10. res := execMust(t, e, "SELECT oid FROM lfs_object")
  11. if res.Rows[0][0] != "ef79c8f0" {
  12. t.Fatalf("SELECT oid = %v (%T), want the real string", res.Rows[0][0], res.Rows[0][0])
  13. }
  14. // Predicate: WHERE oid = '...' must match the real string column.
  15. res = execMust(t, e, "SELECT id FROM lfs_object WHERE oid = 'ef79c8f0'")
  16. if res.RowCount != 1 || res.Rows[0][0] != int64(1) {
  17. t.Fatalf("WHERE oid filter wrong: %v", res.Rows)
  18. }
  19. // SELECT * returns the real oid value, not the rowid in its place.
  20. res = execMust(t, e, "SELECT * FROM lfs_object")
  21. if len(res.Rows[0]) != 3 || res.Rows[0][1] != "ef79c8f0" {
  22. t.Fatalf("SELECT * row = %v, want [1 ef79c8f0 1234]", res.Rows[0])
  23. }
  24. }
  25. func TestRowIDAliasPrecedenceRealRowidColumn(t *testing.T) {
  26. _, schema, table := newTestDB(t)
  27. e := newExec(schema, table)
  28. execMust(t, e, "CREATE TABLE t (id INTEGER PRIMARY KEY, rowid TEXT)")
  29. execMust(t, e, "INSERT INTO t VALUES (5, 'custom-rowid')")
  30. res := execMust(t, e, "SELECT rowid FROM t WHERE id = 5")
  31. if res.Rows[0][0] != "custom-rowid" {
  32. t.Fatalf("SELECT rowid = %v, want 'custom-rowid' (real column)", res.Rows[0][0])
  33. }
  34. }
  35. func TestRowIDAliasWhenNoRealColumn(t *testing.T) {
  36. _, schema, table := newTestDB(t)
  37. e := newExec(schema, table)
  38. execMust(t, e, "CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)")
  39. execMust(t, e, "INSERT INTO t VALUES (1, 'a')")
  40. execMust(t, e, "INSERT INTO t VALUES (2, 'b')")
  41. // Without a real oid/rowid column, the aliases fall back to the hidden rowid.
  42. res := execMust(t, e, "SELECT rowid FROM t WHERE id = 2")
  43. if res.Rows[0][0] != int64(2) {
  44. t.Fatalf("SELECT rowid (hidden alias) = %v, want 2", res.Rows[0][0])
  45. }
  46. res = execMust(t, e, "SELECT oid FROM t WHERE id = 1")
  47. if res.Rows[0][0] != int64(1) {
  48. t.Fatalf("SELECT oid (hidden alias) = %v, want 1", res.Rows[0][0])
  49. }
  50. }