copr: implement json cast as bool#9286
Conversation
Signed-off-by: wshwsh12 <793703860@qq.com>
|
/cc @skyzh |
| Datum::Time(t) => Some(!t.is_zero()), | ||
| Datum::Dur(d) => Some(!d.is_zero()), | ||
| Datum::Dec(d) => Some(ConvertTo::<f64>::convert(&d, ctx)?.round() != 0f64), | ||
| Datum::Json(j) => Some(j.as_ref().as_mysql_bool(ctx).unwrap()), |
There was a problem hiding this comment.
Let's replace unwrap() with ? here, in case that future work makes as_mysql_bool returns Err.
| JsonType::I64 => self.get_i64() == 0, | ||
| JsonType::U64 => self.get_u64() == 0, | ||
| JsonType::Double => self.get_double() == 0f64, | ||
| JsonType::String => false, |
There was a problem hiding this comment.
We don't need to check the json string's length here?
There was a problem hiding this comment.
I don't think so. I have a test in MySQL8.0.
MySQL [test]> select *,json_extract(j, '$.test') from testjson where json_extract(j, '$.test');
+----+-----------------+---------------------------+
| id | j | json_extract(j, '$.test') |
+----+-----------------+---------------------------+
| 1 | {"test": ""} | "" |
| 2 | {"test": ""} | "" |
| 3 | {"test": true} | true |
| 4 | {"test": false} | false |
+----+-----------------+---------------------------+
4 rows in set, 1 warning (0.000 sec)
There was a problem hiding this comment.
Yes, the logic here is correctly, here always return false no matter the value is.
Maybe we can add ref: https://dev.mysql.com/doc/refman/8.0/en/json.html#Converting%20between%20JSON%20and%20non-JSON%20values here?
MariaDB [test]> CREATE TABLE JSON (c JSON);
MariaDB [test]> insert into json values row('{"a":"","b":"abc"}');
MariaDB [test]> select if(JSON_EXTRACT(c, '$.a') = true, 'TRUE', 'FALSE') from JSON;
+----------------------------------------------------+
| if(JSON_EXTRACT(c, '$.a') = true, 'TRUE', 'FALSE') |
+----------------------------------------------------+
| FALSE |
+----------------------------------------------------+
1 row in set (0.001 sec)
MariaDB [test]> select if(JSON_EXTRACT(c, '$.b') = true, 'TRUE', 'FALSE') from JSON;
+----------------------------------------------------+
| if(JSON_EXTRACT(c, '$.b') = true, 'TRUE', 'FALSE') |
+----------------------------------------------------+
| FALSE |
+----------------------------------------------------+
1 row in set (0.001 sec)
Signed-off-by: wshwsh12 <793703860@qq.com>
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
|
/run-all-tests |
|
@wshwsh12 merge failed. |
|
/merge |
|
/run-all-tests |
|
@wshwsh12 merge failed. |
|
/merge |
|
@Xuanwo Oops! auto merge is restricted to Committers of the SIG.You are not a committer or co-leader or leader. |
|
/merge |
|
/run-all-tests |
Signed-off-by: wshwsh12 793703860@qq.com
What problem does this PR solve?
Issue Number: close pingcap/tidb#12233
Problem Summary: port pingcap/tidb#18948
What is changed and how it works?
Proposal: xxx
What's Changed:
Related changes
Check List
Tests
Side effects
Release note