jsonb_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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'`, false},
  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_set('{"a":1}', '$.b', 1.5)`, `{"a":1,"b":1.5}`},
  344. {`SELECT jsonb_insert('{}', '$.b', 2.5)`, `{"b":2.5}`},
  345. {`SELECT jsonb_replace('{"b":0}', '$.b', 2.5)`, `{"b":2.5}`},
  346. {`SELECT jsonb_insert('{"a":1}', '$.b', '2')`, `{"a":1,"b":"2"}`},
  347. {`SELECT jsonb_replace('{"a":1}', '$.a', '2')`, `{"a":"2"}`},
  348. {`SELECT jsonb_remove('{"a":1,"b":2}', '$.a')`, `{"b":2}`},
  349. }
  350. for _, tc := range cases {
  351. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  352. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  353. }
  354. }
  355. // JSON1 scalar functions keep their text surface.
  356. if got := jsonbValue(t, e, `SELECT json_type('{"a":1.5}', '$.a')`); got != "real" {
  357. t.Fatalf("json_type text surface = %v", got)
  358. }
  359. if got := jsonbValue(t, e, `SELECT json_set('{"a":1}', '$.b', '2')`); got != `{"a":1,"b":"2"}` {
  360. t.Fatalf("json_set text surface = %v", got)
  361. }
  362. for _, tc := range []struct {
  363. sql string
  364. code string
  365. }{
  366. {`SELECT jsonb_array_length('{"a":1}')`, pgerror.CodeInvalidParameter},
  367. {`SELECT jsonb_typeof('not json')`, pgerror.CodeInvalidText},
  368. {`SELECT jsonb_build_object('a')`, pgerror.CodeInvalidParameter},
  369. {`SELECT jsonb_build_object(NULL, 1)`, pgerror.CodeInvalidParameter},
  370. {`SELECT jsonb_set('[1,2]', '{a}', '2')`, pgerror.CodeInvalidText},
  371. {`SELECT jsonb_insert('[1,2]', '{a}', '2')`, pgerror.CodeInvalidText},
  372. {`SELECT jsonb_insert('{"a":1}', '{a}', '2')`, pgerror.CodeInvalidParameter},
  373. {`SELECT jsonb_set('1', '{a}', '2')`, pgerror.CodeInvalidParameter},
  374. {`SELECT jsonb_insert('1', '{a}', '2')`, pgerror.CodeInvalidParameter},
  375. } {
  376. if _, err := execSQL(e, tc.sql); jsonbErrorCode(t, err) != tc.code {
  377. t.Errorf("%s error = %v, want %s", tc.sql, err, tc.code)
  378. }
  379. }
  380. }
  381. func TestJSONBSetReturningFunctionsInFrom(t *testing.T) {
  382. _, schema, table := newTestDB(t)
  383. e := newExec(schema, table)
  384. res := execMust(t, e, `SELECT * FROM jsonb_each('{"b":2,"a":{"c":1}}')`)
  385. if len(res.Columns) != 2 || res.Columns[0] != "key" || res.Columns[1] != "value" {
  386. t.Fatalf("jsonb_each columns = %v", res.Columns)
  387. }
  388. 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` {
  389. t.Fatalf("jsonb_each rows = %v", res.Rows)
  390. }
  391. if len(res.ColumnTypes) != 2 || res.ColumnTypes[1] != "JSONB" {
  392. t.Fatalf("jsonb_each column types = %v", res.ColumnTypes)
  393. }
  394. res = execMust(t, e, `SELECT e.key, e.value FROM jsonb_each('{"a":1}') AS e`)
  395. if res.RowCount != 1 || res.Rows[0][0] != "a" || res.Rows[0][1] != `1` {
  396. t.Fatalf("aliased jsonb_each rows = %v", res.Rows)
  397. }
  398. res = execMust(t, e, `SELECT key, value FROM jsonb_each_text('{"a":null,"b":true}')`)
  399. if res.RowCount != 2 || res.Rows[0][0] != "a" || res.Rows[0][1] != nil || res.Rows[1][1] != "true" {
  400. t.Fatalf("jsonb_each_text rows = %v", res.Rows)
  401. }
  402. res = execMust(t, e, `SELECT value FROM jsonb_array_elements('[1,{"a":2}]')`)
  403. if res.RowCount != 2 || res.Rows[0][0] != `1` || res.Rows[1][0] != `{"a":2}` {
  404. t.Fatalf("jsonb_array_elements rows = %v", res.Rows)
  405. }
  406. if res.ColumnTypes[0] != "JSONB" {
  407. t.Fatalf("jsonb_array_elements type = %v", res.ColumnTypes)
  408. }
  409. res = execMust(t, e, `SELECT value FROM jsonb_array_elements_text('[1,null,"x"]')`)
  410. if res.RowCount != 3 || res.Rows[0][0] != "1" || res.Rows[1][0] != nil || res.Rows[2][0] != "x" {
  411. t.Fatalf("jsonb_array_elements_text rows = %v", res.Rows)
  412. }
  413. res = execMust(t, e, `SELECT jsonb_object_keys FROM jsonb_object_keys('{"b":1,"a":2}')`)
  414. if res.RowCount != 2 || res.Rows[0][0] != "a" || res.Rows[1][0] != "b" {
  415. t.Fatalf("jsonb_object_keys rows = %v", res.Rows)
  416. }
  417. // A NULL document produces no rows.
  418. res = execMust(t, e, `SELECT * FROM jsonb_array_elements(NULL)`)
  419. if res.RowCount != 0 {
  420. t.Fatalf("jsonb_array_elements(NULL) rows = %v", res.Rows)
  421. }
  422. // Wrong document types and projections of set-returning functions are
  423. // rejected clearly instead of executing as scalar calls.
  424. for _, tc := range []struct {
  425. sql string
  426. code string
  427. }{
  428. {`SELECT * FROM jsonb_array_elements('{"a":1}')`, pgerror.CodeInvalidParameter},
  429. {`SELECT * FROM jsonb_each('[1]')`, pgerror.CodeInvalidParameter},
  430. {`SELECT * FROM jsonb_object_keys('[1]')`, pgerror.CodeInvalidParameter},
  431. {`SELECT * FROM jsonb_each('null')`, pgerror.CodeInvalidParameter},
  432. {`SELECT * FROM jsonb_array_elements('null')`, pgerror.CodeInvalidParameter},
  433. {`SELECT jsonb_each('{"a":1}')`, pgerror.CodeFeatureNotSupported},
  434. {`SELECT jsonb_array_elements('[1]')`, pgerror.CodeFeatureNotSupported},
  435. {`SELECT * FROM jsonb_each('{"a":1}') AS e JOIN docs d ON TRUE`, pgerror.CodeFeatureNotSupported},
  436. } {
  437. if _, err := execSQL(e, tc.sql); jsonbErrorCode(t, err) != tc.code {
  438. t.Errorf("%s error = %v, want %s", tc.sql, err, tc.code)
  439. }
  440. }
  441. }
  442. func TestJSONBEqualityOrderingAndDistinct(t *testing.T) {
  443. _, schema, table := newTestDB(t)
  444. e := newExec(schema, table)
  445. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  446. execMust(t, e, `INSERT INTO docs VALUES (1, '{"b":1,"a":2}'), (2, '{"a":2,"b":1}'), (3, '[1,2]')`)
  447. if got := jsonbValue(t, e, `SELECT '{"b":1,"a":2}'::jsonb = '{"a":2, "b": 1}'::jsonb`); got != true {
  448. t.Fatalf("jsonb equality = %v, want true", got)
  449. }
  450. if got := jsonbValue(t, e, `SELECT '{"a":1}'::jsonb = '[1]'::jsonb`); got != false {
  451. t.Fatalf("jsonb inequality = %v, want false", got)
  452. }
  453. if got := jsonbValue(t, e, `SELECT doc IS NOT DISTINCT FROM '{"a":2,"b":1}'::jsonb FROM docs WHERE id = 2`); got != true {
  454. t.Fatalf("IS NOT DISTINCT FROM = %v", got)
  455. }
  456. if got := jsonbValue(t, e, `SELECT '{"b":1,"a":2}'::jsonb IS DISTINCT FROM '{"a":2,"b":1.0}'::jsonb`); got != false {
  457. t.Fatalf("IS DISTINCT FROM key-order/number-spelling = %v, want false", got)
  458. }
  459. res := execMust(t, e, `SELECT COUNT(*) FROM docs WHERE doc = '{"a":2,"b":1}'::jsonb`)
  460. if res.Rows[0][0] != int64(2) {
  461. t.Fatalf("key-order-independent equality matched %v rows, want 2", res.Rows[0][0])
  462. }
  463. res = execMust(t, e, `SELECT DISTINCT doc FROM docs ORDER BY doc`)
  464. if res.RowCount != 2 {
  465. t.Fatalf("DISTINCT jsonb rows = %v, want 2", res.Rows)
  466. }
  467. // Canonical numeric scalars order numerically, and non-scalars order by
  468. // canonical text.
  469. execMust(t, e, "CREATE TABLE scalars (id INTEGER PRIMARY KEY, v JSONB)")
  470. execMust(t, e, `INSERT INTO scalars VALUES (1, '10'), (2, '2')`)
  471. res = execMust(t, e, `SELECT v FROM scalars ORDER BY v`)
  472. if res.RowCount != 2 || res.Rows[0][0] != `2` || res.Rows[1][0] != `10` {
  473. t.Fatalf("jsonb scalar ordering = %v", res.Rows)
  474. }
  475. res = execMust(t, e, `SELECT doc FROM docs WHERE id IN (1,3) ORDER BY doc`)
  476. if res.RowCount != 2 || res.Rows[0][0] != `[1,2]` || res.Rows[1][0] != `{"a":2,"b":1}` {
  477. t.Fatalf("jsonb text ordering = %v", res.Rows)
  478. }
  479. // A jsonb column cannot be compared with a number.
  480. if _, err := execSQL(e, `SELECT id FROM docs WHERE doc = 5`); err == nil {
  481. t.Fatal("jsonb = integer was accepted")
  482. }
  483. }
  484. func TestJSONBRejectedShapes(t *testing.T) {
  485. _, schema, table := newTestDB(t)
  486. e := newExec(schema, table)
  487. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  488. for _, sql := range []string{
  489. `SELECT jsonb_path_exists('{"a":1}', '$.a')`,
  490. `SELECT jsonb_path_query('{"a":1}', '$.a')`,
  491. `CREATE INDEX docs_gin ON docs USING GIN (doc)`,
  492. `CREATE INDEX docs_expr ON docs ((doc -> 'a'))`,
  493. `CREATE TABLE bad_arrays (doc JSONB[])`,
  494. } {
  495. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeFeatureNotSupported {
  496. t.Errorf("%s error = %v, want 0A000", sql, err)
  497. }
  498. }
  499. // A scalar extraction index over jsonb is a text expression and stays legal.
  500. execMust(t, e, `CREATE INDEX docs_kind ON docs ((doc ->> 'kind'))`)
  501. }
  502. func TestJSONBColumnMetadataAndDefaults(t *testing.T) {
  503. _, schema, table := newTestDB(t)
  504. e := newExec(schema, table)
  505. execMust(t, e, `CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB DEFAULT '{"b":2,"a":1}')`)
  506. execMust(t, e, `INSERT INTO docs (id) VALUES (1)`)
  507. res := execMust(t, e, `SELECT doc FROM docs`)
  508. if res.Rows[0][0] != `{"a":1,"b":2}` {
  509. t.Fatalf("canonical default = %v", res.Rows[0][0])
  510. }
  511. if len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "JSONB" {
  512. t.Fatalf("column types = %v, want [JSONB]", res.ColumnTypes)
  513. }
  514. // ALTER TABLE backfills the canonical default into existing rows.
  515. execMust(t, e, "CREATE TABLE raw (id INTEGER PRIMARY KEY)")
  516. execMust(t, e, "INSERT INTO raw VALUES (1)")
  517. execMust(t, e, `ALTER TABLE raw ADD COLUMN doc JSONB DEFAULT '{"b":2,"a":1}'`)
  518. res = execMust(t, e, `SELECT doc FROM raw`)
  519. if res.Rows[0][0] != `{"a":1,"b":2}` {
  520. t.Fatalf("backfilled default = %v", res.Rows[0][0])
  521. }
  522. // UPDATE canonicalizes too.
  523. execMust(t, e, `UPDATE raw SET doc = '{"z": 1.50}'`)
  524. res = execMust(t, e, `SELECT doc FROM raw`)
  525. if res.Rows[0][0] != `{"z":1.5}` {
  526. t.Fatalf("updated doc = %v", res.Rows[0][0])
  527. }
  528. }
  529. func TestCanonicalJSONBNumber(t *testing.T) {
  530. cases := map[string]string{
  531. "0": "0",
  532. "-0": "0",
  533. "-0.0": "0",
  534. "1": "1",
  535. "1.0": "1",
  536. "1.50": "1.5",
  537. "1e2": "100",
  538. "1E+2": "100",
  539. "1e-2": "0.01",
  540. "-12.340": "-12.34",
  541. "123456789012345678901234567890": "123456789012345678901234567890",
  542. }
  543. for input, want := range cases {
  544. got, err := canonicalJSONBNumber(json.Number(input))
  545. if err != nil || got != want {
  546. t.Errorf("canonicalJSONBNumber(%q) = %q, %v; want %q", input, got, err, want)
  547. }
  548. }
  549. for _, input := range []string{"1e999999", "1e-999999"} {
  550. if _, err := canonicalJSONBNumber(json.Number(input)); err == nil {
  551. t.Errorf("canonicalJSONBNumber(%q) accepted an unbounded expansion", input)
  552. }
  553. }
  554. }
  555. // JSON operations must keep working from a derived table and a CTE, where the
  556. // column type travels through Result column metadata.
  557. func TestJSONBTypePropagation(t *testing.T) {
  558. _, schema, table := newTestDB(t)
  559. e := newExec(schema, table)
  560. execMust(t, e, "CREATE TABLE docs (id INTEGER PRIMARY KEY, doc JSONB)")
  561. execMust(t, e, `INSERT INTO docs VALUES (1, '{"a":1}')`)
  562. if got := jsonbValue(t, e, `SELECT doc -> 'a' FROM (SELECT doc FROM docs) AS d`); got != `1` {
  563. t.Fatalf("derived jsonb -> = %v", got)
  564. }
  565. if got := jsonbValue(t, e, `SELECT doc ? 'a' FROM (SELECT doc FROM docs) AS d`); got != true {
  566. t.Fatalf("derived jsonb ? = %v", got)
  567. }
  568. if got := jsonbValue(t, e, `WITH c AS (SELECT doc FROM docs) SELECT doc @> '{"a":1}' FROM c`); got != true {
  569. t.Fatalf("CTE jsonb @> = %v", got)
  570. }
  571. if got := jsonbValue(t, e, `SELECT COALESCE(doc, '{}'::jsonb) ->> 'a' FROM docs`); got != "1" {
  572. t.Fatalf("COALESCE jsonb ->> = %v", got)
  573. }
  574. // A jsonb column keeps its type when it is the left side of a join and when
  575. // it is referenced through an alias.
  576. 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}'`)
  577. if res.RowCount != 1 || res.Rows[0][0] != int64(1) {
  578. t.Fatalf("joined jsonb containment rows = %v", res.Rows)
  579. }
  580. if got := jsonbValue(t, e, `SELECT a.doc -> 'a' FROM docs AS a`); got != `1` {
  581. t.Fatalf("aliased jsonb -> = %v", got)
  582. }
  583. if got := jsonbValue(t, e, `SELECT MAX(jsonb_build_object('a', doc)) FROM docs`); got != `{"a":{"a":1}}` {
  584. t.Fatalf("jsonb build function inside an aggregate = %v", got)
  585. }
  586. }
  587. func TestJSONBRejectsUnboundedCanonicalExpansion(t *testing.T) {
  588. _, schema, table := newTestDB(t)
  589. e := newExec(schema, table)
  590. if _, err := execSQL(e, `SELECT '{"a":1e999999}'::jsonb`); !strings.Contains(err.Error(), "invalid input syntax") {
  591. t.Fatalf("unbounded number error = %v", err)
  592. }
  593. }
  594. // A PostgreSQL path element is resolved against the container it reaches:
  595. // numeric-looking text is an object key in an object and an integer index in an
  596. // array. This must hold for #>, #>>, #-, jsonb_set, and jsonb_insert.
  597. func TestJSONBNumericLookingObjectKeys(t *testing.T) {
  598. _, schema, table := newTestDB(t)
  599. e := newExec(schema, table)
  600. cases := []struct {
  601. sql string
  602. want interface{}
  603. }{
  604. {`SELECT '{"1":{"2":3}}'::jsonb #> '{1,2}'`, `3`},
  605. {`SELECT '{"1":{"2":3}}'::jsonb #>> '{1,2}'`, `3`},
  606. {`SELECT '{"1":{"2":3}}'::jsonb #- '{1,2}'`, `{"1":{}}`},
  607. {`SELECT jsonb_set('{"1":{"2":3}}', '{1,2}', '4')`, `{"1":{"2":4}}`},
  608. {`SELECT jsonb_insert('{"1":{"2":3}}', '{1,3}', '4')`, `{"1":{"2":3,"3":4}}`},
  609. {`SELECT '{"01":"x"}'::jsonb #> '{01}'`, `"x"`},
  610. {`SELECT '{"-1":"x"}'::jsonb #> '{-1}'`, `"x"`},
  611. {`SELECT '{"1.5":"x"}'::jsonb #> '{1.5}'`, `"x"`},
  612. {`SELECT jsonb_set('{"01":"x"}', '{01}', '"y"')`, `{"01":"y"}`},
  613. {`SELECT jsonb_insert('{"01":"x"}', '{02}', '"y"')`, `{"01":"x","02":"y"}`},
  614. {`SELECT jsonb_set('{"-1":"x"}', '{-1}', '"y"')`, `{"-1":"y"}`},
  615. {`SELECT jsonb_insert('{"-1":"x"}', '{-2}', '"y"')`, `{"-1":"x","-2":"y"}`},
  616. {`SELECT '{"1.5":"x"}'::jsonb #- '{1.5}'`, `{}`},
  617. {`SELECT jsonb_set('{"1.5":"x"}', '{1.5}', '"y"')`, `{"1.5":"y"}`},
  618. {`SELECT '[10,20]'::jsonb #> '{01}'`, `20`},
  619. {`SELECT '[10,20]'::jsonb #> '{-1}'`, `20`},
  620. {`SELECT '[10,20]'::jsonb #> '{a}'`, nil},
  621. }
  622. for _, tc := range cases {
  623. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  624. t.Errorf("%s = %v (%T), want %v", tc.sql, got, got, tc.want)
  625. }
  626. }
  627. // A non-integer path element on an array is an error for #-, matching
  628. // PostgreSQL's path mutation and delete behavior.
  629. if _, err := execSQL(e, `SELECT '[10,20]'::jsonb #- '{a}'`); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  630. t.Errorf("#- array non-integer element error = %v, want 22P02", err)
  631. }
  632. }
  633. // #- must produce jsonb metadata and keep jsonb semantics for chained
  634. // operators, not fall back to text.
  635. func TestJSONBPathDeleteKeepsJSONBType(t *testing.T) {
  636. _, schema, table := newTestDB(t)
  637. e := newExec(schema, table)
  638. res := execMust(t, e, `SELECT '{"t":true}'::jsonb #- '{x}'`)
  639. if len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "JSONB" {
  640. t.Fatalf("#- column type = %v, want JSONB", res.ColumnTypes)
  641. }
  642. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') ->> 't'`); got != "true" {
  643. t.Fatalf("#- ->> = %v (%T), want true text", got, got)
  644. }
  645. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') = '{"t":true}'::jsonb`); got != true {
  646. t.Fatalf("#- jsonb equality = %v", got)
  647. }
  648. if got := jsonbValue(t, e, `SELECT ('{"t":true}'::jsonb #- '{x}') ? 't'`); got != true {
  649. t.Fatalf("#- ? = %v", got)
  650. }
  651. }
  652. // jsonb equality and ordering use exact canonical bytes, not float64. DISTINCT,
  653. // GROUP BY, and containment must agree with =.
  654. func TestJSONBExactEqualityAndAggregateAgreement(t *testing.T) {
  655. _, schema, table := newTestDB(t)
  656. e := newExec(schema, table)
  657. for _, sql := range []string{
  658. `SELECT '1'::jsonb = '1.0000000000000000001'::jsonb`,
  659. `SELECT '9007199254740993'::jsonb = '9007199254740992'::jsonb`,
  660. } {
  661. if got := jsonbValue(t, e, sql); got != false {
  662. t.Errorf("%s = %v, want false", sql, got)
  663. }
  664. }
  665. if got := jsonbValue(t, e, `SELECT '1'::jsonb = '1.0'::jsonb`); got != true {
  666. t.Fatalf("1 = 1.0 = %v, want true", got)
  667. }
  668. if got := jsonbValue(t, e, `SELECT '1'::jsonb < '1.0000000000000000001'::jsonb`); got != true {
  669. t.Fatalf("1 < 1.0000000000000000001 = %v, want true", got)
  670. }
  671. if got := jsonbValue(t, e, `SELECT '1'::jsonb IS DISTINCT FROM '1.0000000000000000001'::jsonb`); got != true {
  672. t.Fatalf("1 IS DISTINCT FROM 1.0000000000000000001 = %v, want true", got)
  673. }
  674. if got := jsonbValue(t, e, `SELECT CASE '1'::jsonb WHEN '1.0000000000000000001'::jsonb THEN 'same' ELSE 'different' END`); got != "different" {
  675. t.Fatalf("simple CASE exact jsonb equality = %v, want different", got)
  676. }
  677. if got := jsonbValue(t, e, `SELECT '1'::jsonb IN ('1.0'::jsonb, '1.0000000000000000001'::jsonb)`); got != true {
  678. t.Fatalf("jsonb IN = %v, want true", got)
  679. }
  680. if got := jsonbValue(t, e, `SELECT '{"n":1}'::jsonb @> '{"n":1.0000000000000000001}'::jsonb`); got != false {
  681. t.Fatalf("containment rounded a number: %v, want false", got)
  682. }
  683. if got := jsonbValue(t, e, `SELECT '{"n":1}'::jsonb @> '{"n":1.0}'::jsonb`); got != true {
  684. t.Fatalf("containment of equal canonical numbers = %v, want true", got)
  685. }
  686. execMust(t, e, "CREATE TABLE exact (id INTEGER PRIMARY KEY, doc JSONB)")
  687. execMust(t, e, `INSERT INTO exact VALUES (1, '1'), (2, '1.0'), (3, '1.0000000000000000001'), (4, '9007199254740993'), (5, '9007199254740992')`)
  688. if res := execMust(t, e, `SELECT DISTINCT doc FROM exact`); res.RowCount != 4 {
  689. t.Fatalf("DISTINCT exact jsonb rows = %v, want 4", res.Rows)
  690. }
  691. res := execMust(t, e, `SELECT doc, COUNT(*) FROM exact GROUP BY doc ORDER BY doc`)
  692. if res.RowCount != 4 {
  693. t.Fatalf("GROUP BY exact jsonb rows = %v, want 4", res.Rows)
  694. }
  695. if res.Rows[0][1] != int64(2) {
  696. t.Fatalf("GROUP BY merged canonical numbers = %v, want two spellings of 1", res.Rows[0])
  697. }
  698. res = execMust(t, e, `SELECT COUNT(*) FROM exact WHERE doc = '1'::jsonb`)
  699. if res.Rows[0][0] != int64(2) {
  700. t.Fatalf("equality count = %v, want 2", res.Rows[0][0])
  701. }
  702. res = execMust(t, e, `SELECT COUNT(*) FROM exact WHERE doc @> '1'::jsonb`)
  703. if res.Rows[0][0] != int64(2) {
  704. t.Fatalf("containment count = %v, want 2", res.Rows[0][0])
  705. }
  706. }
  707. // A jsonb extraction result must not be silently compared with a SQL number.
  708. func TestJSONBExtractionComparisonTypeError(t *testing.T) {
  709. _, schema, table := newTestDB(t)
  710. e := newExec(schema, table)
  711. for _, sql := range []string{
  712. `SELECT '{"a":1}'::jsonb -> 'a' = 1`,
  713. `SELECT '{"a":1}'::jsonb -> 'a' = TRUE`,
  714. } {
  715. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeDatatypeMismatch {
  716. t.Errorf("%s error = %v, want 42804", sql, err)
  717. }
  718. }
  719. if got := jsonbValue(t, e, `SELECT '{"a":1}'::jsonb -> 'a' = '1'::jsonb`); got != true {
  720. t.Fatalf("jsonb -> = jsonb = %v, want true", got)
  721. }
  722. }
  723. // PostgreSQL array containment is duplicate-insensitive: each candidate
  724. // element only needs some matching container element, and duplicates may
  725. // consume the same match.
  726. func TestJSONBArrayContainmentDuplicates(t *testing.T) {
  727. _, schema, table := newTestDB(t)
  728. e := newExec(schema, table)
  729. cases := []struct {
  730. sql string
  731. want interface{}
  732. }{
  733. {`SELECT '[1]'::jsonb @> '[1,1]'::jsonb`, true},
  734. {`SELECT '[1,1]'::jsonb @> '[1]'::jsonb`, true},
  735. {`SELECT '[1,2,2]'::jsonb @> '[2,2]'::jsonb`, true},
  736. {`SELECT '[1,2]'::jsonb @> '[2,2]'::jsonb`, true},
  737. {`SELECT '[1,2]'::jsonb @> '[2,3]'::jsonb`, false},
  738. {`SELECT '[1]'::jsonb @> '1'::jsonb`, true},
  739. }
  740. for _, tc := range cases {
  741. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  742. t.Errorf("%s = %v, want %v", tc.sql, got, tc.want)
  743. }
  744. }
  745. }
  746. // A JSONB column written before native JSONB may hold non-canonical text. Reads
  747. // canonicalize it so projection, DISTINCT, GROUP BY, and ORDER BY all observe
  748. // the canonical value.
  749. func TestJSONBLegacyValuesCanonicalizeOnRead(t *testing.T) {
  750. _, schema, table := newTestDB(t)
  751. e := newExec(schema, table)
  752. execMust(t, e, "CREATE TABLE legacy (id INTEGER PRIMARY KEY, doc JSONB)")
  753. legacy := []string{
  754. `{ "a" : 1 }`,
  755. `{"a":1}`,
  756. ` { "a" :1 } `,
  757. `{"b": 2,"a": 1}`,
  758. `{"a":1,"b":2}`,
  759. ` { "b" : 2, "a" : 1 } `,
  760. }
  761. for i, raw := range legacy {
  762. if err := table.Insert("legacy", storage.Row{"id": int64(i + 1), "doc": raw}); err != nil {
  763. t.Fatalf("legacy insert %d: %v", i, err)
  764. }
  765. }
  766. e = newExec(schema, table)
  767. res := execMust(t, e, `SELECT doc FROM legacy ORDER BY id`)
  768. if res.RowCount != len(legacy) {
  769. t.Fatalf("legacy rows = %d", res.RowCount)
  770. }
  771. for _, row := range res.Rows {
  772. if row[0] != `{"a":1}` && row[0] != `{"a":1,"b":2}` {
  773. t.Fatalf("legacy read = %v, want canonical text", row[0])
  774. }
  775. }
  776. res = execMust(t, e, `SELECT DISTINCT doc FROM legacy`)
  777. if res.RowCount != 2 {
  778. t.Fatalf("legacy DISTINCT rows = %v, want 2", res.Rows)
  779. }
  780. res = execMust(t, e, `SELECT DISTINCT * FROM legacy`)
  781. if res.RowCount != len(legacy) {
  782. t.Fatalf("legacy DISTINCT * rows = %d", res.RowCount)
  783. }
  784. res = execMust(t, e, `SELECT * FROM legacy ORDER BY id`)
  785. for _, row := range res.Rows {
  786. if row[1] != `{"a":1}` && row[1] != `{"a":1,"b":2}` {
  787. t.Fatalf("legacy SELECT * read = %v, want canonical text", row[1])
  788. }
  789. }
  790. if got := jsonbValue(t, e, `SELECT COUNT(DISTINCT doc) FROM legacy`); got != int64(2) {
  791. t.Fatalf("legacy COUNT(DISTINCT doc) = %v, want 2", got)
  792. }
  793. res = execMust(t, e, `SELECT doc, COUNT(*) FROM legacy GROUP BY doc`)
  794. if res.RowCount != 2 {
  795. t.Fatalf("legacy GROUP BY rows = %v, want 2", res.Rows)
  796. }
  797. res = execMust(t, e, `SELECT id FROM legacy ORDER BY doc, id`)
  798. if res.RowCount != len(legacy) {
  799. t.Fatalf("legacy ORDER BY rows = %d", res.RowCount)
  800. }
  801. if got := jsonbValue(t, e, `SELECT doc FROM legacy WHERE id = 1`); got != `{"a":1}` {
  802. t.Fatalf("legacy selected value = %v, want canonical", got)
  803. }
  804. }
  805. // Escapes that encoding/json would silently rewrite are invalid jsonb input.
  806. func TestJSONBRejectsNULAndLoneSurrogates(t *testing.T) {
  807. _, schema, table := newTestDB(t)
  808. e := newExec(schema, table)
  809. for _, sql := range []string{
  810. `SELECT '{"a":"\u0000"}'::jsonb`,
  811. `SELECT '"\ud800"'::jsonb`,
  812. `SELECT '"\ud800x"'::jsonb`,
  813. `SELECT '"\udc00"'::jsonb`,
  814. `SELECT '{"\u0000":1}'::jsonb`,
  815. } {
  816. if _, err := execSQL(e, sql); jsonbErrorCode(t, err) != pgerror.CodeInvalidText {
  817. t.Errorf("%s error = %v, want 22P02", sql, err)
  818. }
  819. }
  820. if got := jsonbValue(t, e, `SELECT '"\u0041"'::jsonb`); got != `"A"` {
  821. t.Fatalf("escaped ASCII = %v", got)
  822. }
  823. if got := jsonbValue(t, e, `SELECT '"\ud83d\ude00"'::jsonb`); got != "\"\U0001F600\"" {
  824. t.Fatalf("surrogate pair = %v", got)
  825. }
  826. }
  827. // Zero-row text set-returning functions must still report TEXT for the value
  828. // column.
  829. func TestJSONBTextSetReturningTypesOnNull(t *testing.T) {
  830. _, schema, table := newTestDB(t)
  831. e := newExec(schema, table)
  832. res := execMust(t, e, `SELECT * FROM jsonb_each_text(NULL)`)
  833. if res.RowCount != 0 || len(res.ColumnTypes) != 2 || res.ColumnTypes[0] != "TEXT" || res.ColumnTypes[1] != "TEXT" {
  834. t.Fatalf("jsonb_each_text(NULL) = rows %d types %v, want 0 [TEXT TEXT]", res.RowCount, res.ColumnTypes)
  835. }
  836. res = execMust(t, e, `SELECT * FROM jsonb_array_elements_text(NULL)`)
  837. if res.RowCount != 0 || len(res.ColumnTypes) != 1 || res.ColumnTypes[0] != "TEXT" {
  838. t.Fatalf("jsonb_array_elements_text(NULL) = rows %d types %v, want 0 [TEXT]", res.RowCount, res.ColumnTypes)
  839. }
  840. }
  841. // JSON1 $ path patch values may be non-integral numeric expressions. They are
  842. // normalized to json.Number before encoding, including values read from REAL
  843. // columns.
  844. func TestJSONBJSON1PatchFloatValues(t *testing.T) {
  845. _, schema, table := newTestDB(t)
  846. e := newExec(schema, table)
  847. execMust(t, e, "CREATE TABLE realvals (id INTEGER PRIMARY KEY, r REAL)")
  848. execMust(t, e, `INSERT INTO realvals VALUES (1, 1.5)`)
  849. for _, tc := range []struct {
  850. sql string
  851. want string
  852. }{
  853. {`SELECT jsonb_set('{"a":1}', '$.b', 1.5)`, `{"a":1,"b":1.5}`},
  854. {`SELECT jsonb_set('{"a":1}', '$.b', 2.5)`, `{"a":1,"b":2.5}`},
  855. {`SELECT jsonb_insert('{}', '$.b', 2.5)`, `{"b":2.5}`},
  856. {`SELECT jsonb_replace('{"b":0}', '$.b', 2.5)`, `{"b":2.5}`},
  857. {`SELECT jsonb_set('{"a":1}', '$.b', r) FROM realvals`, `{"a":1,"b":1.5}`},
  858. {`SELECT jsonb_insert('{}', '$.b', r) FROM realvals`, `{"b":1.5}`},
  859. } {
  860. if got := jsonbValue(t, e, tc.sql); got != tc.want {
  861. t.Errorf("%s = %v, want %s", tc.sql, got, tc.want)
  862. }
  863. }
  864. }
  865. // HAVING evaluates simple CASE and IS DISTINCT FROM through the aggregate
  866. // expression path; it must use the same exact jsonb comparison as the
  867. // top-level evaluators.
  868. func TestJSONBAggregateHavingExactJSONB(t *testing.T) {
  869. _, schema, table := newTestDB(t)
  870. e := newExec(schema, table)
  871. execMust(t, e, "CREATE TABLE ex (id INTEGER PRIMARY KEY, doc JSONB)")
  872. execMust(t, e, `INSERT INTO ex VALUES (1, '1'), (2, '1.0000000000000000001')`)
  873. if got := jsonbValue(t, e, `SELECT COUNT(*) FROM ex GROUP BY doc HAVING '1'::jsonb IS DISTINCT FROM '1.0000000000000000001'::jsonb`); got != int64(1) {
  874. t.Fatalf("HAVING IS DISTINCT FROM group count = %v, want 1", got)
  875. }
  876. res := execMust(t, e, `SELECT doc, COUNT(*) FROM ex GROUP BY doc HAVING '1'::jsonb IS DISTINCT FROM '1.0000000000000000001'::jsonb ORDER BY doc`)
  877. if res.RowCount != 2 {
  878. t.Fatalf("HAVING IS DISTINCT FROM rows = %v, want 2 groups", res.Rows)
  879. }
  880. res = execMust(t, e, `SELECT doc, COUNT(*) FROM ex GROUP BY doc HAVING '1'::jsonb IS NOT DISTINCT FROM '1.0000000000000000001'::jsonb`)
  881. if res.RowCount != 0 {
  882. t.Fatalf("HAVING IS NOT DISTINCT FROM rows = %v, want 0", res.Rows)
  883. }
  884. res = execMust(t, e, `SELECT doc, COUNT(*) FROM ex GROUP BY doc HAVING CASE '1'::jsonb WHEN '1.0000000000000000001'::jsonb THEN FALSE ELSE TRUE END ORDER BY doc`)
  885. if res.RowCount != 2 {
  886. t.Fatalf("HAVING CASE rows = %v, want 2 groups", res.Rows)
  887. }
  888. }