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

[7.x] Fix whereNull/whereNotNull for json in MySQL #32417

Merged
merged 3 commits into from
Apr 20, 2020
Merged

[7.x] Fix whereNull/whereNotNull for json in MySQL #32417

merged 3 commits into from
Apr 20, 2020

Conversation

LastDragon-ru
Copy link
Contributor

This PR fixes #31473, please see issue for more details, in short:

set @a='{"value":null}';

select
-- Actual behavior whereNull/whereNotNull
json_unquote(json_extract(@a, '$."value"')) is null,     -- false but should be true because value = null
json_unquote(json_extract(@a, '$."value"')) is not null, -- true  but should be false 

-- Expected
(json_extract(@a, '$."value"') is null     OR  json_type(json_extract(@a, '$."value"')) = 'NULL'),  -- true, ok
(json_extract(@a, '$."value"') is not null AND json_type(json_extract(@a, '$."value"')) != 'NULL')  -- false, ok
;

PS: I tested it only for MySQL, probably would be good also test for MariaDB and Percona.

@LastDragon-ru LastDragon-ru changed the title [7.x] MySQL: whereNull/whereNotNull fir for json. [7.x] MySQL: whereNull/whereNotNull for for json. Apr 17, 2020
@LastDragon-ru LastDragon-ru changed the title [7.x] MySQL: whereNull/whereNotNull for for json. [7.x] MySQL: whereNull/whereNotNull for json. Apr 17, 2020
@BSN4
Copy link
Contributor

BSN4 commented Apr 17, 2020

whereJsonLength works for me

//where not null
->whereJsonLength('json', '>=', 1); 
//where  null
->whereJsonLength('json', '<=', 0); 

@LastDragon-ru
Copy link
Contributor Author

LastDragon-ru commented Apr 17, 2020

whereJsonLength works for me

@if4lcon is it work if key does not exist? Seems no? In any case whereNull much more readable for me...

@GrahamCampbell GrahamCampbell changed the title [7.x] MySQL: whereNull/whereNotNull for json. [7.x] Fix whereNull/whereNotNull for json in MySQL Apr 17, 2020
@taylorotwell
Copy link
Member

@staudenmeir do you have any feedback on this?

@staudenmeir
Copy link
Contributor

Looks good to me. Great tests!

@driesvints driesvints dismissed their stale review April 20, 2020 09:48

Changes made.

@@ -244,4 +244,35 @@ protected function wrapJsonBooleanSelector($value)

return 'json_extract('.$field.$path.')';
}

protected function whereNull(Builder $query, $where)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing phpdoc.

return parent::whereNull($query, $where);
}

protected function whereNotNull(Builder $query, $where)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

@taylorotwell taylorotwell merged commit 79e420f into laravel:7.x Apr 20, 2020
@taylorotwell
Copy link
Member

Fixed PHP doc issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON + whereNull/whereNotNull is non-intuitive or broken
6 participants