jsonb_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. package executor
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "testing"
  7. "github.com/danfragoso/pizzasql-next/pkg/analyzer"
  8. "github.com/danfragoso/pizzasql-next/pkg/pgerror"
  9. "github.com/danfragoso/pizzasql-next/pkg/storage"
  10. )
  11. // jsonbExec parses and executes one statement, returning the value at (0, 0).
  12. func jsonbValue(t *testing.T, e *Executor, sql string) interface{} {
  13. t.Helper()
  14. res := execMust(t, e, sql)
  15. if len(res.Rows) == 0 || len(res.Rows[0]) == 0 {
  16. t.Fatalf("%s: no value", sql)
  17. }
  18. return res.Rows[0][0]
  19. }
  20. func jsonbErrorCode(t *testing.T, err error) string {
  21. t.Helper()
  22. switch {
  23. case err == nil:
  24. return ""
  25. case errors.Is(err, pgerror.ErrInvalidText):
  26. return pgerror.CodeInvalidText
  27. case errors.Is(err, pgerror.ErrInvalidParameter):
  28. return pgerror.CodeInvalidParameter
  29. case errors.Is(err, pgerror.ErrFeatureNotSupported):
  30. return pgerror.CodeFeatureNotSupported
  31. case errors.Is(err, pgerror.ErrDatatypeMismatch):
  32. return pgerror.CodeDatatypeMismatch
  33. }
  34. var analysisErr *analyzer.AnalysisError
  35. if errors.As(err, &analysisErr) && analysisErr.Type == analyzer.ErrTypeMismatch {
  36. return pgerror.CodeDatatypeMismatch
  37. }
  38. return "other:" + err.Error()
  39. }
  40. func TestJSONBCanonicalization(t *testing.T) {
  41. pool, schema, table := newTestDB(t)
  42. e := newExec(schema, table)
  43. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  44. execMust(t, e, `INSERT INTO docs VALUES (1, '{"b": 1, "a": 2, "b": 3}')`)
  45. execMust(t, e, `INSERT INTO docs VALUES (2, '{"z": 1.50, "y": 1e2, "x": -0.0, "w": 1.0}')`)
  46. execMust(t, e, `INSERT INTO docs VALUES (3, ' [ 1 , { "b" : 2 , "a" : 1 } ] ')`)
  47. want := map[int64]string{
  48. 1: `{"a":2,"b":3}`,
  49. 2: `{"w":1,"x":0,"y":100,"z":1.5}`,
  50. 3: `[1,{"a":1,"b":2}]`,
  51. }
  52. res := execMust(t, e, "SELECT id, doc FROM docs ORDER BY id")
  53. for _, row := range res.Rows {
  54. id := row[0].(int64)
  55. if row[1] != want[id] {
  56. t.Fatalf("row %d doc = %v, want %s", id, row[1], want[id])
  57. }
  58. }
  59. // A JSONB column rejects malformed documents and stores nothing.
  60. if _, err := execSQL(e, `INSERT INTO docs VALUES (4, 'not json')`); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  61. t.Fatalf("malformed JSONB insert error = %v", err)
  62. }
  63. if res := execMust(t, e, "SELECT COUNT(*) FROM docs WHERE id = 4"); res.Rows[0][0] != int64(0) {
  64. t.Fatalf("malformed JSONB insert stored a row")
  65. }
  66. // Non-text input is not silently stringified, matching array columns.
  67. if _, err := execSQL(e, `INSERT INTO docs VALUES (5, 5)`); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  68. t.Fatalf("integer JSONB insert error = %v", err)
  69. }
  70. // Canonicalization is idempotent, and a fresh executor over the same store
  71. // reads back the same bytes (no hidden normalization at read time).
  72. reloadedSchema := storage.NewSchemaManager(pool, "testdb")
  73. reloadedTable := storage.NewTableManager(pool, reloadedSchema, "testdb")
  74. reloaded := newExec(reloadedSchema, reloadedTable)
  75. res = execMust(t, reloaded, "SELECT id, doc FROM docs ORDER BY id")
  76. for _, row := range res.Rows {
  77. id := row[0].(int64)
  78. if row[1] != want[id] {
  79. t.Fatalf("reloaded row %d doc = %v, want %s", id, row[1], want[id])
  80. }
  81. canonical, err := CanonicalizeJSONB(row[1].(string))
  82. if err != nil || canonical != row[1] {
  83. t.Fatalf("row %d is not a canonical fixed point: %q (%v)", id, row[1], err)
  84. }
  85. }
  86. }
  87. func TestJSONBCastsAndToJSONB(t *testing.T) {
  88. _, schema, table := newTestDB(t)
  89. e := newExec(schema, table)
  90. cases := []struct {
  91. sql string
  92. want interface{}
  93. }{
  94. {`SELECT '{"b":2,"a":1}'::jsonb`, `{"a":1,"b":2}`},
  95. {`SELECT CAST(' [1, 2] ' AS JSONB)`, `[1,2]`},
  96. {`SELECT ('{"a":1}'::jsonb)::jsonb`, `{"a":1}`},
  97. {`SELECT to_jsonb(5)`, `5`},
  98. {`SELECT to_jsonb('x')`, `"x"`},
  99. {`SELECT to_jsonb(TRUE)`, `true`},
  100. {`SELECT to_jsonb(NULL)`, nil},
  101. {`SELECT to_jsonb(ARRAY[1,2])`, `[1,2]`},
  102. {`SELECT to_jsonb(ARRAY['a','b'])`, `["a","b"]`},
  103. {`SELECT to_jsonb('{"a":1}'::jsonb)`, `{"a":1}`},
  104. {`SELECT CAST('{"a":1}'::jsonb AS TEXT)`, `{"a":1}`},
  105. }
  106. for _, tc := range cases {
  107. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  108. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  109. }
  110. }
  111. for _, sql := range []string{
  112. `SELECT 'not json'::jsonb`,
  113. `SELECT '{"a":}'::jsonb`,
  114. `SELECT '{"a":1} trailing'::jsonb`,
  115. `SELECT 5::jsonb`,
  116. `SELECT '{"a":1e999999}'::jsonb`,
  117. } {
  118. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  119. t.Errorf("%s error = %v, want 22P02", sql, err)
  120. }
  121. }
  122. // The JSON1 normalization function and its json() sibling keep working.
  123. if got := jsonbValue(t, e, `SELECT jsonb('{"a":1}')`); got != `{"a":1}` {
  124. t.Fatalf("jsonb() = %v", got)
  125. }
  126. if got := jsonbValue(t, e, `SELECT jsonb_valid('{"a":1}')`); got != int64(1) {
  127. t.Fatalf("jsonb_valid() = %v", got)
  128. }
  129. }
  130. func TestJSONBArrowOperators(t *testing.T) {
  131. _, schema, table := newTestDB(t)
  132. e := newExec(schema, table)
  133. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  134. execMust(t, e, `INSERT INTO docs VALUES (1, '{"a":1,"s":"x","n":null,"arr":[10,{"b":2}]}')`)
  135. cases := []struct {
  136. sql string
  137. want interface{}
  138. }{
  139. {`SELECT doc -> 'a' FROM docs`, `1`},
  140. {`SELECT doc -> 's' FROM docs`, `"x"`},
  141. {`SELECT doc ->> 's' FROM docs`, `x`},
  142. {`SELECT doc -> 'n' FROM docs`, `null`},
  143. {`SELECT doc ->> 'n' FROM docs`, nil},
  144. {`SELECT doc -> 'missing' FROM docs`, nil},
  145. {`SELECT doc ->> 'missing' FROM docs`, nil},
  146. {`SELECT doc -> 'arr' -> 0 FROM docs`, `10`},
  147. {`SELECT doc -> 'arr' -> -1 ->> 'b' FROM docs`, `2`},
  148. {`SELECT doc -> 'arr' -> 3 FROM docs`, nil},
  149. {`SELECT doc -> '$."arr"[1]' ->> 'b' FROM docs`, `2`},
  150. {`SELECT NULL::jsonb -> 'a'`, nil},
  151. {`SELECT doc -> NULL FROM docs`, nil},
  152. }
  153. for _, tc := range cases {
  154. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  155. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  156. }
  157. }
  158. // The JSON text surface is unchanged: a text-backed document still returns
  159. // JSON1 coercions for ->>.
  160. if got := jsonbValue(t, e, `SELECT '{"a":true}' ->> 'a'`); got != int64(1) {
  161. t.Fatalf("JSON1 ->> boolean = %v (%T), want int64 1", got, got)
  162. }
  163. }
  164. func TestJSONBPathOperators(t *testing.T) {
  165. _, schema, table := newTestDB(t)
  166. e := newExec(schema, table)
  167. cases := []struct {
  168. sql string
  169. want interface{}
  170. }{
  171. {`SELECT '{"a":{"b":2}}'::jsonb #> '{a,b}'`, `2`},
  172. {`SELECT '{"a":{"b":2}}'::jsonb #>> '{a,b}'`, `2`},
  173. {`SELECT '{"a":[1,2,3]}'::jsonb #> '{a,-1}'`, `3`},
  174. {`SELECT '{"a":{"b":2}}'::jsonb #> '$."a"."b"'`, `2`},
  175. {`SELECT '{"a":{"b":2}}'::jsonb #> ARRAY['a','b']`, `2`},
  176. {`SELECT '{"a":1}'::jsonb #> '{missing}'`, nil},
  177. {`SELECT '{"a":1}'::jsonb #- '{a}'`, `{}`},
  178. {`SELECT '{"a":{"b":1},"c":2}'::jsonb #- '{a,b}'`, `{"a":{},"c":2}`},
  179. {`SELECT '[1,2,3]'::jsonb #- '{1}'`, `[1,3]`},
  180. {`SELECT '{"a":1}'::jsonb #- '{missing}'`, `{"a":1}`},
  181. {`SELECT NULL::jsonb #> '{a}'`, nil},
  182. }
  183. for _, tc := range cases {
  184. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  185. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  186. }
  187. }
  188. // Wildcards, slices, and unbounded expansion are rejected, not approximated.
  189. for _, sql := range []string{
  190. `SELECT '{"a":1}'::jsonb #> '$[*]'`,
  191. `SELECT '{"a":1}'::jsonb #> '$.**'`,
  192. `SELECT '{"a":1}'::jsonb -> '$[*]'`,
  193. `SELECT '{"a":1}'::jsonb #- '$[0:1]'`,
  194. } {
  195. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeFeatureNotSupported {
  196. t.Errorf("%s error = %v, want 0A000", sql, err)
  197. }
  198. }
  199. }
  200. func TestJSONBContainmentAndExistence(t *testing.T) {
  201. _, schema, table := newTestDB(t)
  202. e := newExec(schema, table)
  203. cases := []struct {
  204. sql string
  205. want interface{}
  206. }{
  207. {`SELECT '{"a":1,"b":{"c":2}}'::jsonb @> '{"a":1}'::jsonb`, true},
  208. {`SELECT '{"a":1,"b":{"c":2}}'::jsonb @> '{"b":{"c":2}}'::jsonb`, true},
  209. {`SELECT '{"a":1,"b":{"c":2}}'::jsonb @> '{"b":{}}'::jsonb`, true},
  210. {`SELECT '{"a":1}'::jsonb @> '{"b":1}'::jsonb`, false},
  211. {`SELECT '{"a":1}'::jsonb @> '{"a":1,"b":2}'::jsonb`, false},
  212. {`SELECT '[1,2,3]'::jsonb @> '[3,1]'::jsonb`, true},
  213. {`SELECT '[1,2,3]'::jsonb @> '[4]'::jsonb`, false},
  214. {`SELECT '[1,2]'::jsonb @> '1'::jsonb`, true},
  215. {`SELECT '["a"]'::jsonb @> '"a"'::jsonb`, true},
  216. {`SELECT '[true]'::jsonb @> 'true'::jsonb`, true},
  217. {`SELECT '[null]'::jsonb @> 'null'::jsonb`, true},
  218. {`SELECT '[1]'::jsonb @> '2'::jsonb`, false},
  219. {`SELECT '{"a":[1,2]}'::jsonb @> '{"a":[2]}'::jsonb`, true},
  220. {`SELECT '{"a":null}'::jsonb @> '{"a":null}'::jsonb`, true},
  221. {`SELECT '{"a":null}'::jsonb @> '{"a":1}'::jsonb`, false},
  222. {`SELECT '{"a":1.0}'::jsonb @> '{"a":1}'::jsonb`, true},
  223. {`SELECT '{"a":1}'::jsonb <@ '{"a":1,"b":2}'::jsonb`, true},
  224. {`SELECT '{"a":1}'::jsonb @> '{"a":1}'`, true},
  225. {`SELECT NULL::jsonb @> '{}'::jsonb`, nil},
  226. {`SELECT '{"a":1}'::jsonb @> NULL::jsonb`, nil},
  227. {`SELECT '{"a":1}'::jsonb ? 'a'`, true},
  228. {`SELECT '{"a":1}'::jsonb ? 'b'`, false},
  229. {`SELECT '[1,"x"]'::jsonb ? 'x'`, true},
  230. {`SELECT '[1,"x"]'::jsonb ? '1'`, false},
  231. {`SELECT '"x"'::jsonb ? 'x'`, true},
  232. {`SELECT 'null'::jsonb ? 'x'`, false},
  233. {`SELECT '{"a":1,"b":2}'::jsonb ?| '{b,c}'`, true},
  234. {`SELECT '{"a":1,"b":2}'::jsonb ?| '{c,d}'`, false},
  235. {`SELECT '{"a":1,"b":2}'::jsonb ?& '{a,b}'`, true},
  236. {`SELECT '{"a":1,"b":2}'::jsonb ?& '{a,c}'`, false},
  237. {`SELECT '{"1":true}'::jsonb ? '1'`, true},
  238. {`SELECT '{"1":true}'::jsonb ?| '{1,2}'`, true},
  239. {`SELECT '[1,"x"]'::jsonb ?| '{x}'`, true},
  240. {`SELECT NULL::jsonb ? 'a'`, nil},
  241. }
  242. for _, tc := range cases {
  243. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  244. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  245. }
  246. }
  247. // Containment needs a jsonb operand on at least one side.
  248. if _, err := execSQL(e, `SELECT '{"a":1}' @> '{"a":1}'`); jsonbErrorCode(t, err) != pgerror.CodeDatatypeMismatch {
  249. t.Errorf("text @> text error = %v, want 42804", err)
  250. }
  251. }
  252. func TestJSONBConcatAndDelete(t *testing.T) {
  253. _, schema, table := newTestDB(t)
  254. e := newExec(schema, table)
  255. cases := []struct {
  256. sql string
  257. want interface{}
  258. }{
  259. {`SELECT '{"a":1}'::jsonb || '{"b":2,"a":3}'::jsonb`, `{"a":3,"b":2}`},
  260. {`SELECT '[1]'::jsonb || '[2,3]'::jsonb`, `[1,2,3]`},
  261. {`SELECT '[1]'::jsonb || '2'::jsonb`, `[1,2]`},
  262. {`SELECT '{"a":1}'::jsonb || '[1]'::jsonb`, `[{"a":1},1]`},
  263. {`SELECT '1'::jsonb || '2'::jsonb`, `[1,2]`},
  264. {`SELECT NULL::jsonb || '{}'::jsonb`, nil},
  265. {`SELECT '{"a":1,"b":2}'::jsonb - 'a'`, `{"b":2}`},
  266. {`SELECT '{"a":1,"b":2}'::jsonb - '{a,b}'::text[]`, `{}`},
  267. {`SELECT '[1,2,3]'::jsonb - 1`, `[1,3]`},
  268. {`SELECT '[1,2,3]'::jsonb - -1`, `[1,2]`},
  269. {`SELECT '[1,2,3]'::jsonb - 9`, `[1,2,3]`},
  270. {`SELECT '["a","b","a"]'::jsonb - 'a'`, `["b"]`},
  271. {`SELECT '"x"'::jsonb - 'a'`, `"x"`},
  272. {`SELECT NULL::jsonb - 'a'`, nil},
  273. }
  274. for _, tc := range cases {
  275. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  276. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  277. }
  278. }
  279. // Text concatenation is unchanged when neither side is jsonb.
  280. if got := jsonbValue(t, e, `SELECT 'a' || 'b'`); got != "ab" {
  281. t.Fatalf("text concatenation = %v", got)
  282. }
  283. if _, err := execSQL(e, `SELECT '{"a":1}'::jsonb - 1`); jsonbErrorCode(t, err) != pgerror.CodeInvalidParameter {
  284. t.Errorf("jsonb - integer on object error = %v, want 22023", err)
  285. }
  286. }
  287. func TestJSONBScalarFunctions(t *testing.T) {
  288. _, schema, table := newTestDB(t)
  289. e := newExec(schema, table)
  290. cases := []struct {
  291. sql string
  292. want interface{}
  293. }{
  294. {`SELECT jsonb_typeof('{"a":1}')`, "object"},
  295. {`SELECT jsonb_typeof('[1]')`, "array"},
  296. {`SELECT jsonb_typeof('"x"')`, "string"},
  297. {`SELECT jsonb_typeof('1.50')`, "number"},
  298. {`SELECT jsonb_typeof('true')`, "boolean"},
  299. {`SELECT jsonb_typeof('null')`, "null"},
  300. {`SELECT jsonb_typeof(NULL)`, nil},
  301. {`SELECT jsonb_array_length('[1,2,3]')`, int64(3)},
  302. {`SELECT jsonb_array_length('[]')`, int64(0)},
  303. {`SELECT jsonb_array_length(NULL)`, nil},
  304. {`SELECT jsonb_pretty('{"b":2,"a":[1]}')`, "{\n \"a\": [\n 1\n ],\n \"b\": 2\n}"},
  305. {`SELECT jsonb_strip_nulls('{"a":null,"b":{"c":null,"d":1},"e":[null,1]}')`, `{"b":{"d":1},"e":[null,1]}`},
  306. {`SELECT jsonb_build_object('b', 1, 'a', 'x')`, `{"a":"x","b":1}`},
  307. {`SELECT jsonb_build_object('a', NULL)`, `{"a":null}`},
  308. {`SELECT jsonb_build_object('a', '{"b":1}'::jsonb)`, `{"a":{"b":1}}`},
  309. {`SELECT jsonb_build_array(1, 'x', NULL, TRUE)`, `[1,"x",null,true]`},
  310. {`SELECT jsonb_build_array()`, `[]`},
  311. {`SELECT jsonb_build_object()`, `{}`},
  312. {`SELECT jsonb_set('{"a":1}', '{a}', '2')`, `{"a":2}`},
  313. {`SELECT jsonb_set('{"a":1}', '{b}', '2')`, `{"a":1,"b":2}`},
  314. {`SELECT jsonb_set('{"a":1}', '{b}', '2', false)`, `{"a":1}`},
  315. {`SELECT jsonb_set('{"a":{"b":1}}', '{a,b}', '2')`, `{"a":{"b":2}}`},
  316. {`SELECT jsonb_set('{"a":{"b":1}}', '{x,b}', '2')`, `{"a":{"b":1}}`},
  317. {`SELECT jsonb_set('[1,2]', '{2}', '3')`, `[1,2,3]`},
  318. {`SELECT jsonb_set('[1,2]', '{5}', '3')`, `[1,2,3]`},
  319. {`SELECT jsonb_set('[1,2]', '{5}', '3', false)`, `[1,2]`},
  320. {`SELECT jsonb_set('[1,2]', '{-3}', '9')`, `[9,1,2]`},
  321. {`SELECT jsonb_set('[1,2]', '{-3}', '9', false)`, `[1,2]`},
  322. {`SELECT jsonb_set('{"a":1}', '{}', '2')`, `{"a":1}`},
  323. {`SELECT jsonb_insert('{"a":1}', '{}', '2')`, `{"a":1}`},
  324. {`SELECT jsonb_set('{"a":1}', '{a,b}', '2')`, `{"a":1}`},
  325. {`SELECT jsonb_insert('{"a":1}', '{a,b}', '2')`, `{"a":1}`},
  326. {`SELECT jsonb_set('{"a":1}', NULL, '2')`, nil},
  327. {`SELECT jsonb_insert('{"a":1}', NULL, '2')`, nil},
  328. {`SELECT jsonb_insert('{"a":[0,1,2]}', '{a,1}', '"n"')`, `{"a":[0,"n",1,2]}`},
  329. {`SELECT jsonb_insert('{"a":[0,1,2]}', '{a,1}', '"n"', true)`, `{"a":[0,1,"n",2]}`},
  330. {`SELECT jsonb_insert('{"a":1}', '{b}', '2')`, `{"a":1,"b":2}`},
  331. {`SELECT jsonb_insert('[0,1,2]', '{5}', '9')`, `[0,1,2,9]`},
  332. {`SELECT jsonb_insert('[0,1,2]', '{-3}', '9')`, `[9,0,1,2]`},
  333. {`SELECT jsonb_insert('[0,1,2]', '{-1}', '9', true)`, `[0,1,2,9]`},
  334. {`SELECT jsonb_insert('[0,1,2]', '{-2}', '9')`, `[0,9,1,2]`},
  335. {`SELECT jsonb_insert('[0,1,2]', '{-2}', '9', true)`, `[0,1,9,2]`},
  336. {`SELECT jsonb_set('{"a":1}', '{a}', NULL)`, nil},
  337. {`SELECT jsonb_insert('[0,1]', '{-1}', '9')`, `[0,9,1]`},
  338. // The JSON1 $ path form keeps its previous value semantics: a plain SQL
  339. // string becomes a JSON string, while a jsonb-typed value is embedded.
  340. {`SELECT jsonb_set('{"a":1}', '$.b', '2')`, `{"a":1,"b":"2"}`},
  341. {`SELECT jsonb_set('{"a":1}', '$.a', '2')`, `{"a":"2"}`},
  342. {`SELECT jsonb_set('{"a":1}', '$.b', '{"x":1}'::jsonb)`, `{"a":1,"b":{"x":1}}`},
  343. {`SELECT jsonb_insert('{"a":1}', '$.b', '2')`, `{"a":1,"b":"2"}`},
  344. {`SELECT jsonb_replace('{"a":1}', '$.a', '2')`, `{"a":"2"}`},
  345. {`SELECT jsonb_remove('{"a":1,"b":2}', '$.a')`, `{"b":2}`},
  346. }
  347. for _, tc := range cases {
  348. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  349. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  350. }
  351. }
  352. // JSON1 scalar functions keep their text surface.
  353. if got := jsonbValue(t, e, `SELECT json_type('{"a":1.5}', '$.a')`); got != "real" {
  354. t.Fatalf("json_type text surface = %v", got)
  355. }
  356. if got := jsonbValue(t, e, `SELECT json_set('{"a":1}', '$.b', '2')`); got != `{"a":1,"b":"2"}` {
  357. t.Fatalf("json_set text surface = %v", got)
  358. }
  359. for _, tc := range []struct {
  360. sql string
  361. code string
  362. }{
  363. {`SELECT jsonb_array_length('{"a":1}')`, pgerror.CodeInvalidParameter},
  364. {`SELECT jsonb_typeof('not json')`, pgerror.CodeInvalidText},
  365. {`SELECT jsonb_build_object('a')`, pgerror.CodeInvalidParameter},
  366. {`SELECT jsonb_build_object(NULL, 1)`, pgerror.CodeInvalidParameter},
  367. {`SELECT jsonb_set('[1,2]', '{a}', '2')`, pgerror.CodeInvalidText},
  368. {`SELECT jsonb_insert('[1,2]', '{a}', '2')`, pgerror.CodeInvalidText},
  369. {`SELECT jsonb_insert('{"a":1}', '{a}', '2')`, pgerror.CodeInvalidParameter},
  370. {`SELECT jsonb_set('1', '{a}', '2')`, pgerror.CodeInvalidParameter},
  371. {`SELECT jsonb_insert('1', '{a}', '2')`, pgerror.CodeInvalidParameter},
  372. } {
  373. if _, err := execSQL(e, tc.sql); jsonbErrorCode(t, err) != tc.code {
  374. t.Errorf("%s error = %v, want %s", tc.sql, err, tc.code)
  375. }
  376. }
  377. }
  378. func TestJSONBSetReturningFunctionsInFrom(t *testing.T) {
  379. _, schema, table := newTestDB(t)
  380. e := newExec(schema, table)
  381. res := execMust(t, e, `SELECT * FROM jsonb_each('{"b":2,"a":{"c":1}}')`)
  382. if len(res.Columns) != 2 || res.Columns[0] != "key" || res.Columns[1] != "value" {
  383. t.Fatalf("jsonb_each columns = %v", res.Columns)
  384. }
  385. if res.RowCount != 2 || res.Rows[0][0] != "a" || res.Rows[0][1] != `{"c":1}` || res.Rows[1][0] != "b" || res.Rows[1][1] != `2` {
  386. t.Fatalf("jsonb_each rows = %v", res.Rows)
  387. }
  388. if len(res.ColumnTypes) != 2 || res.ColumnTypes[1] != "JSONB" {
  389. t.Fatalf("jsonb_each column types = %v", res.ColumnTypes)
  390. }
  391. res = execMust(t, e, `SELECT e.key, e.value FROM jsonb_each('{"a":1}') AS e`)
  392. if res.RowCount != 1 || res.Rows[0][0] != "a" || res.Rows[0][1] != `1` {
  393. t.Fatalf("aliased jsonb_each rows = %v", res.Rows)
  394. }
  395. res = execMust(t, e, `SELECT key, value FROM jsonb_each_text('{"a":null,"b":true}')`)
  396. if res.RowCount != 2 || res.Rows[0][0] != "a" || res.Rows[0][1] != nil || res.Rows[1][1] != "true" {
  397. t.Fatalf("jsonb_each_text rows = %v", res.Rows)
  398. }
  399. res = execMust(t, e, `SELECT value FROM jsonb_array_elements('[1,{"a":2}]')`)
  400. if res.RowCount != 2 || res.Rows[0][0] != `1` || res.Rows[1][0] != `{"a":2}` {
  401. t.Fatalf("jsonb_array_elements rows = %v", res.Rows)
  402. }
  403. if res.ColumnTypes[0] != "JSONB" {
  404. t.Fatalf("jsonb_array_elements type = %v", res.ColumnTypes)
  405. }
  406. res = execMust(t, e, `SELECT value FROM jsonb_array_elements_text('[1,null,"x"]')`)
  407. if res.RowCount != 3 || res.Rows[0][0] != "1" || res.Rows[1][0] != nil || res.Rows[2][0] != "x" {
  408. t.Fatalf("jsonb_array_elements_text rows = %v", res.Rows)
  409. }
  410. res = execMust(t, e, `SELECT jsonb_object_keys FROM jsonb_object_keys('{"b":1,"a":2}')`)
  411. if res.RowCount != 2 || res.Rows[0][0] != "a" || res.Rows[1][0] != "b" {
  412. t.Fatalf("jsonb_object_keys rows = %v", res.Rows)
  413. }
  414. // A NULL document produces no rows.
  415. res = execMust(t, e, `SELECT * FROM jsonb_array_elements(NULL)`)
  416. if res.RowCount != 0 {
  417. t.Fatalf("jsonb_array_elements(NULL) rows = %v", res.Rows)
  418. }
  419. // Wrong document types and projections of set-returning functions are
  420. // rejected clearly instead of executing as scalar calls.
  421. for _, tc := range []struct {
  422. sql string
  423. code string
  424. }{
  425. {`SELECT * FROM jsonb_array_elements('{"a":1}')`, pgerror.CodeInvalidParameter},
  426. {`SELECT * FROM jsonb_each('[1]')`, pgerror.CodeInvalidParameter},
  427. {`SELECT * FROM jsonb_object_keys('[1]')`, pgerror.CodeInvalidParameter},
  428. {`SELECT * FROM jsonb_each('null')`, pgerror.CodeInvalidParameter},
  429. {`SELECT * FROM jsonb_array_elements('null')`, pgerror.CodeInvalidParameter},
  430. {`SELECT jsonb_each('{"a":1}')`, pgerror.CodeFeatureNotSupported},
  431. {`SELECT jsonb_array_elements('[1]')`, pgerror.CodeFeatureNotSupported},
  432. {`SELECT * FROM jsonb_each('{"a":1}') AS e JOIN docs d ON TRUE`, pgerror.CodeFeatureNotSupported},
  433. } {
  434. if _, err := execSQL(e, tc.sql); jsonbErrorCode(t, err) != tc.code {
  435. t.Errorf("%s error = %v, want %s", tc.sql, err, tc.code)
  436. }
  437. }
  438. }
  439. func TestJSONBEqualityOrderingAndDistinct(t *testing.T) {
  440. _, schema, table := newTestDB(t)
  441. e := newExec(schema, table)
  442. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  443. execMust(t, e, `INSERT INTO docs VALUES (1, '{"b":1,"a":2}'), (2, '{"a":2,"b":1}'), (3, '[1,2]')`)
  444. if got := jsonbValue(t, e, `SELECT '{"b":1,"a":2}'::jsonb = '{"a":2, "b": 1}'::jsonb`); got != true {
  445. t.Fatalf("jsonb equality = %v, want true", got)
  446. }
  447. if got := jsonbValue(t, e, `SELECT '{"a":1}'::jsonb = '[1]'::jsonb`); got != false {
  448. t.Fatalf("jsonb inequality = %v, want false", got)
  449. }
  450. if got := jsonbValue(t, e, `SELECT doc IS NOT DISTINCT FROM '{"a":2,"b":1}'::jsonb FROM docs WHERE id = 2`); got != true {
  451. t.Fatalf("IS NOT DISTINCT FROM = %v", got)
  452. }
  453. if got := jsonbValue(t, e, `SELECT '{"b":1,"a":2}'::jsonb IS DISTINCT FROM '{"a":2,"b":1.0}'::jsonb`); got != false {
  454. t.Fatalf("IS DISTINCT FROM key-order/number-spelling = %v, want false", got)
  455. }
  456. res := execMust(t, e, `SELECT COUNT(*) FROM docs WHERE doc = '{"a":2,"b":1}'::jsonb`)
  457. if res.Rows[0][0] != int64(2) {
  458. t.Fatalf("key-order-independent equality matched %v rows, want 2", res.Rows[0][0])
  459. }
  460. res = execMust(t, e, `SELECT DISTINCT doc FROM docs ORDER BY doc`)
  461. if res.RowCount != 2 {
  462. t.Fatalf("DISTINCT jsonb rows = %v, want 2", res.Rows)
  463. }
  464. // Canonical numeric scalars order numerically, and non-scalars order by
  465. // canonical text.
  466. execMust(t, e, "CREATE TABLE scalars (id INTEGER PRIMARY KEY, v JSONB)")
  467. execMust(t, e, `INSERT INTO scalars VALUES (1, '10'), (2, '2')`)
  468. res = execMust(t, e, `SELECT v FROM scalars ORDER BY v`)
  469. if res.RowCount != 2 || res.Rows[0][0] != `2` || res.Rows[1][0] != `10` {
  470. t.Fatalf("jsonb scalar ordering = %v", res.Rows)
  471. }
  472. res = execMust(t, e, `SELECT doc FROM docs WHERE id IN (1,3) ORDER BY doc`)
  473. if res.RowCount != 2 || res.Rows[0][0] != `[1,2]` || res.Rows[1][0] != `{"a":2,"b":1}` {
  474. t.Fatalf("jsonb text ordering = %v", res.Rows)
  475. }
  476. // A jsonb column cannot be compared with a number.
  477. if _, err := execSQL(e, `SELECT id FROM docs WHERE doc = 5`); err == nil {
  478. t.Fatal("jsonb = integer was accepted")
  479. }
  480. }
  481. func TestJSONBRejectedShapes(t *testing.T) {
  482. _, schema, table := newTestDB(t)
  483. e := newExec(schema, table)
  484. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  485. for _, sql := range []string{
  486. `SELECT jsonb_path_exists('{"a":1}', '$.a')`,
  487. `SELECT jsonb_path_query('{"a":1}', '$.a')`,
  488. `CREATE INDEX docs_gin ON docs USING GIN (doc)`,
  489. `CREATE INDEX docs_expr ON docs ((doc -> 'a'))`,
  490. `CREATE TABLE bad_arrays (doc JSONB[])`,
  491. } {
  492. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeFeatureNotSupported {
  493. t.Errorf("%s error = %v, want 0A000", sql, err)
  494. }
  495. }
  496. // A scalar extraction index over jsonb is a text expression and stays legal.
  497. execMust(t, e, `CREATE INDEX docs_kind ON docs ((doc ->> 'kind'))`)
  498. }
  499. func TestJSONBColumnMetadataAndDefaults(t *testing.T) {
  500. _, schema, table := newTestDB(t)
  501. e := newExec(schema, table)
  502. execMust(t, e, `CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB DEFAULT '{"b":2,"a":1}')`)
  503. execMust(t, e, `INSERT INTO docs (id) VALUES (1)`)
  504. res := execMust(t, e, `SELECT doc FROM docs`)
  505. if res.Rows[0][0] != `{"a":1,"b":2}` {
  506. t.Fatalf("canonical default = %v", res.Rows[0][0])
  507. }
  508. if len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "JSONB" {
  509. t.Fatalf("column types = %v, want [JSONB]", res.ColumnTypes)
  510. }
  511. // ALTER TABLE backfills the canonical default into existing rows.
  512. execMust(t, e, "CREATE TABLE raw (id INTEGER PRIMARY KEY)")
  513. execMust(t, e, "INSERT INTO raw VALUES (1)")
  514. execMust(t, e, `ALTER TABLE raw ADD COLUMN doc JSONB DEFAULT '{"b":2,"a":1}'`)
  515. res = execMust(t, e, `SELECT doc FROM raw`)
  516. if res.Rows[0][0] != `{"a":1,"b":2}` {
  517. t.Fatalf("backfilled default = %v", res.Rows[0][0])
  518. }
  519. // UPDATE canonicalizes too.
  520. execMust(t, e, `UPDATE raw SET doc = '{"z": 1.50}'`)
  521. res = execMust(t, e, `SELECT doc FROM raw`)
  522. if res.Rows[0][0] != `{"z":1.5}` {
  523. t.Fatalf("updated doc = %v", res.Rows[0][0])
  524. }
  525. }
  526. func TestCanonicalJSONBNumber(t *testing.T) {
  527. cases := map[string]string{
  528. "0": "0",
  529. "-0": "0",
  530. "-0.0": "0",
  531. "1": "1",
  532. "1.0": "1",
  533. "1.50": "1.5",
  534. "1e2": "100",
  535. "1E+2": "100",
  536. "1e-2": "0.01",
  537. "-12.340": "-12.34",
  538. "123456789012345678901234567890": "123456789012345678901234567890",
  539. }
  540. for input, want := range cases {
  541. got, err := canonicalJSONBNumber(json.Number(input))
  542. if err != nil || got != want {
  543. t.Errorf("canonicalJSONBNumber(%q) = %q, %v; want %q", input, got, err, want)
  544. }
  545. }
  546. for _, input := range []string{"1e999999", "1e-999999"} {
  547. if _, err := canonicalJSONBNumber(json.Number(input)); err == nil {
  548. t.Errorf("canonicalJSONBNumber(%q) accepted an unbounded expansion", input)
  549. }
  550. }
  551. }
  552. // JSON operations must keep working from a derived table and a CTE, where the
  553. // column type travels through Result column metadata.
  554. func TestJSONBTypePropagation(t *testing.T) {
  555. _, schema, table := newTestDB(t)
  556. e := newExec(schema, table)
  557. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  558. execMust(t, e, `INSERT INTO docs VALUES (1, '{"a":1}')`)
  559. if got := jsonbValue(t, e, `SELECT doc -> 'a' FROM (SELECT doc FROM docs) AS d`); got != `1` {
  560. t.Fatalf("derived jsonb -> = %v", got)
  561. }
  562. if got := jsonbValue(t, e, `SELECT doc ? 'a' FROM (SELECT doc FROM docs) AS d`); got != true {
  563. t.Fatalf("derived jsonb ? = %v", got)
  564. }
  565. if got := jsonbValue(t, e, `WITH c AS (SELECT doc FROM docs) SELECT doc @> '{"a":1}' FROM c`); got != true {
  566. t.Fatalf("CTE jsonb @> = %v", got)
  567. }
  568. if got := jsonbValue(t, e, `SELECT COALESCE(doc, '{}'::jsonb) ->> 'a' FROM docs`); got != "1" {
  569. t.Fatalf("COALESCE jsonb ->> = %v", got)
  570. }
  571. // A jsonb column keeps its type when it is the left side of a join and when
  572. // it is referenced through an alias.
  573. res := execMust(t, e, `SELECT a.id FROM docs AS a JOIN docs AS b ON a.id = b.id WHERE a.doc @> '{"a":1}'`)
  574. if res.RowCount != 1 || res.Rows[0][0] != int64(1) {
  575. t.Fatalf("joined jsonb containment rows = %v", res.Rows)
  576. }
  577. if got := jsonbValue(t, e, `SELECT a.doc -> 'a' FROM docs AS a`); got != `1` {
  578. t.Fatalf("aliased jsonb -> = %v", got)
  579. }
  580. if got := jsonbValue(t, e, `SELECT MAX(jsonb_build_object('a', doc)) FROM docs`); got != `{"a":{"a":1}}` {
  581. t.Fatalf("jsonb build function inside an aggregate = %v", got)
  582. }
  583. }
  584. func TestJSONBRejectsUnboundedCanonicalExpansion(t *testing.T) {
  585. _, schema, table := newTestDB(t)
  586. e := newExec(schema, table)
  587. if _, err := execSQL(e, `SELECT '{"a":1e999999}'::jsonb`); !strings.Contains(err.Error(), "invalid input syntax") {
  588. t.Fatalf("unbounded number error = %v", err)
  589. }
  590. }
  591. // A PostgreSQL path element is resolved against the container it reaches:
  592. // numeric-looking text is an object key in an object and an integer index in an
  593. // array. This must hold for #>, #>>, #-, jsonb_set, and jsonb_insert.
  594. func TestJSONBNumericLookingObjectKeys(t *testing.T) {
  595. _, schema, table := newTestDB(t)
  596. e := newExec(schema, table)
  597. cases := []struct {
  598. sql string
  599. want interface{}
  600. }{
  601. {`SELECT '{"1":{"2":3}}'::jsonb #> '{1,2}'`, `3`},
  602. {`SELECT '{"1":{"2":3}}'::jsonb #>> '{1,2}'`, `3`},
  603. {`SELECT '{"1":{"2":3}}'::jsonb #- '{1,2}'`, `{"1":{}}`},
  604. {`SELECT jsonb_set('{"1":{"2":3}}', '{1,2}', '4')`, `{"1":{"2":4}}`},
  605. {`SELECT jsonb_insert('{"1":{"2":3}}', '{1,3}', '4')`, `{"1":{"2":3,"3":4}}`},
  606. {`SELECT '{"01":"x"}'::jsonb #> '{01}'`, `"x"`},
  607. {`SELECT '{"-1":"x"}'::jsonb #> '{-1}'`, `"x"`},
  608. {`SELECT '{"1.5":"x"}'::jsonb #> '{1.5}'`, `"x"`},
  609. {`SELECT jsonb_set('{"01":"x"}', '{01}', '"y"')`, `{"01":"y"}`},
  610. {`SELECT jsonb_insert('{"01":"x"}', '{02}', '"y"')`, `{"01":"x","02":"y"}`},
  611. {`SELECT jsonb_set('{"-1":"x"}', '{-1}', '"y"')`, `{"-1":"y"}`},
  612. {`SELECT jsonb_insert('{"-1":"x"}', '{-2}', '"y"')`, `{"-1":"x","-2":"y"}`},
  613. {`SELECT '{"1.5":"x"}'::jsonb #- '{1.5}'`, `{}`},
  614. {`SELECT jsonb_set('{"1.5":"x"}', '{1.5}', '"y"')`, `{"1.5":"y"}`},
  615. {`SELECT '[10,20]'::jsonb #> '{01}'`, `20`},
  616. {`SELECT '[10,20]'::jsonb #> '{-1}'`, `20`},
  617. {`SELECT '[10,20]'::jsonb #> '{a}'`, nil},
  618. }
  619. for _, tc := range cases {
  620. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  621. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  622. }
  623. }
  624. // A non-integer path element on an array is an error for #-, matching
  625. // PostgreSQL's path mutation and delete behavior.
  626. if _, err := execSQL(e, `SELECT '[10,20]'::jsonb #- '{a}'`); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  627. t.Errorf("#- array non-integer element error = %v, want 22P02", err)
  628. }
  629. }
  630. // #- must produce jsonb metadata and keep jsonb semantics for chained
  631. // operators, not fall back to text.
  632. func TestJSONBPathDeleteKeepsJSONBType(t *testing.T) {
  633. _, schema, table := newTestDB(t)
  634. e := newExec(schema, table)
  635. res := execMust(t, e, `SELECT '{"t":true}'::jsonb #- '{x}'`)
  636. if len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "JSONB" {
  637. t.Fatalf("#- column type = %v, want JSONB", res.ColumnTypes)
  638. }
  639. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') ->> 't'`); got != "true" {
  640. t.Fatalf("#- ->> = %v (%T), want true text", got, got)
  641. }
  642. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') = '{"t":true}'::jsonb`); got != true {
  643. t.Fatalf("#- jsonb equality = %v", got)
  644. }
  645. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') ? 't'`); got != true {
  646. t.Fatalf("#- ? = %v", got)
  647. }
  648. }
  649. // jsonb equality and ordering use exact canonical bytes, not float64. DISTINCT,
  650. // GROUP BY, and containment must agree with =.
  651. func TestJSONBExactEqualityAndAggregateAgreement(t *testing.T) {
  652. _, schema, table := newTestDB(t)
  653. e := newExec(schema, table)
  654. for _, sql := range []string{
  655. `SELECT '1'::jsonb = '1.0000000000000000001'::jsonb`,
  656. `SELECT '9007199254740993'::jsonb = '9007199254740992'::jsonb`,
  657. } {
  658. if got := jsonbValue(t, e, sql); got != false {
  659. t.Errorf("%s = %v, want false", sql, got)
  660. }
  661. }
  662. if got := jsonbValue(t, e, `SELECT '1'::jsonb = '1.0'::jsonb`); got != true {
  663. t.Fatalf("1 = 1.0 = %v, want true", got)
  664. }
  665. if got := jsonbValue(t, e, `SELECT '1'::jsonb < '1.0000000000000000001'::jsonb`); got != true {
  666. t.Fatalf("1 < 1.0000000000000000001 = %v, want true", got)
  667. }
  668. if got := jsonbValue(t, e, `SELECT '1'::jsonb IS DISTINCT FROM '1.0000000000000000001'::jsonb`); got != true {
  669. t.Fatalf("1 IS DISTINCT FROM 1.0000000000000000001 = %v, want true", got)
  670. }
  671. if got := jsonbValue(t, e, `SELECT CASE '1'::jsonb WHEN '1.0000000000000000001'::jsonb THEN 'same' ELSE 'different' END`); got != "different" {
  672. t.Fatalf("simple CASE exact jsonb equality = %v, want different", got)
  673. }
  674. if got := jsonbValue(t, e, `SELECT '1'::jsonb IN ('1.0'::jsonb, '1.0000000000000000001'::jsonb)`); got != true {
  675. t.Fatalf("jsonb IN = %v, want true", got)
  676. }
  677. if got := jsonbValue(t, e, `SELECT '{"n":1}'::jsonb @> '{"n":1.0000000000000000001}'::jsonb`); got != false {
  678. t.Fatalf("containment rounded a number: %v, want false", got)
  679. }
  680. if got := jsonbValue(t, e, `SELECT '{"n":1}'::jsonb @> '{"n":1.0}'::jsonb`); got != true {
  681. t.Fatalf("containment of equal canonical numbers = %v, want true", got)
  682. }
  683. execMust(t, e, "CREATE TABLE exact (id INTEGER PRIMARY KEY, doc JSONB)")
  684. execMust(t, e, `INSERT INTO exact VALUES (1, '1'), (2, '1.0'), (3, '1.0000000000000000001'), (4, '9007199254740993'), (5, '9007199254740992')`)
  685. if res := execMust(t, e, `SELECT DISTINCT doc FROM exact`); res.RowCount != 4 {
  686. t.Fatalf("DISTINCT exact jsonb rows = %v, want 4", res.Rows)
  687. }
  688. res := execMust(t, e, `SELECT doc, COUNT(*) FROM exact GROUP BY doc ORDER BY doc`)
  689. if res.RowCount != 4 {
  690. t.Fatalf("GROUP BY exact jsonb rows = %v, want 4", res.Rows)
  691. }
  692. if res.Rows[0][1] != int64(2) {
  693. t.Fatalf("GROUP BY merged canonical numbers = %v, want two spellings of 1", res.Rows[0])
  694. }
  695. res = execMust(t, e, `SELECT COUNT(*) FROM exact WHERE doc = '1'::jsonb`)
  696. if res.Rows[0][0] != int64(2) {
  697. t.Fatalf("equality count = %v, want 2", res.Rows[0][0])
  698. }
  699. res = execMust(t, e, `SELECT COUNT(*) FROM exact WHERE doc @> '1'::jsonb`)
  700. if res.Rows[0][0] != int64(2) {
  701. t.Fatalf("containment count = %v, want 2", res.Rows[0][0])
  702. }
  703. }
  704. // Containment tracks consumed array matches, as required by the review gate:
  705. // a candidate with duplicates needs a distinct container element per copy.
  706. // A jsonb extraction result must not be silently compared with a SQL number.
  707. func TestJSONBExtractionComparisonTypeError(t *testing.T) {
  708. _, schema, table := newTestDB(t)
  709. e := newExec(schema, table)
  710. for _, sql := range []string{
  711. `SELECT '{"a":1}'::jsonb -> 'a' = 1`,
  712. `SELECT '{"a":1}'::jsonb -> 'a' = TRUE`,
  713. } {
  714. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeDatatypeMismatch {
  715. t.Errorf("%s error = %v, want 42804", sql, err)
  716. }
  717. }
  718. if got := jsonbValue(t, e, `SELECT '{"a":1}'::jsonb -> 'a' = '1'::jsonb`); got != true {
  719. t.Fatalf("jsonb -> = jsonb = %v, want true", got)
  720. }
  721. }
  722. func TestJSONBArrayContainmentMultiplicity(t *testing.T) {
  723. _, schema, table := newTestDB(t)
  724. e := newExec(schema, table)
  725. cases := []struct {
  726. sql string
  727. want interface{}
  728. }{
  729. {`SELECT '[1]'::jsonb @> '[1,1]'::jsonb`, false},
  730. {`SELECT '[1,1]'::jsonb @> '[1]'::jsonb`, true},
  731. {`SELECT '[1,2,2]'::jsonb @> '[2,2]'::jsonb`, true},
  732. {`SELECT '[1,2]'::jsonb @> '[2,2]'::jsonb`, false},
  733. {`SELECT '[1]'::jsonb @> '1'::jsonb`, true},
  734. }
  735. for _, tc := range cases {
  736. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  737. t.Errorf("%s = %v, want %v", tc.sql, got, tc.want)
  738. }
  739. }
  740. }
  741. // A JSONB column written before native JSONB may hold non-canonical text. Reads
  742. // canonicalize it so projection, DISTINCT, GROUP BY, and ORDER BY all observe
  743. // the canonical value.
  744. func TestJSONBLegacyValuesCanonicalizeOnRead(t *testing.T) {
  745. _, schema, table := newTestDB(t)
  746. e := newExec(schema, table)
  747. execMust(t, e, "CREATE TABLE legacy (id INTEGER PRIMARY KEY, doc JSONB)")
  748. legacy := []string{
  749. `{ "a" : 1 }`,
  750. `{"a":1}`,
  751. ` { "a" :1 } `,
  752. `{"b": 2,"a": 1}`,
  753. `{"a":1,"b":2}`,
  754. ` { "b" : 2, "a" : 1 } `,
  755. }
  756. for i, raw := range legacy {
  757. if err := table.Insert("legacy", storage.Row{"id": int64(i + 1), "doc": raw}); err != nil {
  758. t.Fatalf("legacy insert %d: %v", i, err)
  759. }
  760. }
  761. e = newExec(schema, table)
  762. res := execMust(t, e, `SELECT doc FROM legacy ORDER BY id`)
  763. if res.RowCount != len(legacy) {
  764. t.Fatalf("legacy rows = %d", res.RowCount)
  765. }
  766. for _, row := range res.Rows {
  767. if row[0] != `{"a":1}` && row[0] != `{"a":1,"b":2}` {
  768. t.Fatalf("legacy read = %v, want canonical text", row[0])
  769. }
  770. }
  771. res = execMust(t, e, `SELECT DISTINCT doc FROM legacy`)
  772. if res.RowCount != 2 {
  773. t.Fatalf("legacy DISTINCT rows = %v, want 2", res.Rows)
  774. }
  775. res = execMust(t, e, `SELECT DISTINCT * FROM legacy`)
  776. if res.RowCount != len(legacy) {
  777. t.Fatalf("legacy DISTINCT * rows = %d", res.RowCount)
  778. }
  779. res = execMust(t, e, `SELECT * FROM legacy ORDER BY id`)
  780. for _, row := range res.Rows {
  781. if row[1] != `{"a":1}` && row[1] != `{"a":1,"b":2}` {
  782. t.Fatalf("legacy SELECT * read = %v, want canonical text", row[1])
  783. }
  784. }
  785. if got := jsonbValue(t, e, `SELECT COUNT(DISTINCT doc) FROM legacy`); got != int64(2) {
  786. t.Fatalf("legacy COUNT(DISTINCT doc) = %v, want 2", got)
  787. }
  788. res = execMust(t, e, `SELECT doc, COUNT(*) FROM legacy GROUP BY doc`)
  789. if res.RowCount != 2 {
  790. t.Fatalf("legacy GROUP BY rows = %v, want 2", res.Rows)
  791. }
  792. res = execMust(t, e, `SELECT id FROM legacy ORDER BY doc, id`)
  793. if res.RowCount != len(legacy) {
  794. t.Fatalf("legacy ORDER BY rows = %d", res.RowCount)
  795. }
  796. if got := jsonbValue(t, e, `SELECT doc FROM legacy WHERE id = 1`); got != `{"a":1}` {
  797. t.Fatalf("legacy selected value = %v, want canonical", got)
  798. }
  799. }
  800. // Escapes that encoding/json would silently rewrite are invalid jsonb input.
  801. func TestJSONBRejectsNULAndLoneSurrogates(t *testing.T) {
  802. _, schema, table := newTestDB(t)
  803. e := newExec(schema, table)
  804. for _, sql := range []string{
  805. `SELECT '{"a":"\u0000"}'::jsonb`,
  806. `SELECT '"\ud800"'::jsonb`,
  807. `SELECT '"\ud800x"'::jsonb`,
  808. `SELECT '"\udc00"'::jsonb`,
  809. `SELECT '{"\u0000":1}'::jsonb`,
  810. } {
  811. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  812. t.Errorf("%s error = %v, want 22P02", sql, err)
  813. }
  814. }
  815. if got := jsonbValue(t, e, `SELECT '"\u0041"'::jsonb`); got != `"A"` {
  816. t.Fatalf("escaped ASCII = %v", got)
  817. }
  818. if got := jsonbValue(t, e, `SELECT '"\ud83d\ude00"'::jsonb`); got != "\"\U0001F600\"" {
  819. t.Fatalf("surrogate pair = %v", got)
  820. }
  821. }
  822. // Zero-row text set-returning functions must still report TEXT for the value
  823. // column.
  824. func TestJSONBTextSetReturningTypesOnNull(t *testing.T) {
  825. _, schema, table := newTestDB(t)
  826. e := newExec(schema, table)
  827. res := execMust(t, e, `SELECT * FROM jsonb_each_text(NULL)`)
  828. if res.RowCount != 0 || len(res.ColumnTypes) != 2 || res.ColumnTypes[0] != "TEXT" || res.ColumnTypes[1] != "TEXT" {
  829. t.Fatalf("jsonb_each_text(NULL) = rows %d types %v, want 0 [TEXT TEXT]", res.RowCount, res.ColumnTypes)
  830. }
  831. res = execMust(t, e, `SELECT * FROM jsonb_array_elements_text(NULL)`)
  832. if res.RowCount != 0 || len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "TEXT" {
  833. t.Fatalf("jsonb_array_elements_text(NULL) = rows %d types %v, want 0 [TEXT]", res.RowCount, res.ColumnTypes)
  834. }
  835. }