Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: implement json compare #43123

Closed
maddyblue opened this issue Dec 12, 2019 · 7 comments
Closed

sql: implement json compare #43123

maddyblue opened this issue Dec 12, 2019 · 7 comments
Assignees
Labels
A-sql-json JSON handling in SQL. A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team

Comments

@maddyblue
Copy link
Contributor

maddyblue commented Dec 12, 2019

While working on sqlsmith, we discovered that the json type doesn't implement the < and <= (and > and >=) operators. There's a json.Compare method, so I went ahead and just added it to the list, assuming that it had been merely forgotten. I wrote a test to generate some json stuff and compare them, and ran it against postgres. The results differed, and should probably be fixed before we add this feature to postgres.

WITH
	t (c)
		AS (
			VALUES
				('1'::JSONB),
				('true'::JSONB),
				('[]'::JSONB),
				('{}'::JSONB)
		)
SELECT
	a.c, b.c, a.c < b.c, a.c <= b.c
FROM
	t AS a, t AS b
ORDER BY
	a.c, b.c;

cockroach:

   c   |  c   | ?column? | ?column?  
+------+------+----------+----------+
     1 |    1 |  false   |   true    
     1 | []   |   true   |   true    
     1 | true |   true   |   true    
     1 | {}   |   true   |   true    
  []   |    1 |  false   |  false    
  []   | []   |  false   |   true    
  []   | true |  false   |  false    
  []   | {}   |   true   |   true    
  true |    1 |  false   |  false    
  true | []   |   true   |   true    
  true | true |  false   |   true    
  true | {}   |   true   |   true    
  {}   |    1 |  false   |  false    
  {}   | []   |  false   |  false    
  {}   | true |  false   |  false    
  {}   | {}   |  false   |   true  

postgres:

  c   |  c   | ?column? | ?column? 
------+------+----------+----------
 []   | []   | f        | t
 []   | {}   | t        | t
 []   | 1    | t        | t
 []   | true | t        | t
 {}   | []   | f        | f
 {}   | {}   | f        | t
 {}   | 1    | f        | f
 {}   | true | f        | f
 1    | []   | f        | f
 1    | {}   | t        | t
 1    | 1    | f        | t
 1    | true | t        | t
 true | []   | f        | f
 true | {}   | t        | t
 true | 1    | f        | f
 true | true | f        | t

Although the orders differ here (which is weird?), the output also differs. Take the 1, [] case, where postgres says false, false and cockroach says true, true.

Jira issue: CRDB-5297

@maddyblue maddyblue added the A-sql-json JSON handling in SQL. label Dec 12, 2019
@maddyblue
Copy link
Contributor Author

@justinj do you have memories about why this is? For example

// In Postgres's sorting rules, the empty array comes before everything (even null),
says that we know we differ from postgres. Do you recall a technical reason why fixing this would be hard?

@justinj
Copy link
Contributor

justinj commented Dec 16, 2019

I don't think there's a technical reason, I didn't do it because I thought it would be tricky/error prone (but possible) and not that helpful, but it's caused problems so if someone has the bandwidth to do it it would probably be worthwhile.

@knz
Copy link
Contributor

knz commented Dec 16, 2019

The pg docs have an entry somewhere that details the json compare algorithm. If we add the operator we should follow that doc to the letter.

@knz
Copy link
Contributor

knz commented Dec 16, 2019

There's also an entry about array compares.

@knz knz added A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) labels Dec 16, 2019
@asubiotto asubiotto moved this from Triage to [TENT] SQL Exec in [DEPRECATED] Old SQLExec board. Don't move stuff here Apr 2, 2020
@solongordon solongordon removed their assignment May 4, 2021
@github-actions
Copy link

We have marked this issue as stale because it has been inactive for
18 months. If this issue is still relevant, removing the stale label
or adding a comment will keep it active. Otherwise, we'll close it in
10 days to keep the issue queue tidy. Thank you for your contribution
to CockroachDB!

@knz knz added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed no-issue-activity labels Sep 19, 2023
@blathers-crl blathers-crl bot added this to Triage in SQL Foundations Sep 19, 2023
@rafiss rafiss added T-sql-queries SQL Queries Team and removed T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels Sep 19, 2023
@rafiss rafiss removed this from Triage in SQL Foundations Sep 19, 2023
@michae2
Copy link
Collaborator

michae2 commented Oct 3, 2023

[triage] @mgartner to verify that this is now fixed

@mgartner
Copy link
Collaborator

mgartner commented Oct 3, 2023

Comparison of JSON values was added in #99275 and adjusted to match Postgres in #101260, both of which will be included in v23.2.

@mgartner mgartner closed this as completed Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-json JSON handling in SQL. A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants