-- MariaDB MDEV draft reproducer
-- Tested on MariaDB 12.3.2-MariaDB, 127.0.0.1:3307.
-- NO_BACKSLASH_ESCAPES keeps the backslashes visible to the JSON parser.

SET SESSION sql_mode = 'STRICT_ALL_TABLES,NO_BACKSLASH_ESCAPES';
SET NAMES utf8mb4 COLLATE utf8mb4_bin;

CREATE DATABASE IF NOT EXISTS mdev_json_unique_keys_repro;
USE mdev_json_unique_keys_repro;

SELECT @@version, @@sql_mode, @@character_set_connection,
       @@collation_connection;

SELECT
  _utf8mb4'{"a":1,"a":2}' IS JSON AS valid_direct,
  _utf8mb4'{"a":1,"a":2}' IS JSON WITH UNIQUE KEYS AS unique_direct;

SELECT
  _utf8mb4'{"a":1,"\u0061":2}' IS JSON AS valid_escaped,
  _utf8mb4'{"a":1,"\u0061":2}' IS JSON WITH UNIQUE KEYS AS unique_escaped;

SELECT
  _utf8mb4'{"a\\b":1,"a\\b":2}' IS JSON WITH UNIQUE KEYS
    AS unique_literal_backslash,
  _utf8mb4'{"a\\b":1,"a\u005Cb":2}' IS JSON WITH UNIQUE KEYS
    AS unique_escaped_backslash;

DROP TABLE IF EXISTS mdev_json_unique_keys_check;
CREATE TABLE mdev_json_unique_keys_check (
  id INT PRIMARY KEY,
  doc LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
  CONSTRAINT unique_json_keys CHECK (doc IS JSON WITH UNIQUE KEYS)
);

INSERT IGNORE INTO mdev_json_unique_keys_check
  VALUES (1, _utf8mb4'{"a":1,"a":2}');
SHOW WARNINGS;

INSERT IGNORE INTO mdev_json_unique_keys_check
  VALUES (2, _utf8mb4'{"a":1,"\u0061":2}');

SELECT id, doc FROM mdev_json_unique_keys_check ORDER BY id;
DROP TABLE mdev_json_unique_keys_check;
