Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-23143

Add JSON_EQUALS function to check JSON equality

Details

    Description

      JSON_CONTAINS can be used to test for JSON object equality in some cases, but we seem to lack a clear JSON_EQUALS function.

      Attachments

        Issue Links

          Activity

            Eric_Herman Eric Herman added a comment - - edited

            MariaDB [test]> show create table foo\G
            *************************** 1. row ***************************
                   Table: foo
            Create Table: CREATE TABLE `foo` (
              `foo_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
              `foo_dat` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`foo_dat`)),
              PRIMARY KEY (`foo_id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
            1 row in set (0.000 sec)
             
            MariaDB [test]> SELECT * FROM foo;
            +--------+--------------------------+
            | foo_id | foo_dat                  |
            +--------+--------------------------+
            |      1 | {"A": 1, "C": 3, "B": 2} |
            |      2 | {"A": 7, "C": 9, "B": 8} |
            +--------+--------------------------+
            2 rows in set (0.000 sec)
             
            MariaDB [test]> SELECT * FROM foo WHERE JSON_CONTAINS(foo_dat, '{"A": 1, "B": 2, "C": 3}');
            +--------+--------------------------+
            | foo_id | foo_dat                  |
            +--------+--------------------------+
            |      1 | {"A": 1, "C": 3, "B": 2} |
            +--------+--------------------------+
            1 row in set (0.000 sec)
            

            Thank you @Kartik Soneji , this was non-obvious to me from the https://mariadb.com/kb/en/json-functions/ page.

            While JSON_CONTAINS can work, to avoid matching larger objects we probably want two comparisons, e.g.::

            MariaDB [test]> SELECT foo_id FROM foo WHERE
                ->      JSON_CONTAINS(foo_dat, '{"A": 1, "B": 2, "C": 3}')
                ->  AND JSON_CONTAINS('{"A": 1, "B": 2, "C": 3}', foo_dat);
            +--------+
            | foo_id |
            +--------+
            |      1 |
            +--------+
            1 row in set (0.000 sec)
            

            I feel like a more explicit JSON_EQUALS is still desired.

            It looks like fairly common problem:
            https://stackoverflow.com/questions/59471116/mariadb-compare-two-json-objects
            https://stackoverflow.com/questions/46734547/compare-json-values-in-mariadb
            https://dba.stackexchange.com/questions/208481/mariadb-compare-json

            Eric_Herman Eric Herman added a comment - - edited MariaDB [test]> show create table foo\G *************************** 1. row *************************** Table: foo Create Table: CREATE TABLE `foo` ( `foo_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `foo_dat` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`foo_dat`)), PRIMARY KEY (`foo_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 1 row in set (0.000 sec)   MariaDB [test]> SELECT * FROM foo; +--------+--------------------------+ | foo_id | foo_dat | +--------+--------------------------+ | 1 | {"A": 1, "C": 3, "B": 2} | | 2 | {"A": 7, "C": 9, "B": 8} | +--------+--------------------------+ 2 rows in set (0.000 sec)   MariaDB [test]> SELECT * FROM foo WHERE JSON_CONTAINS(foo_dat, '{"A": 1, "B": 2, "C": 3}'); +--------+--------------------------+ | foo_id | foo_dat | +--------+--------------------------+ | 1 | {"A": 1, "C": 3, "B": 2} | +--------+--------------------------+ 1 row in set (0.000 sec) Thank you @Kartik Soneji , this was non-obvious to me from the https://mariadb.com/kb/en/json-functions/ page. While JSON_CONTAINS can work, to avoid matching larger objects we probably want two comparisons, e.g.:: MariaDB [test]> SELECT foo_id FROM foo WHERE -> JSON_CONTAINS(foo_dat, '{"A": 1, "B": 2, "C": 3}') -> AND JSON_CONTAINS('{"A": 1, "B": 2, "C": 3}', foo_dat); +--------+ | foo_id | +--------+ | 1 | +--------+ 1 row in set (0.000 sec) I feel like a more explicit JSON_EQUALS is still desired. It looks like fairly common problem: https://stackoverflow.com/questions/59471116/mariadb-compare-two-json-objects https://stackoverflow.com/questions/46734547/compare-json-values-in-mariadb https://dba.stackexchange.com/questions/208481/mariadb-compare-json
            rodfsouza Rodrigo Souza added a comment -

            @Daniel Black I wonder if would be OK for me to take this task?

            Do we have anybody to talk about it and get advices?

            rodfsouza Rodrigo Souza added a comment - @Daniel Black I wonder if would be OK for me to take this task? Do we have anybody to talk about it and get advices?
            LiuYuJi LiuYuJi added a comment -

            Hello, I am Liu Yuji, I have studied C/C++ programming and SQL and JSON. I would like to work on this project in GSOC 2021, and I have installed MariaDB and MariaDB's source code on my system. Hope someone can give me some guidance, I would be very grateful for that.

            LiuYuJi LiuYuJi added a comment - Hello, I am Liu Yuji, I have studied C/C++ programming and SQL and JSON. I would like to work on this project in GSOC 2021, and I have installed MariaDB and MariaDB's source code on my system. Hope someone can give me some guidance, I would be very grateful for that.

            Logged MDEV-26678 JSON_EQUALS validates invalid JSON and valid JSON as being equal when using a backslash
            Also logged mostly unrelated MDEV-26679 JSON_VALID validates invalid JSON

            Roel Roel Van de Paar added a comment - Logged MDEV-26678 JSON_EQUALS validates invalid JSON and valid JSON as being equal when using a backslash Also logged mostly unrelated MDEV-26679 JSON_VALID validates invalid JSON

            People

              cvicentiu Vicențiu Ciorbaru
              Eric_Herman Eric Herman
              Votes:
              2 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.