Skip to content

Commit 39d687f

Browse files
committed
Update tests and documentation
1 parent 427446e commit 39d687f

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

docs/en/sql-reference/data-types/newjson.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,39 @@ SELECT json, json.a, json.b, json.c FROM test;
694694
└──────────────────────────────┴────────┴─────────┴────────────┘
695695
```
696696

697+
## Comparison between values of the JSON type
698+
699+
Values of the `JSON` column cannot be compared by `less/greater` functions, but can be compared using `equal` function.
700+
Two JSON objects considered equal when they have the same set of paths and value of each path have the same type and value in both objects.
701+
702+
Example:
703+
```sql
704+
CREATE TABLE test (json1 JSON(a UInt32), json2 JSON(a UInt32)) ENGINE=Memory;
705+
INSERT INTO test FORMAT JSONEachRow
706+
{"json1" : {"a" : 42, "b" : 42, "c" : "Hello"}, "json2" : {"a" : 42, "b" : 42, "c" : "Hello"}}
707+
{"json1" : {"a" : 42, "b" : 42, "c" : "Hello"}, "json2" : {"a" : 43, "b" : 42, "c" : "Hello"}}
708+
{"json1" : {"a" : 42, "b" : 42, "c" : "Hello"}, "json2" : {"a" : 43, "b" : 42, "c" : "Hello"}}
709+
{"json1" : {"a" : 42, "b" : 42, "c" : "Hello"}, "json2" : {"a" : 42, "b" : 42, "c" : "World"}}
710+
{"json1" : {"a" : 42, "b" : [1, 2, 3], "c" : "Hello"}, "json2" : {"a" : 42, "b" : 42, "c" : "Hello"}}
711+
{"json1" : {"a" : 42, "b" : 42.0, "c" : "Hello"}, "json2" : {"a" : 42, "b" : 42, "c" : "Hello"}}
712+
{"json1" : {"a" : 42, "b" : "42", "c" : "Hello"}, "json2" : {"a" : 42, "b" : 42, "c" : "Hello"}};
713+
714+
SELECT json1, json2, json1 == json2 FROM test;
715+
```
716+
717+
```text
718+
┌─json1──────────────────────────────────┬─json2─────────────────────────┬─equals(json1, json2)─┐
719+
│ {"a":42,"b":"42","c":"Hello"} │ {"a":42,"b":"42","c":"Hello"} │ 1 │
720+
│ {"a":42,"b":"42","c":"Hello"} │ {"a":43,"b":"42","c":"Hello"} │ 0 │
721+
│ {"a":42,"b":"42","c":"Hello"} │ {"a":43,"b":"42","c":"Hello"} │ 0 │
722+
│ {"a":42,"b":"42","c":"Hello"} │ {"a":42,"b":"42","c":"World"} │ 0 │
723+
│ {"a":42,"b":["1","2","3"],"c":"Hello"} │ {"a":42,"b":"42","c":"Hello"} │ 0 │
724+
│ {"a":42,"b":42,"c":"Hello"} │ {"a":42,"b":"42","c":"Hello"} │ 0 │
725+
│ {"a":42,"b":"42","c":"Hello"} │ {"a":42,"b":"42","c":"Hello"} │ 0 │
726+
└────────────────────────────────────────┴───────────────────────────────┴──────────────────────┘
727+
```
728+
729+
697730
## Tips for better usage of the JSON type
698731

699732
Before creating `JSON` column and loading data into it, consider the following tips:
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{"a":0} {"a":0} 1
2-
{"a":42} {"a":42} 1
3-
{"a":42} {"a":43} 0
4-
{"a":42,"b":"42"} {"a":42,"b":"42"} 1
5-
{"a":42,"b":"42"} {"a":42,"b":"43"} 0
6-
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","d":"42"} 0
7-
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"42"} 1
8-
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"43"} 0
9-
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"43","d":"42"} 0
10-
{"a":42,"b":"42","d":"42"} {"a":42,"b":"42","d":"42"} 1
11-
{"a":42,"b":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"42"} 0
12-
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","c":"42"} 1
13-
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","c":"42","d":"42"} 0
14-
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":"42"} 1
15-
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":"42"} 0
16-
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":42} 0
1+
{"a":0} {"a":0} 1 0
2+
{"a":42} {"a":42} 1 0
3+
{"a":42} {"a":43} 0 1
4+
{"a":42,"b":"42"} {"a":42,"b":"42"} 1 0
5+
{"a":42,"b":"42"} {"a":42,"b":"43"} 0 1
6+
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","d":"42"} 0 1
7+
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"42"} 1 0
8+
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"43"} 0 1
9+
{"a":42,"b":"42","c":"42","d":"42"} {"a":42,"b":"42","c":"43","d":"42"} 0 1
10+
{"a":42,"b":"42","d":"42"} {"a":42,"b":"42","d":"42"} 1 0
11+
{"a":42,"b":"42","d":"42"} {"a":42,"b":"42","c":"42","d":"42"} 0 1
12+
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","c":"42"} 1 0
13+
{"a":42,"b":"42","c":"42"} {"a":42,"b":"42","c":"42","d":"42"} 0 1
14+
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":"42"} 1 0
15+
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":"42"} 0 1
16+
{"a":42,"b":"42","c":"42","d":"42","e":"42"} {"a":42,"b":"42","c":"42","d":"42","e":42} 0 1

tests/queries/0_stateless/03282_json_equal_comparison.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ insert into test format JSONEachRow
1818
{"json1" : {"a" : 42, "b" : 42, "c" : 42, "d" : 42, "e" : 42}, "json2" : {"a" : 42, "b" : 42, "d" : 42, "c" : 42, "e" : "42"}}
1919
{"json1" : {"a" : 42, "b" : 42, "c" : 42, "d" : 42, "e" : 42}, "json2" : {"a" : 42, "b" : 42, "d" : 42, "c" : 42, "e" : 42.0}};
2020

21-
select json1, json2, json1 == json2 from test;
21+
select json1, json2, json1 == json2, json1 != json2 from test;
2222
drop table test;

0 commit comments

Comments
 (0)