Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: tarantool-patches@freelists.org
Subject: [PATCH] box: fix autoincrement for json path indexes
Date: Wed, 15 May 2019 16:44:59 +0300	[thread overview]
Message-ID: <8b90af23b8f527fa73cfa25e3804ef1b1d5fb6ff.1557927828.git.vdavydov.dev@gmail.com> (raw)
In-Reply-To: <20190515131616.tbsb7zme4oomie3h@esperanza>

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 9d3287f9..041a8d54 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 8a7c349c..3cac4dc9 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 d931a94e..c39f1d41 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()
-- 
2.11.0

  reply	other threads:[~2019-05-15 13:44 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-15 10:33 [PATCH 0/4] A few fixes/improvements for autoincrement indexes Vladimir Davydov
2019-05-15 10:33 ` [PATCH 1/4] schema: use tuple field names in Lua Vladimir Davydov
2019-05-15 10:33 ` [PATCH 2/4] schema: fix error while altering index with sequence Vladimir Davydov
2019-05-16  7:45   ` [tarantool-patches] " Konstantin Osipov
2019-05-15 10:33 ` [PATCH 3/4] schema: allow to set sequence for any index part, not just the first Vladimir Davydov
2019-05-16  7:45   ` [tarantool-patches] " Konstantin Osipov
2019-05-16  8:02     ` Vladimir Davydov
2019-05-15 10:33 ` [PATCH 4/4] schema: explicitly forbid setting sequence for json path key part Vladimir Davydov
2019-05-15 13:00   ` [tarantool-patches] " Konstantin Osipov
2019-05-15 13:11     ` Vladimir Davydov
2019-05-15 13:16       ` Vladimir Davydov
2019-05-15 13:44         ` Vladimir Davydov [this message]
2019-05-16  7:42           ` [tarantool-patches] Re: [PATCH] box: fix autoincrement for json path indexes Konstantin Osipov
2019-05-21 13:28             ` Vladimir Davydov
2019-05-21 10:42 ` [tarantool-patches] Re: [PATCH 0/4] A few fixes/improvements for autoincrement indexes Kirill Yukhin
2019-05-21 14:58   ` Konstantin Osipov
2019-05-21 16:02     ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8b90af23b8f527fa73cfa25e3804ef1b1d5fb6ff.1557927828.git.vdavydov.dev@gmail.com \
    --to=vdavydov.dev@gmail.com \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH] box: fix autoincrement for json path indexes' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox