From a6a33e9f344a8e798720f699ff79720e35c03120 Mon Sep 17 00:00:00 2001 From: Vladimir Davydov <vdavydov.dev@gmail.com> Date: Wed, 15 May 2019 16:39:48 +0300 Subject: [PATCH] box: fix autoincrement for json path indexes The autoincrement code was written when there were no nested field. Now, it isn't enough to just skip to the autoincrement field - we also need to descend deeper if key_part->path is set. Note, the code expects the nested field to be present and set to NULL. That is, if field path is [1].a.b, the tuple must have all intermediate fields set: {{a = {b = box.NULL}}} (usage of box.NULL is mandatory to create a tuple like that in Lua). Closes #4210 --- src/box/request.c | 10 +++++++++- test/box/sequence.result | 32 ++++++++++++++++++++++++++++++++ test/box/sequence.test.lua | 12 ++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/box/request.c b/src/box/request.c index 9d3287f972..041a8d5487 100644 --- a/src/box/request.c +++ b/src/box/request.c @@ -163,7 +163,8 @@ request_handle_sequence(struct request *request, struct space *space) const char *data = request->tuple; const char *data_end = request->tuple_end; int len = mp_decode_array(&data); - int fieldno = pk->def->key_def->parts[space->sequence_part].fieldno; + struct key_part *part = &pk->def->key_def->parts[space->sequence_part]; + int fieldno = part->fieldno; if (unlikely(len < fieldno + 1)) return 0; @@ -174,6 +175,13 @@ request_handle_sequence(struct request *request, struct space *space) } while (--fieldno > 0); } + if (part->path != NULL) { + tuple_go_to_path(&key, part->path, part->path_len, + MULTIKEY_NONE); + if (key == NULL) + return 0; /* field not found */ + } + int64_t value; if (mp_typeof(*key) == MP_NIL) { /* diff --git a/test/box/sequence.result b/test/box/sequence.result index 8a7c349cff..3cac4dc9a0 100644 --- a/test/box/sequence.result +++ b/test/box/sequence.result @@ -2033,3 +2033,35 @@ s:insert{'d', box.NULL, 1001} s:drop() --- ... +-- +-- gh-4210: using sequence with a json path key part. +-- +s = box.schema.space.create('test') +--- +... +_ = s:create_index('pk', {parts = {{'[1].a.b[1]', 'unsigned'}}, sequence = true}) +--- +... +s:replace{} -- error +--- +- error: Tuple field [1]["a"]["b"][1] required by space format is missing +... +s:replace{{c = {}}} -- error +--- +- error: Tuple field [1]["a"]["b"][1] required by space format is missing +... +s:replace{{a = {c = {}}}} -- error +--- +- error: Tuple field [1]["a"]["b"][1] required by space format is missing +... +s:replace{{a = {b = {}}}} -- error +--- +- error: Tuple field [1]["a"]["b"][1] required by space format is missing +... +s:replace{{a = {b = {box.NULL}}}} -- ok +--- +- [{'a': {'b': [1]}}] +... +s:drop() +--- +... diff --git a/test/box/sequence.test.lua b/test/box/sequence.test.lua index d931a94e29..c39f1d4125 100644 --- a/test/box/sequence.test.lua +++ b/test/box/sequence.test.lua @@ -685,3 +685,15 @@ s.index.pk.sequence_id == sequence_id s:insert{'d', 1000, 1000} s:insert{'d', box.NULL, 1001} s:drop() + +-- +-- gh-4210: using sequence with a json path key part. +-- +s = box.schema.space.create('test') +_ = s:create_index('pk', {parts = {{'[1].a.b[1]', 'unsigned'}}, sequence = true}) +s:replace{} -- error +s:replace{{c = {}}} -- error +s:replace{{a = {c = {}}}} -- error +s:replace{{a = {b = {}}}} -- error +s:replace{{a = {b = {box.NULL}}}} -- ok +s:drop() -- GitLab