| 1 |
--echo # Test of the JSON functions used in SQL statements |
| 2 |
--echo # ---------------------------------------------------------------------- |
| 3 |
|
| 4 |
SET NAMES utf8; |
| 5 |
CREATE TABLE t1 (pk int NOT NULL PRIMARY KEY AUTO_INCREMENT, i INT, j JSON); |
| 6 |
INSERT INTO t1(i, j) VALUES (0, NULL); |
| 7 |
INSERT INTO t1(i, j) VALUES (1, '{"a": 2}'); |
| 8 |
INSERT INTO t1(i, j) VALUES (2, '[1,2]'); |
| 9 |
INSERT INTO t1(i, j) VALUES (3, '{"a":"b", "c":"d","ab":"abc", "bc": ["x", "y"]}'); |
| 10 |
INSERT INTO t1(i, j) VALUES (4, '["here", ["I", "am"], "!!!"]'); |
| 11 |
INSERT INTO t1(i, j) VALUES (5, '"scalar string"'); |
| 12 |
INSERT INTO t1(i, j) VALUES (6, 'true'); |
| 13 |
INSERT INTO t1(i, j) VALUES (7, 'false'); |
| 14 |
INSERT INTO t1(i, j) VALUES (8, 'null'); |
| 15 |
INSERT INTO t1(i, j) VALUES (9, '-1'); |
| 16 |
INSERT INTO t1(i, j) VALUES (10, CAST(CAST(1 AS UNSIGNED) AS JSON)); |
| 17 |
INSERT INTO t1(i, j) VALUES (11, '32767'); |
| 18 |
INSERT INTO t1(i, j) VALUES (12, '32768'); |
| 19 |
INSERT INTO t1(i, j) VALUES (13, '-32768'); |
| 20 |
INSERT INTO t1(i, j) VALUES (14, '-32769'); |
| 21 |
INSERT INTO t1(i, j) VALUES (15, '2147483647'); |
| 22 |
INSERT INTO t1(i, j) VALUES (16, '2147483648'); |
| 23 |
INSERT INTO t1(i, j) VALUES (17, '-2147483648'); |
| 24 |
INSERT INTO t1(i, j) VALUES (18, '-2147483649'); |
| 25 |
INSERT INTO t1(i, j) VALUES (19, '18446744073709551615'); |
| 26 |
INSERT INTO t1(i, j) VALUES (20, '18446744073709551616'); |
| 27 |
INSERT INTO t1(i, j) VALUES (21, '3.14'); |
| 28 |
INSERT INTO t1(i, j) VALUES (22, '{}'); |
| 29 |
INSERT INTO t1(i, j) VALUES (23, '[]'); |
| 30 |
INSERT INTO t1(i, j) VALUES (24, CAST(CAST('2015-01-15 23:24:25' AS DATETIME) AS JSON)); |
| 31 |
INSERT INTO t1(i, j) VALUES (25, CAST(CAST('23:24:25' AS TIME) AS JSON)); |
| 32 |
INSERT INTO t1(i, j) VALUES (26, CAST(CAST('2015-01-15' AS DATE) AS JSON)); |
| 33 |
INSERT INTO t1(i, j) VALUES (27, CAST(TIMESTAMP'2015-01-15 23:24:25' AS JSON)); |
| 34 |
INSERT INTO t1(i, j) VALUES (28, CAST(ST_GeomFromText('POINT(1 1)') AS JSON)); |
| 35 |
# auto-convert to utf8mb4 |
| 36 |
INSERT INTO t1(i, j) VALUES (29, CAST('[]' AS CHAR CHARACTER SET 'ascii')); |
| 37 |
INSERT INTO t1(i, j) VALUES (30, CAST(x'cafe' AS JSON)); |
| 38 |
INSERT INTO t1(i, j) VALUES (31, CAST(x'cafebabe' AS JSON)); |
| 39 |
|
| 40 |
|
| 41 |
--echo # |
| 42 |
--echo # Test of JSON comparator. |
| 43 |
--echo # |
| 44 |
|
| 45 |
SELECT i, |
| 46 |
(j = '"scalar string"') AS c1, |
| 47 |
(j = 'scalar string') AS c2, |
| 48 |
(j = CAST('"scalar string"' AS JSON)) AS c3, |
| 49 |
(j = CAST(CAST(j AS CHAR CHARACTER SET 'utf8mb4') AS JSON)) AS c4, |
| 50 |
(j = CAST(NULL AS JSON)) AS c5, |
| 51 |
(j = NULL) AS c6, |
| 52 |
(j <=> NULL) AS c7, |
| 53 |
(j <=> CAST(NULL AS JSON)) AS c8, |
| 54 |
(j IN (-1, 2, 32768, 3.14)) AS c9, |
| 55 |
(j IN (CAST('[1, 2]' AS JSON), CAST('{}' AS JSON), CAST(3.14 AS JSON))) AS c10, |
| 56 |
(j = (SELECT j FROM t1 WHERE j = CAST('null' AS JSON))) AS c11, |
| 57 |
(j = (SELECT j FROM t1 WHERE j IS NULL)) AS c12, |
| 58 |
(j = (SELECT j FROM t1 WHERE 1<>1)) AS c13, |
| 59 |
(j = DATE'2015-01-15') AS c14, |
| 60 |
(j = TIME'23:24:25') AS c15, |
| 61 |
(j = TIMESTAMP'2015-01-15 23:24:25') AS c16, |
| 62 |
(j = CURRENT_TIMESTAMP) AS c17, |
| 63 |
(j = ST_GeomFromText('POINT(1 1)')) AS c18, |
| 64 |
(JSON_EXTRACT(j, '$.a') = 2) AS c19 |
| 65 |
FROM t1 |
| 66 |
ORDER BY i; |
| 67 |
|
| 68 |
SELECT i FROM t1 |
| 69 |
WHERE j = CAST(CAST(j AS CHAR CHARACTER SET 'utf8') AS JSON) |
| 70 |
ORDER BY i; |
| 71 |
SELECT CAST(NULL AS UNSIGNED) = CAST(NULL AS JSON); |
| 72 |
SELECT CAST(NULL AS JSON) = CAST(NULL AS JSON); |
| 73 |
SELECT CAST(NULL AS JSON) = NULL; |
| 74 |
SELECT CAST(1 AS JSON) = NULL; |
| 75 |
SELECT CAST('true' AS JSON) = 1; |
| 76 |
SELECT CAST('true' AS JSON) = true; |
| 77 |
|
| 78 |
SELECT a.i, b.i, a.j < b.j, a.j = b.j, a.j > b.j, a.j <> b.j, a.j <=> b.j |
| 79 |
FROM t1 a, t1 b |
| 80 |
ORDER BY a.i, b.i; |
| 81 |
|
| 82 |
--echo # Verify that the index on the int column is not used when |
| 83 |
--echo # comparing the int column to a JSON column. The two columns |
| 84 |
--echo # should be compared using the JSON comparator. |
| 85 |
CREATE TABLE t2(i int, j json); |
| 86 |
CREATE INDEX t2_i ON t2(i); |
| 87 |
INSERT INTO t2 values (1, CAST(1 AS JSON)); |
| 88 |
INSERT INTO t2 values (1, CAST('"1"' AS JSON)); |
| 89 |
ANALYZE TABLE t2; |
| 90 |
let $query=SELECT * FROM t2 where i = j; |
| 91 |
eval EXPLAIN $query; |
| 92 |
eval $query; |
| 93 |
DROP TABLE t2; |
| 94 |
|
| 95 |
# Create a table full of JSON numeric scalars to verify that the JSON |
| 96 |
# comparator returns the expected result when comparing all |
| 97 |
# combinations of those values. |
| 98 |
# |
| 99 |
# The values should be inserted in ascending order. The table has a |
| 100 |
# rank column that tells how the comparator is expected to order the |
| 101 |
# JSON values. If two rows have the same rank, the comparator is |
| 102 |
# expected to say that the JSON values on the two rows are equal. If a |
| 103 |
# row has a lower rank than another, the JSON value in that row is |
| 104 |
# expected to be smaller than the JSON value in the other row. |
| 105 |
CREATE TABLE numbers(id INT NOT NULL AUTO_INCREMENT, |
| 106 |
`rank` INT, |
| 107 |
j JSON, |
| 108 |
PRIMARY KEY(id)); |
| 109 |
INSERT INTO numbers(`rank`, j) VALUES |
| 110 |
(1, '-1e100'), |
| 111 |
(2, '-1e65'), |
| 112 |
# smallest DECIMAL (negative with 65 digits) |
| 113 |
(3, CAST(-99999999999999999999999999999999999999999999999999999999999999999 AS JSON)), |
| 114 |
(4, CAST(-9223372036854776001 AS JSON)), |
| 115 |
(5, CAST(-9223372036854776000 AS JSON)), |
| 116 |
# closest DOUBLE approximation of the smallest SIGNED BIGINT |
| 117 |
(5 /* same rank as previous */, '-9.223372036854776e18'), |
| 118 |
(6, CAST(-9223372036854775999 AS JSON)), |
| 119 |
(7, CAST(-9223372036854775809 AS JSON)), # smallest SIGNED BIGINT - 1 |
| 120 |
(8, CAST(-9223372036854775808 AS JSON)), # smallest SIGNED BIGINT |
| 121 |
(9, CAST(-9223372036854775807 AS JSON)), # smallest SIGNED BIGINT + 1 |
| 122 |
(10, '-1e-50'), # close to zero, fits in a DECIMAL |
| 123 |
(11, '-1.2345678901234e-71'), # has to be truncated to fit in a DECIMAL |
| 124 |
(12, CAST(-0.000000000000000000000000000000000000000000000000000000000000000000000012 AS JSON)), |
| 125 |
(12 /* same rank as previous */, '-1.2e-71'), |
| 126 |
(13, '-1.0345678901234e-71'), # has to be truncated to fit in a DECIMAL |
| 127 |
(14, '-1e-100'), # too close to zero to fit in a DECIMAL |
| 128 |
(15, '0'), |
| 129 |
(15 /* same rank as previous */, '0.0'), |
| 130 |
(15 /* same rank as previous */, '-0.0'), |
| 131 |
(15 /* same rank as previous */, CAST(0.0 AS JSON)), |
| 132 |
(15 /* same rank as previous */, CAST(CAST(-0.0e0 AS DECIMAL) AS JSON)), |
| 133 |
(16, '1e-100'), # too close to zero to fit in a DECIMAL |
| 134 |
(17, '1.0345678901234e-71'), # has to be truncated to fit in a DECIMAL |
| 135 |
(18, CAST(0.000000000000000000000000000000000000000000000000000000000000000000000012 AS JSON)), |
| 136 |
(18 /* same rank as previous */, '1.2e-71'), |
| 137 |
(19, '1.2345678901234e-71'), # has to be truncated to fit in a DECIMAL |
| 138 |
(20, '1e-50'), # close to zero, fits in a DECIMAL |
| 139 |
(21, CAST(9223372036854775806 AS JSON)), # largest SIGNED BIGINT - 1 |
| 140 |
(22, CAST(9223372036854775807 AS JSON)), # largest SIGNED BIGINT |
| 141 |
(23, CAST(9223372036854775808 AS JSON)), # largest SIGNED BIGINT + 1 |
| 142 |
(24, CAST(9223372036854775999 AS JSON)), |
| 143 |
# closest DOUBLE approximation of the largest SIGNED BIGINT |
| 144 |
(25, '9.223372036854776e18'), |
| 145 |
(25 /* same rank as previous */, CAST(9223372036854776000 AS JSON)), |
| 146 |
(26, CAST(9223372036854776001 AS JSON)), |
| 147 |
(27, CAST(18446744073709551614 AS JSON)), # largest UNSIGNED BIGINT - 1 |
| 148 |
(28, CAST(18446744073709551615 AS JSON)), # largest UNSIGNED BIGINT |
| 149 |
(29, CAST(18446744073709551616 AS JSON)), # largest UNSIGNED BIGINT + 1 |
| 150 |
# Gets converted to the closest DOUBLE approximation of UNSIGNED BIGINT + 1 |
| 151 |
# by the JSON parser |
| 152 |
(30, '18446744073709551616'), |
| 153 |
# biggest DECIMAL (65 digits) |
| 154 |
(31, CAST(99999999999999999999999999999999999999999999999999999999999999999 AS JSON)), |
| 155 |
(32, CAST('1e65' AS JSON)), |
| 156 |
(33, CAST('1e100' AS JSON)); |
| 157 |
SELECT *, JSON_TYPE(j) FROM numbers ORDER BY id; |
| 158 |
|
| 159 |
# Now compare every combination of scalars in the table using <, =, >, |
| 160 |
# <> and <=>, and cross-check the results against the ranks. The query |
| 161 |
# returns the rows where the comparison returned an unexpected result. |
| 162 |
# If all is well, the query returns no rows. |
| 163 |
SELECT a.j, b.j, a.j < b.j, a.j = b.j, a.j > b.j, a.j <=> b.j |
| 164 |
FROM numbers a, numbers b |
| 165 |
WHERE ((a.j < b.j) <> (a.`rank` < b.`rank`)) OR |
| 166 |
((a.j = b.j) <> (a.`rank` = b.`rank`)) OR |
| 167 |
((a.j > b.j) <> (a.`rank` > b.`rank`)) OR |
| 168 |
((a.j <=> b.j) <> (a.`rank` <=> b.`rank`)); |
| 169 |
|
| 170 |
DROP TABLE numbers; |
| 171 |
|
| 172 |
# Verify handling of errors during evaluation of the arguments to the |
| 173 |
# comparator, both in the left argument and in the right argument. |
| 174 |
CREATE TABLE t(txt TEXT); |
| 175 |
INSERT INTO t VALUES (''); |
| 176 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 177 |
SELECT COUNT(*) FROM t WHERE JSON_EXTRACT(txt, '$') = 5; |
| 178 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 179 |
SELECT COUNT(*) FROM t WHERE 5 = JSON_EXTRACT(txt, '$'); |
| 180 |
DROP TABLE t; |
| 181 |
|
| 182 |
--echo # |
| 183 |
--echo # WL#8539 - Ordering of scalar JSON values |
| 184 |
--echo # |
| 185 |
|
| 186 |
# Create some timestamps. |
| 187 |
CREATE TABLE timestamps (ts TIMESTAMP(6)); |
| 188 |
INSERT INTO timestamps VALUES |
| 189 |
('2000-01-01 00:00:00'), |
| 190 |
('2000-01-01 00:00:00.01'), |
| 191 |
('2000-01-01 00:00:00.001'), |
| 192 |
('2000-01-01 00:00:00.002'), |
| 193 |
('2000-01-01 00:00:00.02'), |
| 194 |
('2000-01-01 23:59:59.999999'), |
| 195 |
('2000-01-02 00:00:00'), |
| 196 |
('2000-02-01 00:00:00'), |
| 197 |
('2010-12-02 01:00:00'), |
| 198 |
('2010-12-02 01:02:00'), |
| 199 |
('2010-12-02 01:02:03'), |
| 200 |
('2010-12-02 02:01:00'), |
| 201 |
('1970-01-02 00:00:01'), |
| 202 |
('1970-01-02 00:00:01.000001'); |
| 203 |
SELECT * FROM timestamps ORDER BY CAST(ts AS JSON); |
| 204 |
|
| 205 |
# Create datetimes that correspond to the above timestamps, and add some values |
| 206 |
# that are outside the accepted range of the timestamp data type. |
| 207 |
CREATE TABLE datetimes (dt DATETIME(6)); |
| 208 |
INSERT INTO datetimes SELECT ts FROM timestamps; |
| 209 |
INSERT INTO datetimes VALUES |
| 210 |
('1960-01-02 03:04:05'), |
| 211 |
('1960-01-02 03:04:06'), |
| 212 |
('1000-01-01 00:00:00'), |
| 213 |
('9999-12-31 23:59:59.999999'); |
| 214 |
SELECT * FROM datetimes ORDER BY CAST(dt AS JSON); |
| 215 |
|
| 216 |
# Create some times using the time component of the above datetimes. Also add |
| 217 |
# some times that go outside of the 0-24 range of the time component of |
| 218 |
# datetime. |
| 219 |
CREATE TABLE times (t TIME(6)); |
| 220 |
INSERT INTO times SELECT DISTINCT TIME(dt) FROM datetimes; |
| 221 |
INSERT INTO times VALUES |
| 222 |
('-838:59:59'), |
| 223 |
('838:59:59'), |
| 224 |
('-00:00:00.000001'), |
| 225 |
('-00:00:00'), |
| 226 |
('24:00:00'), |
| 227 |
('-12:00:00'), |
| 228 |
('-24:00:00'); |
| 229 |
SELECT * FROM times ORDER BY CAST(t AS JSON); |
| 230 |
|
| 231 |
# Create dates using the date component of the above datetimes. |
| 232 |
CREATE TABLE dates(d DATE); |
| 233 |
INSERT INTO dates SELECT DISTINCT DATE(dt) FROM datetimes; |
| 234 |
|
| 235 |
# Create some signed integers. |
| 236 |
CREATE TABLE signed_integers(i BIGINT); |
| 237 |
INSERT INTO signed_integers VALUES |
| 238 |
(0), (1), (2), (3), (4), (5), (10), (11), (12), (20), (21), (22), |
| 239 |
(99), (100), (101), (999), (1000), (1001), |
| 240 |
(9223372036854775806), (9223372036854775807); |
| 241 |
INSERT INTO signed_integers SELECT -i FROM signed_integers; |
| 242 |
INSERT INTO signed_integers VALUES (-9223372036854775808); |
| 243 |
SELECT * FROM signed_integers ORDER BY CAST(i AS JSON); |
| 244 |
|
| 245 |
# Create some unsigned integers. |
| 246 |
CREATE TABLE unsigned_integers(i BIGINT UNSIGNED); |
| 247 |
INSERT INTO unsigned_integers SELECT i FROM signed_integers where i >= 0; |
| 248 |
INSERT INTO unsigned_integers VALUES |
| 249 |
(9223372036854775808), (18446744073709551614), (18446744073709551615); |
| 250 |
SELECT * FROM unsigned_integers ORDER BY CAST(i AS JSON); |
| 251 |
|
| 252 |
# Create some decimals. |
| 253 |
CREATE TABLE decimals (d DECIMAL(25,3)); |
| 254 |
INSERT INTO decimals SELECT i FROM signed_integers; |
| 255 |
INSERT INTO decimals SELECT i FROM unsigned_integers; |
| 256 |
INSERT INTO decimals VALUES |
| 257 |
(9223372036854776000), (-9223372036854776000), |
| 258 |
(9223372036854776001), (-9223372036854776001), |
| 259 |
(3.13), (3.14), (3.15), (-3.13), (-3.14), (-3.15), |
| 260 |
(3.131), (3.141), (3.151), (-3.131), (-3.141), (-3.151), |
| 261 |
(3.129), (3.139), (3.149), (-3.129), (-3.139), (-3.149), |
| 262 |
(0.1), (0.01), (0.001), (-0.1), (-0.01), (-0.001); |
| 263 |
SELECT * FROM decimals ORDER BY CAST(d AS JSON); |
| 264 |
|
| 265 |
# Create some doubles. |
| 266 |
CREATE TABLE doubles (d DOUBLE); |
| 267 |
INSERT INTO doubles SELECT d FROM decimals; |
| 268 |
INSERT INTO doubles VALUES |
| 269 |
(1.5E-200), (1.5E200), (-1.5E-200), (-1.5E200), |
| 270 |
(-1E-323), (-1E-322), (-1E-321), (1E-323), (1E-322), (1E-321), |
| 271 |
(-1E308), (-1E307), (-1E306), (1E308), (1E307), (1E306); |
| 272 |
SELECT * FROM doubles ORDER BY CAST(d AS JSON); |
| 273 |
|
| 274 |
# Now convert all of the above values to JSON. |
| 275 |
CREATE TABLE t(id INT PRIMARY KEY AUTO_INCREMENT, j JSON); |
| 276 |
INSERT INTO t(j) SELECT CAST(ts AS JSON) FROM timestamps ORDER BY ts; |
| 277 |
INSERT INTO t(j) SELECT CAST(dt AS JSON) FROM datetimes ORDER BY dt; |
| 278 |
INSERT INTO t(j) SELECT CAST(t AS JSON) FROM times ORDER BY t; |
| 279 |
INSERT INTO t(j) SELECT CAST(d AS JSON) FROM dates ORDER BY d; |
| 280 |
INSERT INTO t(j) SELECT CAST(i AS JSON) FROM signed_integers ORDER BY i; |
| 281 |
INSERT INTO t(j) SELECT CAST(i AS JSON) FROM unsigned_integers ORDER BY i; |
| 282 |
INSERT INTO t(j) SELECT CAST(d AS JSON) FROM decimals ORDER BY d; |
| 283 |
INSERT INTO t(j) SELECT CAST(d AS JSON) FROM doubles ORDER BY d; |
| 284 |
|
| 285 |
# Insert some more JSON values. |
| 286 |
INSERT INTO t(j) VALUES |
| 287 |
(NULL), (NULL), ('true'), ('false'), ('null'), |
| 288 |
('"abc"'), ('""'), ('"abcd"'), ('"bc"'), |
| 289 |
('"abc\\u0000\\u0000"'), ('"abc\\u0000"'), |
| 290 |
('0.0'), ('-0.0'), ('9223372036854776000'), |
| 291 |
('1.0e-1'), ('1.0e-2'), |
| 292 |
(CAST(0.000000000000001 AS JSON)), |
| 293 |
(CAST(0.00000000000000115 AS JSON)), |
| 294 |
(CAST(0.0000000000000001 AS JSON)), |
| 295 |
(CAST(0.000000000000000116 AS JSON)), |
| 296 |
(CAST(0.0 AS JSON)), |
| 297 |
(CAST(-999999999999999999999999999999999999999999999999999999999999999999999999999999999 AS JSON)), |
| 298 |
(CAST(-999999999999999999999999999999999999999999999999999999999999999999999999999999998 AS JSON)), |
| 299 |
(CAST(-999999999999999999999999999999999999999999999999999999999999999999999999999999997 AS JSON)), |
| 300 |
(CAST(999999999999999999999999999999999999999999999999999999999999999999999999999999997 AS JSON)), |
| 301 |
(CAST(999999999999999999999999999999999999999999999999999999999999999999999999999999998 AS JSON)), |
| 302 |
(CAST(999999999999999999999999999999999999999999999999999999999999999999999999999999999 AS JSON)), |
| 303 |
(CAST(-1E81 AS JSON)), |
| 304 |
(CAST(-9.99E80 AS JSON)), |
| 305 |
(CAST(9.99E80 AS JSON)), |
| 306 |
(CAST(1E81 AS JSON)), |
| 307 |
(JSON_ARRAY('an array')), |
| 308 |
(JSON_ARRAY('another array')), |
| 309 |
(JSON_OBJECT('an', 'object')), |
| 310 |
(JSON_OBJECT('another', 'object')), |
| 311 |
(CAST(ST_GeomFromText('POINT(0 0)') AS JSON)), |
| 312 |
(CAST(ST_GeomFromText('POINT(0 1)') AS JSON)), |
| 313 |
(CAST(CAST('1234abcd' AS BINARY) AS JSON)); |
| 314 |
|
| 315 |
--disable_query_log |
| 316 |
# Minimal buffer size, to test merging as well |
| 317 |
set @@sort_buffer_size=32 * 1024; |
| 318 |
--enable_query_log |
| 319 |
|
| 320 |
# Now order the table on the JSON column. |
| 321 |
SELECT j, JSON_TYPE(j) AS tp FROM t ORDER BY j, id; |
| 322 |
SELECT j, JSON_TYPE(j) AS tp FROM t ORDER BY j, id limit 2 offset 2; |
| 323 |
|
| 324 |
SELECT j, JSON_TYPE(j) AS tp FROM t ORDER BY j DESC, id; |
| 325 |
SELECT j, JSON_TYPE(j) AS tp FROM t ORDER BY j DESC, id limit 2 offset 2; |
| 326 |
|
| 327 |
# Ordering on a JSON expression should give the same result. |
| 328 |
SELECT JSON_EXTRACT(j, '$') AS je, JSON_TYPE(j) AS tp FROM t ORDER BY je, id; |
| 329 |
|
| 330 |
--disable_query_log |
| 331 |
set @@sort_buffer_size=default; |
| 332 |
--enable_query_log |
| 333 |
|
| 334 |
# GROUP BY uses a temporary for grouping, GROUP BY WITH ROLLUP uses filesort to |
| 335 |
# do the grouping. |
| 336 |
ANALYZE TABLE t; |
| 337 |
EXPLAIN SELECT j, COUNT(*) FROM t GROUP BY j ORDER BY j; |
| 338 |
EXPLAIN SELECT j, COUNT(*) FROM t GROUP BY j WITH ROLLUP; |
| 339 |
|
| 340 |
# Grouping produces indeterminate results based on the order of evaluation. For example, |
| 341 |
# either '2' or '2.0' could be the name of the group |
| 342 |
# either '20' or '20.000' could be the name of the group |
| 343 |
# either '-0.1' or '-0.100' could be the name of the group |
| 344 |
# either '9223372036854775807.000' or '9223372036854775807' could be the name of the group |
| 345 |
# These issues are resolved by removing trailing zeros from decimals and replacing -0 with 0. |
| 346 |
# This also replaces .000010 with 010 and .000001 with 001 [because \s is not supported]. |
| 347 |
--skip_if_hypergraph # Different warnings. |
| 348 |
--replace_regex /1\.0e\-1/0.1/ /1\.0e\-2/0.01/ /0\.010/0.01/ /\.000// /\.0// /0\.100/0.1/ /\-0/0/ /3\.130/3.13/ /3\.140/3.14/ /3\.150/3.15/ /9\.223372036854776e18/9223372036854776000/ |
| 349 |
SELECT j, COUNT(*) FROM t GROUP BY j ORDER BY j; |
| 350 |
|
| 351 |
--skip_if_hypergraph # Different warnings. |
| 352 |
--replace_regex /1\.0e\-1/0.1/ /1\.0e\-2/0.01/ /0\.010/0.01/ /\.000// /\.0// /0\.100/0.1/ /\-0/0/ /3\.130/3.13/ /3\.140/3.14/ /3\.150/3.15/ /9\.223372036854776e18/9223372036854776000/ |
| 353 |
SELECT JSON_EXTRACT(j, '$') AS je, COUNT(*) FROM t GROUP BY je ORDER BY je; |
| 354 |
|
| 355 |
--replace_regex /1\.0e\-1/0.1/ /1\.0e\-2/0.01/ /0\.010/0.01/ /\.000// /\.0// /0\.100/0.1/ /\-0/0/ /3\.130/3.13/ /3\.140/3.14/ /3\.150/3.15/ /9\.223372036854776e18/9223372036854776000/ |
| 356 |
SELECT j, COUNT(*) FROM t GROUP BY j WITH ROLLUP; |
| 357 |
|
| 358 |
--replace_regex /1\.0e\-1/0.1/ /1\.0e\-2/0.01/ /0\.010/0.01/ /\.000// /\.0// /0\.100/0.1/ /\-0/0/ /3\.130/3.13/ /3\.140/3.14/ /3\.150/3.15/ /9\.223372036854776e18/9223372036854776000/ |
| 359 |
SELECT JSON_EXTRACT(j, '$') AS je, COUNT(*) FROM t GROUP BY je WITH ROLLUP; |
| 360 |
|
| 361 |
DROP TABLE t, timestamps, datetimes, times, dates, signed_integers, |
| 362 |
unsigned_integers, decimals, doubles; |
| 363 |
|
| 364 |
# Test ordering of a not nullable column. |
| 365 |
CREATE TABLE t(j JSON NOT NULL); |
| 366 |
INSERT INTO t VALUES ('1'), ('2'), ('10'), ('"1"'), ('"2"'), ('"10"'), |
| 367 |
('true'), ('false'), ('null'); |
| 368 |
SELECT j FROM t ORDER BY j; |
| 369 |
SELECT j FROM t ORDER BY JSON_EXTRACT(j, '$'); |
| 370 |
SELECT JSON_EXTRACT(j, '$') FROM t ORDER BY 1; |
| 371 |
|
| 372 |
# Ordering on (j+1) will convert to a numeric type. |
| 373 |
SELECT j FROM t ORDER BY j+1, JSON_TYPE(j); |
| 374 |
DROP TABLE t; |
| 375 |
|
| 376 |
CREATE TABLE t(vc varchar(10)); |
| 377 |
INSERT INTO t VALUES ('["abc"]'), ('[1'); |
| 378 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 379 |
SELECT * FROM t ORDER BY CAST(vc AS JSON); |
| 380 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 381 |
SELECT * FROM t ORDER BY JSON_EXTRACT(vc, '$[0]'); |
| 382 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 383 |
SELECT CAST(vc AS JSON) AS j FROM t ORDER BY j; |
| 384 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 385 |
SELECT JSON_EXTRACT(vc, '$[0]') AS j FROM t ORDER BY j; |
| 386 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 387 |
SELECT CAST(vc AS JSON) FROM t ORDER BY 1; |
| 388 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 389 |
SELECT JSON_EXTRACT(vc, '$[0]') FROM t ORDER BY 1; |
| 390 |
DROP TABLE t; |
| 391 |
|
| 392 |
--echo # |
| 393 |
--echo # Internal ordering of arrays and objects. Ordered by cardinality. |
| 394 |
--echo # |
| 395 |
CREATE TABLE t(i int, j json); |
| 396 |
INSERT INTO t VALUES |
| 397 |
(1, '{}'), (2, '{"a":1}'), (3, '{"ab":2}'), (4, '{"a":1,"b":2}'), |
| 398 |
(5, '{"c":3,"d":4}'), (6, '{"a":1,"b":2,"c":3,"d":4}'); |
| 399 |
INSERT INTO t VALUES |
| 400 |
(1, '[]'), (2, '[1]'), (3, '[2]'), (4, '[1,2]'), (5, '[2,1]'), (6, '[1,2,3]'), |
| 401 |
(7, '[1,2,3,4]'), (8, '[4,3,2,1]'), (9, '[1,2,3,4,5]'); |
| 402 |
INSERT INTO t SELECT i+100, j FROM t; |
| 403 |
SELECT * FROM t ORDER BY j, i; |
| 404 |
SELECT * FROM t ORDER BY j DESC, i; |
| 405 |
# GROUP BY knows how to distinguish the arrays and the objects, even |
| 406 |
# if they have the same cardinality. |
| 407 |
# Group by produces indeterminate results based on the order the items are evaluated |
| 408 |
# SELECT j, COUNT(*) FROM t GROUP BY j ORDER BY j; |
| 409 |
# GROUP BY WITH ROLLUP, on the other hand, doesn't know how to |
| 410 |
# distinguish them, and produces confusing results for arrays/objects. |
| 411 |
# GROUP BY WITH ROLLUP is only useful on scalar results for now. |
| 412 |
SELECT j, COUNT(*) FROM t GROUP BY j WITH ROLLUP; |
| 413 |
DROP TABLE t; |
| 414 |
|
| 415 |
--echo # Test NULLs sorting. |
| 416 |
CREATE TABLE t(i int, j json); |
| 417 |
INSERT INTO t(i) VALUES (1),(2),(3),(2),(1); |
| 418 |
SELECT * FROM t ORDER BY j, i; |
| 419 |
SELECT * FROM t ORDER BY j DESC, i; |
| 420 |
SELECT i, JSON_EXTRACT(j, '$') AS je FROM t ORDER BY je, i; |
| 421 |
SELECT i, JSON_EXTRACT(j, '$') AS je FROM t ORDER BY je DESC, i; |
| 422 |
INSERT INTO t(i, j) VALUES (1, '1'); |
| 423 |
SELECT * FROM t ORDER BY j, i; |
| 424 |
SELECT * FROM t ORDER BY j DESC, i; |
| 425 |
SELECT i, JSON_EXTRACT(j, '$') AS je FROM t ORDER BY je, i; |
| 426 |
SELECT i, JSON_EXTRACT(j, '$') AS je FROM t ORDER BY je DESC, i; |
| 427 |
DROP TABLE t; |
| 428 |
|
| 429 |
# Merging of sort results should not get confused if one of the sort columns is |
| 430 |
# a JSON column. |
| 431 |
CREATE TABLE t(vc TEXT, j JSON); |
| 432 |
INSERT INTO t (vc) VALUES ('a'), ('b'), ('c'); |
| 433 |
INSERT INTO t SELECT * FROM t; |
| 434 |
INSERT INTO t SELECT * FROM t; |
| 435 |
INSERT INTO t SELECT * FROM t; |
| 436 |
INSERT INTO t SELECT * FROM t; |
| 437 |
INSERT INTO t SELECT * FROM t; |
| 438 |
INSERT INTO t SELECT * FROM t; |
| 439 |
INSERT INTO t SELECT * FROM t; |
| 440 |
SELECT * FROM t ORDER BY vc, j; |
| 441 |
DROP TABLE t; |
| 442 |
|
| 443 |
--echo # ---------------------------------------------------------------------- |
| 444 |
--echo # Test of JSON_VALID function. |
| 445 |
--echo # ---------------------------------------------------------------------- |
| 446 |
|
| 447 |
--echo |
| 448 |
--echo # Table - Json string column - utf-8, NULL |
| 449 |
--echo Note: 'utf8' is a subset of internal 'utf8mb4' |
| 450 |
--echo |
| 451 |
create table utf8_t (c varchar(20)) CHARACTER SET 'utf8'; |
| 452 |
insert into utf8_t values (NULL); |
| 453 |
-- echo # Expect NULL: |
| 454 |
select JSON_VALID(c) from utf8_t; |
| 455 |
delete from utf8_t; |
| 456 |
|
| 457 |
--echo |
| 458 |
--echo # Table - Json string column - utf-8, valid |
| 459 |
insert into utf8_t values ('[123]'); |
| 460 |
select JSON_VALID(c) from utf8_t; |
| 461 |
delete from utf8_t; |
| 462 |
|
| 463 |
--echo |
| 464 |
--echo # Table - Json string column - utf-8, non-utf8 |
| 465 |
insert into utf8_t values ('[123'); |
| 466 |
--echo expect 0 (false) |
| 467 |
select JSON_VALID(c) from utf8_t; |
| 468 |
delete from utf8_t; |
| 469 |
|
| 470 |
--echo |
| 471 |
--echo # Table - Try to extract JSON from TIMESTAMP column |
| 472 |
ALTER TABLE utf8_t ADD d TIMESTAMP; |
| 473 |
|
| 474 |
--echo # Should give false; not string or JSON type |
| 475 |
--echo # and we do not convert automatically from TIMESTAMP to JSON |
| 476 |
insert into utf8_t values (NULL, '2014-11-25 18:00'); |
| 477 |
select JSON_VALID(d) from utf8_t; |
| 478 |
|
| 479 |
--echo # Explicit cast to a character data type |
| 480 |
--echo # allows MySQL to parse this is a JSON text |
| 481 |
--echo # The string isn't a legal JSON document, tho, so not valid. |
| 482 |
select JSON_VALID(CAST(d as CHAR)) from utf8_t; |
| 483 |
|
| 484 |
--echo # Should give true |
| 485 |
select JSON_VALID(CONCAT( CONCAT('"', CAST(d as CHAR)), '"')) from utf8_t; |
| 486 |
delete from utf8_t; |
| 487 |
drop table utf8_t; |
| 488 |
|
| 489 |
--echo |
| 490 |
--echo # Table - JSON type; should give true by definition |
| 491 |
create table json_t(t json); |
| 492 |
insert into json_t values ('[123]'); |
| 493 |
select json_VALID(t) from json_t; |
| 494 |
|
| 495 |
|
| 496 |
--echo |
| 497 |
--echo # Function result - JSON |
| 498 |
select JSON_VALID( JSON_ARRAY(t, t) ) from json_t; |
| 499 |
|
| 500 |
drop table json_t; |
| 501 |
|
| 502 |
--echo # ---------------------------------------------------------------------- |
| 503 |
--echo # Test of JSON_LENGTH function. |
| 504 |
--echo # ---------------------------------------------------------------------- |
| 505 |
|
| 506 |
create table utf8_mj_length (a int, c varchar(20)) CHARACTER SET 'utf8'; |
| 507 |
insert into utf8_mj_length values( 1, null ); |
| 508 |
insert into utf8_mj_length values( 2, '1' ); |
| 509 |
insert into utf8_mj_length values( 3, 'abc' ); |
| 510 |
insert into utf8_mj_length values( 4, '"abc"' ); |
| 511 |
insert into utf8_mj_length values ( 5, 'true' ); |
| 512 |
insert into utf8_mj_length values ( 6, 'false' ); |
| 513 |
insert into utf8_mj_length values ( 7, 'null' ); |
| 514 |
|
| 515 |
select a, c, json_length( c ) from utf8_mj_length where a = 1; |
| 516 |
|
| 517 |
select a, c, json_length( c ) from utf8_mj_length where a = 2; |
| 518 |
|
| 519 |
--echo |
| 520 |
--echo # invalid json text |
| 521 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 522 |
select a, c, json_length( c ) from utf8_mj_length where a = 3; |
| 523 |
|
| 524 |
select a, c, json_length( c ) from utf8_mj_length where a = 4; |
| 525 |
select a, c, json_length( c ) from utf8_mj_length where a = 5; |
| 526 |
select a, c, json_length( c ) from utf8_mj_length where a = 6; |
| 527 |
select a, c, json_length( c ) from utf8_mj_length where a = 7; |
| 528 |
|
| 529 |
create table json_mj_length( a int, b json ); |
| 530 |
|
| 531 |
insert into json_mj_length values( 1, NULL ); |
| 532 |
|
| 533 |
select a, b, json_length( b ) from json_mj_length where a = 1; |
| 534 |
|
| 535 |
# json_length() with vacuous path expressions |
| 536 |
|
| 537 |
set names 'ascii'; |
| 538 |
|
| 539 |
--echo |
| 540 |
--echo # path auto-converted to a utf8 string from ascii |
| 541 |
--echo |
| 542 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 2; |
| 543 |
|
| 544 |
set names 'utf8'; |
| 545 |
|
| 546 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 1; |
| 547 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 2; |
| 548 |
|
| 549 |
--echo |
| 550 |
--echo # invalid json text |
| 551 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 552 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 3; |
| 553 |
|
| 554 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 4; |
| 555 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 5; |
| 556 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 6; |
| 557 |
select a, c, json_length( c, '$' ) from utf8_mj_length where a = 7; |
| 558 |
|
| 559 |
select a, b, json_length( b, '$' ) from json_mj_length where a = 1; |
| 560 |
|
| 561 |
drop table utf8_mj_length; |
| 562 |
drop table json_mj_length; |
| 563 |
|
| 564 |
# different paths for each row |
| 565 |
CREATE TABLE json_remove_t(j JSON, p TEXT); |
| 566 |
INSERT INTO json_remove_t(p) VALUES ('$.a'), ('$.b'), ('$.c'); |
| 567 |
UPDATE json_remove_t SET j = '{"a":1,"b":2,"c":3}'; |
| 568 |
SELECT j, p, json_remove(j, p) FROM json_remove_t ORDER BY p; |
| 569 |
DROP TABLE json_remove_t; |
| 570 |
|
| 571 |
CREATE TABLE json_merge_t(i INT, j JSON); |
| 572 |
INSERT INTO json_merge_t VALUES |
| 573 |
(0, NULL), |
| 574 |
(1, 'true'), |
| 575 |
(2, '5'), |
| 576 |
(3, '[1,2]'), |
| 577 |
(4, '{"a":["x", "y"]}'), |
| 578 |
(5, '{"a":"b","c":"d"}'); |
| 579 |
SELECT t1.j, t2.j, |
| 580 |
JSON_MERGE_PRESERVE(t1.j, t2.j) AS m1, |
| 581 |
JSON_MERGE_PRESERVE(t2.j, t1.j) AS m2 |
| 582 |
FROM json_merge_t t1, json_merge_t t2 ORDER BY t1.i, t2.i; |
| 583 |
DROP TABLE json_merge_t; |
| 584 |
|
| 585 |
create table keys1(i int, j json); |
| 586 |
insert into keys1 select i, j from t1; |
| 587 |
|
| 588 |
DROP TABLE t1; |
| 589 |
|
| 590 |
# example from the wl7909 spec |
| 591 |
|
| 592 |
create table rawOrders( orderID int, doc json ); |
| 593 |
insert into rawOrders values ( 1, '100' ), ( 2, '{ "id": 2, "quantity": 200 }' ); |
| 594 |
|
| 595 |
create table orders( orderID int, quantity int unsigned ); |
| 596 |
|
| 597 |
INSERT INTO orders( orderID, quantity ) |
| 598 |
SELECT |
| 599 |
r.orderID, |
| 600 |
CASE( JSON_TYPE( r.doc ) ) |
| 601 |
WHEN "INTEGER" THEN CAST( r.doc AS UNSIGNED INT ) |
| 602 |
WHEN "OBJECT" THEN CAST( JSON_EXTRACT( r.doc, '$.quantity' ) AS UNSIGNED INT ) |
| 603 |
ELSE NULL |
| 604 |
END |
| 605 |
FROM rawOrders r; |
| 606 |
|
| 607 |
select * from rawOrders order by orderID; |
| 608 |
select * from orders order by orderID; |
| 609 |
|
| 610 |
drop table rawOrders; |
| 611 |
drop table orders; |
| 612 |
|
| 613 |
# the value here isn't important, but it should be stable |
| 614 |
select charset(json_type('{}')); |
| 615 |
|
| 616 |
|
| 617 |
--echo # ---------------------------------------------------------------------- |
| 618 |
--echo # Test of CAST(<column> AS JSON) |
| 619 |
--echo # ---------------------------------------------------------------------- |
| 620 |
create table t1(dati datetime, da date, |
| 621 |
tim time, ts timestamp, |
| 622 |
y year, |
| 623 |
-- |
| 624 |
ti tinyint, tiu tinyint unsigned, |
| 625 |
si smallint, siu smallint unsigned, |
| 626 |
mi mediumint, miu mediumint unsigned, |
| 627 |
i int, iu int unsigned, |
| 628 |
bi bigint, biu bigint unsigned, |
| 629 |
boo boolean, |
| 630 |
-- |
| 631 |
dc decimal(5,2), |
| 632 |
n numeric(5,2), |
| 633 |
-- |
| 634 |
f float, d double, |
| 635 |
bitt bit(10), |
| 636 |
blb blob, |
| 637 |
bin binary(10), |
| 638 |
en enum('a','b','c'), |
| 639 |
se set('a','b','c'), |
| 640 |
-- |
| 641 |
ge geometry, |
| 642 |
po point, |
| 643 |
ls linestring, |
| 644 |
py polygon, |
| 645 |
js json |
| 646 |
); |
| 647 |
|
| 648 |
|
| 649 |
insert into t1 values('2014-11-25 18:00', '2014-11-25', |
| 650 |
'18:00:59', '2014-11-25 18:00', |
| 651 |
'1999', |
| 652 |
-- |
| 653 |
127, 255, |
| 654 |
32767, 65535, |
| 655 |
8388607, 16777215, -- 3 bytes |
| 656 |
2147483647, 4294967295, -- 4 bytes |
| 657 |
9223372036854775807, 18446744073709551615, |
| 658 |
true, |
| 659 |
-- |
| 660 |
3.14, |
| 661 |
3.14, |
| 662 |
-- |
| 663 |
3.14, 3.14, |
| 664 |
b'10101', |
| 665 |
'10101abcde', |
| 666 |
'10101abcde', |
| 667 |
'b', |
| 668 |
'a,c', |
| 669 |
-- |
| 670 |
ST_GeomFromText('POINT(1 1)'), |
| 671 |
ST_GeomFromText('POINT(1 1)'), |
| 672 |
ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'), |
| 673 |
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 674 |
(5 5,7 5,7 7,5 7, 5 5))'), |
| 675 |
'[123]' |
| 676 |
); |
| 677 |
|
| 678 |
select json_type(cast(dati as json)) from t1; |
| 679 |
select json_type(cast(da as json)) from t1; |
| 680 |
select json_type(cast(tim as json)) from t1; |
| 681 |
select json_type(cast(ts as json)) from t1; |
| 682 |
|
| 683 |
select json_type(cast(y as json)) from t1; |
| 684 |
select json_type(cast(ti as json)) from t1; |
| 685 |
select json_type(cast(tiu as json)) from t1; |
| 686 |
select json_type(cast(si as json)) from t1; |
| 687 |
select json_type(cast(siu as json)) from t1; |
| 688 |
select json_type(cast(mi as json)) from t1; |
| 689 |
select json_type(cast(miu as json)) from t1; |
| 690 |
select json_type(cast(i as json)) from t1; |
| 691 |
select json_type(cast(iu as json)) from t1; |
| 692 |
select json_type(cast(bi as json)) from t1; |
| 693 |
select json_type(cast(biu as json)) from t1; |
| 694 |
select json_type(cast(boo as json)) from t1; # INTEGER (not enough info) |
| 695 |
|
| 696 |
select json_type(cast(dc as json)) from t1; |
| 697 |
# select json_type(cast(n as json)) from t1; |
| 698 |
|
| 699 |
select json_type(cast(f as json)) from t1; |
| 700 |
select json_type(cast(d as json)) from t1; |
| 701 |
|
| 702 |
select json_type(cast(bitt as json)) from t1; |
| 703 |
select json_type(cast(blb as json)) from t1; |
| 704 |
select json_type(cast(bin as json)) from t1; |
| 705 |
|
| 706 |
select json_type(cast(en as json)) from t1; |
| 707 |
select json_type(cast(se as json)) from t1; |
| 708 |
|
| 709 |
select json_type(cast(ge as json)) from t1; |
| 710 |
select json_type(cast(po as json)) from t1; |
| 711 |
select json_type(cast(ls as json)) from t1; |
| 712 |
select json_type(cast(py as json)) from t1; |
| 713 |
|
| 714 |
select json_type(cast(js as json)) from t1; |
| 715 |
|
| 716 |
# |
| 717 |
# same, but now show the printable value: |
| 718 |
# |
| 719 |
select cast(dati as json) from t1; |
| 720 |
select cast(da as json) from t1; |
| 721 |
select cast(tim as json) from t1; |
| 722 |
select cast(ts as json) from t1; |
| 723 |
|
| 724 |
select cast(y as json) from t1; |
| 725 |
select cast(ti as json) from t1; |
| 726 |
select cast(tiu as json) from t1; |
| 727 |
select cast(si as json) from t1; |
| 728 |
select cast(siu as json) from t1; |
| 729 |
select cast(mi as json) from t1; |
| 730 |
select cast(miu as json) from t1; |
| 731 |
select cast(i as json) from t1; |
| 732 |
select cast(iu as json) from t1; |
| 733 |
select cast(bi as json) from t1; |
| 734 |
select cast(biu as json) from t1; |
| 735 |
select cast(boo as json) from t1; # INTEGER (not enough info) |
| 736 |
|
| 737 |
select cast(dc as json) from t1; |
| 738 |
# select cast(n as json) from t1; |
| 739 |
|
| 740 |
select cast(f as json) from t1; |
| 741 |
select cast(d as json) from t1; |
| 742 |
|
| 743 |
select cast(bitt as json) from t1; |
| 744 |
select cast(blb as json) from t1; |
| 745 |
select cast(bin as json) from t1; |
| 746 |
|
| 747 |
select cast(en as json) from t1; |
| 748 |
select cast(se as json) from t1; |
| 749 |
|
| 750 |
select cast(ge as json) from t1; |
| 751 |
select cast(po as json) from t1; |
| 752 |
select cast(ls as json) from t1; |
| 753 |
select cast(py as json) from t1; |
| 754 |
|
| 755 |
select cast(js as json) from t1; |
| 756 |
|
| 757 |
--echo # |
| 758 |
--echo # Bug#21442878 INCORRECT RETURN STATUS FROM |
| 759 |
--echo # ITEM_JSON_TYPECAST::VAL_JSON() ON PARSE ERRORS |
| 760 |
--echo # |
| 761 |
--error ER_INVALID_TYPE_FOR_JSON |
| 762 |
select json_extract(en, '$') from t1; |
| 763 |
|
| 764 |
drop table t1; |
| 765 |
|
| 766 |
create table t1 ( c1 varchar(200) character set 'latin1', |
| 767 |
c2 varchar(200) character set 'utf8' ); |
| 768 |
insert into t1 values ('[1,2]', # legal json, but not utf-8 |
| 769 |
'[1,2 '); # illegal json, but utf-8 |
| 770 |
|
| 771 |
# convert latin1 to UTF-8 |
| 772 |
select cast(c1 as json) from t1; |
| 773 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 774 |
select cast(c2 as json) from t1; |
| 775 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 776 |
select cast(c2 as json) is null from t1; |
| 777 |
|
| 778 |
drop table t1; |
| 779 |
|
| 780 |
# Two distinct but related bugs detected by Knut 2015-02-05 caused NULL for y here: |
| 781 |
create table t2(x int); |
| 782 |
insert into t2 values (1), (2); |
| 783 |
select x, cast(y as json) from (select x, cast(x as json) as y from t2) s order by x; |
| 784 |
select x, cast(y as json) from (select x, cast(cast(x as json) as char charset utf8) as y from t2) s order by x; |
| 785 |
|
| 786 |
drop table t2; |
| 787 |
|
| 788 |
--echo # ---------------------------------------------------------------------- |
| 789 |
--echo # Test of CAST(<select> AS JSON) |
| 790 |
--echo # ---------------------------------------------------------------------- |
| 791 |
# positive test cases |
| 792 |
select cast((select 1) as json); |
| 793 |
|
| 794 |
create table t(i int, j json, c char(10) character set 'utf8'); |
| 795 |
insert into t values (5, '6', '{}'); |
| 796 |
select cast((select i from t) as json); |
| 797 |
select cast((select j from t) as json); |
| 798 |
select cast((select c from t) as json); |
| 799 |
select cast((select cast(i as json) from t) as json); |
| 800 |
select cast((select cast(j as json) from t) as json); |
| 801 |
select cast((select cast(c as json) from t) as json); |
| 802 |
insert into t values (7, '8', '[]'); |
| 803 |
--error ER_SUBQUERY_NO_1_ROW |
| 804 |
select cast((select i from t) as json); |
| 805 |
|
| 806 |
# Test what happens if the subquery returns NULL. The casts should |
| 807 |
# return SQL NULL. |
| 808 |
delete from t; |
| 809 |
insert into t values (null, null, null); |
| 810 |
select cast((select i from t) as json); |
| 811 |
select cast((select j from t) as json); |
| 812 |
select cast((select cast(i as json) from t) as json); |
| 813 |
select cast((select cast(j as json) from t) as json); |
| 814 |
select cast((select cast(c as json) from t) as json); |
| 815 |
|
| 816 |
# negative test cases |
| 817 |
--error ER_OPERAND_COLUMNS |
| 818 |
select cast((select i,i from t) as json); |
| 819 |
--error ER_OPERAND_COLUMNS |
| 820 |
select cast((select * from t) as json); |
| 821 |
drop table t; |
| 822 |
|
| 823 |
--echo # ---------------------------------------------------------------------- |
| 824 |
--echo # Test of JSON_KEYS function. |
| 825 |
--echo # ---------------------------------------------------------------------- |
| 826 |
|
| 827 |
select i, json_keys(j) from keys1 order by i; |
| 828 |
|
| 829 |
delete from keys1; |
| 830 |
insert into keys1 values (0, NULL), |
| 831 |
(1, '{"a": 1, "b": {"e": "foo", "b": 3}}'); |
| 832 |
select i, json_keys(j), json_keys(j, '$.b') from keys1 order by i; |
| 833 |
select cast(j as char) from keys1 order by i; |
| 834 |
|
| 835 |
create table t(i int); |
| 836 |
select cast(json_extract(j, '$.b.b') as char) from keys1 order by i; |
| 837 |
insert into t select cast(json_extract(j, '$.b.b') as char) from keys1; |
| 838 |
select * from t order by i; |
| 839 |
drop table t; |
| 840 |
drop table keys1; |
| 841 |
|
| 842 |
# positive test cases |
| 843 |
create table t(j json); |
| 844 |
insert into t values ('[ 1, 2, 3, {"a": [4,5,6]}]'); |
| 845 |
select json_array_append(j, '$[3].a', cast(7 as json)) from t; |
| 846 |
select json_array_append(j, '$', 7) from t; |
| 847 |
select json_array_append(j, '$', cast(7 as json), '$[3].a', 3.14) from t; |
| 848 |
--echo # second path's append ignored since it doesn't specify an array |
| 849 |
--echo # nor is it an existing scalar, so no auto-wrapping either |
| 850 |
select json_array_append(j, '$', 7, '$[3].b', cast(8 as json)) from t; |
| 851 |
drop table t; |
| 852 |
|
| 853 |
# path caching and leg popping |
| 854 |
create table jdoc( id int, doc json ); |
| 855 |
insert into jdoc values |
| 856 |
( 1, '[ [ true ], [ false ] ]' ), |
| 857 |
( 2, '[ [ 0 ], [ 1 ] ]' ), |
| 858 |
( 3, '[ [ "abc" ], [ "def" ] ]' ); |
| 859 |
|
| 860 |
select id, json_array_insert( doc, '$[0][1]', 'fred' ) |
| 861 |
from jdoc order by id; |
| 862 |
|
| 863 |
select id, json_array_insert( doc, '$[1][0]', 'fred' ) |
| 864 |
from jdoc order by id; |
| 865 |
|
| 866 |
drop table jdoc; |
| 867 |
|
| 868 |
create table t( id int, v varchar(10)); |
| 869 |
insert into t values (1, 'a'), (2, null), (3, 'a'); |
| 870 |
select id v, json_array_insert('[[1]]', '$[0][0]', v) from t order by id; |
| 871 |
drop table t; |
| 872 |
|
| 873 |
--echo # |
| 874 |
--echo # Bug #21304639: JSON_SET() WITH MULTI-LEG PATH RETURNS DIFFERENT |
| 875 |
--echo # RESULTS ON FIRST ROW VS NEXT |
| 876 |
--echo # |
| 877 |
create table t21304639(pk int); |
| 878 |
insert into t21304639 values (2), (1), (3); |
| 879 |
select json_set( |
| 880 |
json_object('existing', pk), |
| 881 |
'$.key_b.test', |
| 882 |
json_object('new', 'apple') |
| 883 |
) as field1 from t21304639 order by field1; |
| 884 |
|
| 885 |
select json_set( |
| 886 |
json_object('existing', pk), |
| 887 |
'$.key_b.test', |
| 888 |
json_object('new', 'apple') |
| 889 |
) as field1 from t21304639 order by field1; |
| 890 |
|
| 891 |
drop table t21304639; |
| 892 |
|
| 893 |
create table t (i int, j json, d double); |
| 894 |
insert into t values (3, '["a", "b"]', 3.14); |
| 895 |
select json_array(i, j, d) from t; |
| 896 |
drop table t; |
| 897 |
|
| 898 |
# Array with the smallest possible signed integer and the largest possible |
| 899 |
# unsigned integer. |
| 900 |
CREATE TABLE t(j JSON); |
| 901 |
INSERT INTO t VALUES (JSON_ARRAY(-9223372036854775808, 18446744073709551614)); |
| 902 |
SELECT * FROM t; |
| 903 |
DROP TABLE t; |
| 904 |
|
| 905 |
# examples from the wl7909 spec |
| 906 |
create table department( id int, deptName varchar(50), isExempt boolean, blobColumn blob ); |
| 907 |
insert into department values ( 405, 'Accounting', true, '<a><b>ccc</b><d></d></a>' ); |
| 908 |
|
| 909 |
# returns ["Accounting", {"processed": true }] |
| 910 |
SELECT JSON_ARRAY( d.deptName, CAST( '{ "processed" : true }' AS JSON ) ) |
| 911 |
FROM department d |
| 912 |
WHERE id = 405; |
| 913 |
|
| 914 |
# stores a JSON value in a JSON-typed column |
| 915 |
create table jsn_table( json_column json ); |
| 916 |
INSERT INTO jsn_table( json_column ) |
| 917 |
SELECT JSON_ARRAY( d.deptName, d.id, d.blobColumn ) |
| 918 |
FROM department d |
| 919 |
WHERE id = 405; |
| 920 |
drop table jsn_table; |
| 921 |
|
| 922 |
drop table department; |
| 923 |
|
| 924 |
create table misc_dt |
| 925 |
( |
| 926 |
id int, py polygon |
| 927 |
); |
| 928 |
|
| 929 |
insert into misc_dt values |
| 930 |
( |
| 931 |
1, ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 932 |
(5 5,7 5,7 7,5 7, 5 5))') |
| 933 |
), |
| 934 |
( |
| 935 |
2, null |
| 936 |
); |
| 937 |
|
| 938 |
select id, json_array( true, py, false ) from misc_dt order by id; |
| 939 |
|
| 940 |
drop table misc_dt; |
| 941 |
|
| 942 |
# construct from data in a table |
| 943 |
create table jro |
| 944 |
( |
| 945 |
a int, |
| 946 |
b varchar( 10 ), |
| 947 |
c boolean |
| 948 |
); |
| 949 |
insert into jro( a, b, c ) values |
| 950 |
( 0, 'zero', false ), |
| 951 |
( 1, 'one', true ), |
| 952 |
( null, null, null ); |
| 953 |
|
| 954 |
select a, json_object( 'a', a, 'b', b, 'c', c ) |
| 955 |
from jro |
| 956 |
order by a; |
| 957 |
|
| 958 |
drop table jro; |
| 959 |
|
| 960 |
create table jro2( a int, b varchar( 10 ), c json ); |
| 961 |
insert into jro2 ( a, b, c ) values |
| 962 |
( 1, 'array', '[ 1, 2, 3 ]' ), ( 2, 'object', '{ "d": "foo", "e": true }' ); |
| 963 |
|
| 964 |
select a, json_object( 'type', b, 'value', c ) |
| 965 |
from jro2 order by a; |
| 966 |
|
| 967 |
drop table jro2; |
| 968 |
|
| 969 |
# examples from the wl7909 spec |
| 970 |
create table department( id int, deptName varchar(50), isExempt boolean, blobColumn blob ); |
| 971 |
insert into department values ( 405, 'Accounting', true, '<a><b>ccc</b><d></d></a>' ); |
| 972 |
|
| 973 |
# returns {"deptName": "Accounting", "id": 405, "isExempt": true, "date": 2014-11-0400:00:00.000000} |
| 974 |
SELECT JSON_OBJECT |
| 975 |
( |
| 976 |
'deptName', d.deptName, |
| 977 |
'id', d.id, |
| 978 |
'isExempt', d.isExempt and true |
| 979 |
) |
| 980 |
FROM department d |
| 981 |
WHERE id = 405; |
| 982 |
|
| 983 |
drop table department; |
| 984 |
|
| 985 |
# key names which aren't strings |
| 986 |
|
| 987 |
create table misc_dt |
| 988 |
( |
| 989 |
py polygon |
| 990 |
); |
| 991 |
|
| 992 |
insert into misc_dt values |
| 993 |
( |
| 994 |
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 995 |
(5 5,7 5,7 7,5 7, 5 5))') |
| 996 |
); |
| 997 |
|
| 998 |
--error ER_INVALID_JSON_CHARSET |
| 999 |
select json_object( py, 'def' ) from misc_dt; |
| 1000 |
|
| 1001 |
drop table misc_dt; |
| 1002 |
|
| 1003 |
|
| 1004 |
create table json_search_table( id_col int, json_col json ); |
| 1005 |
insert into json_search_table values |
| 1006 |
( 1, '{ "a": "foobar" }' ), |
| 1007 |
( 2, '{ "a": "foobar", "b": "focus", "c": [ "arm", "foot", "shoulder" ] }' ); |
| 1008 |
|
| 1009 |
select id_col, json_search( json_col, 'all', 'foo%' ) |
| 1010 |
from json_search_table |
| 1011 |
order by id_col; |
| 1012 |
|
| 1013 |
select id_col, json_search( json_col, 'all', 'foot' ) |
| 1014 |
from json_search_table |
| 1015 |
order by id_col; |
| 1016 |
|
| 1017 |
select id_col, json_search( json_col, 'all', 'f__us' ) |
| 1018 |
from json_search_table |
| 1019 |
order by id_col; |
| 1020 |
|
| 1021 |
# tests with path arguments |
| 1022 |
delete from json_search_table; |
| 1023 |
insert into json_search_table values |
| 1024 |
( 1, '{ "a": "foobar" }' ), |
| 1025 |
( 2, '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }' ); |
| 1026 |
|
| 1027 |
select id_col, json_search( json_col, 'all', 'foo%', null, '$.a' ) |
| 1028 |
from json_search_table |
| 1029 |
order by id_col; |
| 1030 |
select id_col, json_search( json_col, 'all', 'foo%', null, '$.a', '$.b' ) |
| 1031 |
from json_search_table |
| 1032 |
order by id_col; |
| 1033 |
select id_col, json_search( json_col, 'one', 'foo%', null, '$.a', '$.b' ) |
| 1034 |
from json_search_table |
| 1035 |
order by id_col; |
| 1036 |
|
| 1037 |
delete from json_search_table; |
| 1038 |
insert into json_search_table values |
| 1039 |
( 1, '{ "a": "foobar" }' ), |
| 1040 |
( 2, '[ { "a": { "b": { "c": "fool" } } }, { "b": { "c": "shoulder" } }, { "c": { "c": "food"} } ]' ); |
| 1041 |
|
| 1042 |
select id_col, json_search( json_col, 'all', 'foo%', null, '$.a', '$**.c' ) |
| 1043 |
from json_search_table |
| 1044 |
order by id_col; |
| 1045 |
select id_col, json_search( json_col, 'one', 'foo%', null, '$.a', '$**.c' ) |
| 1046 |
from json_search_table |
| 1047 |
order by id_col; |
| 1048 |
|
| 1049 |
drop table json_search_table; |
| 1050 |
|
| 1051 |
# verify that the double-quoted strings returned by json_search() |
| 1052 |
# are valid path expressions when unpacked via json_unquote(). |
| 1053 |
|
| 1054 |
create table jep( key_col int primary key, doc json, path varchar( 50 ) ); |
| 1055 |
insert into jep values |
| 1056 |
( 1, '{ "onepotato": "seven" }', '$.onepotato' ), |
| 1057 |
( 2, '{ "one potato": "seven" }', '$."one potato"' ), |
| 1058 |
( 3, '{ "one \\"potato": "seven" }', '$."one \\"potato"' ), |
| 1059 |
( 4, '{ "one \\npotato": "seven" }', '$."one \\npotato"' ); |
| 1060 |
|
| 1061 |
select key_col, |
| 1062 |
json_search( doc, 'all', 'seven' ) paths, |
| 1063 |
json_unquote( cast( json_search( doc, 'all', 'seven' ) as char ) ) unquoted, |
| 1064 |
path |
| 1065 |
from jep order by key_col; |
| 1066 |
|
| 1067 |
drop table jep; |
| 1068 |
|
| 1069 |
--echo # ---------------------------------------------------------------------- |
| 1070 |
--echo # Test of CASE and IF expressions returning JSON |
| 1071 |
--echo # ---------------------------------------------------------------------- |
| 1072 |
create table t(j json); |
| 1073 |
insert into t values (null), ('[3,4,5]'); |
| 1074 |
|
| 1075 |
select json_type(case (j is null) when 1 then |
| 1076 |
cast('null' as json) else |
| 1077 |
cast('[1,2,3]' as json) end) from t order by j; |
| 1078 |
|
| 1079 |
# no else clause |
| 1080 |
select json_type(case (j is null) when 1 then cast(1 as json) end) from t order by j; |
| 1081 |
|
| 1082 |
select json_type( if(j is null, |
| 1083 |
cast('{"a": 6}' as json), |
| 1084 |
cast('[1,2,3]' as json))) from t order by j; |
| 1085 |
|
| 1086 |
select json_type( if(j is null, |
| 1087 |
NULL, |
| 1088 |
cast('[1,2,3]' as json)) ) from t order by j; |
| 1089 |
--echo # ---------------------------------------------------------------------- |
| 1090 |
--echo # Test of CASE and IF expressions with mix of JSON and other types |
| 1091 |
--echo # Common result type is VARCHAR |
| 1092 |
--echo # ---------------------------------------------------------------------- |
| 1093 |
|
| 1094 |
select json_type(case (j is null) when 1 then |
| 1095 |
3.14 else |
| 1096 |
cast('[1,2,3]' as json) end) from t order by j; |
| 1097 |
|
| 1098 |
select case (j is null) when 1 then |
| 1099 |
3.14 else |
| 1100 |
cast('[1,2,3]' as json) end from t order by j; |
| 1101 |
|
| 1102 |
select case (j is null) when 1 then |
| 1103 |
'foobar' else |
| 1104 |
cast('[1,2,3]' as json) end from t order by j; |
| 1105 |
|
| 1106 |
select json_type( if(j is null, |
| 1107 |
3.14, |
| 1108 |
cast('[1,2,3]' as json))) from t order by j; |
| 1109 |
|
| 1110 |
select if(j is null, |
| 1111 |
3.14, |
| 1112 |
cast('[1,2,3]' as json)) from t order by j; |
| 1113 |
|
| 1114 |
|
| 1115 |
--echo # ---------------------------------------------------------------------- |
| 1116 |
--echo # Test of IFNULL |
| 1117 |
--echo # ---------------------------------------------------------------------- |
| 1118 |
select json_type(ifnull(j, cast(3 as json))) from t order by j; |
| 1119 |
select ifnull(j, cast(3 as json)) from t order by j; # json_type masked a bug |
| 1120 |
select json_type(ifnull(NULL, cast(3 as json))); |
| 1121 |
select json_type(ifnull(cast(3 as json), NULL)); |
| 1122 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 1123 |
SELECT JSON_TYPE(IFNULL(JSON_EXTRACT(CONCAT(t1.j, 'abc'), '$'), t2.j)) |
| 1124 |
FROM t t1, t t2; |
| 1125 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 1126 |
SELECT JSON_TYPE(IFNULL(t1.j, JSON_EXTRACT(CONCAT(t2.j, 'abc'), '$'))) |
| 1127 |
FROM t t1, t t2; |
| 1128 |
|
| 1129 |
--echo # ---------------------------------------------------------------------- |
| 1130 |
--echo # Json values used in text contexts |
| 1131 |
--echo # ---------------------------------------------------------------------- |
| 1132 |
delete from t; |
| 1133 |
insert into t values (NULL), (cast('"aBc"' as json)); |
| 1134 |
select upper(j) from t order by j; |
| 1135 |
delete from t; |
| 1136 |
insert into t values (cast(1 as json)), (cast(10 as json)), (cast(2 as json)); |
| 1137 |
select * from t order by j; |
| 1138 |
|
| 1139 |
select max(j) from t; |
| 1140 |
select json_type(max(j)) from t; |
| 1141 |
select min(j) from t; |
| 1142 |
select json_type(max(j)) from t; |
| 1143 |
|
| 1144 |
# if we want another sorting, cast to suitable type |
| 1145 |
select max(cast(j as unsigned)) from t; |
| 1146 |
--error ER_INVALID_TYPE_FOR_JSON |
| 1147 |
select json_type(max(cast(j as unsigned))) from t; |
| 1148 |
drop table t; |
| 1149 |
|
| 1150 |
--echo # ---------------------------------------------------------------------- |
| 1151 |
--echo # Test JSON arguments and return values of stored functions |
| 1152 |
--echo # ---------------------------------------------------------------------- |
| 1153 |
|
| 1154 |
create function make_message |
| 1155 |
( |
| 1156 |
sender varchar(50), |
| 1157 |
receiver varchar(50), |
| 1158 |
subject text, |
| 1159 |
received datetime, |
| 1160 |
body text |
| 1161 |
) |
| 1162 |
returns json |
| 1163 |
language sql deterministic no sql |
| 1164 |
return json_object |
| 1165 |
( |
| 1166 |
'sender', sender, |
| 1167 |
'receiver', receiver, |
| 1168 |
'subject', subject, |
| 1169 |
'received', received, |
| 1170 |
'body', body |
| 1171 |
); |
| 1172 |
|
| 1173 |
create function extract_date( message json ) |
| 1174 |
returns datetime |
| 1175 |
language sql deterministic no sql |
| 1176 |
return json_extract( message, '$.received' ); |
| 1177 |
|
| 1178 |
create table messages |
| 1179 |
( |
| 1180 |
id int, |
| 1181 |
raw_message json |
| 1182 |
); |
| 1183 |
|
| 1184 |
insert into messages(id, raw_message) values |
| 1185 |
( |
| 1186 |
1, |
| 1187 |
make_message |
| 1188 |
( |
| 1189 |
'fred', |
| 1190 |
'alice', |
| 1191 |
'lunch today?', |
| 1192 |
timestamp( '2015-05-11 09:30:05' ), |
| 1193 |
'How about lunch at 11:30?' |
| 1194 |
) |
| 1195 |
), |
| 1196 |
( |
| 1197 |
2, |
| 1198 |
make_message |
| 1199 |
( |
| 1200 |
'alice', |
| 1201 |
'fred', |
| 1202 |
're: lunch today?', |
| 1203 |
timestamp( '2015-05-11 09:45:05' ), |
| 1204 |
'Sorry. I am in meetings all day long.' |
| 1205 |
) |
| 1206 |
), |
| 1207 |
( |
| 1208 |
3, |
| 1209 |
json_object |
| 1210 |
( |
| 1211 |
'sender', 'fred', |
| 1212 |
'receiver', 'alice', |
| 1213 |
'subject', 're: lunch today?', |
| 1214 |
'received', timestamp( '2015-05-11 09:50:05' ), |
| 1215 |
'body', 'Oh, bummer.' |
| 1216 |
) |
| 1217 |
) |
| 1218 |
; |
| 1219 |
select * from messages order by id; |
| 1220 |
|
| 1221 |
# should be DATETIME |
| 1222 |
select json_type |
| 1223 |
( |
| 1224 |
json_extract |
| 1225 |
( |
| 1226 |
json_object |
| 1227 |
( |
| 1228 |
'sender', 'fred', |
| 1229 |
'receiver', 'alice', |
| 1230 |
'subject', 'lunch today?', |
| 1231 |
'received', timestamp( '2015-05-11 09:45:05' ), |
| 1232 |
'body', 'How about lunch at 11:30?' |
| 1233 |
), |
| 1234 |
'$.received' |
| 1235 |
) |
| 1236 |
) received_type |
| 1237 |
; |
| 1238 |
|
| 1239 |
select id, extract_date( raw_message ) extracted_date |
| 1240 |
from messages order by id; |
| 1241 |
|
| 1242 |
create function show_received_type( message json ) |
| 1243 |
returns tinytext |
| 1244 |
language sql deterministic no sql |
| 1245 |
return json_type( json_extract( message, '$.received' ) ); |
| 1246 |
|
| 1247 |
# should be DATETIME |
| 1248 |
select show_received_type |
| 1249 |
( |
| 1250 |
json_object |
| 1251 |
( |
| 1252 |
'sender', 'fred', |
| 1253 |
'receiver', 'alice', |
| 1254 |
'subject', 're: lunch today?', |
| 1255 |
'received', timestamp( '2015-05-11 09:50:05' ), |
| 1256 |
'body', 'Oh, bummer.' |
| 1257 |
) |
| 1258 |
) received_type; |
| 1259 |
|
| 1260 |
# should be DATETIME |
| 1261 |
select show_received_type |
| 1262 |
( |
| 1263 |
make_message |
| 1264 |
( |
| 1265 |
'fred', |
| 1266 |
'alice', |
| 1267 |
'lunch today?', |
| 1268 |
timestamp( '2015-05-11 09:30:05' ), |
| 1269 |
'How about lunch at 11:30?' |
| 1270 |
) |
| 1271 |
) received_type; |
| 1272 |
|
| 1273 |
# should be DATETIME |
| 1274 |
select id, show_received_type( raw_message ) received_type |
| 1275 |
from messages order by id; |
| 1276 |
|
| 1277 |
drop function show_received_type; |
| 1278 |
drop function make_message; |
| 1279 |
drop function extract_date; |
| 1280 |
drop table messages; |
| 1281 |
|
| 1282 |
--echo # Test a function that fails. |
| 1283 |
CREATE FUNCTION func_that_fails() RETURNS JSON |
| 1284 |
LANGUAGE SQL DETERMINISTIC NO SQL |
| 1285 |
RETURN '[not valid json]'; |
| 1286 |
--error ER_INVALID_JSON_TEXT |
| 1287 |
SELECT JSON_EXTRACT(func_that_fails(), '$'); |
| 1288 |
DROP FUNCTION func_that_fails; |
| 1289 |
|
| 1290 |
# test a more complicated stored function which declares a JSON variable |
| 1291 |
|
| 1292 |
delimiter //; |
| 1293 |
create function get_types( input_value json ) |
| 1294 |
returns json |
| 1295 |
language sql deterministic contains sql |
| 1296 |
begin |
| 1297 |
declare array_length integer; |
| 1298 |
declare return_value json; |
| 1299 |
declare idx int; |
| 1300 |
declare path varchar(100); |
| 1301 |
|
| 1302 |
set array_length = json_length( input_value ); |
| 1303 |
set return_value = json_array(); |
| 1304 |
set idx = 0; |
| 1305 |
|
| 1306 |
while idx < array_length do |
| 1307 |
set path = concat( '$[', idx, ']' ); |
| 1308 |
set return_value = json_array_append |
| 1309 |
( |
| 1310 |
return_value, |
| 1311 |
'$', |
| 1312 |
json_type( json_extract( input_value, path ) ) |
| 1313 |
); |
| 1314 |
|
| 1315 |
set idx = idx + 1; |
| 1316 |
end while; |
| 1317 |
|
| 1318 |
return return_value; |
| 1319 |
end// |
| 1320 |
|
| 1321 |
|
| 1322 |
delimiter ;// |
| 1323 |
|
| 1324 |
create table blob_table( blob_col blob ); |
| 1325 |
insert into blob_table values( '10101abcde' ); |
| 1326 |
|
| 1327 |
select json_type( dt.a ), dt.a |
| 1328 |
from |
| 1329 |
( select get_types |
| 1330 |
( |
| 1331 |
json_array |
| 1332 |
( |
| 1333 |
cast( '{}' as json ), |
| 1334 |
cast( '[]' as json ), |
| 1335 |
'null', |
| 1336 |
true, |
| 1337 |
1, |
| 1338 |
2.3, |
| 1339 |
timestamp( '2015-05-11 09:30:05' ), |
| 1340 |
cast('23:24:25' as time), |
| 1341 |
cast('2015-01-15' as date), |
| 1342 |
b'10101', |
| 1343 |
blob_col |
| 1344 |
) |
| 1345 |
) a |
| 1346 |
from blob_table |
| 1347 |
) dt; |
| 1348 |
|
| 1349 |
drop table blob_table; |
| 1350 |
drop function get_types; |
| 1351 |
|
| 1352 |
delimiter //; |
| 1353 |
create procedure merge_docs |
| 1354 |
( |
| 1355 |
inout inout_value json |
| 1356 |
) |
| 1357 |
begin |
| 1358 |
set inout_value = json_object(); |
| 1359 |
end// |
| 1360 |
delimiter ;// |
| 1361 |
|
| 1362 |
|
| 1363 |
delimiter //; |
| 1364 |
create procedure merge_doc_types() |
| 1365 |
begin |
| 1366 |
declare proc_inout json; |
| 1367 |
declare tmp_types varchar(100); |
| 1368 |
|
| 1369 |
set proc_inout = null; |
| 1370 |
|
| 1371 |
call merge_docs( proc_inout ); |
| 1372 |
set tmp_types = json_type( proc_inout ); |
| 1373 |
end// |
| 1374 |
delimiter ;// |
| 1375 |
|
| 1376 |
call merge_doc_types(); |
| 1377 |
|
| 1378 |
drop procedure merge_doc_types; |
| 1379 |
drop procedure merge_docs; |
| 1380 |
|
| 1381 |
delimiter //; |
| 1382 |
create function get_types( input_value json ) |
| 1383 |
returns json |
| 1384 |
language sql deterministic contains sql |
| 1385 |
begin |
| 1386 |
declare array_length integer; |
| 1387 |
declare return_value json; |
| 1388 |
declare idx int; |
| 1389 |
declare path varchar(100); |
| 1390 |
|
| 1391 |
set array_length = json_length( input_value ); |
| 1392 |
set return_value = json_array(); |
| 1393 |
set idx = 0; |
| 1394 |
|
| 1395 |
while idx < array_length do |
| 1396 |
set path = concat( '$[', idx, ']' ); |
| 1397 |
set return_value = json_array_append |
| 1398 |
( |
| 1399 |
return_value, |
| 1400 |
'$', |
| 1401 |
json_type( json_extract( input_value, path ) ) |
| 1402 |
); |
| 1403 |
|
| 1404 |
set idx = idx + 1; |
| 1405 |
end while; |
| 1406 |
|
| 1407 |
return return_value; |
| 1408 |
end// |
| 1409 |
delimiter ;// |
| 1410 |
|
| 1411 |
delimiter //; |
| 1412 |
create procedure merge_docs |
| 1413 |
( |
| 1414 |
in in_value json, |
| 1415 |
inout inout_value json, |
| 1416 |
out out_value json |
| 1417 |
) |
| 1418 |
language sql deterministic contains sql |
| 1419 |
begin |
| 1420 |
set out_value = json_merge_preserve(in_value, inout_value); |
| 1421 |
set inout_value = in_value; |
| 1422 |
end// |
| 1423 |
delimiter ;// |
| 1424 |
|
| 1425 |
|
| 1426 |
delimiter //; |
| 1427 |
create procedure merge_doc_types |
| 1428 |
( |
| 1429 |
out in_types varchar(100), |
| 1430 |
out inout_types varchar(100), |
| 1431 |
out out_types varchar(100) |
| 1432 |
) |
| 1433 |
language sql deterministic contains sql |
| 1434 |
begin |
| 1435 |
declare proc_in json; |
| 1436 |
declare proc_inout json; |
| 1437 |
declare proc_out json; |
| 1438 |
|
| 1439 |
set proc_in = json_array |
| 1440 |
( |
| 1441 |
cast( '{}' as json ), |
| 1442 |
cast( '[]' as json ), |
| 1443 |
'null', |
| 1444 |
true |
| 1445 |
); |
| 1446 |
|
| 1447 |
set proc_inout = json_array |
| 1448 |
( |
| 1449 |
1, |
| 1450 |
2.3, |
| 1451 |
timestamp( '2015-05-11 09:30:05' ), |
| 1452 |
cast('23:24:25' as time), |
| 1453 |
cast('2015-01-15' as date), |
| 1454 |
b'10101' |
| 1455 |
); |
| 1456 |
|
| 1457 |
set proc_out = null; |
| 1458 |
|
| 1459 |
call merge_docs( proc_in, proc_inout, proc_out ); |
| 1460 |
set in_types = get_types( proc_in ); |
| 1461 |
set inout_types = get_types( proc_inout ); |
| 1462 |
set out_types = get_types( proc_out ); |
| 1463 |
end// |
| 1464 |
delimiter ;// |
| 1465 |
|
| 1466 |
call merge_doc_types( @in_types, @inout_types, @out_types ); |
| 1467 |
|
| 1468 |
select @in_types, @inout_types, @out_types; |
| 1469 |
|
| 1470 |
drop procedure merge_doc_types; |
| 1471 |
drop procedure merge_docs; |
| 1472 |
drop function get_types; |
| 1473 |
|
| 1474 |
--echo # |
| 1475 |
--echo # Bug#20898238: WRONG RESULT FOR MAX() OF JSON SCALARS RETURNED |
| 1476 |
--echo # WHEN NULL IS PRESENT |
| 1477 |
--echo # |
| 1478 |
CREATE TABLE bug20898238(j JSON); |
| 1479 |
INSERT INTO bug20898238 VALUES ('{"id":1}'), (NULL), ('{"id":2}'), ('{"id":0}'); |
| 1480 |
SELECT MIN(JSON_EXTRACT(j, '$.id')), |
| 1481 |
MAX(JSON_EXTRACT(j, '$.id')) FROM bug20898238; |
| 1482 |
DROP TABLE bug20898238; |
| 1483 |
|
| 1484 |
--echo # ---------------------------------------------------------------------- |
| 1485 |
--echo # Test of aggregate function SUM, AVG: in constrast to strings, we do not |
| 1486 |
--echo # auto-convert to numeric (double) type: |
| 1487 |
--echo # ---------------------------------------------------------------------- |
| 1488 |
create table t(j json, c varchar(20)); |
| 1489 |
insert into t values (cast('[1,2,3]' as json), '[a,b,c]'); |
| 1490 |
insert into t values (cast(7 as json), '7'), (cast(2 as json), '2'); |
| 1491 |
--disable_warnings |
| 1492 |
select sum(j), sum(cast(j as unsigned)), sum(c) from t; |
| 1493 |
select avg(j), avg(cast(j as unsigned)), avg(c) from t; |
| 1494 |
--enable_warnings |
| 1495 |
|
| 1496 |
--echo # ---------------------------------------------------------------------- |
| 1497 |
--echo # Test of aggregate function COUNT(DISTINCT) and unaggregated DISTINCT |
| 1498 |
--echo # ---------------------------------------------------------------------- |
| 1499 |
|
| 1500 |
create table t_doc( bucket int, doc json); |
| 1501 |
|
| 1502 |
insert into t_doc values |
| 1503 |
( 1, cast( 1 as json ) ), |
| 1504 |
( 1, cast( 1.0 as json ) ), |
| 1505 |
( 1, cast( 1e0 as json ) ), |
| 1506 |
( 2, cast( cast( 1 as unsigned ) as json ) ), |
| 1507 |
( 2, cast( 2 as json ) ), |
| 1508 |
( 2, cast( 2.0 as json ) ), |
| 1509 |
( 3, cast( 2e0 as json ) ), |
| 1510 |
( 3, cast( cast( 7 as unsigned ) as json ) ), |
| 1511 |
( 3, cast( 7 as json ) ), |
| 1512 |
( 4, cast( 7.0 as json ) ), |
| 1513 |
( 4, cast( 7e0 as json ) ), |
| 1514 |
( 4, cast( cast( 7 as unsigned ) as json ) ), |
| 1515 |
( 5, cast( true as json ) ), |
| 1516 |
( 5, cast( true as json ) ), |
| 1517 |
( 5, cast( false as json ) ), |
| 1518 |
( 6, cast( false as json ) ), |
| 1519 |
( 6, cast( 'null' as json ) ), |
| 1520 |
( 6, cast( 'null' as json ) ), |
| 1521 |
( 7, cast( '"abc"' as json ) ), |
| 1522 |
( 7, cast( '"abc"' as json ) ), |
| 1523 |
( 7, cast( '"abcd"' as json ) ), |
| 1524 |
( 8, cast( '"abcd"' as json ) ), |
| 1525 |
( 8, cast( '{ "a": 1, "b": 2 }' as json ) ), |
| 1526 |
( 8, cast( '{ "a": 1, "b": 2 }' as json ) ), |
| 1527 |
( 9, cast( '{ "a": 1, "b": 3 }' as json ) ), |
| 1528 |
( 9, cast( '{ "a": 1, "b": 3 }' as json ) ), |
| 1529 |
( 9, cast( '[ true, false ]' as json ) ), |
| 1530 |
( 10, cast( '[ true, false ]' as json ) ), |
| 1531 |
( 10, cast( '[ true, true ]' as json ) ); |
| 1532 |
|
| 1533 |
# The results depend on the order of evaluation of rows. |
| 1534 |
# Values 7, 7.0, and 7e0 compare equal for distinct but the result |
| 1535 |
# depends on which row is evaluated first so we remove .0 and e0. |
| 1536 |
--replace_regex /\.0// /e0// |
| 1537 |
select distinct( doc ) a from t_doc order by a; |
| 1538 |
|
| 1539 |
select count( distinct doc ) from t_doc; |
| 1540 |
select bucket, count( distinct doc ) from t_doc group by bucket; |
| 1541 |
|
| 1542 |
delete from t_doc; |
| 1543 |
|
| 1544 |
create table dt(dati datetime, da date, |
| 1545 |
tim time, ts timestamp, |
| 1546 |
y year, |
| 1547 |
-- |
| 1548 |
ti tinyint, tiu tinyint unsigned, |
| 1549 |
si smallint, siu smallint unsigned, |
| 1550 |
mi mediumint, miu mediumint unsigned, |
| 1551 |
i int, iu int unsigned, |
| 1552 |
bi bigint, biu bigint unsigned, |
| 1553 |
boo boolean, |
| 1554 |
-- |
| 1555 |
dc decimal(5,2), |
| 1556 |
n numeric(5,2), |
| 1557 |
-- |
| 1558 |
f float, d double, |
| 1559 |
bitt bit(10), |
| 1560 |
blb blob, |
| 1561 |
bin binary(10), |
| 1562 |
en enum('a','b','c'), |
| 1563 |
se set('a','b','c'), |
| 1564 |
-- |
| 1565 |
ge geometry, |
| 1566 |
po point, |
| 1567 |
ls linestring, |
| 1568 |
py polygon, |
| 1569 |
jso json, |
| 1570 |
jsa json, |
| 1571 |
id int |
| 1572 |
); |
| 1573 |
|
| 1574 |
# test with distinct values |
| 1575 |
insert into dt values('2014-11-25 18:00', '2014-11-25', |
| 1576 |
'18:00:59', '2014-11-25 17:00', |
| 1577 |
'1999', |
| 1578 |
-- |
| 1579 |
127, 255, |
| 1580 |
32767, 65535, |
| 1581 |
8388607, 16777215, -- 3 bytes |
| 1582 |
2147483647, 4294967295, -- 4 bytes |
| 1583 |
9223372036854775807, 18446744073709551615, |
| 1584 |
true, |
| 1585 |
-- |
| 1586 |
3.1, |
| 1587 |
3.2, |
| 1588 |
-- |
| 1589 |
3.3, 3.4, |
| 1590 |
b'10101', |
| 1591 |
'10101abcde', |
| 1592 |
'10001abcde', |
| 1593 |
'b', |
| 1594 |
'a,c', |
| 1595 |
-- |
| 1596 |
ST_GeomFromText('POINT(1 1)'), |
| 1597 |
ST_GeomFromText('POINT(1 2)'), |
| 1598 |
ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'), |
| 1599 |
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 1600 |
(5 5,7 5,7 7,5 7, 5 5))'), |
| 1601 |
'{"a": 1, "b": 2 }', |
| 1602 |
'[1, 2]', |
| 1603 |
1 |
| 1604 |
), |
| 1605 |
|
| 1606 |
('2013-11-25 18:00', '2013-11-25', |
| 1607 |
'17:00:59', '2013-11-25 17:00', |
| 1608 |
'1998', |
| 1609 |
-- |
| 1610 |
126, 254, |
| 1611 |
32766, 65534, |
| 1612 |
8388606, 16777214, -- 3 bytes |
| 1613 |
2147483646, 4294967294, -- 4 bytes |
| 1614 |
9223372036854775806, 18446744073709551614, |
| 1615 |
false, |
| 1616 |
-- |
| 1617 |
4.1, |
| 1618 |
4.2, |
| 1619 |
-- |
| 1620 |
4.3, 4.4, |
| 1621 |
b'10111', |
| 1622 |
'10001abcdf', |
| 1623 |
'10101abcdf', |
| 1624 |
'a', |
| 1625 |
'a,b', |
| 1626 |
-- |
| 1627 |
ST_GeomFromText('POINT(1 3)'), |
| 1628 |
ST_GeomFromText('POINT(1 4)'), |
| 1629 |
ST_GeomFromText('LINESTRING(0 0,1 1,2 3)'), |
| 1630 |
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 9,0 0), |
| 1631 |
(5 5,7 5,7 7,5 7, 5 5))'), |
| 1632 |
'{"a": 1, "b": 3 }', |
| 1633 |
'[1, 3]', |
| 1634 |
2 |
| 1635 |
); |
| 1636 |
|
| 1637 |
# types whose representations are unstable across platforms |
| 1638 |
insert into t_doc select id, cast(f as json) from dt; |
| 1639 |
insert into t_doc select id, cast(d as json) from dt; |
| 1640 |
|
| 1641 |
insert into t_doc select * from t_doc; |
| 1642 |
|
| 1643 |
select count( distinct doc ) from t_doc; |
| 1644 |
select bucket, count( distinct doc ) from t_doc group by bucket; |
| 1645 |
|
| 1646 |
delete from t_doc; |
| 1647 |
|
| 1648 |
# types which have stable representations across platforms |
| 1649 |
|
| 1650 |
insert into t_doc select id, cast(dati as json) from dt; |
| 1651 |
insert into t_doc select id, cast(da as json) from dt; |
| 1652 |
insert into t_doc select id, cast(tim as json) from dt; |
| 1653 |
insert into t_doc select id, cast(ts as json) from dt; |
| 1654 |
insert into t_doc select id, cast(y as json) from dt; |
| 1655 |
|
| 1656 |
insert into t_doc select id, cast(ti as json) from dt; |
| 1657 |
insert into t_doc select id, cast(tiu as json) from dt; |
| 1658 |
insert into t_doc select id, cast(si as json) from dt; |
| 1659 |
insert into t_doc select id, cast(siu as json) from dt; |
| 1660 |
insert into t_doc select id, cast(mi as json) from dt; |
| 1661 |
insert into t_doc select id, cast(miu as json) from dt; |
| 1662 |
insert into t_doc select id, cast(i as json) from dt; |
| 1663 |
insert into t_doc select id, cast(iu as json) from dt; |
| 1664 |
insert into t_doc select id, cast(bi as json) from dt; |
| 1665 |
insert into t_doc select id, cast(biu as json) from dt; |
| 1666 |
|
| 1667 |
# FIXME: booleans don't retain their boolean values. they become ints. |
| 1668 |
#insert into t_doc select id, cast(boo as json) from dt; |
| 1669 |
|
| 1670 |
insert into t_doc select id, cast(dc as json) from dt; |
| 1671 |
insert into t_doc select id, cast(n as json) from dt; |
| 1672 |
|
| 1673 |
insert into t_doc select id, cast(bitt as json) from dt; |
| 1674 |
insert into t_doc select id, cast(blb as json) from dt; |
| 1675 |
insert into t_doc select id, cast(bin as json) from dt; |
| 1676 |
insert into t_doc select id, cast(en as json) from dt; |
| 1677 |
insert into t_doc select id, cast(se as json) from dt; |
| 1678 |
|
| 1679 |
insert into t_doc select id, cast(ge as json) from dt; |
| 1680 |
insert into t_doc select id, cast(po as json) from dt; |
| 1681 |
insert into t_doc select id, cast(ls as json) from dt; |
| 1682 |
insert into t_doc select id, cast(py as json) from dt; |
| 1683 |
insert into t_doc select id, jso from dt; |
| 1684 |
insert into t_doc select id, jsa from dt; |
| 1685 |
|
| 1686 |
insert into t_doc select * from t_doc; |
| 1687 |
|
| 1688 |
#The results depend on the order of evaluation of rows |
| 1689 |
select distinct( doc ) a from t_doc order by a; |
| 1690 |
select count( distinct doc ) from t_doc; |
| 1691 |
select bucket, count( distinct doc ) from t_doc group by bucket; |
| 1692 |
|
| 1693 |
# test with non-distinct values |
| 1694 |
|
| 1695 |
delete from t_doc; |
| 1696 |
|
| 1697 |
create table ndt(dati datetime, |
| 1698 |
ts timestamp, |
| 1699 |
-- |
| 1700 |
ti tinyint, tiu tinyint unsigned, |
| 1701 |
si smallint, siu smallint unsigned, |
| 1702 |
mi mediumint, miu mediumint unsigned, |
| 1703 |
i int, iu int unsigned, |
| 1704 |
bi bigint, biu bigint unsigned, |
| 1705 |
-- |
| 1706 |
dc decimal(5,2), |
| 1707 |
n numeric(5,2), |
| 1708 |
-- |
| 1709 |
f float, d double, |
| 1710 |
id int |
| 1711 |
); |
| 1712 |
|
| 1713 |
|
| 1714 |
insert into ndt values('2014-11-25 18:00', |
| 1715 |
'2014-11-25 18:00', |
| 1716 |
-- |
| 1717 |
1, 1, |
| 1718 |
1, 1, |
| 1719 |
1, 1, |
| 1720 |
1, 1, |
| 1721 |
1, 1, |
| 1722 |
-- |
| 1723 |
1.0, |
| 1724 |
1.0, |
| 1725 |
-- |
| 1726 |
1.0, 1.0, |
| 1727 |
1 |
| 1728 |
), |
| 1729 |
|
| 1730 |
('2013-11-25 18:00', |
| 1731 |
'2013-11-25 18:00', |
| 1732 |
-- |
| 1733 |
2, 2, |
| 1734 |
2, 2, |
| 1735 |
2, 2, |
| 1736 |
2, 2, |
| 1737 |
2, 2, |
| 1738 |
-- |
| 1739 |
2.0, |
| 1740 |
2.0, |
| 1741 |
-- |
| 1742 |
2.0, 2.0, |
| 1743 |
2 |
| 1744 |
); |
| 1745 |
|
| 1746 |
insert into t_doc select id, cast(dati as json) from ndt; |
| 1747 |
insert into t_doc select id, cast(ts as json) from ndt; |
| 1748 |
|
| 1749 |
insert into t_doc select id, cast(ti as json) from ndt; |
| 1750 |
insert into t_doc select id, cast(tiu as json) from ndt; |
| 1751 |
insert into t_doc select id, cast(si as json) from ndt; |
| 1752 |
insert into t_doc select id, cast(siu as json) from ndt; |
| 1753 |
insert into t_doc select id, cast(mi as json) from ndt; |
| 1754 |
insert into t_doc select id, cast(miu as json) from ndt; |
| 1755 |
insert into t_doc select id, cast(i as json) from ndt; |
| 1756 |
insert into t_doc select id, cast(iu as json) from ndt; |
| 1757 |
insert into t_doc select id, cast(bi as json) from ndt; |
| 1758 |
insert into t_doc select id, cast(biu as json) from ndt; |
| 1759 |
|
| 1760 |
insert into t_doc select id, cast(dc as json) from ndt; |
| 1761 |
insert into t_doc select id, cast(n as json) from ndt; |
| 1762 |
|
| 1763 |
insert into t_doc select id, cast(f as json) from ndt; |
| 1764 |
insert into t_doc select id, cast(d as json) from ndt; |
| 1765 |
|
| 1766 |
insert into t_doc select * from t_doc; |
| 1767 |
|
| 1768 |
# The results depend on the order of evaluation of rows |
| 1769 |
#select distinct( doc ) a from t_doc order by a; |
| 1770 |
select count( distinct doc ) from t_doc; |
| 1771 |
select bucket, count( distinct doc ) from t_doc group by bucket; |
| 1772 |
|
| 1773 |
drop table t_doc; |
| 1774 |
drop table dt; |
| 1775 |
drop table ndt; |
| 1776 |
|
| 1777 |
--echo # ---------------------------------------------------------------------- |
| 1778 |
--echo # Special CASTing behavior of geometry types |
| 1779 |
--echo # ---------------------------------------------------------------------- |
| 1780 |
|
| 1781 |
create table jtable( id int, descr varchar(20), doc json ); |
| 1782 |
|
| 1783 |
create table misc_dt |
| 1784 |
( |
| 1785 |
ge geometry, |
| 1786 |
po point, |
| 1787 |
ls linestring, |
| 1788 |
py polygon |
| 1789 |
); |
| 1790 |
|
| 1791 |
insert into misc_dt values |
| 1792 |
( |
| 1793 |
ST_GeomFromText('POINT(1 1)'), |
| 1794 |
ST_GeomFromText('POINT(1 1)'), |
| 1795 |
ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'), |
| 1796 |
ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 1797 |
(5 5,7 5,7 7,5 7, 5 5))') |
| 1798 |
); |
| 1799 |
|
| 1800 |
insert into jtable select 1, 'geometry', cast(ge as json) from misc_dt; |
| 1801 |
insert into jtable select 2, 'point', cast(po as json) from misc_dt; |
| 1802 |
insert into jtable select 3, 'linestring', cast(ls as json) from misc_dt; |
| 1803 |
insert into jtable select 4, 'polygon', cast(py as json) from misc_dt; |
| 1804 |
# |
| 1805 |
select id, descr, json_type( doc ), doc from jtable order by id; |
| 1806 |
|
| 1807 |
select json_object |
| 1808 |
( |
| 1809 |
'geometry', ST_GeomFromText('POINT(1 1)'), |
| 1810 |
'point', ST_GeomFromText('POINT(1 1)'), |
| 1811 |
'linestring', ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'), |
| 1812 |
'polygon', ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), |
| 1813 |
(5 5,7 5,7 7,5 7, 5 5))') |
| 1814 |
); |
| 1815 |
|
| 1816 |
# verify the workaround for CASTing JSON values to GEOMETRY |
| 1817 |
delete from misc_dt; |
| 1818 |
select * from misc_dt; |
| 1819 |
insert into misc_dt values |
| 1820 |
( |
| 1821 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 1), |
| 1822 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 2), |
| 1823 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 3), |
| 1824 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 4) |
| 1825 |
); |
| 1826 |
select ST_AsGeoJSON( ge ), |
| 1827 |
ST_AsGeoJSON( po ), |
| 1828 |
ST_AsGeoJSON( ls ), |
| 1829 |
ST_AsGeoJSON( py ) |
| 1830 |
from misc_dt; |
| 1831 |
|
| 1832 |
drop table misc_dt; |
| 1833 |
drop table jtable; |
| 1834 |
|
| 1835 |
create table jtable( id int, descr varchar(20), doc json ); |
| 1836 |
|
| 1837 |
create table misc_dt |
| 1838 |
( |
| 1839 |
ge geometrycollection, |
| 1840 |
po multipoint, |
| 1841 |
ls multilinestring, |
| 1842 |
py multipolygon |
| 1843 |
); |
| 1844 |
|
| 1845 |
insert into misc_dt values |
| 1846 |
( |
| 1847 |
geometrycollection(point(1, 1), point(2, 2)), |
| 1848 |
multipoint(point(1, 1), point(2, 2)), |
| 1849 |
multilinestring |
| 1850 |
( |
| 1851 |
linestring(point(0, 0), point(1, 1), point(2, 2)), |
| 1852 |
linestring(point(0, 0), point(11, 11), point(12, 12)) |
| 1853 |
), |
| 1854 |
multipolygon |
| 1855 |
( |
| 1856 |
polygon |
| 1857 |
( |
| 1858 |
linestring(point(0, 0), point(10, 0), point(10, 10), point(0, 10), point(0, 0)), |
| 1859 |
linestring(point(5, 5), point(7, 5), point(7, 7), point(5, 7), point(5, 5)) |
| 1860 |
), |
| 1861 |
polygon |
| 1862 |
( |
| 1863 |
linestring(point(0, 0), point(10, 0), point(10, 10), point(0, 10), point(0, 0)), |
| 1864 |
linestring(point(5, 5), point(7, 5), point(7, 7), point(5, 7), point(5, 5)) |
| 1865 |
) |
| 1866 |
) |
| 1867 |
); |
| 1868 |
|
| 1869 |
insert into jtable select 1, 'geometrycollection', cast(ge as json) from misc_dt; |
| 1870 |
insert into jtable select 2, 'multipoint', cast(po as json) from misc_dt; |
| 1871 |
insert into jtable select 3, 'multilinestring', cast(ls as json) from misc_dt; |
| 1872 |
insert into jtable select 4, 'multipolygon', cast(py as json) from misc_dt; |
| 1873 |
# |
| 1874 |
select id, descr, json_type( doc ), doc from jtable order by id; |
| 1875 |
|
| 1876 |
select ST_AsGeoJSON( ge ), |
| 1877 |
ST_AsGeoJSON( po ), |
| 1878 |
ST_AsGeoJSON( ls ), |
| 1879 |
ST_AsGeoJSON( py ) |
| 1880 |
from misc_dt; |
| 1881 |
|
| 1882 |
delete from misc_dt; |
| 1883 |
select * from misc_dt; |
| 1884 |
insert into misc_dt values |
| 1885 |
( |
| 1886 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 1), |
| 1887 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 2), |
| 1888 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 3), |
| 1889 |
(select ST_GeomFromGeoJSON( cast( doc as char ) ) from jtable where id = 4) |
| 1890 |
); |
| 1891 |
select ST_AsGeoJSON( ge ), |
| 1892 |
ST_AsGeoJSON( po ), |
| 1893 |
ST_AsGeoJSON( ls ), |
| 1894 |
ST_AsGeoJSON( py ) |
| 1895 |
from misc_dt; |
| 1896 |
|
| 1897 |
drop table misc_dt; |
| 1898 |
drop table jtable; |
| 1899 |
|
| 1900 |
--echo # ---------------------------------------------------------------------- |
| 1901 |
--echo # Test of COALESCE |
| 1902 |
--echo # ---------------------------------------------------------------------- |
| 1903 |
|
| 1904 |
select coalesce(cast(1 as json), cast(2 as json)); |
| 1905 |
--sorted_result |
| 1906 |
select j, coalesce(j, cast(3 as json)) from t; |
| 1907 |
--sorted_result |
| 1908 |
select j, coalesce(j, 666) from t; |
| 1909 |
--sorted_result |
| 1910 |
select j, json_type(coalesce(j, '[1,2,3]')) from t; |
| 1911 |
--sorted_result |
| 1912 |
select j, json_type(coalesce(j, 'abc')) from t; |
| 1913 |
--sorted_result |
| 1914 |
select j, json_type(coalesce(j, cast('"arg2"' as json))) from t; |
| 1915 |
--sorted_result |
| 1916 |
select j, json_type(coalesce(j, j)) from t; |
| 1917 |
--echo inconsistent result: error message depends on the order of evaluation of rows |
| 1918 |
--echo --error ER_INVALID_JSON_TEXT_IN_PARAM |
| 1919 |
--echo select json_type(coalesce(json_extract(concat(j, 'abc'), '\$'), j)) from t; |
| 1920 |
--echo --error ER_INVALID_JSON_TEXT_IN_PARAM |
| 1921 |
--echo #select json_type(coalesce(t1.j, json_extract(concat(t2.j, 'abc'), '\$'))) |
| 1922 |
--echo from t t1, t t2; |
| 1923 |
|
| 1924 |
drop table t; |
| 1925 |
|
| 1926 |
--echo # ---------------------------------------------------------------------- |
| 1927 |
--echo # Auto-convert of non-utf8 returning system function |
| 1928 |
--echo # ---------------------------------------------------------------------- |
| 1929 |
create table t(j json, id int); |
| 1930 |
insert into t values ('{"user": "foo"}', 8), (NULL, 8); |
| 1931 |
update t set j=json_set(j, '$.user', current_user()) where id=8; |
| 1932 |
select j from t order by j; |
| 1933 |
update t set j=json_set(j, '$.user', rtrim('foo ')) where id=8; |
| 1934 |
select j from t order by j; |
| 1935 |
update t set j=json_set(j, '$.user', hex('abc')) where id=8; |
| 1936 |
select j from t order by j; |
| 1937 |
update t set j=json_set(j, '$.user', md5('bingle')) where id=8; |
| 1938 |
select j from t order by j; |
| 1939 |
update t set j=json_set(j, '$.user', database()) where id=8; |
| 1940 |
select j from t order by j; |
| 1941 |
update t set j=json_set(j, '$.user', schema()) where id=8; |
| 1942 |
select j from t order by j; |
| 1943 |
# |
| 1944 |
# The hex of some UTF-8 from supplementary plane: U+2070E |
| 1945 |
update t set j=json_set(j, '$.user', |
| 1946 |
cast(UNHEX('F0A09C8E') as char character set 'utf8mb4')) where id=8; |
| 1947 |
set names 'utf8mb4'; # se we can see the character |
| 1948 |
select j from t order by j; |
| 1949 |
select char_length(json_extract(j, '$.user')) from t order by j; |
| 1950 |
drop table t; |
| 1951 |
|
| 1952 |
|
| 1953 |
--echo # |
| 1954 |
--echo # Bug#21257946 JSON_TYPE(TEXT) OF TABLE COLUMN STICKS WITH NULL |
| 1955 |
--echo # AFTER FIRST ENCOUNTER OF NULL |
| 1956 |
--echo # |
| 1957 |
CREATE TABLE T_WITH_NULLS(i INT, j JSON); |
| 1958 |
INSERT INTO T_WITH_NULLS VALUES |
| 1959 |
(0, NULL), |
| 1960 |
(1, '[1]'), |
| 1961 |
(2, NULL), |
| 1962 |
(3, '{"a":"b"}'), |
| 1963 |
(4, NULL), |
| 1964 |
(5, '"abc"'); |
| 1965 |
let $query= SELECT |
| 1966 |
JSON_VALID(j), |
| 1967 |
JSON_TYPE(j), |
| 1968 |
JSON_KEYS(j), |
| 1969 |
JSON_EXTRACT(j, '\$'), |
| 1970 |
JSON_REMOVE(j, '\$.a.b.c'), |
| 1971 |
JSON_ARRAY_APPEND(j, '\$', 2), |
| 1972 |
JSON_SET(j, '\$[0]', 2), |
| 1973 |
JSON_INSERT(j, '\$[0]', 2), |
| 1974 |
JSON_REPLACE(j, '\$[0]', 2), |
| 1975 |
JSON_MERGE_PRESERVE(j, j), |
| 1976 |
JSON_SEARCH(j, 'one', 'abc'), |
| 1977 |
JSON_CONTAINS(j, '[1]'), |
| 1978 |
JSON_CONTAINS_PATH(j, 'all', '\$.a'), |
| 1979 |
JSON_LENGTH(j), |
| 1980 |
JSON_DEPTH(j), |
| 1981 |
JSON_ARRAY(j, j), |
| 1982 |
JSON_OBJECT('k', j), |
| 1983 |
JSON_UNQUOTE(CAST(j AS CHAR)), |
| 1984 |
JSON_QUOTE(CAST(j AS CHAR)), |
| 1985 |
JSON_PRETTY(j), |
| 1986 |
JSON_STORAGE_FREE(j), |
| 1987 |
JSON_STORAGE_SIZE(j), |
| 1988 |
JSON_SCHEMA_VALID('{"type":"object"}', j) |
| 1989 |
FROM T_WITH_NULLS |
| 1990 |
ORDER BY i; |
| 1991 |
eval $query; |
| 1992 |
# It should work the same way with a TEXT column as with a JSON column. |
| 1993 |
ALTER TABLE T_WITH_NULLS MODIFY COLUMN j TEXT; |
| 1994 |
eval $query; |
| 1995 |
DROP TABLE T_WITH_NULLS; |
| 1996 |
|
| 1997 |
# Make sure that every JSON function accepts latin1 text arguments. The JSON |
| 1998 |
# functions use utf8mb4 internally, so they will need to perform charset |
| 1999 |
# conversion. |
| 2000 |
CREATE TABLE t_latin1(id INT PRIMARY KEY AUTO_INCREMENT, |
| 2001 |
json_text VARCHAR(20), |
| 2002 |
json_atom_text VARCHAR(20), |
| 2003 |
json_path VARCHAR(20)) |
| 2004 |
CHARACTER SET 'latin1'; |
| 2005 |
INSERT INTO t_latin1 (json_text, json_atom_text, json_path) VALUES |
| 2006 |
(CONVERT(X'5B22E6F8E5225D' USING latin1), # ["\u00e6\u00f8\u00e5"] |
| 2007 |
CONVERT(X'E5F8E6' USING latin1), # \u00e5\u00f8\u00e6 |
| 2008 |
'$[0]'), |
| 2009 |
(CONVERT(X'7B22E6F8E5223A22E6F8E5227D' USING latin1), |
| 2010 |
# {"\u00e6\u00f8\u00e5":"\u00e6\u00f8\u00e5"} |
| 2011 |
CONVERT(X'E5F8E6' USING latin1), # \u00e5\u00f8\u00e6 |
| 2012 |
CONVERT(X'242E22E6F8E522' USING latin1)); # $."\u00e6\u00f8\u00e5" |
| 2013 |
SELECT * FROM t_latin1 ORDER BY id; |
| 2014 |
SELECT CAST(json_text AS JSON) FROM t_latin1 ORDER BY id; |
| 2015 |
SELECT JSON_VALID(json_text) FROM t_latin1 ORDER BY id; |
| 2016 |
SELECT JSON_VALID(json_atom_text) FROM t_latin1 ORDER BY id; |
| 2017 |
SELECT JSON_TYPE(json_text) FROM t_latin1 ORDER BY id; |
| 2018 |
SELECT JSON_EXTRACT(json_text, json_path) FROM t_latin1 ORDER BY id; |
| 2019 |
SELECT JSON_REMOVE(json_text, json_path) FROM t_latin1 ORDER BY id; |
| 2020 |
SELECT JSON_ARRAY_APPEND(json_text, json_path, json_atom_text) |
| 2021 |
FROM t_latin1 ORDER BY id; |
| 2022 |
SELECT JSON_SET(json_text, json_path, json_atom_text) FROM t_latin1 ORDER BY id; |
| 2023 |
SELECT JSON_INSERT(json_text, json_path, json_atom_text) |
| 2024 |
FROM t_latin1 ORDER BY id; |
| 2025 |
SELECT JSON_REPLACE(json_text, json_path, json_atom_text) |
| 2026 |
FROM t_latin1 ORDER BY id; |
| 2027 |
SELECT JSON_MERGE_PRESERVE(json_text, json_text) FROM t_latin1 ORDER BY id; |
| 2028 |
SELECT JSON_SEARCH(json_text, CONVERT('one' USING latin1), json_atom_text, |
| 2029 |
CONVERT(X'F8' USING latin1), json_path) |
| 2030 |
FROM t_latin1 ORDER BY id; |
| 2031 |
SELECT JSON_CONTAINS(json_text, json_text, json_path) FROM t_latin1 ORDER BY id; |
| 2032 |
SELECT JSON_CONTAINS_PATH(json_text, CONVERT('one' USING latin1), json_path) |
| 2033 |
FROM t_latin1 ORDER BY id; |
| 2034 |
SELECT JSON_LENGTH(json_text, json_path) FROM t_latin1 ORDER BY id; |
| 2035 |
SELECT JSON_DEPTH(json_text) FROM t_latin1 ORDER BY id; |
| 2036 |
SELECT JSON_ARRAY(json_atom_text, json_atom_text) FROM t_latin1 ORDER BY id; |
| 2037 |
SELECT JSON_OBJECT(json_atom_text, json_atom_text) FROM t_latin1 ORDER BY id; |
| 2038 |
SELECT JSON_UNQUOTE(json_atom_text) FROM t_latin1 ORDER BY id; |
| 2039 |
SELECT JSON_UNQUOTE(CONVERT(CONCAT('"', json_atom_text, '"') USING latin1)) |
| 2040 |
FROM t_latin1 ORDER BY id; |
| 2041 |
SELECT JSON_QUOTE(json_atom_text) FROM t_latin1 ORDER BY id; |
| 2042 |
DROP TABLE t_latin1; |
| 2043 |
|
| 2044 |
--echo # ---------------------------------------------------------------------- |
| 2045 |
--echo # Test that boolean expressions are treated as boolean atom literals |
| 2046 |
--echo # ---------------------------------------------------------------------- |
| 2047 |
|
| 2048 |
create table t_bool_literals( a int, b varchar(10) ); |
| 2049 |
insert into t_bool_literals values ( 1, 'food' ), ( 2, 'fool' ), ( 3, 'water' ); |
| 2050 |
|
| 2051 |
# expressions built out of logical connectives should evaluate to boolean literals, but they don't |
| 2052 |
select a, json_array( ((a < 3) and (a > 1)) ) from t_bool_literals order by a; |
| 2053 |
select a, json_array_append( '[]', '$', ((a < 3) and (a > 1)) ) from t_bool_literals order by a; |
| 2054 |
|
| 2055 |
select a, json_array( not ((a < 3) and (a > 1)) ) from t_bool_literals order by a; |
| 2056 |
select a, json_array_append( '[]', '$', not ((a < 3) and (a > 1)) ) from t_bool_literals order by a; |
| 2057 |
|
| 2058 |
select a, json_array( ((a < 3) or (a > 1)) ) from t_bool_literals order by a; |
| 2059 |
select a, json_array_append( '[]', '$', ((a < 3) or (a > 1)) ) from t_bool_literals order by a; |
| 2060 |
|
| 2061 |
select a, json_array( not ((a < 3) or (a > 1)) ) from t_bool_literals order by a; |
| 2062 |
select a, json_array_append( '[]', '$', not ((a < 3) or (a > 1)) ) from t_bool_literals order by a; |
| 2063 |
|
| 2064 |
select json_array( not true, not false ); |
| 2065 |
select json_array_append( '[]', '$', not true, '$', not false ); |
| 2066 |
|
| 2067 |
select a, json_array( 1 and true ) from t_bool_literals order by a; |
| 2068 |
select a, json_array_append( '[]', '$', 1 and true ) from t_bool_literals order by a; |
| 2069 |
|
| 2070 |
select a, json_array( not 1 ) from t_bool_literals order by a; |
| 2071 |
select a, json_array_append( '[]', '$', not 1 ) from t_bool_literals order by a; |
| 2072 |
|
| 2073 |
# true and false literals |
| 2074 |
select json_array( true, false ); |
| 2075 |
select json_array_append( '[]', '$', true, '$', false ); |
| 2076 |
|
| 2077 |
# comparison operators should evaluate to boolean literals |
| 2078 |
select a, json_array( (a < 3) ) from t_bool_literals order by a; |
| 2079 |
select a, json_array_append( '[]', '$', (a < 3) ) from t_bool_literals order by a; |
| 2080 |
|
| 2081 |
select a, json_array( (a <= 3) ) from t_bool_literals order by a; |
| 2082 |
select a, json_array_append( '[]', '$', (a <= 3) ) from t_bool_literals order by a; |
| 2083 |
|
| 2084 |
select a, json_array( (a > 3) ) from t_bool_literals order by a; |
| 2085 |
select a, json_array_append( '[]', '$', (a > 3) ) from t_bool_literals order by a; |
| 2086 |
|
| 2087 |
select a, json_array( (a >= 3) ) from t_bool_literals order by a; |
| 2088 |
select a, json_array_append( '[]', '$', (a >= 3) ) from t_bool_literals order by a; |
| 2089 |
|
| 2090 |
select a, json_array( (a <> 3) ) from t_bool_literals order by a; |
| 2091 |
select a, json_array_append( '[]', '$', (a <> 3) ) from t_bool_literals order by a; |
| 2092 |
|
| 2093 |
select a, json_array( (a != 3) ) from t_bool_literals order by a; |
| 2094 |
select a, json_array_append( '[]', '$', (a != 3) ) from t_bool_literals order by a; |
| 2095 |
|
| 2096 |
# IS NULL and IS NOT NULL |
| 2097 |
select a, json_array( a is null ) from t_bool_literals order by a; |
| 2098 |
select a, json_array_append( '[]', '$', a is null ) from t_bool_literals order by a; |
| 2099 |
|
| 2100 |
select a, json_array( a is not null ) from t_bool_literals order by a; |
| 2101 |
select a, json_array_append( '[]', '$', a is not null ) from t_bool_literals order by a; |
| 2102 |
|
| 2103 |
# IS TRUE and IS NOT TRUE |
| 2104 |
|
| 2105 |
select a, json_array( a is true ) from t_bool_literals order by a; |
| 2106 |
select a, json_array_append( '[]', '$', a is true ) from t_bool_literals order by a; |
| 2107 |
|
| 2108 |
select a, json_array( a is not true ) from t_bool_literals order by a; |
| 2109 |
select a, json_array_append( '[]', '$', a is not true ) from t_bool_literals order by a; |
| 2110 |
|
| 2111 |
# NULLIF which coalesce booleans should evaluate to integer |
| 2112 |
# in order to be consistent with CASE WHEN |
| 2113 |
select a, json_array(nullif(true, false) ) from t_bool_literals order by a; |
| 2114 |
select a, json_array_append |
| 2115 |
( |
| 2116 |
'[]', |
| 2117 |
'$', nullif(true, false) |
| 2118 |
) from t_bool_literals order by a; |
| 2119 |
|
| 2120 |
# would be nice if CASE coalesced a boolean type if all branches are boolean. FIXME maybe |
| 2121 |
#select a, json_array( case when (a > 1) then true else false end ) from t_bool_literals order by a; |
| 2122 |
#select a, json_array_append |
| 2123 |
#( |
| 2124 |
# '[]', |
| 2125 |
# '$', case when (a > 1) then true else false end |
| 2126 |
#) from t_bool_literals order by a; |
| 2127 |
|
| 2128 |
# as a workaround, you can always AND problematic expressions with true |
| 2129 |
select a, json_array( (case when (a > 1) then true else false end) and true ) from t_bool_literals order by a; |
| 2130 |
select a, json_array_append |
| 2131 |
( |
| 2132 |
'[]', |
| 2133 |
'$', (case when (a > 1) then true else false end) and true |
| 2134 |
) from t_bool_literals order by a; |
| 2135 |
|
| 2136 |
# between predicates should evaluate to boolean literals |
| 2137 |
select a, json_array( a between 2 and 4 ) from t_bool_literals order by a; |
| 2138 |
select a, json_array_append( '[]', '$', a between 2 and 4 ) from t_bool_literals order by a; |
| 2139 |
|
| 2140 |
# in predicates should evaluate to boolean literals |
| 2141 |
select a, json_array( a in (1,3) ) from t_bool_literals order by a; |
| 2142 |
select a, json_array_append( '[]', '$', a in (1,3) ) from t_bool_literals order by a; |
| 2143 |
|
| 2144 |
# like predicates should evaluate to boolean literals |
| 2145 |
select a, json_array( b like 'foo%' ) from t_bool_literals order by a; |
| 2146 |
select a, json_array_append( '[]', '$', b like 'foo%' ) from t_bool_literals order by a; |
| 2147 |
|
| 2148 |
# regexp predicates should evaluate to boolean literals |
| 2149 |
select a, json_array( b REGEXP '^fo+d' ) from t_bool_literals order by a; |
| 2150 |
select a, json_array_append( '[]', '$', b REGEXP '^fo+d' ) from t_bool_literals order by a; |
| 2151 |
|
| 2152 |
select a, json_array( b rlike '^fo+d' ) from t_bool_literals order by a; |
| 2153 |
select a, json_array_append( '[]', '$', b rlike '^fo+d' ) from t_bool_literals order by a; |
| 2154 |
|
| 2155 |
select a, json_array( b not REGEXP '^fo+d' ) from t_bool_literals order by a; |
| 2156 |
select a, json_array_append( '[]', '$', b not REGEXP '^fo+d' ) from t_bool_literals order by a; |
| 2157 |
|
| 2158 |
select a, json_array( b not rlike '^fo+d' ) from t_bool_literals order by a; |
| 2159 |
select a, json_array_append( '[]', '$', b not rlike '^fo+d' ) from t_bool_literals order by a; |
| 2160 |
|
| 2161 |
# quantified comparisons should evaluate to boolean literals |
| 2162 |
select a, json_array( b = some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2163 |
select a, json_array_append( '[]', '$', b = some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2164 |
|
| 2165 |
select a, json_array( b = all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2166 |
select a, json_array_append( '[]', '$', b = all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2167 |
|
| 2168 |
select a, json_array( b = any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2169 |
select a, json_array_append( '[]', '$', b = any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2170 |
|
| 2171 |
select a, json_array( b > some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2172 |
select a, json_array_append( '[]', '$', b > some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2173 |
|
| 2174 |
select a, json_array( b > all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2175 |
select a, json_array_append( '[]', '$', b > all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2176 |
|
| 2177 |
select a, json_array( b > any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2178 |
select a, json_array_append( '[]', '$', b > any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2179 |
|
| 2180 |
select a, json_array( b < some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2181 |
select a, json_array_append( '[]', '$', b < some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2182 |
|
| 2183 |
select a, json_array( b < all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2184 |
select a, json_array_append( '[]', '$', b < all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2185 |
|
| 2186 |
select a, json_array( b < any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2187 |
select a, json_array_append( '[]', '$', b < any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2188 |
|
| 2189 |
select a, json_array( b <= some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2190 |
select a, json_array_append( '[]', '$', b <= some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2191 |
|
| 2192 |
select a, json_array( b <= all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2193 |
select a, json_array_append( '[]', '$', b <= all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2194 |
|
| 2195 |
select a, json_array( b <= any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2196 |
select a, json_array_append( '[]', '$', b <= any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2197 |
|
| 2198 |
select a, json_array( b >= some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2199 |
select a, json_array_append( '[]', '$', b >= some( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2200 |
|
| 2201 |
select a, json_array( b >= all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2202 |
select a, json_array_append( '[]', '$', b >= all( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2203 |
|
| 2204 |
select a, json_array( b >= any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2205 |
select a, json_array_append( '[]', '$', b >= any( select b from t_bool_literals ) ) from t_bool_literals order by a; |
| 2206 |
|
| 2207 |
# exists predicates should evaluate to boolean literals |
| 2208 |
select a, json_array( exists( select b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2209 |
select a, json_array_append( '[]', '$', exists( select b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2210 |
|
| 2211 |
select a, json_array( not exists( select b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2212 |
select a, json_array_append( '[]', '$', not exists( select b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2213 |
|
| 2214 |
# json_valid() calls should evaluate to boolean literals |
| 2215 |
select a, json_array( json_valid( b ) ) from t_bool_literals order by a; |
| 2216 |
select a, json_array_append( '[]', '$', json_valid( b ) ) from t_bool_literals order by a; |
| 2217 |
|
| 2218 |
select a, json_array( not json_valid( b ) ) from t_bool_literals order by a; |
| 2219 |
select a, json_array_append( '[]', '$', not json_valid( b ) ) from t_bool_literals order by a; |
| 2220 |
|
| 2221 |
# json_contains_path() calls should evaluate to boolean literals |
| 2222 |
select json_array( json_contains_path( '{ "a" : { "b" : 100 } }', 'all', '$.a.b' ) ); |
| 2223 |
|
| 2224 |
# gtid_subset() calls should evaluate to boolean literals |
| 2225 |
select a, json_array( gtid_subset('3E11FA47-71CA-11E1-9E33-C80AA9429562:23', '3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57') ) |
| 2226 |
from t_bool_literals order by a; |
| 2227 |
select a, json_array_append( '[]', '$', gtid_subset('3E11FA47-71CA-11E1-9E33-C80AA9429562:23', '3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57') ) |
| 2228 |
from t_bool_literals order by a; |
| 2229 |
|
| 2230 |
select a, json_array( not gtid_subset('3E11FA47-71CA-11E1-9E33-C80AA9429562:23', '3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57') ) |
| 2231 |
from t_bool_literals order by a; |
| 2232 |
select a, json_array_append( '[]', '$', not gtid_subset('3E11FA47-71CA-11E1-9E33-C80AA9429562:23', '3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57') ) |
| 2233 |
from t_bool_literals order by a; |
| 2234 |
|
| 2235 |
# comparisons to subqueries should evaluate to boolean literals |
| 2236 |
select a, json_array( b = ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2237 |
select a, json_array_append( '[]', '$', b = ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2238 |
|
| 2239 |
select a, json_array( b > ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2240 |
select a, json_array_append( '[]', '$', b > ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2241 |
|
| 2242 |
select a, json_array( b >= ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2243 |
select a, json_array_append( '[]', '$', b >= ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2244 |
|
| 2245 |
select a, json_array( b < ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2246 |
select a, json_array_append( '[]', '$', b < ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2247 |
|
| 2248 |
select a, json_array( b <= ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2249 |
select a, json_array_append( '[]', '$', b <= ( select distinct b from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2250 |
|
| 2251 |
# make sure ordinary subselects still function correctly |
| 2252 |
select a, json_array( ( select distinct a from t_bool_literals where a = 1 ) ) from t_bool_literals order by a; |
| 2253 |
|
| 2254 |
drop table t_bool_literals; |
| 2255 |
|
| 2256 |
--echo # ---------------------------------------------------------------------- |
| 2257 |
--echo # Verify that all of the string types behave similarly when used as ANY_JSON_ATOMS |
| 2258 |
--echo # ---------------------------------------------------------------------- |
| 2259 |
|
| 2260 |
create table t_char( a int, b char(20) ); |
| 2261 |
insert into t_char values ( 1, 'foo' ); |
| 2262 |
|
| 2263 |
create table t_varchar( a int, b varchar(20) ); |
| 2264 |
insert into t_varchar values ( 1, 'foo' ); |
| 2265 |
|
| 2266 |
Create table t_tinytext( a int, b tinytext ); |
| 2267 |
insert into t_tinytext values ( 1, 'foo' ); |
| 2268 |
|
| 2269 |
create table t_text( a int, b text ); |
| 2270 |
insert into t_text values ( 1, 'foo' ); |
| 2271 |
|
| 2272 |
create table t_mediumtext( a int, b mediumtext ); |
| 2273 |
insert into t_mediumtext values ( 1, 'foo' ); |
| 2274 |
|
| 2275 |
create table t_longtext( a int, b longtext ); |
| 2276 |
insert into t_longtext values ( 1, 'foo' ); |
| 2277 |
|
| 2278 |
# treated as a string. evaluates to ["foo"] |
| 2279 |
select json_array( b ) from t_char; |
| 2280 |
select json_array( b ) from t_varchar; |
| 2281 |
select json_array( b ) from t_tinytext; |
| 2282 |
select json_array( b ) from t_text; |
| 2283 |
select json_array( b ) from t_mediumtext; |
| 2284 |
select json_array( b ) from t_longtext; |
| 2285 |
|
| 2286 |
# casts to CHAR should still be strings |
| 2287 |
select json_array( cast( b as char ) ) from t_char; |
| 2288 |
select json_array( cast( b as char ) ) from t_varchar; |
| 2289 |
select json_array( cast( b as char ) ) from t_tinytext; |
| 2290 |
select json_array( cast( b as char ) ) from t_text; |
| 2291 |
select json_array( cast( b as char ) ) from t_mediumtext; |
| 2292 |
select json_array( cast( b as char ) ) from t_longtext; |
| 2293 |
|
| 2294 |
# string-valued XML functions should behave as strings when used as ANY_JSON_ATOMs |
| 2295 |
select json_array( UpdateXML('<a><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>') ); |
| 2296 |
select json_array( cast( UpdateXML('<a><b>ccc</b><d></d></a>', '/a/d', '<e>fff</e>') as char ) ); |
| 2297 |
select json_array( ExtractValue('<r><n id="1">v1</n><n id="2">v2</n></r>','//n[@id=1]' ) ); |
| 2298 |
select json_array( cast( ExtractValue('<r><n id="1">v1</n><n id="2">v2</n></r>','//n[@id=1]' ) as char ) ); |
| 2299 |
|
| 2300 |
drop table t_char; |
| 2301 |
drop table t_varchar; |
| 2302 |
drop table t_tinytext; |
| 2303 |
drop table t_text; |
| 2304 |
drop table t_mediumtext; |
| 2305 |
drop table t_longtext; |
| 2306 |
|
| 2307 |
--echo # ---------------------------------------------------------------------- |
| 2308 |
--echo # Check that JSON values stemming from views and derived tables work |
| 2309 |
--echo # ---------------------------------------------------------------------- |
| 2310 |
create table t(x int); |
| 2311 |
insert into t values (NULL), (4); |
| 2312 |
select json_array(x) from (select x from t) tt order by x; |
| 2313 |
create view v as select * from t; |
| 2314 |
select json_array(x) from v order by x; |
| 2315 |
|
| 2316 |
drop view v; |
| 2317 |
drop table t; |
| 2318 |
|
| 2319 |
--echo # ---------------------------------------------------------------------- |
| 2320 |
--echo # Ignore collation.collation when handing off val_str to a JSON field - |
| 2321 |
--echo # bug found by John E. |
| 2322 |
--echo # ---------------------------------------------------------------------- |
| 2323 |
create table t3( col_json json ); |
| 2324 |
insert into t3(col_json) values ( json_quote( '1' ) ); |
| 2325 |
select * from t3; |
| 2326 |
select json_type(col_json) from t3; |
| 2327 |
|
| 2328 |
drop table t3; |
| 2329 |
|
| 2330 |
--echo # ---------------------------------------------------------------------- |
| 2331 |
--echo # Correctly escape key names when pretty-printing JSON objects. |
| 2332 |
--echo # Correct behavior means that the strings can be re-used for |
| 2333 |
--echo # their original purposes as key names and paths. |
| 2334 |
--echo # ---------------------------------------------------------------------- |
| 2335 |
|
| 2336 |
create table jep( key_col int primary key, doc json, path varchar( 50 ) ); |
| 2337 |
insert into jep values |
| 2338 |
( 1, '{ "one \\"potato": "seven" }', '$."one \\"potato"' ), |
| 2339 |
( 2, '{ "one \\npotato": "seven" }', '$."one \\npotato"' ), |
| 2340 |
( 3, '{ "one \\tpotato": "seven" }', '$."one \\tpotato"' ), |
| 2341 |
( 4, '{ "one \\bpotato": "seven" }', '$."one \\bpotato"' ), |
| 2342 |
( 5, '{ "one \\fpotato": "seven" }', '$."one \\fpotato"' ), |
| 2343 |
( 6, '{ "one \\rpotato": "seven" }', '$."one \\rpotato"' ), |
| 2344 |
( 7, '{ "one \\\\potato": "seven" }', '$."one \\\\potato"' ); |
| 2345 |
|
| 2346 |
insert into jep select key_col + 100, cast( doc as char ), path from jep; |
| 2347 |
|
| 2348 |
select key_col, doc, json_keys( doc ) from jep order by key_col; |
| 2349 |
|
| 2350 |
select key_col, doc, json_extract( doc, cast(path as char) ) from jep order by key_col; |
| 2351 |
|
| 2352 |
select * from jep order by key_col; |
| 2353 |
|
| 2354 |
drop table jep; |
| 2355 |
|
| 2356 |
--echo # ---------------------------------------------------------------------- |
| 2357 |
--echo # Test that cached, constant path objects are restored |
| 2358 |
--echo # after the leg popping which happens inside json_insert() |
| 2359 |
--echo # and json_replace(). |
| 2360 |
--echo # ---------------------------------------------------------------------- |
| 2361 |
|
| 2362 |
create table t_cache( id int, doc json ); |
| 2363 |
|
| 2364 |
insert into t_cache values |
| 2365 |
( 1, '{ "a": { "b": 1 } }' ), |
| 2366 |
( 2, '{ "a": { "c": 1 } }' ), |
| 2367 |
( 3, '{ "a": { "d": 1 } }' ); |
| 2368 |
|
| 2369 |
select id, doc, json_insert( doc, '$.a.c', 2 ) from t_cache order by id; |
| 2370 |
select id, doc, json_insert( doc, '$.a.c', 2, '$.a.d', 3 ) from t_cache order by id; |
| 2371 |
|
| 2372 |
delete from t_cache; |
| 2373 |
|
| 2374 |
insert into t_cache values |
| 2375 |
( 1, '{ "a": { "b": 1, "c": 2, "d": 3 } }' ), |
| 2376 |
( 2, '{ "a": { "c": 2, "d": 3 } }' ), |
| 2377 |
( 3, '{ "a": { "b": 1, "d": 3 } }' ), |
| 2378 |
( 4, '{ "a": { "b": 1, "c": 2 } }' ), |
| 2379 |
( 5, '{ "a": { "b": 1 } }' ), |
| 2380 |
( 6, '{ "a": { "c": 2 } }' ), |
| 2381 |
( 7, '{ "a": { "d": 3 } }' ), |
| 2382 |
( 8, '{ "a": {} }' ); |
| 2383 |
|
| 2384 |
select id, doc, json_replace( doc, '$.a.c', 20 ) from t_cache order by id; |
| 2385 |
select id, doc, json_replace( doc, '$.a.c', 20, '$.a.d', 30 ) from t_cache order by id; |
| 2386 |
|
| 2387 |
drop table t_cache; |
| 2388 |
|
| 2389 |
--echo # ---------------------------------------------------------------------- |
| 2390 |
--echo # Test that one_or_all arguments are cached correctly. |
| 2391 |
--echo # ---------------------------------------------------------------------- |
| 2392 |
|
| 2393 |
create table t_ooa( id int, doc json, one_or_all varchar(10) ); |
| 2394 |
|
| 2395 |
insert into t_ooa values |
| 2396 |
( 1, '{ "a": 1, "b": 2, "c": 3 }', 'one' ), |
| 2397 |
( 2, '{ "d": 4 }', 'one' ), |
| 2398 |
( 3, '{ "a": 1, "b": 2, "d": 4 }', 'all' ), |
| 2399 |
( 4, '{ "a": 1, "c": 3 }', 'all' ), |
| 2400 |
( 5, '{ "d": 4 }', 'all' ), |
| 2401 |
( 6, '{ "a": 1, "b": 2, "c": 3 }', null ); |
| 2402 |
|
| 2403 |
select id, doc, one_or_all, json_contains_path( doc, one_or_all, '$.a', '$.b' ) from t_ooa order by id; |
| 2404 |
select id, doc, json_contains_path( doc, 'one', '$.a', '$.b' ) from t_ooa order by id; |
| 2405 |
select id, doc, json_contains_path( doc, 'all', '$.a', '$.b' ) from t_ooa order by id; |
| 2406 |
select id, doc, json_contains_path( doc, null, '$.a', '$.b' ) from t_ooa order by id; |
| 2407 |
|
| 2408 |
delete from t_ooa; |
| 2409 |
|
| 2410 |
insert into t_ooa values |
| 2411 |
( 1, '{ "a": "foot", "b": "fool", "c": "food" }', 'one' ), |
| 2412 |
( 1, '{ "a": "foot", "b": "fool", "c": "food" }', 'all' ), |
| 2413 |
( 1, '{ "a": "foot", "b": "fool", "c": "food" }', null ); |
| 2414 |
|
| 2415 |
--sorted_result |
| 2416 |
select id, doc, one_or_all, json_search( doc, one_or_all, 'foo%' ) from t_ooa order by id; |
| 2417 |
select id, doc, json_search( doc, 'one', 'foo%' ) from t_ooa order by id; |
| 2418 |
select id, doc, json_search( doc, 'all', 'foo%' ) from t_ooa order by id; |
| 2419 |
select id, doc, json_search( doc, null, 'foo%' ) from t_ooa order by id; |
| 2420 |
|
| 2421 |
drop table t_ooa; |
| 2422 |
|
| 2423 |
# This test case reproduces a problem seen during development. The update |
| 2424 |
# statement crashed if the target table was the inner table of the join. |
| 2425 |
CREATE TABLE t1(j JSON); |
| 2426 |
CREATE TABLE t2(j JSON); |
| 2427 |
INSERT INTO t1 VALUES ('[1]'), ('[2]'), ('[3]'), ('[4]'); |
| 2428 |
INSERT INTO t2 VALUES ('[1]'); |
| 2429 |
ANALYZE TABLE t1, t2; |
| 2430 |
let $query= |
| 2431 |
UPDATE t1, t2 SET t1.j = JSON_INSERT(t2.j, '\$[1]', t2.j) WHERE t1.j=t2.j; |
| 2432 |
eval EXPLAIN $query; |
| 2433 |
eval $query; |
| 2434 |
SELECT * FROM t1 ORDER BY (CAST(j AS CHAR)); |
| 2435 |
DROP TABLE t1, t2; |
| 2436 |
|
| 2437 |
--echo # |
| 2438 |
--echo # Bug#20888919: ASSERT `!THD->IS_ERROR()' FAILED IN HANDLE_QUERY() |
| 2439 |
--echo # ON EXPLAIN SELECT JSON |
| 2440 |
--echo # |
| 2441 |
create table t (pk int primary key, col_json json); |
| 2442 |
ANALYZE TABLE t; |
| 2443 |
explain SELECT col_json FROM t WHERE pk = 1; |
| 2444 |
drop table t; |
| 2445 |
|
| 2446 |
--echo # ---------------------------------------------------------------------- |
| 2447 |
--echo # Bug#20889248 Used to crash the server |
| 2448 |
--echo # ---------------------------------------------------------------------- |
| 2449 |
create table tt(i int, j json, si int); |
| 2450 |
select count(*) , json_keys('{"key17": {"a": {"b": "c"}}, "key88": "value94"}'); |
| 2451 |
|
| 2452 |
# Tests Item_copy_json::save_in_field. int target column here gets assigned via |
| 2453 |
# JSON->string->int parse since Field_long::store doesn't have an overload for |
| 2454 |
# JSON. Similar for other non-JSON target columns. The JSON column assignment |
| 2455 |
# does not go via string, since Field_json knows how to store JSON. |
| 2456 |
insert into tt(i, j) |
| 2457 |
select count(*), json_extract('{"key17": {"a": {"b": "c"}}, "key88": 100}', |
| 2458 |
'$.key88'); |
| 2459 |
insert into tt(i, si) |
| 2460 |
select count(*), json_extract('{"key17": {"a": {"b": "c"}}, "key88": 100}', |
| 2461 |
'$.key88'); |
| 2462 |
--sorted_result |
| 2463 |
select * from tt order by i; |
| 2464 |
|
| 2465 |
# This exercises Item_copy_json::val_real |
| 2466 |
delete from tt; |
| 2467 |
insert into tt(j) values (cast(1 as json)), (null); |
| 2468 |
select sum( distinct j ) from tt group by j having j in ( avg( 1 ), 1 + j); |
| 2469 |
|
| 2470 |
# Exercise Item_copy_json::val_json |
| 2471 |
SELECT JSON_ARRAY(j), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2472 |
|
| 2473 |
# Exercise Item_copy_json::val_int |
| 2474 |
SELECT REPEAT('abc', j), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2475 |
|
| 2476 |
# Exercise Item_copy_json::val_str |
| 2477 |
SELECT REPEAT(j, 2), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2478 |
|
| 2479 |
# Exercise Item_copy_json::val_decimal |
| 2480 |
SELECT CAST(j AS DECIMAL(5,2)), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2481 |
|
| 2482 |
# Exercise Item_copy_json::get_time |
| 2483 |
UPDATE tt SET j = CAST(CAST('12:13:14' AS TIME) AS JSON) WHERE j IS NOT NULL; |
| 2484 |
SELECT CAST(j AS TIME), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2485 |
|
| 2486 |
# Exercise Item_copy_json::get_date |
| 2487 |
SELECT CAST(j AS DATE) = CURRENT_DATE, COUNT(*) FROM tt |
| 2488 |
GROUP BY j, i WITH ROLLUP; |
| 2489 |
UPDATE tt SET j = CAST(CAST('2015-06-19' AS DATE) AS JSON) WHERE j IS NOT NULL; |
| 2490 |
SELECT CAST(j AS DATE), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2491 |
|
| 2492 |
# Exercise an error path through Item_copy_json::val_str. |
| 2493 |
# Note that most functions would return NULL here, but Field_json::val_str() |
| 2494 |
# sets length to zero on error, so it becomes the empty string instead. |
| 2495 |
# This is consistent with non-ROLLUP, which we also test for comparison. |
| 2496 |
DELETE FROM tt; |
| 2497 |
INSERT INTO tt(j) VALUES (JSON_ARRAY(REPEAT('abc', 100))); |
| 2498 |
UPDATE tt SET j = JSON_ARRAY(j,j,j,j); |
| 2499 |
SET GLOBAL net_buffer_length = 1024; |
| 2500 |
SET GLOBAL max_allowed_packet = 1024; |
| 2501 |
CONNECT (con1,localhost,root,,); |
| 2502 |
CONNECTION con1; |
| 2503 |
SELECT REPEAT(j, 2), COUNT(*) FROM tt GROUP BY j, i WITH ROLLUP; |
| 2504 |
SELECT REPEAT(j, 2), COUNT(*) FROM tt GROUP BY j, i; |
| 2505 |
CONNECTION default; |
| 2506 |
DISCONNECT con1; |
| 2507 |
SET GLOBAL max_allowed_packet = default; |
| 2508 |
SET GLOBAL net_buffer_length = default; |
| 2509 |
|
| 2510 |
DROP TABLE tt; |
| 2511 |
|
| 2512 |
--echo # ---------------------------------------------------------------------- |
| 2513 |
--echo # Bug#20914054 Used to crash the server |
| 2514 |
--echo # ---------------------------------------------------------------------- |
| 2515 |
CREATE TABLE t1 ( |
| 2516 |
pk INT NOT NULL, |
| 2517 |
col_int_key INT, |
| 2518 |
col_json json, |
| 2519 |
PRIMARY KEY (pk), |
| 2520 |
KEY col_int_key (col_int_key) |
| 2521 |
); |
| 2522 |
|
| 2523 |
INSERT INTO t1 VALUES (8, 4, '{}'); |
| 2524 |
|
| 2525 |
CREATE TABLE t2 ( |
| 2526 |
pk INT NOT NULL, |
| 2527 |
PRIMARY KEY (pk) |
| 2528 |
); |
| 2529 |
|
| 2530 |
INSERT INTO t2 VALUES (20); |
| 2531 |
|
| 2532 |
SELECT MIN(JSON_KEYS( t1.col_json )) AS field1 |
| 2533 |
FROM t1 JOIN t2 |
| 2534 |
HAVING field1 = 7; |
| 2535 |
|
| 2536 |
drop table t1; |
| 2537 |
drop table t2; |
| 2538 |
|
| 2539 |
--echo # ---------------------------------------------------------------------- |
| 2540 |
--echo # Bug#20920788 Used to give SQL state 22032: Cannot create a JSON value |
| 2541 |
--echo # from a string with CHARACTER SET 'binary'. |
| 2542 |
--echo #---------------------------------------------------------------------- |
| 2543 |
|
| 2544 |
CREATE TABLE t ( |
| 2545 |
col_json JSON, |
| 2546 |
col_varchar VARCHAR(1), |
| 2547 |
col_varchar_key VARCHAR(1), |
| 2548 |
KEY col_varchar_key (col_varchar_key) |
| 2549 |
); |
| 2550 |
|
| 2551 |
INSERT INTO t VALUES ('{}', 'a', 'a'); |
| 2552 |
|
| 2553 |
--echo # This always succeeded, group by column is indexed, optimizer does not |
| 2554 |
--echo # use filesort: |
| 2555 |
SELECT MAX(col_json) AS field1, col_varchar_key AS field2 FROM t GROUP BY field2; |
| 2556 |
|
| 2557 |
--echo # This used to fail, group by column is not indexed, EXPLAIN says |
| 2558 |
--echo # filesort is used: |
| 2559 |
SELECT MAX(col_json) AS field1, col_varchar AS field2 FROM t GROUP BY field2; |
| 2560 |
|
| 2561 |
drop table t; |
| 2562 |
|
| 2563 |
--echo # ---------------------------------------------------------------------- |
| 2564 |
--echo # Bug#20962317 WARNING 3150 'INVALID JSON VALUE FOR CAST TO INTEGER' ON |
| 2565 |
--echo # SUBQUERY IN JSON_VALID |
| 2566 |
--echo #---------------------------------------------------------------------- |
| 2567 |
create table myt(col_json json); |
| 2568 |
insert into myt values ('{}'); |
| 2569 |
--echo # This statement used to give two wrong warnings |
| 2570 |
select json_valid((select col_json from myt)); |
| 2571 |
drop table myt; |
| 2572 |
|
| 2573 |
--echo # ---------------------------------------------------------------------- |
| 2574 |
--echo # Bug#20954309 JSON_SEARCH() IN VIEWS DOES NOT WORK, ALWAYS RETURNS NULL |
| 2575 |
--echo #---------------------------------------------------------------------- |
| 2576 |
|
| 2577 |
CREATE TABLE t_20954309 (id int, col_json JSON); |
| 2578 |
INSERT INTO t_20954309 VALUES |
| 2579 |
(2, '{"keyA": "eleven"}'); |
| 2580 |
CREATE VIEW v1_20954309 AS SELECT id, JSON_SEARCH(col_json, 'one', 'ele%' ) FROM t_20954309; |
| 2581 |
|
| 2582 |
CREATE VIEW v2_20954309 AS SELECT id, col_json FROM t_20954309; |
| 2583 |
|
| 2584 |
|
| 2585 |
SELECT id, JSON_SEARCH(col_json, 'one', 'ele%' ) from t_20954309 order by id; |
| 2586 |
SELECT id, JSON_SEARCH(col_json, 'one', 'eleven' ) from v2_20954309 order by id; |
| 2587 |
|
| 2588 |
SELECT * FROM v1_20954309 order by id; |
| 2589 |
|
| 2590 |
drop view v1_20954309; |
| 2591 |
drop view v2_20954309; |
| 2592 |
drop table t_20954309; |
| 2593 |
|
| 2594 |
# |
| 2595 |
# Arguments vary from row to row. |
| 2596 |
# |
| 2597 |
create table t_20954309 (id int, doc JSON, search_string varchar(20), escape_char varchar(10) ); |
| 2598 |
insert into t_20954309 values |
| 2599 |
(1, '{"match11": "eleven", "match12": "element", "notMatch": "elven" }', 'ele%', null ), |
| 2600 |
(2, '{"match21": "eleven", "match22": "element", "notMatch": "elven" }', 'ele%', 'z' ), |
| 2601 |
(3, '{"match31": "tw%elve", "match32": "tw%ilight", "notMatch": "twitter" }', 'tw|%%', '|' ); |
| 2602 |
|
| 2603 |
select id, json_search( doc, 'all', search_string, '|' ) from t_20954309 order by id; |
| 2604 |
|
| 2605 |
create view v_20954309 as select id, json_search( doc, 'all', search_string, '|' ) from t_20954309 order by id; |
| 2606 |
select * from v_20954309; |
| 2607 |
|
| 2608 |
select id, json_search( doc, 'all', search_string, null ) from t_20954309 where id < 3 order by id; |
| 2609 |
create view v2_20954309 as select id, json_search( doc, 'all', search_string, null ) result from t_20954309 where id < 3 order by id; |
| 2610 |
select * from v2_20954309; |
| 2611 |
|
| 2612 |
drop view v_20954309; |
| 2613 |
drop view v2_20954309; |
| 2614 |
drop table t_20954309; |
| 2615 |
|
| 2616 |
create table t_doc (id int, doc JSON ); |
| 2617 |
insert into t_doc values |
| 2618 |
(1, '{"match11": "eleven", "match12": "element", "notMatch": "elven" }' ), |
| 2619 |
(2, '{"match21": "eleven", "match22": "element", "notMatch": "elven" }' ), |
| 2620 |
(3, '{"match31": "tw%elve", "match32": "tw%ilight", "notMatch": "twitter" }' ); |
| 2621 |
|
| 2622 |
create table t_search_string (id int, search_string varchar(20) ); |
| 2623 |
insert into t_search_string values |
| 2624 |
(1, 'ele%' ), |
| 2625 |
(2, 'ele%' ), |
| 2626 |
(3, 'tw|%%' ); |
| 2627 |
|
| 2628 |
select t.id, json_search( doc, 'all', (select search_string from t_search_string s where s.id = t.id), '|' ) |
| 2629 |
from t_doc t order by id; |
| 2630 |
|
| 2631 |
create view v_doc as |
| 2632 |
select t.id, json_search( doc, 'all', (select search_string from t_search_string s where s.id = t.id), '|' ) |
| 2633 |
from t_doc t order by id; |
| 2634 |
|
| 2635 |
select * from v_doc; |
| 2636 |
|
| 2637 |
drop view v_doc; |
| 2638 |
drop table t_doc; |
| 2639 |
drop table t_search_string; |
| 2640 |
|
| 2641 |
set names default; |
| 2642 |
|
| 2643 |
-- echo # |
| 2644 |
-- echo # Bug#20972793 ASSERT FIELD_TYPE() == MYSQL_TYPE_JSON... |
| 2645 |
-- echo # IN ARG_COMPARATOR::COMPARE_JSON |
| 2646 |
-- echo # |
| 2647 |
CREATE TABLE t1 ( |
| 2648 |
pk INT NOT NULL, |
| 2649 |
col_int_key INT, |
| 2650 |
col_int INT, |
| 2651 |
col_json JSON, |
| 2652 |
PRIMARY KEY (pk), |
| 2653 |
KEY col_int_key (col_int_key) |
| 2654 |
); |
| 2655 |
INSERT INTO t1 VALUES (2,4,2,NULL); |
| 2656 |
CREATE TABLE t2 ( |
| 2657 |
pk INT NOT NULL, |
| 2658 |
col_int_key INT, |
| 2659 |
PRIMARY KEY (pk), |
| 2660 |
KEY col_int_key (col_int_key) |
| 2661 |
); |
| 2662 |
SELECT |
| 2663 |
(SELECT MAX(sq1_alias1.pk) AS sq1_field1 |
| 2664 |
FROM (t1 AS sq1_alias1 |
| 2665 |
INNER JOIN t2 AS sq1_alias2 |
| 2666 |
ON (sq1_alias2.col_int_key = sq1_alias1.col_int_key) |
| 2667 |
) |
| 2668 |
WHERE sq1_alias2.pk <= alias1.col_int |
| 2669 |
) AS field1, |
| 2670 |
MAX(alias1.col_json) AS field2 |
| 2671 |
FROM ( |
| 2672 |
SELECT sq2_alias1.* |
| 2673 |
FROM t1 AS sq2_alias1 |
| 2674 |
) AS alias1 |
| 2675 |
GROUP BY field1 |
| 2676 |
HAVING field2 > 1; |
| 2677 |
DROP TABLE t1, t2; |
| 2678 |
|
| 2679 |
--echo # ---------------------------------------------------------------------- |
| 2680 |
--echo # Bug#20987329 VALUE OF PREPARED STATEMENT PLACEHOLDER FOR PARAMETER |
| 2681 |
--echo # IN JSON_EXTRACT IS STICKY |
| 2682 |
--echo #---------------------------------------------------------------------- |
| 2683 |
|
| 2684 |
# should get different results with different parameter values |
| 2685 |
|
| 2686 |
# json_extract() |
| 2687 |
|
| 2688 |
CREATE TABLE t_reuse (pk INT, col_json JSON); |
| 2689 |
INSERT INTO t_reuse VALUES (1, '{"keyA": 1}'), (2, '{"keyA": 2, "keyB": 22}'); |
| 2690 |
|
| 2691 |
PREPARE getjson FROM 'SELECT JSON_EXTRACT(col_json, ?) FROM t_reuse order by pk'; |
| 2692 |
SET @mypath = '$.keyA'; |
| 2693 |
EXECUTE getjson USING @mypath; |
| 2694 |
SET @mypath = '$.keyB'; |
| 2695 |
EXECUTE getjson USING @mypath; |
| 2696 |
|
| 2697 |
drop table t_reuse; |
| 2698 |
|
| 2699 |
--echo # |
| 2700 |
--echo # Test that max_allowed_packet is respected. |
| 2701 |
--echo # |
| 2702 |
SET GLOBAL net_buffer_length = 1024; |
| 2703 |
SET GLOBAL max_allowed_packet = 1024; |
| 2704 |
CONNECT (con1,localhost,root,,); |
| 2705 |
CONNECTION con1; |
| 2706 |
CREATE TABLE t1(j JSON); |
| 2707 |
INSERT INTO t1 VALUES (JSON_ARRAY(REPEAT('abc', 100))); |
| 2708 |
SELECT JSON_ARRAY(j, j, j, j) FROM t1; |
| 2709 |
--error ER_WARN_ALLOWED_PACKET_OVERFLOWED |
| 2710 |
UPDATE t1 SET j = JSON_ARRAY(j, j, j, j); |
| 2711 |
CREATE TABLE t2(s TEXT); |
| 2712 |
--error ER_WARN_ALLOWED_PACKET_OVERFLOWED |
| 2713 |
INSERT INTO t2 SELECT JSON_ARRAY(j, j, j, j) FROM t1; |
| 2714 |
SELECT * FROM t2; |
| 2715 |
INSERT INTO t2 SELECT * FROM t1; |
| 2716 |
--error ER_WARN_ALLOWED_PACKET_OVERFLOWED |
| 2717 |
UPDATE t2 SET s = JSON_ARRAY(s, s, s, s); |
| 2718 |
DROP TABLE t1, t2; |
| 2719 |
CONNECTION default; |
| 2720 |
DISCONNECT con1; |
| 2721 |
SET GLOBAL max_allowed_packet = default; |
| 2722 |
SET GLOBAL net_buffer_length = default; |
| 2723 |
|
| 2724 |
--echo # |
| 2725 |
--echo # Test that very deep documents are rejected. |
| 2726 |
--echo # |
| 2727 |
|
| 2728 |
# Currently, we cannot nest more than 100 levels of objects and arrays. Make |
| 2729 |
# some documents that are nested to that exact depth. |
| 2730 |
CREATE TABLE t(jarray JSON, jobject JSON, jmix JSON) ROW_FORMAT=DYNAMIC; |
| 2731 |
INSERT INTO t VALUES ('1', '1', '1'); |
| 2732 |
let $depth=0; |
| 2733 |
while ($depth < 100) |
| 2734 |
{ |
| 2735 |
eval UPDATE t SET jarray = JSON_ARRAY(jarray), |
| 2736 |
jobject = JSON_OBJECT('a', jobject), |
| 2737 |
jmix = CASE WHEN MOD($depth, 2) = 0 |
| 2738 |
THEN JSON_ARRAY(jmix) |
| 2739 |
ELSE JSON_OBJECT('a', jmix) |
| 2740 |
END; |
| 2741 |
inc $depth; |
| 2742 |
} |
| 2743 |
SELECT JSON_DEPTH(jarray), JSON_DEPTH(jobject), JSON_DEPTH(jmix) FROM t; |
| 2744 |
|
| 2745 |
SELECT JSON_DEPTH(CAST(jarray AS CHAR)), |
| 2746 |
JSON_DEPTH(CAST(jobject AS CHAR)), |
| 2747 |
JSON_DEPTH(CAST(jmix AS CHAR)) FROM t; |
| 2748 |
|
| 2749 |
SELECT JSON_VALID(jarray), JSON_VALID(jobject), JSON_VALID(jmix) FROM t; |
| 2750 |
|
| 2751 |
SELECT JSON_VALID(CAST(jarray AS CHAR)), |
| 2752 |
JSON_VALID(CAST(jobject AS CHAR)), |
| 2753 |
JSON_VALID(CAST(jmix AS CHAR)) FROM t; |
| 2754 |
|
| 2755 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2756 |
INSERT INTO t(jarray) SELECT JSON_ARRAY(jarray) FROM t; |
| 2757 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2758 |
INSERT INTO t(jobject) SELECT JSON_OBJECT('a', jobject) FROM t; |
| 2759 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2760 |
INSERT INTO t(jmix) SELECT JSON_ARRAY(jmix) FROM t; |
| 2761 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2762 |
INSERT INTO t(jmix) SELECT JSON_OBJECT('a', jmix) FROM t; |
| 2763 |
|
| 2764 |
CREATE TABLE too_deep_docs(id INT PRIMARY KEY AUTO_INCREMENT, x text); |
| 2765 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('[', jarray, ']') FROM t; |
| 2766 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('[', jobject, ']') FROM t; |
| 2767 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('[', jmix, ']') FROM t; |
| 2768 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('{"a":', jarray, '}') FROM t; |
| 2769 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('{"a":', jobject, '}') FROM t; |
| 2770 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('{"a":', jmix, '}') FROM t; |
| 2771 |
INSERT INTO too_deep_docs(x) SELECT CONCAT('["abc", [', jarray, ']]') FROM t; |
| 2772 |
INSERT INTO too_deep_docs(x) |
| 2773 |
SELECT CONCAT('{"a":2,"b":{"c":', jobject, '}}') FROM t; |
| 2774 |
|
| 2775 |
let $count= `SELECT COUNT(*) FROM too_deep_docs`; |
| 2776 |
while ($count) |
| 2777 |
{ |
| 2778 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2779 |
eval SELECT CAST(x AS JSON) FROM too_deep_docs WHERE id=$count; |
| 2780 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2781 |
eval SELECT JSON_DEPTH(x) FROM too_deep_docs WHERE id=$count; |
| 2782 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2783 |
eval SELECT JSON_VALID(x) FROM too_deep_docs WHERE id=$count; |
| 2784 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2785 |
eval INSERT INTO t(jarray) SELECT x FROM too_deep_docs WHERE id=$count; |
| 2786 |
dec $count; |
| 2787 |
} |
| 2788 |
|
| 2789 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2790 |
SELECT CAST(JSON_ARRAY(jarray) AS CHAR) FROM t; |
| 2791 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2792 |
SELECT CAST(JSON_OBJECT('a', jobject) AS CHAR) FROM t; |
| 2793 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2794 |
SELECT CAST(JSON_ARRAY(jmix) AS CHAR) FROM t; |
| 2795 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 2796 |
SELECT CAST(JSON_OBJECT('a', jmix) AS CHAR) FROM t; |
| 2797 |
|
| 2798 |
DROP TABLE t, too_deep_docs; |
| 2799 |
|
| 2800 |
--echo # |
| 2801 |
--echo # Bug#21054252 QUERY HAVING SQL_BIG_RESULT ON JSON DATA GIVES INVALID |
| 2802 |
--echo # DATA ERROR |
| 2803 |
--echo # |
| 2804 |
CREATE TABLE t1 (i1 INT, KEY(i1)); |
| 2805 |
CREATE TABLE t2 (i2 INT, j2 JSON); |
| 2806 |
INSERT INTO t2 (i2, j2) VALUES |
| 2807 |
(1, '["a"]'), |
| 2808 |
(2, '["ab"]'); |
| 2809 |
--source include/turn_off_only_full_group_by.inc |
| 2810 |
SELECT SQL_BIG_RESULT i1, j2 |
| 2811 |
FROM t2 LEFT JOIN t1 ON i2 < i1 GROUP BY j2 ORDER BY i2; |
| 2812 |
--source include/restore_sql_mode_after_turn_off_only_full_group_by.inc |
| 2813 |
DROP TABLE t1, t2; |
| 2814 |
|
| 2815 |
--echo # |
| 2816 |
--echo # Bug#21104470 WL8132:ASSERTION `! IS_SET()' FAILED. |
| 2817 |
--echo # |
| 2818 |
|
| 2819 |
# abbreviated test case |
| 2820 |
|
| 2821 |
CREATE TABLE t_21104470(j JSON); |
| 2822 |
INSERT INTO t_21104470 VALUES (NULL), (NULL); |
| 2823 |
# Should return one row with the value NULL. |
| 2824 |
SELECT j FROM t_21104470 GROUP BY j; |
| 2825 |
# This one should return NULL too |
| 2826 |
SELECT DISTINCT j FROM t_21104470; |
| 2827 |
|
| 2828 |
DROP TABLE t_21104470; |
| 2829 |
|
| 2830 |
CREATE TABLE t(j JSON NOT NULL); |
| 2831 |
# There is no default for the field, and it can't be NULL, so this should fail. |
| 2832 |
--error ER_NO_DEFAULT_FOR_FIELD |
| 2833 |
INSERT INTO t(j) VALUES (DEFAULT); |
| 2834 |
# Loosen up the checks. It works in non-strict mode. |
| 2835 |
SET sql_mode = ''; |
| 2836 |
INSERT INTO t(j) VALUES (DEFAULT); |
| 2837 |
# This query failed with 'invalid data' before bug#21104470. |
| 2838 |
SELECT * FROM t; |
| 2839 |
SET sql_mode = default; |
| 2840 |
DROP TABLE t; |
| 2841 |
|
| 2842 |
--echo # |
| 2843 |
--echo # Bug#21072360 ASSERTION `(*A)->FIELD_TYPE() == MYSQL_TYPE_JSON || |
| 2844 |
--echo # (*B)->FIELD_TYPE() == FAILED |
| 2845 |
--echo # |
| 2846 |
|
| 2847 |
CREATE TABLE t (j JSON); |
| 2848 |
INSERT INTO t VALUES ('true'), ('"abc"'); |
| 2849 |
SELECT j FROM t WHERE j <= 'xyz' AND j = 'abc'; |
| 2850 |
DROP TABLE t; |
| 2851 |
|
| 2852 |
--echo # |
| 2853 |
--echo # Bug#21094905 VALGRIND ERRORS WITH LATEST BUILDS OF WL7909 |
| 2854 |
--echo # |
| 2855 |
|
| 2856 |
CREATE TABLE F (i1 INT, j JSON); |
| 2857 |
INSERT INTO F VALUES (1, '1'), (2, '2'); |
| 2858 |
CREATE TABLE H (i2 INT); |
| 2859 |
SELECT SUM(DISTINCT i2), j FROM F LEFT JOIN H ON i1 = i2 GROUP BY j ORDER BY j; |
| 2860 |
DROP TABLE F, H; |
| 2861 |
|
| 2862 |
--echo # |
| 2863 |
--echo # Bug#21110783 WL8132:DEBUG CRASH AT WRAPPER_TO_STRING | SQL/JSON_DOM.CC |
| 2864 |
--echo # |
| 2865 |
|
| 2866 |
CREATE TABLE t1 (i1 INT) ENGINE=MyISAM; |
| 2867 |
INSERT INTO t1 VALUES (1); |
| 2868 |
CREATE TABLE t2 (i2 INT, j JSON) ENGINE=MyISAM; |
| 2869 |
CREATE TABLE t3(v VARCHAR(100), j JSON) ENGINE=MyISAM; |
| 2870 |
INSERT INTO t3(v) SELECT j FROM t1 LEFT JOIN t2 ON i1 = i2 GROUP BY j; |
| 2871 |
INSERT INTO t3(j) SELECT j FROM t1 LEFT JOIN t2 ON i1 = i2 GROUP BY j; |
| 2872 |
SELECT * FROM t3; |
| 2873 |
DROP TABLE t1, t2, t3; |
| 2874 |
|
| 2875 |
--echo # |
| 2876 |
--echo # Bug#21119971 WL8132:DEBUG CRASH AT ITEM_CACHE_JSON::CACHE_VALUE |
| 2877 |
--echo # |
| 2878 |
CREATE TABLE t1(j1 JSON); |
| 2879 |
CREATE TABLE t2(j2 JSON); |
| 2880 |
CREATE TABLE t3(j3 JSON); |
| 2881 |
INSERT INTO t1 VALUES ('1'); |
| 2882 |
INSERT INTO t2 VALUES ('1'); |
| 2883 |
INSERT INTO t3 VALUES ('1'); |
| 2884 |
SELECT * FROM t1 WHERE j1 >= ALL (SELECT j3 FROM t2 LEFT JOIN t3 ON (j2 > j3)); |
| 2885 |
DROP TABLE t1, t2, t3; |
| 2886 |
|
| 2887 |
--echo # |
| 2888 |
--echo # Bug#21145759 ER_INVALID_CAST_TO_JSON ON CALL TO JSON_REMOVE |
| 2889 |
--echo # WITH EMPTY ARG + ORDER BY |
| 2890 |
--echo # |
| 2891 |
CREATE TABLE t (pk INT PRIMARY KEY, col_json JSON); |
| 2892 |
INSERT INTO t VALUES (1, JSON_OBJECT()); |
| 2893 |
SELECT JSON_REMOVE((SELECT col_json FROM t WHERE pk = 3), |
| 2894 |
'$.testpath1') AS field1 |
| 2895 |
FROM t HAVING field1 >= 'c' ORDER BY field1; |
| 2896 |
SELECT JSON_REMOVE((SELECT col_json FROM t WHERE pk = 3), |
| 2897 |
'$.testpath1') AS field1 |
| 2898 |
FROM t HAVING field1 >= 'c'; |
| 2899 |
SELECT JSON_REMOVE((SELECT col_json FROM t WHERE pk = 3), |
| 2900 |
'$.testpath1') AS field1 |
| 2901 |
FROM t ORDER BY field1; |
| 2902 |
SELECT JSON_REMOVE((SELECT col_json FROM t WHERE pk = 3), |
| 2903 |
'$.testpath1') AS field1 |
| 2904 |
FROM t; |
| 2905 |
DROP TABLE t; |
| 2906 |
|
| 2907 |
--echo # |
| 2908 |
--echo # Bug#21135312 BUG IN JSON COMPARE OPARATOR WHEN USING JSON_QUOTE |
| 2909 |
--echo # |
| 2910 |
CREATE TABLE t1(c VARCHAR(10) CHARACTER SET latin1); |
| 2911 |
INSERT INTO t1 VALUES ('abc'), ('"abc"'); |
| 2912 |
CREATE TABLE t2(j JSON); |
| 2913 |
INSERT INTO t2 VALUES ('"abc"'); |
| 2914 |
SELECT c, |
| 2915 |
c = CAST('"abc"' AS JSON) AS eq1, |
| 2916 |
c = JSON_EXTRACT('"abc"', '$') AS eq2, |
| 2917 |
c = j AS eq3 |
| 2918 |
FROM t1, t2 ORDER BY c; |
| 2919 |
DROP TABLE t1, t2; |
| 2920 |
|
| 2921 |
--echo # |
| 2922 |
--echo # Bug#21147943 JSON_CONTAINS() RETURNS INCORRECT RESULT |
| 2923 |
--echo # |
| 2924 |
SELECT JSON_CONTAINS('[1, 2, [4] ]', '{ "b" : 2 }'); |
| 2925 |
SELECT JSON_CONTAINS('[1, 2, [4,5] ]', '[1,2,3,4,5,6,7,8,9]'); |
| 2926 |
SELECT JSON_CONTAINS('[1, 2, [4,5] ]', '[111111111111111111]'); |
| 2927 |
|
| 2928 |
--echo # |
| 2929 |
--echo # Bug#21169109 WL8249:WRONG RESULTS WHILE COMPARING A JSON COLUMN |
| 2930 |
--echo # WITH AN INDEXED INT COLUMN |
| 2931 |
--echo # |
| 2932 |
|
| 2933 |
CREATE TABLE t1(j JSON, i INT); |
| 2934 |
INSERT INTO t1 VALUES |
| 2935 |
('0', 0), ('1', 1), ('1.0', 2), ('[1,2,3]', 3), ('{}', 4), ('"abc"', 5); |
| 2936 |
ANALYZE TABLE t1; |
| 2937 |
let $query1=SELECT * FROM t1 WHERE j > i ORDER BY i; |
| 2938 |
let $query2=SELECT * FROM t1 AS a, t1 AS b WHERE a.j > b.i ORDER BY b.i, a.i; |
| 2939 |
eval EXPLAIN $query1; |
| 2940 |
eval EXPLAIN $query2; |
| 2941 |
eval $query1; |
| 2942 |
eval $query2; |
| 2943 |
--echo # Create an index on the INT column and verify that it is not used. |
| 2944 |
CREATE INDEX t1_idx ON t1(i); |
| 2945 |
ANALYZE TABLE t1; |
| 2946 |
eval EXPLAIN $query1; |
| 2947 |
eval EXPLAIN $query2; |
| 2948 |
eval $query1; |
| 2949 |
eval $query2; |
| 2950 |
CREATE TABLE t2(i INT, vc VARCHAR(10)) |
| 2951 |
ENGINE=InnoDB CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin'; |
| 2952 |
INSERT INTO t2 VALUES (1, 'abc'), (2, '"abc"'); |
| 2953 |
ANALYZE TABLE t2; |
| 2954 |
let $query= SELECT i FROM t2 WHERE vc = CAST('"abc"' AS JSON); |
| 2955 |
eval EXPLAIN $query; |
| 2956 |
eval $query; |
| 2957 |
--echo # Create an index on the VARCHAR column and verify that it is not used. |
| 2958 |
--echo # Used to return 2 instead of 1. |
| 2959 |
CREATE INDEX t2_idx ON t2(vc); |
| 2960 |
ANALYZE TABLE t2; |
| 2961 |
eval EXPLAIN $query; |
| 2962 |
eval $query; |
| 2963 |
DROP INDEX t2_idx ON t2; |
| 2964 |
--echo # Create a unique index on the VARCHAR column and verify that it is not |
| 2965 |
--echo # used. Used to return an empty result. |
| 2966 |
CREATE UNIQUE INDEX t2_idx ON t2(vc); |
| 2967 |
ANALYZE TABLE t2; |
| 2968 |
eval EXPLAIN $query; |
| 2969 |
eval $query; |
| 2970 |
DROP TABLE t1, t2; |
| 2971 |
|
| 2972 |
# test that expanded escape sequences are not truncated |
| 2973 |
create table t_varchar( a varchar(3) ); |
| 2974 |
insert into t_varchar values ( json_unquote( '"\\u0000\\u0001\\u0002"' ) ); |
| 2975 |
select length(a) l from t_varchar; |
| 2976 |
select length( json_quote( a ) ) l, json_quote( a ) v from t_varchar; |
| 2977 |
select * from |
| 2978 |
( |
| 2979 |
select length( json_quote( a ) ) as field0, |
| 2980 |
json_quote( a ) as field1 |
| 2981 |
from t_varchar |
| 2982 |
) as derived_table; |
| 2983 |
drop table t_varchar; |
| 2984 |
|
| 2985 |
--echo # |
| 2986 |
--echo # Bug#21193273 CREATE TABLE SELECT JSN_QUOTE() RESULTS |
| 2987 |
--echo # IN TRUNCATED DATA |
| 2988 |
--echo # |
| 2989 |
|
| 2990 |
set names latin1; |
| 2991 |
|
| 2992 |
SELECT JSON_QUOTE('table') AS field1; |
| 2993 |
CREATE TABLE t SELECT JSON_QUOTE('table') AS field1; |
| 2994 |
SELECT * FROM t; |
| 2995 |
DESCRIBE t; |
| 2996 |
DROP TABLE t; |
| 2997 |
|
| 2998 |
SELECT JSON_UNQUOTE( '"table"' ) AS field1; |
| 2999 |
CREATE TABLE t1 SELECT JSON_unquote( '"table"' ) AS field1; |
| 3000 |
SELECT * FROM t1; |
| 3001 |
DESCRIBE t1; |
| 3002 |
DROP TABLE t1; |
| 3003 |
|
| 3004 |
set names default; |
| 3005 |
|
| 3006 |
--echo # |
| 3007 |
--echo # Bug#25455065 JSON_QUOTE AND JSON_UNQUOTE |
| 3008 |
--echo # ARE WRONG FOR MULTIBYTE CHARSETS |
| 3009 |
--echo # |
| 3010 |
|
| 3011 |
set names utf8mb4; |
| 3012 |
|
| 3013 |
SELECT JSON_QUOTE('table') AS field1; |
| 3014 |
CREATE TABLE t SELECT JSON_QUOTE('table') AS field1; |
| 3015 |
SELECT * FROM t; |
| 3016 |
DESCRIBE t; |
| 3017 |
DROP TABLE t; |
| 3018 |
|
| 3019 |
SELECT JSON_UNQUOTE( '"table"' ) AS field1; |
| 3020 |
CREATE TABLE t1 SELECT JSON_unquote( '"table"' ) AS field1; |
| 3021 |
SELECT * FROM t1; |
| 3022 |
DESCRIBE t1; |
| 3023 |
DROP TABLE t1; |
| 3024 |
|
| 3025 |
set names default; |
| 3026 |
|
| 3027 |
--echo # |
| 3028 |
--echo # Bug#21230644 JSON_MERGE MEMORY USAGE |
| 3029 |
--echo # |
| 3030 |
|
| 3031 |
CREATE TABLE t (doc json); |
| 3032 |
INSERT INTO t VALUES('{"array":[1,2,3,4]}'); |
| 3033 |
UPDATE t SET doc=JSON_MERGE_PRESERVE(doc, doc); |
| 3034 |
UPDATE t SET doc=JSON_MERGE_PRESERVE(doc, doc); |
| 3035 |
drop table t; |
| 3036 |
|
| 3037 |
--echo # |
| 3038 |
--echo # Bug#21224267 DEEPLY NESTED JSON OBJECTS MAY CAUSE CRASH |
| 3039 |
--echo # |
| 3040 |
CREATE TABLE t (j JSON); |
| 3041 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 3042 |
INSERT INTO t VALUES (CONCAT(REPEAT('{"v":', 5000), '1', REPEAT('}', 5000))); |
| 3043 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 3044 |
INSERT INTO t VALUES (CONCAT(REPEAT('{"v":[', 5000), '1', REPEAT(']}', 5000))); |
| 3045 |
--error ER_JSON_DOCUMENT_TOO_DEEP |
| 3046 |
INSERT INTO t VALUES (CONCAT(REPEAT('[{"v":', 5000), '1', REPEAT('}]', 5000))); |
| 3047 |
DROP TABLE t; |
| 3048 |
|
| 3049 |
--echo # |
| 3050 |
--echo # JSON should work with INSERT .. ON DUPLICATE KEY UPDATE |
| 3051 |
--echo # |
| 3052 |
CREATE TABLE t(id INT PRIMARY KEY, j JSON); |
| 3053 |
INSERT INTO t VALUES (1, '[1]') |
| 3054 |
ON DUPLICATE KEY UPDATE j = JSON_OBJECT("a", VALUES(j)); |
| 3055 |
SELECT * FROM t; |
| 3056 |
INSERT INTO t VALUES (1, '[1,2]') |
| 3057 |
ON DUPLICATE KEY UPDATE j = JSON_OBJECT("ab", VALUES(j)); |
| 3058 |
SELECT * FROM t; |
| 3059 |
INSERT INTO t VALUES (1, '[1,2,3]') |
| 3060 |
ON DUPLICATE KEY UPDATE j = JSON_OBJECT("abc", VALUES(j)); |
| 3061 |
SELECT * FROM t; |
| 3062 |
DROP TABLE t; |
| 3063 |
|
| 3064 |
--echo # |
| 3065 |
--echo # Bug#21278178 JSON_QUOTE(LITERAL) GETS APPLIED DOUBLE DURING SELECT |
| 3066 |
--echo # FROM TABLE WITH MTR |
| 3067 |
--echo # |
| 3068 |
CREATE TABLE t(x INT); |
| 3069 |
INSERT INTO t VALUES (1), (2), (3); |
| 3070 |
SET NAMES latin1; |
| 3071 |
SELECT JSON_QUOTE('abc') FROM t; |
| 3072 |
SET NAMES utf8mb4; |
| 3073 |
SELECT JSON_QUOTE('abc') FROM t; |
| 3074 |
SET NAMES default; |
| 3075 |
DROP TABLE t; |
| 3076 |
|
| 3077 |
--echo # |
| 3078 |
--echo # Bug#21291993 ASSERT `0' FAILED AT THD::SEND_STATEMENT_STATUS() |
| 3079 |
--echo # ON JSON_SEARCH(NULL, ...)+JOIN |
| 3080 |
--echo # |
| 3081 |
CREATE TABLE t (pk INT NOT NULL PRIMARY KEY, col_varchar VARCHAR(1)); |
| 3082 |
SELECT COUNT(*), JSON_SEARCH(NULL, 'one', '5%') |
| 3083 |
FROM t t1, t t2 WHERE t1.pk = t2.pk; |
| 3084 |
DROP TABLE t; |
| 3085 |
|
| 3086 |
--echo # |
| 3087 |
--echo # Bug#21376088 JSON: CRASH IN VAL_JSON_FUNC_FIELD_SUBSELECT |
| 3088 |
--echo # |
| 3089 |
create table t(a json); |
| 3090 |
insert into t values('{}'); |
| 3091 |
select a from t where (select a from t where 1) in (select 1 from t); # repro |
| 3092 |
select a from t where (select a from t where 1) in (select cast('{}' as json) from t); |
| 3093 |
drop table t; |
| 3094 |
|
| 3095 |
--echo # |
| 3096 |
--echo # Bug#21437989: ASSERTION FAILED: |
| 3097 |
--echo # JSON_BINARY::PARSE_BINARY(PTR, LENGTH).IS_VALID() |
| 3098 |
--echo # |
| 3099 |
CREATE TABLE t(j JSON NOT NULL); |
| 3100 |
--error ER_NO_DEFAULT_FOR_FIELD |
| 3101 |
INSERT INTO t VALUES (); |
| 3102 |
--error ER_BAD_NULL_ERROR |
| 3103 |
INSERT INTO t VALUES (NULL); |
| 3104 |
INSERT IGNORE INTO t VALUES (); |
| 3105 |
INSERT IGNORE INTO t VALUES (NULL); |
| 3106 |
SELECT * FROM t; |
| 3107 |
# The next two statements used to trigger an assertion. |
| 3108 |
INSERT INTO t SELECT j FROM t; |
| 3109 |
REPLACE INTO t SELECT j FROM t; |
| 3110 |
SELECT * FROM t; |
| 3111 |
DROP TABLE t; |
| 3112 |
|
| 3113 |
--echo # |
| 3114 |
--echo # Bug#21448719: WRONG RESULT FOR JSON VALUE IN OUTER JOIN WITH VIEW |
| 3115 |
--echo # |
| 3116 |
CREATE TABLE t1(j1 JSON); |
| 3117 |
CREATE TABLE t2(j2 JSON); |
| 3118 |
CREATE VIEW v AS SELECT CAST('1' AS JSON) AS jv, j2 FROM t2; |
| 3119 |
INSERT INTO t1 VALUES ('1'); |
| 3120 |
SELECT j1, jv, j2, JSON_ARRAY(j1, jv, j2) FROM t1 LEFT JOIN v ON j1 = jv; |
| 3121 |
INSERT INTO t2 VALUES ('1'); |
| 3122 |
SELECT j1, jv, j2, JSON_ARRAY(j1, jv, j2) FROM t1 LEFT JOIN v ON j1 = jv; |
| 3123 |
DROP TABLE t1, t2; |
| 3124 |
DROP VIEW v; |
| 3125 |
|
| 3126 |
|
| 3127 |
--echo # |
| 3128 |
--echo # Bug#21472872 WRONG RESULTS CAUSED BY PATH LEG POPPING |
| 3129 |
--echo # IN JSON FUNCTIONS |
| 3130 |
--echo # |
| 3131 |
|
| 3132 |
create table tdoc( id int, doc json ); |
| 3133 |
insert into tdoc values |
| 3134 |
( 1, '[]' ), |
| 3135 |
( 2, '{ "a": { "b": true } }' ); |
| 3136 |
|
| 3137 |
select id, json_insert( doc, '$.a.c', false ) from tdoc where id = 2; |
| 3138 |
select id, json_insert( doc, '$.a.c', false ) from tdoc order by id; |
| 3139 |
|
| 3140 |
drop table tdoc; |
| 3141 |
create table tdoc( id int, doc json, new_value varchar( 10 ) ); |
| 3142 |
insert into tdoc values |
| 3143 |
( 1, '{ "a": { "b": true } }', null ), |
| 3144 |
( 2, '{ "a": { "b": true } }', 'abc' ); |
| 3145 |
|
| 3146 |
select id, json_insert( doc, '$.a.c', new_value ) from tdoc where id = 2; |
| 3147 |
select id, json_insert( doc, '$.a.c', new_value ) from tdoc order by id; |
| 3148 |
|
| 3149 |
drop table tdoc; |
| 3150 |
|
| 3151 |
--echo # |
| 3152 |
--echo # Bug#21487833: DBUG_ABORT() IN JSON_WRAPPER::MAKE_HASH_KEY |
| 3153 |
--echo # WITH ORDERED JSON |
| 3154 |
--echo # |
| 3155 |
--source include/turn_off_strict_mode.inc |
| 3156 |
CREATE TABLE t (a BLOB, b JSON NOT NULL); |
| 3157 |
INSERT INTO t VALUES ('', NULL), ('', NULL); |
| 3158 |
UPDATE t SET a = 1 ORDER BY b; |
| 3159 |
SELECT COUNT(a) FROM t GROUP BY b; |
| 3160 |
SELECT DISTINCT B FROM t; |
| 3161 |
SELECT b FROM t UNION DISTINCT SELECT b FROM t; |
| 3162 |
SELECT * FROM t ORDER BY b; |
| 3163 |
DROP TABLE t; |
| 3164 |
--source include/restore_strict_mode.inc |
| 3165 |
|
| 3166 |
--echo # |
| 3167 |
--echo # Bug#21541481: MEMORY LEAK OF ALLOCATIONS MADE IN |
| 3168 |
--echo # VAL_JSON_FUNC_FIELD_SUBSELECT |
| 3169 |
--echo # |
| 3170 |
--echo # This comparison gave valgrind leakage before the fix |
| 3171 |
create table t(a json not null) engine=innodb; |
| 3172 |
insert into t values('{}'); |
| 3173 |
select row(uuid(), a) < row(a, str_to_date(1,1)) from t; |
| 3174 |
drop table t; |
| 3175 |
--echo # Bug#21547877: UPDATE/INSERT JSON COLUMN CRASHES IF EXPRESSION |
| 3176 |
--echo # REFERS TO SELF |
| 3177 |
--echo # |
| 3178 |
SET NAMES latin1; |
| 3179 |
CREATE TABLE t (j JSON); |
| 3180 |
INSERT INTO t VALUES ('{}'); |
| 3181 |
--error ER_INVALID_JSON_TEXT |
| 3182 |
UPDATE t SET j='1', j='1111-11-11', j=('1' NOT BETWEEN j AND '1'); |
| 3183 |
SELECT * FROM t; |
| 3184 |
DROP TABLE t; |
| 3185 |
SET NAMES DEFAULT; |
| 3186 |
|
| 3187 |
--echo # |
| 3188 |
--echo # Bug#21602361: ASSERTION FAILED: (VECTOR_IDX == -1) || (VECTOR_IDX >= 0) |
| 3189 |
--echo # |
| 3190 |
create table t(b int, key(b)); |
| 3191 |
insert into t values(1),(2); |
| 3192 |
|
| 3193 |
# more than one row |
| 3194 |
--error ER_TOO_MANY_ROWS |
| 3195 |
select json_length('{}',@uninitialized_21602361) |
| 3196 |
from t group by b into @uninitialized_21602361; |
| 3197 |
|
| 3198 |
# The user variable is assumed to be constant for SELECT ... INTO, even |
| 3199 |
# though INTO clause refers the same variable. |
| 3200 |
set @initialized_21602361 = '$'; |
| 3201 |
--error ER_TOO_MANY_ROWS |
| 3202 |
select json_length('{}',@initialized_21602361) |
| 3203 |
from t group by b into @initialized_21602361; |
| 3204 |
|
| 3205 |
# invalid path |
| 3206 |
set @error_value_21602361 = '$['; |
| 3207 |
--error ER_INVALID_JSON_PATH |
| 3208 |
select json_length('{}',@error_value_21602361) |
| 3209 |
from t group by b into @error_value_21602361; |
| 3210 |
|
| 3211 |
# more than one row |
| 3212 |
set @valid_path_21602361 = '$'; |
| 3213 |
--error ER_TOO_MANY_ROWS |
| 3214 |
select concat( '$[', json_length('{}',@valid_path_21602361), ']' ) |
| 3215 |
from t group by b into @valid_path_21602361; |
| 3216 |
|
| 3217 |
# used to trip the assertion |
| 3218 |
set @null_value_21602361 = null; |
| 3219 |
--error ER_TOO_MANY_ROWS |
| 3220 |
select json_length('{}',@null_value_21602361) |
| 3221 |
from t group by b into @null_value_21602361; |
| 3222 |
|
| 3223 |
drop table t; |
| 3224 |
|
| 3225 |
--echo # |
| 3226 |
--echo # Bug#21649073: JSON_TYPE RETURNS OPAQUE FOR SOME BINARY VALUES |
| 3227 |
--echo # |
| 3228 |
SELECT JSON_TYPE(CAST(CAST('abcd' AS BINARY) AS JSON)); |
| 3229 |
CREATE TABLE t (bn BINARY(5), vb VARBINARY(5), |
| 3230 |
tb TINYBLOB, mb MEDIUMBLOB, bb BLOB, lb LONGBLOB); |
| 3231 |
INSERT INTO t (bn) VALUES (x'cafe'); |
| 3232 |
UPDATE t SET vb = bn, tb = bn, mb = bn, bb = bn, lb = bn; |
| 3233 |
SELECT JSON_TYPE(CAST(bn AS JSON)) AS bn, JSON_TYPE(CAST(vb AS JSON)) AS vb, |
| 3234 |
JSON_TYPE(CAST(tb AS JSON)) AS tb, JSON_TYPE(CAST(mb AS JSON)) AS mb, |
| 3235 |
JSON_TYPE(CAST(bb AS JSON)) AS bb, JSON_TYPE(CAST(lb AS JSON)) AS lb |
| 3236 |
FROM t; |
| 3237 |
DROP TABLE t; |
| 3238 |
|
| 3239 |
--echo # |
| 3240 |
--echo # Basic tests for inlined JSON path |
| 3241 |
--echo # |
| 3242 |
CREATE TABLE t1(autopk int primary key auto_increment, f1 JSON); |
| 3243 |
INSERT INTO t1(f1) VALUES |
| 3244 |
('{"a":1}'), |
| 3245 |
('{"a":3}'), |
| 3246 |
('{"a":2}'), |
| 3247 |
('{"a":11, "b":3}'), |
| 3248 |
('{"a":33, "b":1}'), |
| 3249 |
('{"a":22,"b":2}'); |
| 3250 |
ANALYZE TABLE t1; |
| 3251 |
|
| 3252 |
SELECT f1->"$.a" FROM t1 order by autopk; |
| 3253 |
EXPLAIN SELECT f1->"$.a" FROM t1 order by autopk; |
| 3254 |
|
| 3255 |
SELECT f1->"$.a" FROM t1 WHERE f1->"$.b" > 1 order by autopk; |
| 3256 |
EXPLAIN SELECT f1->"$.a" FROM t1 WHERE f1->"$.b" > 1 order by autopk; |
| 3257 |
|
| 3258 |
SELECT f1->"$.a", f1->"$.b" FROM t1 ORDER BY autopk; |
| 3259 |
EXPLAIN SELECT f1->"$.a", f1->"$.b" FROM t1 ORDER BY autopk; |
| 3260 |
|
| 3261 |
--sorted_result |
| 3262 |
SELECT MAX(f1->"$.a"), f1->"$.b" FROM t1 GROUP BY f1->"$.b"; |
| 3263 |
EXPLAIN SELECT MAX(f1->"$.a"), f1->"$.b" FROM t1 GROUP BY f1->"$.b"; |
| 3264 |
|
| 3265 |
SELECT JSON_OBJECT("c",f1->"$.b") AS f2 |
| 3266 |
FROM t1 HAVING JSON_TYPE(f2->"$.c") <> 'NULL' ORDER BY autopk; |
| 3267 |
EXPLAIN |
| 3268 |
SELECT JSON_OBJECT("c",f1->"$.b") AS f2 FROM t1 HAVING JSON_TYPE(f2->"$.c") <> 'NULL' order by autopk; |
| 3269 |
|
| 3270 |
--echo Test unquoting operator |
| 3271 |
INSERT INTO t1(f1) VALUES |
| 3272 |
('{"t":"a"}'), |
| 3273 |
('{"t":"b"}'), |
| 3274 |
('{"t":"c"}'); |
| 3275 |
--echo Returned values should be quoted |
| 3276 |
SELECT f1->"$.t" FROM t1 WHERE f1->"$.t" <> 'NULL' order by autopk; |
| 3277 |
--echo Returned values should be unquoted |
| 3278 |
SELECT f1->>"$.t" FROM t1 WHERE f1->>"$.t" <> 'NULL' order by autopk; |
| 3279 |
EXPLAIN SELECT f1->>"$.t" FROM t1 WHERE f1->>"$.t" <> 'NULL'; |
| 3280 |
|
| 3281 |
--error ER_PARSE_ERROR |
| 3282 |
SELECT f1->>NULL FROM t1; |
| 3283 |
--error ER_INVALID_JSON_PATH |
| 3284 |
SELECT f1 ->> "NULL" FROM t1; |
| 3285 |
--error ER_INVALID_JSON_PATH |
| 3286 |
SELECT f1->>"NULL" FROM t1; |
| 3287 |
--error ER_INVALID_JSON_PATH |
| 3288 |
SELECT f1->>"!@#" FROM t1; |
| 3289 |
--error ER_PARSE_ERROR |
| 3290 |
SELECT COUNT(*)->>"$.t" FROM t1; |
| 3291 |
|
| 3292 |
INSERT INTO t1(f1) VALUES |
| 3293 |
('[ { "a": 1 }, { "a": 2 } ]'), |
| 3294 |
('{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }'), |
| 3295 |
('{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }'), |
| 3296 |
('{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }'); |
| 3297 |
|
| 3298 |
SELECT |
| 3299 |
f1->>"$**.b", |
| 3300 |
cast(json_unquote(json_extract(f1,"$**.b")) as char), |
| 3301 |
cast(f1->>"$**.b" as char) <=> cast(json_unquote(json_extract(f1,"$**.b")) as char) |
| 3302 |
FROM t1 order by autopk; |
| 3303 |
SELECT |
| 3304 |
f1->>"$.c", |
| 3305 |
cast(json_unquote(json_extract(f1,"$.c")) as char), |
| 3306 |
cast(f1->>"$.c" as char) <=> cast(json_unquote(json_extract(f1,"$.c")) as char) |
| 3307 |
FROM t1 order by autopk; |
| 3308 |
SELECT |
| 3309 |
f1->>'$.b[1].c', |
| 3310 |
cast(json_unquote(json_extract(f1,'$.b[1].c')) as char), |
| 3311 |
cast(f1->>'$.b[1].c' as |
| 3312 |
char)<=>cast(json_unquote(json_extract(f1,'$.b[1].c')) as char) |
| 3313 |
FROM t1 order by autopk; |
| 3314 |
SELECT |
| 3315 |
f1->'$.b[1].c', |
| 3316 |
cast(json_extract(f1,'$.b[1].c') as char), |
| 3317 |
cast(f1->'$.b[1].c' as char) |
| 3318 |
<=>cast(json_extract(f1,'$.b[1].c') as char) |
| 3319 |
FROM t1 order by autopk; |
| 3320 |
SELECT |
| 3321 |
f1->>'$.b[1]', |
| 3322 |
cast(json_unquote(json_extract(f1,'$.b[1]')) as char), |
| 3323 |
cast(f1->>'$.b[1]' as char) <=> |
| 3324 |
cast(json_unquote(json_extract(f1,'$.b[1]')) as char) |
| 3325 |
FROM t1 order by autopk; |
| 3326 |
SELECT |
| 3327 |
f1->>'$[0][0]', |
| 3328 |
cast(json_unquote(json_extract(f1,'$[0][0]')) as char), |
| 3329 |
cast(f1->>'$[0][0]' as char) <=> |
| 3330 |
cast(json_unquote(json_extract(f1,'$[0][0]')) as char) |
| 3331 |
FROM t1 order by autopk; |
| 3332 |
SELECT |
| 3333 |
f1->>'$**[0]', |
| 3334 |
cast(json_unquote(json_extract(f1,'$**[0]')) as char), |
| 3335 |
cast(f1->>'$**[0]' as char) <=> |
| 3336 |
cast(json_unquote(json_extract(f1,'$**[0]')) as char) |
| 3337 |
FROM t1 order by autopk; |
| 3338 |
SELECT |
| 3339 |
f1->> '$.a[0]', |
| 3340 |
cast(json_unquote(json_extract(f1, '$.a[0]')) as char), |
| 3341 |
cast(f1->> '$.a[0]' as char) <=> cast(json_unquote(json_extract(f1, |
| 3342 |
'$.a[0]')) as char) |
| 3343 |
FROM t1 order by autopk; |
| 3344 |
SELECT |
| 3345 |
f1->>'$[0].a[0]', |
| 3346 |
cast(json_unquote(json_extract(f1,'$[0].a[0]')) as char), |
| 3347 |
cast(f1->>'$[0].a[0]' as char) <=> |
| 3348 |
cast(json_unquote(json_extract(f1,'$[0].a[0]')) as char) |
| 3349 |
FROM t1 order by autopk; |
| 3350 |
SELECT |
| 3351 |
f1->>'$**.a', |
| 3352 |
cast(json_unquote(json_extract(f1,'$**.a')) as char), |
| 3353 |
cast(f1->>'$**.a' as char) <=> cast(json_unquote(json_extract(f1,'$**.a')) as char) |
| 3354 |
FROM t1 order by autopk; |
| 3355 |
SELECT |
| 3356 |
f1->>'$[0][0][0].a', |
| 3357 |
cast(json_unquote(json_extract(f1,'$[0][0][0].a')) as char), |
| 3358 |
cast(f1->>'$[0][0][0].a' as char) <=> |
| 3359 |
cast(json_unquote(json_extract(f1,'$[0][0][0].a')) as char) |
| 3360 |
FROM t1 order by autopk; |
| 3361 |
SELECT |
| 3362 |
f1->>'$[*].b', |
| 3363 |
cast(json_unquote(json_extract(f1,'$[*].b')) as char), |
| 3364 |
cast(f1->>'$[*].b' as char) <=> |
| 3365 |
cast(json_unquote(json_extract(f1,'$[*].b')) as char) |
| 3366 |
FROM t1 order by autopk; |
| 3367 |
SELECT |
| 3368 |
f1->>'$[*].a', |
| 3369 |
cast(json_unquote(json_extract(f1,'$[*].a')) as char), |
| 3370 |
cast(f1->>'$[*].a' as char) <=> |
| 3371 |
cast(json_unquote(json_extract(f1,'$[*].a')) as char) |
| 3372 |
FROM t1 order by autopk; |
| 3373 |
|
| 3374 |
DROP TABLE t1; |
| 3375 |
|
| 3376 |
--echo # |
| 3377 |
--echo # Bug#21072360 ASSERTION `(*A)->FIELD_TYPE() == MYSQL_TYPE_JSON || |
| 3378 |
--echo # (*B)->FIELD_TYPE() == FAILED |
| 3379 |
--echo # |
| 3380 |
|
| 3381 |
--echo # |
| 3382 |
--echo # Bug#21144949 WL8132:DELETE QUERY HAVING A SUB-QUERY DOES NOT |
| 3383 |
--echo # DELETE ROWS IN THE TABLE |
| 3384 |
--echo # |
| 3385 |
CREATE TABLE t(j JSON); |
| 3386 |
INSERT INTO t VALUES (JSON_OBJECT('a', 'b')), (JSON_OBJECT('a', 'b')); |
| 3387 |
DELETE FROM t WHERE j IN (SELECT JSON_OBJECT('a', 'b') FROM DUAL WHERE 1); |
| 3388 |
SELECT * FROM t; |
| 3389 |
INSERT INTO t VALUES (JSON_OBJECT('a', 'b')), (JSON_OBJECT('a', 'b')); |
| 3390 |
DELETE FROM t WHERE j IN (SELECT JSON_OBJECT('a', 'b') FROM DUAL); |
| 3391 |
SELECT * FROM t; |
| 3392 |
INSERT INTO t VALUES (JSON_OBJECT('a', 'b')), (JSON_OBJECT('a', 'b')); |
| 3393 |
DELETE FROM t WHERE j IN (SELECT CAST(NULL AS JSON) FROM DUAL); |
| 3394 |
DELETE FROM t WHERE j IN (SELECT CAST(NULL AS JSON) FROM DUAL WHERE 1); |
| 3395 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 3396 |
DELETE FROM t WHERE j IN (SELECT CAST('not json' AS JSON) FROM DUAL); |
| 3397 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 3398 |
DELETE FROM t WHERE j IN (SELECT CAST('not json' AS JSON) FROM DUAL WHERE 1); |
| 3399 |
SELECT * FROM t; |
| 3400 |
DROP TABLE t; |
| 3401 |
|
| 3402 |
--echo # |
| 3403 |
--echo # Bug#22366102: EMPTY STRINGS AS KEYS PROBABLY NOT HANDLED CORRECTLY |
| 3404 |
--echo # BY JSON_EXTRACT() |
| 3405 |
--echo # |
| 3406 |
CREATE TABLE t(j JSON); |
| 3407 |
INSERT INTO t VALUES ('{"":"a"}'), ('{"":{"":"a"}}'), ('{"a":{"":{"b":"a"}}}'); |
| 3408 |
--sorted_result |
| 3409 |
SELECT JSON_UNQUOTE(JSON_SEARCH(j, 'one', 'a')) AS path FROM t; |
| 3410 |
--sorted_result |
| 3411 |
SELECT j->'$.""' AS c1, j->'$."".""' AS c2, j->'$.a."".b' AS c3 FROM t; |
| 3412 |
DROP TABLE t; |
| 3413 |
--error ER_INVALID_JSON_PATH |
| 3414 |
SELECT JSON_EXTRACT('[1]', '$.'); |
| 3415 |
--error ER_INVALID_JSON_PATH |
| 3416 |
SELECT JSON_EXTRACT('[1]', '$..'); |
| 3417 |
--error ER_INVALID_JSON_PATH |
| 3418 |
SELECT JSON_EXTRACT('[1]', '$.a..b'); |
| 3419 |
|
| 3420 |
--echo # |
| 3421 |
--echo # Bug#22477046: HANDLE_FATAL_SIGNAL (SIG=6) IN ITEM_FUNC_SP::VAL_JSON |
| 3422 |
--echo # |
| 3423 |
CREATE TABLE t1(j JSON); |
| 3424 |
CREATE TABLE t2(vc VARCHAR(10)); |
| 3425 |
CREATE FUNCTION f1(i INT) RETURNS INT RETURN i; |
| 3426 |
--error ER_INVALID_JSON_TEXT |
| 3427 |
INSERT INTO t1 SELECT f1(3); |
| 3428 |
INSERT INTO t2 SELECT f1(3); |
| 3429 |
CREATE FUNCTION f2(vc VARCHAR(10)) RETURNS VARCHAR(10) RETURN vc; |
| 3430 |
INSERT INTO t1 SELECT f2('[1,2,3]'); |
| 3431 |
INSERT INTO t2 SELECT f2('[1,2,3]'); |
| 3432 |
CREATE FUNCTION f3(j JSON) RETURNS JSON RETURN j; |
| 3433 |
INSERT INTO t1 SELECT f3('[4,5,6]'); |
| 3434 |
INSERT INTO t2 SELECT f3('[4,5,6]'); |
| 3435 |
--sorted_result |
| 3436 |
SELECT * FROM t1; |
| 3437 |
--sorted_result |
| 3438 |
SELECT * FROM t2; |
| 3439 |
DROP TABLE t1, t2; |
| 3440 |
DROP FUNCTION f1; |
| 3441 |
DROP FUNCTION f2; |
| 3442 |
DROP FUNCTION f3; |
| 3443 |
|
| 3444 |
--echo # Bug#22522073: Assertion failed: !thd->is_error() in optimize_cond() |
| 3445 |
|
| 3446 |
CREATE TABLE s(a INTEGER, b CHAR(1)); |
| 3447 |
INSERT INTO s VALUES (1,0), (1,0), (1,0); |
| 3448 |
|
| 3449 |
CREATE TABLE t(c JSON); |
| 3450 |
INSERT INTO t VALUES (), (), (); |
| 3451 |
|
| 3452 |
--error ER_SUBQUERY_NO_1_ROW |
| 3453 |
SELECT 1 |
| 3454 |
FROM s WHERE a NOT IN (SELECT 1 |
| 3455 |
FROM s WHERE (SELECT c FROM t) = b); |
| 3456 |
|
| 3457 |
DROP TABLE s, t; |
| 3458 |
|
| 3459 |
--echo # |
| 3460 |
--echo # Bug#22546013: REMOVE DEPENDENCY ON BOOST IN ITEM_JSON_FUNC.CC |
| 3461 |
--echo # |
| 3462 |
# This test case does not show a bug, it just exercises a code path that was |
| 3463 |
# changed by the fix. The point of the test case is to verify that memory is |
| 3464 |
# freed when a Json_scalar_holder is reused over multiple rows. The test case |
| 3465 |
# must use a scalar type that actually allocates some memory on the heap. |
| 3466 |
# Json_string does that, in its std::string member. Additionally, the string |
| 3467 |
# must be relatively long, so that std::string cannot avoid heap allocation with |
| 3468 |
# Small String Optimization. And the test must run under Valgrind or a similar |
| 3469 |
# tool to detect memory leaks. |
| 3470 |
CREATE TABLE t(j JSON, vc VARCHAR(99)); |
| 3471 |
INSERT INTO t VALUES ('"abc"', REPEAT('abc', 33)), ('"abc"', 'abc'); |
| 3472 |
INSERT INTO t SELECT * FROM t; |
| 3473 |
SELECT * FROM t WHERE j = vc; |
| 3474 |
DROP TABLE t; |
| 3475 |
|
| 3476 |
--echo # |
| 3477 |
--echo # Bug#22816576: ASSERTION FAILED: !THD->IS_ERROR() IN OPTIMIZE_COND() |
| 3478 |
--echo # |
| 3479 |
CREATE TABLE t(a DATE); |
| 3480 |
INSERT INTO t VALUES ('1970-01-01'); |
| 3481 |
|
| 3482 |
--error ER_INVALID_JSON_PATH |
| 3483 |
SELECT 1 FROM t WHERE 1 <> |
| 3484 |
( |
| 3485 |
SELECT 1 FROM |
| 3486 |
( |
| 3487 |
SELECT 1 FROM t e |
| 3488 |
JOIN t ON t.a <=> JSON_REPLACE(JSON_OBJECT('', ''), '', '') |
| 3489 |
WHERE e.a |
| 3490 |
) w |
| 3491 |
); |
| 3492 |
DROP TABLE t; |
| 3493 |
|
| 3494 |
--echo # |
| 3495 |
--echo # Bug#23209914: ASSERTION FAILED: THIS_TYPE != ENUM_JSON_TYPE::J_ERROR |
| 3496 |
--echo # |
| 3497 |
CREATE PROCEDURE p(a JSON) SELECT 1 WHERE a = 1; |
| 3498 |
CALL p(NULL); |
| 3499 |
DROP PROCEDURE p; |
| 3500 |
|
| 3501 |
# |
| 3502 |
# Bug#21573154: ASSERTION `(KP1 + KP1_LEN) < (S1 + M_SIZE) |
| 3503 |
# |
| 3504 |
CREATE TABLE t1 (pk INT AUTO_INCREMENT KEY, f1 INT, fj JSON); |
| 3505 |
INSERT INTO t1 VALUES(1, NULL, JSON_OBJECT('aaa','bbb')); |
| 3506 |
INSERT INTO t1 VALUES(2, NULL, JSON_OBJECT('bbb','aaa')); |
| 3507 |
# Disable warnings due to differences between the old optimizer and the |
| 3508 |
# hypergraph optimizer. The hypergraph optimizer removes "fj" from the sort key, |
| 3509 |
# and does not raise a warning about "sorting of non-scalar JSON values". |
| 3510 |
--disable_warnings |
| 3511 |
SELECT * FROM t1 ORDER BY f1, pk, fj; |
| 3512 |
--enable_warnings |
| 3513 |
|
| 3514 |
DROP TABLE t1; |
| 3515 |
|
| 3516 |
--echo # |
| 3517 |
--echo # Bug#25461627: VALGRIND WARNING WHEN UPDATING A JSON COLUMN |
| 3518 |
--echo # |
| 3519 |
CREATE TABLE t (j JSON); |
| 3520 |
INSERT INTO t VALUES (NULL); |
| 3521 |
UPDATE t SET j = JSON_ARRAY(REPEAT('a', 32)), |
| 3522 |
j = JSON_EXTRACT(j, '$[0]'); |
| 3523 |
SELECT * FROM t; |
| 3524 |
UPDATE t SET j = JSON_ARRAY(CAST(REPEAT('b', 32) AS BINARY)), |
| 3525 |
j = JSON_EXTRACT(j, '$[0]'); |
| 3526 |
SELECT * FROM t; |
| 3527 |
UPDATE t SET j = JSON_ARRAY(JSON_ARRAY(REPEAT('c', 32))), |
| 3528 |
j = JSON_EXTRACT(j, '$[0]'); |
| 3529 |
SELECT * FROM t; |
| 3530 |
UPDATE t SET j = JSON_ARRAY(JSON_OBJECT(REPEAT('d', 16), REPEAT('e', 16))), |
| 3531 |
j = JSON_EXTRACT(j, '$[0]'); |
| 3532 |
SELECT * FROM t; |
| 3533 |
DROP TABLE t; |
| 3534 |
|
| 3535 |
--echo # |
| 3536 |
--echo # WL#9191: JSON_PRETTY function |
| 3537 |
--echo # |
| 3538 |
CREATE TABLE t(id INT PRIMARY KEY AUTO_INCREMENT, |
| 3539 |
uc VARCHAR(128) CHARACTER SET utf8mb4, |
| 3540 |
lc VARCHAR(128) CHARACTER SET latin1, |
| 3541 |
j JSON); |
| 3542 |
INSERT INTO t(uc) VALUES |
| 3543 |
(NULL), |
| 3544 |
('{}'), |
| 3545 |
('[]'), |
| 3546 |
('[1,2,3]'), |
| 3547 |
('{"a":1,"b":2}'), |
| 3548 |
('{"a":[1,{}],"b":[]}'), |
| 3549 |
('{"a":{"b":{"c":{"d":{"e":{"f":null}}}}}}'), |
| 3550 |
('true'), |
| 3551 |
('false'), |
| 3552 |
('null'), |
| 3553 |
('1'), |
| 3554 |
('1.1'), |
| 3555 |
('"hello"'), |
| 3556 |
(JSON_ARRAY('abc |
| 3557 |
def', '"abc"')); |
| 3558 |
UPDATE t SET lc = uc, j = uc; |
| 3559 |
SELECT JSON_PRETTY(uc) AS c1 FROM t ORDER BY id; |
| 3560 |
SELECT JSON_PRETTY(lc) AS c1 FROM t ORDER BY id; |
| 3561 |
SELECT JSON_PRETTY(j) AS c1 FROM t ORDER BY id; |
| 3562 |
SELECT DISTINCT CHARSET(JSON_PRETTY(uc)) AS c1, |
| 3563 |
CHARSET(JSON_PRETTY(lc)) AS c2, |
| 3564 |
CHARSET(JSON_PRETTY(j)) AS c3 FROM t; |
| 3565 |
DROP TABLE t; |
| 3566 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 3567 |
SELECT JSON_PRETTY('this is not JSON') AS j; |
| 3568 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3569 |
SELECT JSON_PRETTY(); |
| 3570 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3571 |
SELECT JSON_PRETTY('{}', 2); |
| 3572 |
|
| 3573 |
--echo # |
| 3574 |
--echo # WL#9192: Add JSON_STORAGE_SIZE / JSON_STORAGE_FREE functions |
| 3575 |
--echo # |
| 3576 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3577 |
SELECT JSON_STORAGE_SIZE(); |
| 3578 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3579 |
SELECT JSON_STORAGE_FREE(); |
| 3580 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3581 |
SELECT JSON_STORAGE_SIZE('{}', '[]'); |
| 3582 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 3583 |
SELECT JSON_STORAGE_FREE('{}', '[]'); |
| 3584 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 3585 |
SELECT JSON_STORAGE_SIZE('this is not JSON'); |
| 3586 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 3587 |
SELECT JSON_STORAGE_FREE('this is not JSON'); |
| 3588 |
SELECT JSON_STORAGE_SIZE(NULL); |
| 3589 |
SELECT JSON_STORAGE_FREE(NULL); |
| 3590 |
SELECT JSON_STORAGE_SIZE(JSON_ARRAY(1,2,3)); |
| 3591 |
SELECT JSON_STORAGE_FREE(JSON_ARRAY(1,2,3)); |
| 3592 |
CREATE TABLE t(id INT PRIMARY KEY, j JSON, v VARCHAR(100)); |
| 3593 |
INSERT INTO t(id, j) VALUES (0, NULL), (1, '[["abc", "def", "ghi"], "jkl"]'); |
| 3594 |
UPDATE t SET v = j; |
| 3595 |
let $dump_table= |
| 3596 |
SELECT *, |
| 3597 |
JSON_STORAGE_SIZE(j), JSON_STORAGE_SIZE(v), |
| 3598 |
JSON_STORAGE_SIZE(j->'\$[0]'), JSON_STORAGE_SIZE(v->'\$[0]'), |
| 3599 |
JSON_STORAGE_FREE(j), JSON_STORAGE_FREE(v), |
| 3600 |
JSON_STORAGE_FREE(j->'\$[0]'), JSON_STORAGE_FREE(v->'\$[0]') |
| 3601 |
FROM t ORDER BY id; |
| 3602 |
--eval $dump_table |
| 3603 |
UPDATE t SET j = JSON_SET(j, '$[0][1]', NULL, '$[1]', 'X'), |
| 3604 |
v = JSON_SET(v, '$[0][1]', NULL, '$[1]', 'X'); |
| 3605 |
--eval $dump_table |
| 3606 |
DROP TABLE t; |
| 3607 |
|
| 3608 |
# |
| 3609 |
# Get some extra coverage for partial update of JSON, WL#8963. |
| 3610 |
# |
| 3611 |
|
| 3612 |
CREATE TABLE t(id INT PRIMARY KEY, j JSON); |
| 3613 |
INSERT INTO t VALUES |
| 3614 |
(1, '{"a":"a"}'), (2, '{"a":"b", "b":"b"}'), (3, '{"a":"c", "c":"c"}'); |
| 3615 |
let $dump_table= SELECT *, JSON_STORAGE_SIZE(j) ss, |
| 3616 |
JSON_STORAGE_FREE(j) sf FROM t ORDER BY id; |
| 3617 |
--eval $dump_table |
| 3618 |
UPDATE t SET j = JSON_SET(j, '$.a', 'x'); |
| 3619 |
--eval $dump_table |
| 3620 |
UPDATE t SET j = JSON_SET(j, '$.a', 'y', '$.b', j->'$.a'); |
| 3621 |
--eval $dump_table |
| 3622 |
UPDATE t SET j = JSON_SET(j, '$.a', 'z'), j = JSON_SET(j, '$.b', j->'$.a'); |
| 3623 |
--eval $dump_table |
| 3624 |
UPDATE t SET j = JSON_SET(j, '$.a', 'w'), |
| 3625 |
j = JSON_SET(j, '$.a', 'v', '$.b', j->'$.a'); |
| 3626 |
--eval $dump_table |
| 3627 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', '1', '$.c', '2', '$.a', '3'); |
| 3628 |
--eval $dump_table |
| 3629 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', '4'), j = JSON_REPLACE(j, '$.c', '5'), |
| 3630 |
j = JSON_REPLACE(j, '$.a', '6'); |
| 3631 |
--eval $dump_table |
| 3632 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', ''); |
| 3633 |
--eval $dump_table |
| 3634 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', 'a'); |
| 3635 |
--eval $dump_table |
| 3636 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', 'ab'); |
| 3637 |
--eval $dump_table |
| 3638 |
UPDATE t SET j = JSON_REPLACE(JSON_REPLACE(j, '$.a', 'A'), '$.b', true); |
| 3639 |
--eval $dump_table |
| 3640 |
UPDATE t SET j = JSON_REPLACE(JSON_REPLACE(j, '$.a', 'X'), '$.unknown', 'Y'); |
| 3641 |
--eval $dump_table |
| 3642 |
UPDATE t SET j = JSON_REPLACE(JSON_REPLACE(j, '$.unknown', 'Z'), '$.a', 'W'); |
| 3643 |
--eval $dump_table |
| 3644 |
UPDATE t SET j = JSON_REPLACE(JSON_REPLACE(j, '$.unknown1', 1), |
| 3645 |
'$.unknown2', 2); |
| 3646 |
--eval $dump_table |
| 3647 |
UPDATE t SET j = JSON_SET(JSON_REPLACE(j, '$.a', 99), '$.b', 88); |
| 3648 |
--eval $dump_table |
| 3649 |
UPDATE t SET j = JSON_REPLACE(JSON_SET(j, '$.a', 77), '$.b', 66); |
| 3650 |
--eval $dump_table |
| 3651 |
UPDATE t SET j = JSON_REPLACE(JSON_SET(j, '$.c', 0), '$.a', 1); |
| 3652 |
--eval $dump_table |
| 3653 |
UPDATE t SET j = JSON_SET(JSON_REPLACE(JSON_SET(j, '$.a', 11), |
| 3654 |
'$.unknown', 12), |
| 3655 |
'$.b', 13); |
| 3656 |
--eval $dump_table |
| 3657 |
DROP TABLE t; |
| 3658 |
|
| 3659 |
CREATE TABLE t(id INT PRIMARY KEY, j1 JSON, j2 JSON); |
| 3660 |
INSERT INTO t VALUES |
| 3661 |
(1, '{"a":"a","b":"b"}', '{"a":"a","b":"b"}'), |
| 3662 |
(2, NULL, NULL), |
| 3663 |
(3, '{"a":"aa","b":"bb"}', '{"a":"aa","b":"bb"}'); |
| 3664 |
ANALYZE TABLE t; |
| 3665 |
let $dump_table= SELECT *, JSON_STORAGE_SIZE(j1) ss1, |
| 3666 |
JSON_STORAGE_FREE(j1) sf1, |
| 3667 |
JSON_STORAGE_SIZE(j2) ss2, |
| 3668 |
JSON_STORAGE_FREE(j2) sf2 FROM t ORDER BY id; |
| 3669 |
--eval $dump_table |
| 3670 |
let $query= UPDATE t SET j1 = JSON_SET(j1, '\$.a', 'x'), |
| 3671 |
j2 = JSON_SET(j2, '\$.a', 'y'), |
| 3672 |
j1 = JSON_SET(j1, '\$.b', 'z'); |
| 3673 |
--skip_if_hypergraph # Depends on the query plan. |
| 3674 |
--eval EXPLAIN FORMAT=JSON $query |
| 3675 |
SET optimizer_trace="enabled=on"; |
| 3676 |
--eval $query |
| 3677 |
SET optimizer_trace="enabled=off"; |
| 3678 |
# View trace, but extract only the parts we are interested in testing. The other |
| 3679 |
# parts of the trace may vary depending on ps protocol mode and which optimizer |
| 3680 |
# is used. |
| 3681 |
let $show_trace= |
| 3682 |
SELECT JSON_PRETTY(JSON_OBJECT( |
| 3683 |
'json_partial_update', trace->'\$**.json_partial_update', |
| 3684 |
'rejected_columns', trace->'\$**.rejected_columns')) |
| 3685 |
AS json_trace |
| 3686 |
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; |
| 3687 |
eval $show_trace; |
| 3688 |
--eval $dump_table |
| 3689 |
let $query= UPDATE t SET j1 = JSON_SET(j1, '\$.a', 's'), |
| 3690 |
j2 = JSON_SET(j2, '\$.a', 'abcdefghi'), |
| 3691 |
j1 = JSON_SET(j1, '\$.b', 't'); |
| 3692 |
--skip_if_hypergraph # Depends on the query plan. |
| 3693 |
--eval EXPLAIN FORMAT=JSON $query |
| 3694 |
SET optimizer_trace="enabled=on"; |
| 3695 |
--eval $query |
| 3696 |
SET optimizer_trace="enabled=off"; |
| 3697 |
eval $show_trace; |
| 3698 |
--eval $dump_table |
| 3699 |
--let $query= UPDATE t SET j1 = JSON_SET(j1, '\$.x.y.z', 'abc', '\$.b', 'X') |
| 3700 |
--skip_if_hypergraph # Depends on the query plan. |
| 3701 |
--eval EXPLAIN FORMAT=JSON $query |
| 3702 |
SET optimizer_trace="enabled=on"; |
| 3703 |
--eval $query |
| 3704 |
SET optimizer_trace="enabled=off"; |
| 3705 |
eval $show_trace; |
| 3706 |
--eval $dump_table |
| 3707 |
--let $query= UPDATE t SET j1 = JSON_REPLACE(j1, '\$.x', 'abc', '\$.b', 'Y') |
| 3708 |
--skip_if_hypergraph # Depends on the query plan. |
| 3709 |
--eval EXPLAIN FORMAT=JSON $query |
| 3710 |
SET optimizer_trace="enabled=on"; |
| 3711 |
--eval $query |
| 3712 |
SET optimizer_trace="enabled=off"; |
| 3713 |
eval $show_trace; |
| 3714 |
--eval $dump_table |
| 3715 |
--let $query= UPDATE t SET j1 = JSON_SET(j1, '\$.x', 'abc', '\$.b', 'Z') |
| 3716 |
--skip_if_hypergraph # Depends on the query plan. |
| 3717 |
--eval EXPLAIN FORMAT=JSON $query |
| 3718 |
SET optimizer_trace="enabled=on"; |
| 3719 |
--eval $query |
| 3720 |
SET optimizer_trace="enabled=off"; |
| 3721 |
eval $show_trace; |
| 3722 |
--eval $dump_table |
| 3723 |
# No partial update here since input column is different from target column. |
| 3724 |
--let $query= UPDATE t SET j1 = JSON_SET(j2, '\$.a', 'ABC') |
| 3725 |
--skip_if_hypergraph # Depends on the query plan. |
| 3726 |
--eval EXPLAIN FORMAT=JSON $query |
| 3727 |
SET optimizer_trace="enabled=on"; |
| 3728 |
--eval $query |
| 3729 |
SET optimizer_trace="enabled=off"; |
| 3730 |
eval $show_trace; |
| 3731 |
--eval $dump_table |
| 3732 |
# No partial update here since JSON_ARRAY doesn't support partial update. |
| 3733 |
--let $query= UPDATE t SET j1 = JSON_ARRAY(j1, j1) |
| 3734 |
--skip_if_hypergraph # Depends on the query plan. |
| 3735 |
--eval EXPLAIN FORMAT=JSON $query |
| 3736 |
SET optimizer_trace="enabled=on"; |
| 3737 |
--eval $query |
| 3738 |
SET optimizer_trace="enabled=off"; |
| 3739 |
eval $show_trace; |
| 3740 |
--eval $dump_table |
| 3741 |
# No partial update of j1, but j2 should be partially updated. |
| 3742 |
let $query= UPDATE t SET j1 = JSON_SET(j2, '\$.a', 1), |
| 3743 |
j2 = JSON_SET(j2, '\$.a', 1); |
| 3744 |
--skip_if_hypergraph # Depends on the query plan. |
| 3745 |
--eval EXPLAIN FORMAT=JSON $query |
| 3746 |
SET optimizer_trace="enabled=on"; |
| 3747 |
--eval $query |
| 3748 |
SET optimizer_trace="enabled=off"; |
| 3749 |
eval $show_trace; |
| 3750 |
--eval $dump_table |
| 3751 |
DROP TABLE t; |
| 3752 |
|
| 3753 |
CREATE TABLE t(j JSON, |
| 3754 |
i INT DEFAULT 42, |
| 3755 |
db DOUBLE DEFAULT 2.34e-10, |
| 3756 |
dc DECIMAL(5, 3) DEFAULT 98.765, |
| 3757 |
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 3758 |
gc JSON GENERATED ALWAYS AS (JSON_ARRAY(i, db, dc, ts))); |
| 3759 |
INSERT INTO t(j, ts) VALUES |
| 3760 |
('[null, "abc", true, "def", false]', '2017-01-02 14:15:16'); |
| 3761 |
let $dump_table= SELECT j, JSON_TYPE(j) jt, JSON_STORAGE_SIZE(j) ss, |
| 3762 |
JSON_STORAGE_FREE(j) sf FROM t; |
| 3763 |
--eval $dump_table |
| 3764 |
# The update of $[3] to a longer value should be able to reuse the space freed |
| 3765 |
# by the update of $[1] to a shorter value. |
| 3766 |
UPDATE t SET j = JSON_SET(j, '$[1]', 'xy', '$[3]', 'xyzw'); |
| 3767 |
--eval $dump_table |
| 3768 |
# Again, increasing the size of $[3] is OK as long as $[1] shrinks first, also |
| 3769 |
# when done in two separate operations. |
| 3770 |
UPDATE t SET j = JSON_SET(j, '$[1]', 'a'); |
| 3771 |
--eval $dump_table |
| 3772 |
UPDATE t SET j = JSON_SET(j, '$[3]', 'abcde'); |
| 3773 |
--eval $dump_table |
| 3774 |
|
| 3775 |
# See what happens if one partial update overwrites a nested array that had a |
| 3776 |
# partial update performed to one of its elements previously in the same |
| 3777 |
# statement. |
| 3778 |
UPDATE t SET j = '[[[[[1]]]]]'; |
| 3779 |
--eval $dump_table |
| 3780 |
UPDATE t SET j = JSON_SET(JSON_SET(j, '$[0][0][0]', 'ab'), '$[0][0]', 'cdefgh'); |
| 3781 |
--eval $dump_table |
| 3782 |
|
| 3783 |
# Replacing the root of a document will result in a full update. |
| 3784 |
UPDATE t SET j = JSON_REPLACE(j, '$', JSON_ARRAY(123)); |
| 3785 |
--eval $dump_table |
| 3786 |
UPDATE t SET j = JSON_SET(j, '$', 456); |
| 3787 |
--eval $dump_table |
| 3788 |
|
| 3789 |
# Replacing with non-existent paths (array access on object, or member access on |
| 3790 |
# array, or member/array access on scalar) cannot be performed as partial |
| 3791 |
# update. |
| 3792 |
UPDATE t SET j = JSON_ARRAY(1,2,3); |
| 3793 |
UPDATE t SET j = JSON_REPLACE(j, '$.a', 4); |
| 3794 |
--eval $dump_table |
| 3795 |
UPDATE t SET j = JSON_REPLACE(j, '$[1000]', 1000); |
| 3796 |
--eval $dump_table |
| 3797 |
UPDATE t SET j = JSON_SET(j, '$[1000]', 1000); |
| 3798 |
--eval $dump_table |
| 3799 |
UPDATE t SET j = JSON_OBJECT('a',1,'b',2,'c',3); |
| 3800 |
UPDATE t SET j = JSON_REPLACE(j, '$[1]', 4); |
| 3801 |
--eval $dump_table |
| 3802 |
UPDATE t SET j = JSON_OBJECT('a',1,'b',2,'c',3); |
| 3803 |
UPDATE t SET j = JSON_REPLACE(j, '$[1]', 4); |
| 3804 |
--eval $dump_table |
| 3805 |
UPDATE t SET j = JSON_REPLACE(j, '$.a.b', 5); |
| 3806 |
--eval $dump_table |
| 3807 |
UPDATE t SET j = JSON_REPLACE(j, '$.a[2]', 6); |
| 3808 |
--eval $dump_table |
| 3809 |
|
| 3810 |
# Test update to some other data types. |
| 3811 |
UPDATE t SET j = JSON_ARRAY('abcdefghijklmnopqrstuvwxyz'); |
| 3812 |
--eval $dump_table |
| 3813 |
UPDATE t SET j = JSON_SET(j, '$[0]', 3.14e0); |
| 3814 |
--eval $dump_table |
| 3815 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(10000000000 AS UNSIGNED)); |
| 3816 |
--eval $dump_table |
| 3817 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(4000000000 AS SIGNED)); |
| 3818 |
--eval $dump_table |
| 3819 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(123 AS UNSIGNED)); |
| 3820 |
--eval $dump_table |
| 3821 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(456 AS SIGNED)); |
| 3822 |
--eval $dump_table |
| 3823 |
UPDATE t SET j = JSON_SET(j, '$[0]', -123); |
| 3824 |
--eval $dump_table |
| 3825 |
UPDATE t SET j = JSON_SET(j, '$[0]', -4000000000); |
| 3826 |
--eval $dump_table |
| 3827 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(76543 AS SIGNED)); |
| 3828 |
--eval $dump_table |
| 3829 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST(87654 AS UNSIGNED)); |
| 3830 |
--eval $dump_table |
| 3831 |
UPDATE t SET j = JSON_SET(j, '$[0]', -100000); |
| 3832 |
--eval $dump_table |
| 3833 |
UPDATE t SET j = JSON_SET(j, '$[0]', TRUE); |
| 3834 |
--eval $dump_table |
| 3835 |
UPDATE t SET j = JSON_SET(j, '$[0]', NULL); |
| 3836 |
--eval $dump_table |
| 3837 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST('abc' AS BINARY)); |
| 3838 |
--eval $dump_table |
| 3839 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST('2016-12-23 13:16:40' AS DATETIME)); |
| 3840 |
--eval $dump_table |
| 3841 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST('2016-12-24' AS DATE)); |
| 3842 |
--eval $dump_table |
| 3843 |
UPDATE t SET j = JSON_SET(j, '$[0]', CAST('14:17:41' AS TIME)); |
| 3844 |
--eval $dump_table |
| 3845 |
UPDATE t SET j = JSON_SET(j, '$[0]', 1.23); |
| 3846 |
--eval $dump_table |
| 3847 |
UPDATE t SET j = JSON_SET(j, '$[0]', i); |
| 3848 |
--eval $dump_table |
| 3849 |
UPDATE t SET j = JSON_SET(j, '$[0]', db); |
| 3850 |
--eval $dump_table |
| 3851 |
UPDATE t SET j = JSON_SET(j, '$[0]', dc); |
| 3852 |
--eval $dump_table |
| 3853 |
UPDATE t SET j = JSON_SET(j, '$[0]', ts); |
| 3854 |
--eval $dump_table |
| 3855 |
UPDATE t SET j = JSON_SET(j, '$[0]', JSON_ARRAY()); |
| 3856 |
--eval $dump_table |
| 3857 |
UPDATE t SET j = JSON_SET(j, '$[0]', JSON_ARRAY('abc')); |
| 3858 |
--eval $dump_table |
| 3859 |
UPDATE t SET j = JSON_SET(j, '$[0]', JSON_OBJECT()); |
| 3860 |
--eval $dump_table |
| 3861 |
UPDATE t SET j = JSON_SET(j, '$[0]', JSON_OBJECT('abc', 'def')); |
| 3862 |
--eval $dump_table |
| 3863 |
UPDATE t SET j = JSON_SET(j, '$[0]', gc->'$[0]'); |
| 3864 |
--eval $dump_table |
| 3865 |
UPDATE t SET j = JSON_SET(j, '$[0]', gc->'$[1]'); |
| 3866 |
--eval $dump_table |
| 3867 |
UPDATE t SET j = JSON_SET(j, '$[0]', gc->'$[2]'); |
| 3868 |
--eval $dump_table |
| 3869 |
UPDATE t SET j = JSON_SET(j, '$[0]', gc->'$[3]'); |
| 3870 |
--eval $dump_table |
| 3871 |
UPDATE t SET j = JSON_SET(j, '$[0]', gc->'$[4]'); |
| 3872 |
--eval $dump_table |
| 3873 |
|
| 3874 |
--echo # Test update with indexes relative to the end of the array. |
| 3875 |
UPDATE t SET j = JSON_ARRAY('a','b','c'); |
| 3876 |
--eval $dump_table |
| 3877 |
UPDATE t SET j = JSON_SET(j, '$[last]', 4); |
| 3878 |
--eval $dump_table |
| 3879 |
UPDATE t SET j = JSON_SET(j, '$[last-1]', 5); |
| 3880 |
--eval $dump_table |
| 3881 |
UPDATE t SET j = JSON_SET(j, '$[last-2]', 6); |
| 3882 |
--eval $dump_table |
| 3883 |
UPDATE t SET j = JSON_SET(j, '$[last-3]', 7); |
| 3884 |
--eval $dump_table |
| 3885 |
UPDATE t SET j = JSON_ARRAY('a','b'); |
| 3886 |
--eval $dump_table |
| 3887 |
UPDATE t SET j = JSON_REPLACE(j, '$[last]', 3); |
| 3888 |
--eval $dump_table |
| 3889 |
UPDATE t SET j = JSON_REPLACE(j, '$[last-1]', 4); |
| 3890 |
--eval $dump_table |
| 3891 |
UPDATE t SET j = JSON_REPLACE(j, '$[last-2]', 5); |
| 3892 |
--eval $dump_table |
| 3893 |
|
| 3894 |
DROP TABLE t; |
| 3895 |
|
| 3896 |
# Some multi-table updates which perform partial updates. |
| 3897 |
CREATE TABLE t1(id INT PRIMARY KEY, j JSON, x INT); |
| 3898 |
INSERT INTO t1 VALUES (1, '{"a":"xy"}', 1), (2, '{"a":"zw"}', 2); |
| 3899 |
let $dump_t1= SELECT *, JSON_STORAGE_SIZE(j) ss, |
| 3900 |
JSON_STORAGE_FREE(j) sf FROM t1 ORDER BY id; |
| 3901 |
--eval $dump_t1 |
| 3902 |
CREATE TABLE t2(id INT PRIMARY KEY, j JSON, x INT); |
| 3903 |
INSERT INTO t2 VALUES |
| 3904 |
(1, '{"b":"X"}', 3), |
| 3905 |
(2, '{"b":"Y"}', 4), |
| 3906 |
(3, '{"b":"Z"}', 9); |
| 3907 |
let $dump_t2= SELECT *, JSON_STORAGE_SIZE(j) ss, |
| 3908 |
JSON_STORAGE_FREE(j) sf FROM t2 ORDER BY id; |
| 3909 |
--eval $dump_t2 |
| 3910 |
ANALYZE TABLE t1, t2; |
| 3911 |
let $query= UPDATE t1, t2 |
| 3912 |
SET t1.j = JSON_SET(t1.j, '\$.a', t2.j->'\$.b') |
| 3913 |
WHERE t1.id = t2.id; |
| 3914 |
--skip_if_hypergraph # Depends on the query plan. |
| 3915 |
--replace_regex /("used_columns": )\[(?:[^\]]*\r?\n)*\s*\]/\1"filtered due to bug#22472365"/ |
| 3916 |
--eval EXPLAIN FORMAT=JSON $query |
| 3917 |
SET optimizer_trace="enabled=on"; |
| 3918 |
--eval $query |
| 3919 |
SET optimizer_trace="enabled=off"; |
| 3920 |
SELECT trace->'$**.json_partial_update' |
| 3921 |
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; |
| 3922 |
--eval $dump_t1 |
| 3923 |
--eval $dump_t2 |
| 3924 |
let $query= UPDATE t1, t2 SET t1.j = JSON_SET(t1.j, '\$.a', t2.x), |
| 3925 |
t2.j = JSON_SET(t2.j, '\$.b', t1.x) |
| 3926 |
WHERE t1.id = t2.id; |
| 3927 |
--skip_if_hypergraph # Depends on the query plan. |
| 3928 |
--eval EXPLAIN FORMAT=JSON $query |
| 3929 |
SET optimizer_trace="enabled=on"; |
| 3930 |
--eval $query |
| 3931 |
SET optimizer_trace="enabled=off"; |
| 3932 |
SELECT trace->'$**.json_partial_update' |
| 3933 |
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; |
| 3934 |
--eval $dump_t1 |
| 3935 |
--eval $dump_t2 |
| 3936 |
let $query= UPDATE t1, t2 SET t1.j = '[123]', t2.j = JSON_SET(t2.j, '\$.b', 99); |
| 3937 |
--skip_if_hypergraph # Depends on the query plan. |
| 3938 |
--replace_regex /("used_columns": )\[(?:[^\]]*\r?\n)*\s*\]/\1"filtered due to bug#22472365"/ |
| 3939 |
--eval EXPLAIN FORMAT=JSON $query |
| 3940 |
SET optimizer_trace="enabled=on"; |
| 3941 |
--eval $query |
| 3942 |
SET optimizer_trace="enabled=off"; |
| 3943 |
SELECT trace->'$**.json_partial_update' |
| 3944 |
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; |
| 3945 |
--eval $dump_t1 |
| 3946 |
--eval $dump_t2 |
| 3947 |
DROP TABLE t1, t2; |
| 3948 |
|
| 3949 |
# MyISAM doesn't support partial update, so don't expect partial |
| 3950 |
# update to be used. |
| 3951 |
CREATE TABLE t(j JSON) ENGINE=MyISAM; |
| 3952 |
INSERT INTO t VALUES ('["abcdef"]'); |
| 3953 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM t; |
| 3954 |
SET optimizer_trace="enabled=on"; |
| 3955 |
UPDATE t SET j = JSON_SET(j, '$[0]', 1); |
| 3956 |
SET optimizer_trace="enabled=off"; |
| 3957 |
SELECT trace->'$**.json_partial_update' |
| 3958 |
FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; |
| 3959 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM t; |
| 3960 |
DROP TABLE t; |
| 3961 |
|
| 3962 |
--echo # Partial update on a DEFAULT NULL value in a NOT NULL column. |
| 3963 |
CREATE TABLE t(j JSON NOT NULL); |
| 3964 |
INSERT IGNORE INTO t VALUES (); |
| 3965 |
UPDATE t SET j = JSON_SET(j, '$[0]', 'abc'); |
| 3966 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM t; |
| 3967 |
UPDATE IGNORE t SET j = DEFAULT; |
| 3968 |
UPDATE t SET j = JSON_REMOVE(j, '$[0]'); |
| 3969 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM t; |
| 3970 |
DROP TABLE t; |
| 3971 |
|
| 3972 |
--echo # |
| 3973 |
--echo # Bug#25893720: TABLE::SETUP_PARTIAL_UPDATE(): |
| 3974 |
--echo # ASSERTION `M_PARTIAL_UPDATE_INFO == NULLPTR' FAIL |
| 3975 |
--echo # |
| 3976 |
CREATE TEMPORARY TABLE t(j JSON); |
| 3977 |
INSERT INTO t VALUES ('["abc"]'); |
| 3978 |
UPDATE t SET j = JSON_SET(j, '$[0]', 'de'); |
| 3979 |
UPDATE t SET j = JSON_SET(j, '$[0]', 'f'); |
| 3980 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM t; |
| 3981 |
DROP TABLE t; |
| 3982 |
|
| 3983 |
--echo # Partial update in JSON_REMOVE. |
| 3984 |
CREATE TABLE t(id INT PRIMARY KEY AUTO_INCREMENT, j JSON); |
| 3985 |
INSERT INTO t(j) VALUES |
| 3986 |
(JSON_ARRAY('x', JSON_ARRAY(1, 2, 3, 'abc', 4, 5, 6))), |
| 3987 |
(JSON_OBJECT('x', JSON_OBJECT('a', 'one', |
| 3988 |
'b', 'two', |
| 3989 |
'c', 'ABC', |
| 3990 |
'defghijkl', 'm'), |
| 3991 |
'y', 'z')), |
| 3992 |
('null'), |
| 3993 |
(NULL); |
| 3994 |
let $dump_table= SELECT *, JSON_STORAGE_SIZE(j) ss, |
| 3995 |
JSON_STORAGE_FREE(j) sf FROM t ORDER BY id; |
| 3996 |
--eval $dump_table |
| 3997 |
ANALYZE TABLE t; |
| 3998 |
--let $query= UPDATE t SET j = JSON_REMOVE(j, '\$.x.b', '\$[1][2]') |
| 3999 |
--skip_if_hypergraph # Depends on the query plan. |
| 4000 |
--eval EXPLAIN FORMAT=JSON $query |
| 4001 |
--eval $query |
| 4002 |
--eval $dump_table |
| 4003 |
--echo # See that the free space can be reused by increasing |
| 4004 |
--echo # the size of some values. |
| 4005 |
UPDATE t SET j = JSON_REPLACE(j, '$[1][2]', 'xyzw', '$.x.c', 'XYZW'); |
| 4006 |
--eval $dump_table |
| 4007 |
--echo # Nested calls |
| 4008 |
UPDATE t SET j = JSON_REMOVE(JSON_REMOVE(j, '$[1][2]'), '$.x.c'); |
| 4009 |
--eval $dump_table |
| 4010 |
UPDATE t SET j = JSON_REMOVE(JSON_SET(j, '$.x.a', 'xyz'), '$.x.a'); |
| 4011 |
--eval $dump_table |
| 4012 |
UPDATE t SET j = JSON_REMOVE(JSON_SET(j, '$.x.a', 'zyx'), '$.x.a'); |
| 4013 |
--eval $dump_table |
| 4014 |
UPDATE t SET j = JSON_SET(JSON_REMOVE(j, '$.x.a'), '$.x.a', 'abcd', '$.x.b', 9); |
| 4015 |
--eval $dump_table |
| 4016 |
UPDATE t SET j = JSON_SET(JSON_REMOVE(j, '$.x.b'), '$.x.a', 'efg'); |
| 4017 |
--eval $dump_table |
| 4018 |
UPDATE t SET j = JSON_REMOVE(JSON_REMOVE(j, '$[0]', '$[0]', '$.x'), |
| 4019 |
'$.b', '$.y', '$.z'); |
| 4020 |
--eval $dump_table |
| 4021 |
DROP TABLE t; |
| 4022 |
|
| 4023 |
--echo # |
| 4024 |
--echo # WL#9692: Add JSON_MERGE_PATCH |
| 4025 |
--echo # |
| 4026 |
|
| 4027 |
CREATE TABLE merge_t( |
| 4028 |
id INT PRIMARY KEY AUTO_INCREMENT, |
| 4029 |
target VARCHAR(100), patch VARCHAR(100) |
| 4030 |
); |
| 4031 |
INSERT INTO merge_t(target, patch) VALUES |
| 4032 |
('{"a":"b"}', '{"a":"c"}'), |
| 4033 |
('{"a":"b"}', '{"b":"c"}'), |
| 4034 |
('{"a":"b"}', '{"a":null}'), |
| 4035 |
('{"a":"b", "b":"c"}', '{"a":null}'), |
| 4036 |
('{"a":["b"]}', '{"a":"c"}'), |
| 4037 |
('{"a":"c"}', '{"a":["b"]}'), |
| 4038 |
('{"a": {"b":"c"}}', '{"a": {"b":"d", "c":null}}'), |
| 4039 |
('{"a":[{"b":"c"}]}', '{"a": [1]}'), |
| 4040 |
('["a","b"]', '["c","d"]'), |
| 4041 |
('{"a":"b"}', '["c"]'), |
| 4042 |
('{"a":"foo"}', 'null'), |
| 4043 |
('{"a":"foo"}', '"bar"'), |
| 4044 |
('{"e":null}', '{"a":1}'), |
| 4045 |
('[1,2]', '{"a":"b", "c":null}'), |
| 4046 |
('{}', '{"a":{"bb":{"ccc":null}}}'), |
| 4047 |
(NULL, '{}'), |
| 4048 |
('{}', NULL); |
| 4049 |
SELECT id, target, patch, |
| 4050 |
JSON_MERGE_PATCH(target, patch) AS merged, |
| 4051 |
JSON_EXTRACT(JSON_MERGE_PATCH(target, patch), '$.a') AS a |
| 4052 |
FROM merge_t ORDER BY id; |
| 4053 |
DROP TABLE merge_t; |
| 4054 |
|
| 4055 |
SELECT JSON_MERGE_PATCH('{"a":"b"}', NULL, '{"c":"d"}'); |
| 4056 |
SELECT JSON_MERGE_PATCH(NULL, '[1,2,3]'); |
| 4057 |
SELECT JSON_MERGE_PATCH('{"a":"b"}', NULL, '[1,2,3]', '{"c":null,"d":"e"}'); |
| 4058 |
|
| 4059 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 4060 |
SELECT JSON_MERGE_PATCH(); |
| 4061 |
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT |
| 4062 |
SELECT JSON_MERGE_PATCH('{}'); |
| 4063 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 4064 |
SELECT JSON_MERGE_PATCH('{', '[1,2,3]'); |
| 4065 |
--error ER_INVALID_JSON_TEXT_IN_PARAM |
| 4066 |
SELECT JSON_MERGE_PATCH('{"a":"b"}', '[1,'); |
| 4067 |
|
| 4068 |
--echo # |
| 4069 |
--echo # Bug#28947381: MAX TREATING NUMBERS AS STRINGS WHEN USED |
| 4070 |
--echo # IN GROUP BY AND JSON_UNQUOTE |
| 4071 |
--echo # |
| 4072 |
|
| 4073 |
CREATE TABLE t(j JSON); |
| 4074 |
INSERT INTO t VALUES |
| 4075 |
('{"group": 1, "value": 20}'), |
| 4076 |
('{"group": 1, "value": 100}'), |
| 4077 |
(NULL), |
| 4078 |
('{"group": 2, "value": "abc"}'), |
| 4079 |
('{"group": 3, "value": "xyz"}'), |
| 4080 |
('{"group": 2, "value": "def"}'), |
| 4081 |
('{"group": 3, "value": "xyzw"}'), |
| 4082 |
('{"group": 1, "value": 3}'), |
| 4083 |
('{"group": 1}'); |
| 4084 |
--sorted_result |
| 4085 |
SELECT j->>'$.group' AS group_name, |
| 4086 |
MIN(j->'$.value') AS min_json, |
| 4087 |
MAX(j->'$.value') AS max_json, |
| 4088 |
MIN(j->>'$.value') AS min_string, |
| 4089 |
MAX(j->>'$.value') AS max_string |
| 4090 |
FROM t GROUP BY j->>'$.group'; |
| 4091 |
DROP TABLE t; |
| 4092 |
|
| 4093 |
--echo # |
| 4094 |
--echo # WL#8199: Add JSON support to IN/GREATEST/LEAST |
| 4095 |
--echo # |
| 4096 |
SELECT CAST(1 AS JSON) IN(CAST(1 AS JSON), CAST(2 AS JSON), CAST(3 AS JSON)); |
| 4097 |
SELECT CAST(4 AS JSON) IN(CAST(1 AS JSON), CAST(2 AS JSON), CAST(3 AS JSON)); |
| 4098 |
|
| 4099 |
SELECT CAST(1 AS JSON) IN(CAST(1 AS JSON), CAST(2 AS JSON), NULL); |
| 4100 |
SELECT CAST(4 AS JSON) IN(CAST(1 AS JSON), CAST(2 AS JSON), NULL); |
| 4101 |
|
| 4102 |
SELECT CAST('{"a":1, "b":2}' AS JSON) IN( |
| 4103 |
CAST(1 AS JSON), |
| 4104 |
CAST('{"a":1, "b":2}' AS JSON), |
| 4105 |
CAST('[1,2,3]' AS JSON)); |
| 4106 |
SELECT CAST('[1,2,3]' AS JSON) IN( |
| 4107 |
CAST(1 AS JSON), |
| 4108 |
CAST('{"a":1, "b":2}' AS JSON), |
| 4109 |
CAST('[1,2,3]' AS JSON)); |
| 4110 |
SELECT CAST('{"a":1, "b":2}' AS JSON) IN( |
| 4111 |
NULL, |
| 4112 |
CAST('{"a":1, "b":2}' AS JSON), |
| 4113 |
CAST('[1,2,3]' AS JSON)); |
| 4114 |
SELECT CAST('{"a":1, "b":2}' AS JSON) IN( |
| 4115 |
CAST(1 AS JSON), |
| 4116 |
CAST('{"a":1, "b":20}' AS JSON), |
| 4117 |
CAST('[1,2,3]' AS JSON)); |
| 4118 |
SELECT CAST('{"a":1, "b":2}' AS JSON) IN( |
| 4119 |
NULL, |
| 4120 |
CAST('{"a":1, "b":20}' AS JSON), |
| 4121 |
CAST('[1,2,3]' AS JSON)); |
| 4122 |
SELECT CAST('{"b":2, "a":2}' AS JSON) IN(CAST(@a_var AS JSON), CAST(NULL AS JSON)); |
| 4123 |
|
| 4124 |
|
| 4125 |
--echo # |
| 4126 |
--echo # BUG#28179109: JSON COLUMN WITH STRING VALUE GOT INCORRECT RESULT |
| 4127 |
--echo # BUG#28182471: JSON COLUMN WITH INTEGER VALUE GOT INCORRECT RESULT |
| 4128 |
--echo # |
| 4129 |
CREATE TABLE t1 (id INT, jdoc JSON); |
| 4130 |
|
| 4131 |
INSERT INTO t1 VALUES(1, '{"key1": "111", "key2": "222"}'); |
| 4132 |
INSERT INTO t1 VALUES(2, '{"key1": "333", "key2": "444"}'); |
| 4133 |
SELECT *, JSON_EXTRACT(jdoc,'$.key1') from t1 ORDER BY id; |
| 4134 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('"111"'); |
| 4135 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') = ('"111"'); |
| 4136 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('"111"', '"333"') |
| 4137 |
ORDER BY id; |
| 4138 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('111'); |
| 4139 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') = ('111'); |
| 4140 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('111', '333') |
| 4141 |
ORDER BY id; |
| 4142 |
|
| 4143 |
SELECT * FROM t1 WHERE '111' IN (JSON_EXTRACT(jdoc,'$.key1'), '333'); |
| 4144 |
|
| 4145 |
INSERT INTO t1 VALUES(3, '{"key1": 1, "key2": 11}'); |
| 4146 |
INSERT INTO t1 VALUES(4, '{"key1": 2, "key2": 22}'); |
| 4147 |
|
| 4148 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('1'); |
| 4149 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') = ('1'); |
| 4150 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN ('1', '2') |
| 4151 |
ORDER BY id; |
| 4152 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN (1); |
| 4153 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') = (1); |
| 4154 |
SELECT * FROM t1 WHERE JSON_EXTRACT(jdoc,'$.key1') IN (1, 2) |
| 4155 |
ORDER BY id; |
| 4156 |
|
| 4157 |
SELECT * FROM t1 WHERE 1 IN (JSON_EXTRACT(jdoc,'$.key1'), 2); |
| 4158 |
|
| 4159 |
SELECT (5, CAST(1 AS JSON)) IN ((1, CAST(5 AS JSON)),(5, CAST(1 AS JSON))); |
| 4160 |
SELECT (5, 1) IN ((1, CAST(5 AS JSON)),(5, CAST(1 AS JSON))); |
| 4161 |
|
| 4162 |
DROP TABLE t1; |
| 4163 |
|
| 4164 |
--echo # |
| 4165 |
--echo # Bug#30753642: ASSERTION `FALSE' FAILED. IN ITEM_JSON::VAL_STR |
| 4166 |
--echo # |
| 4167 |
CREATE TABLE t(a DATETIME); |
| 4168 |
INSERT INTO t VALUES ('2019-01-13 00:00:00'); |
| 4169 |
SELECT 1 IN (SELECT 1 FROM t WHERE JSON_OBJECT('a', 'b') = a); |
| 4170 |
DROP TABLE t; |
| 4171 |
|
| 4172 |
--echo # |
| 4173 |
--echo # Bug#25840784: PARTIAL UPDATE ON VIEWS DOES NOT WORK AS EXPECTED ON BASE TABLES |
| 4174 |
--echo # |
| 4175 |
CREATE TABLE t1(id INT PRIMARY KEY, j JSON); |
| 4176 |
CREATE VIEW v1 AS SELECT id, j, JSON_REMOVE(j, '$.b') jj FROM t1; |
| 4177 |
INSERT INTO t1 VALUES |
| 4178 |
(1, '{"a": "abcdefghijklmnopqrstuvwxyz", "b":"abcdef"}'), |
| 4179 |
(2, '{"a": "abcdefghijklmnopqrstuvwxyz"}'), |
| 4180 |
(3, '{"b":"abcdef"}'); |
| 4181 |
UPDATE v1 SET j = JSON_SET(j, '$.a', 'abcdefghijklm'); |
| 4182 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM v1 ORDER BY id; |
| 4183 |
UPDATE v1 SET j = JSON_SET(jj, '$.a', 'abcdefg'); |
| 4184 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM v1 ORDER BY id; |
| 4185 |
UPDATE v1 SET j = JSON_REMOVE(j, '$.a'); |
| 4186 |
SELECT j, JSON_STORAGE_SIZE(j), JSON_STORAGE_FREE(j) FROM v1 ORDER BY id; |
| 4187 |
DROP TABLE t1; |
| 4188 |
DROP VIEW v1; |
| 4189 |
|
| 4190 |
--echo # |
| 4191 |
--echo # Bug#32248705: PUTTING A MISSING ROW INTO JSON_OBJECT CAUSES |
| 4192 |
--echo # JSON_EXTRACT TO FAIL |
| 4193 |
--echo # |
| 4194 |
|
| 4195 |
CREATE TABLE t1(x VARCHAR(1)); |
| 4196 |
CREATE TABLE t2(y VARCHAR(1)); |
| 4197 |
INSERT INTO t1 VALUES ('a'); |
| 4198 |
# These statements used to return zero rows because the left joins were |
| 4199 |
# incorrectly transformed to inner joins. Expect one row. |
| 4200 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_ARRAY(y) <> 3; |
| 4201 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_OBJECT('y', y) <> 3; |
| 4202 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_ARRAY_APPEND('[]', '$', y) <> 3; |
| 4203 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_INSERT('{}', '$.a', y) <> 3; |
| 4204 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_ARRAY_INSERT('[]', '$[0]', y) <> 3; |
| 4205 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_SET('{}', '$.a', y) <> 3; |
| 4206 |
SELECT * FROM t1 LEFT JOIN t2 ON x=y WHERE JSON_REPLACE('{}', '$.a', y) <> 3; |
| 4207 |
DROP TABLE t1, t2; |
| 4208 |
|
| 4209 |
# Local Variables: |
| 4210 |
# mode: sql |
| 4211 |
# sql-product: mysql |
| 4212 |
# comment-column: 48 |
| 4213 |
# comment-start: "# " |
| 4214 |
# fill-column: 80 |
| 4215 |
# End: |