Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Kirill Yukhin <kyukhin@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: [tarantool-patches] Re: [PATCH] sql: after table rename properly update indexes
Date: Wed, 15 Aug 2018 01:22:25 +0300	[thread overview]
Message-ID: <d65e21b3-a1ea-0133-7fa2-b641fbbd5772@tarantool.org> (raw)
In-Reply-To: <b9f53b11c907e1e076efbf223b822ad4c722a4ed.1534135985.git.kyukhin@tarantool.org>

Hi! Thanks for the patch!

See 6 comments below and a patch on the branch.

On 13/08/2018 07:54, Kirill Yukhin wrote:
> After table was altered due to rename, indexes were not updated.
> Re-create all table's indexes (in legacy SQL DD), also perform
> udate of corresponding entry in _index for each index to fix table

1. Typo: 'udate'.

> name in create statement.
> 
> Closes #3613
> ---

2. Please, put here the links to the issue and branch.

>   src/box/sql.c                              | 55 +++++++++++++++++++++++++++---
>   src/box/sql/build.c                        |  6 ++--
>   src/box/sql/tarantoolInt.h                 | 25 ++++++++++++--
>   src/box/sql/vdbe.c                         | 27 +++++++++++++--
>   test/sql/gh-3613-idx-alter-update.result   | 37 ++++++++++++++++++++
>   test/sql/gh-3613-idx-alter-update.test.lua | 21 ++++++++++++
>   6 files changed, 161 insertions(+), 10 deletions(-)
>   create mode 100644 test/sql/gh-3613-idx-alter-update.result
>   create mode 100644 test/sql/gh-3613-idx-alter-update.test.lua
> 

3. Running the tests I caught an assertion:

[010] sql-tap/alter.test.lua                          vinyl           [ fail ]
[010] Test failed! Output from reject file sql-tap/alter.reject:
[010] TAP version 13
[010] 1..41
[010] ok - alter-1.1
[010] ok - alter-1.2
[010]
[010] Last 15 lines of Tarantool Log file [Instance "app_server"][/Users/v.shpilevoy/Work/Repositories/tarantool/test/var/010_sql-tap/alter.test.lua:vinyl.tarantool.log]:
[010] ls: */: No such file or directory
[010] Assertion failed: (iid < space->index_count), function sql_update_index_table_name, file /Users/v.shpilevoy/Work/Repositories/tarantool/src/box/sql.c, line 851.

Also just failing test:

[016] sql/gh-3613-idx-alter-update.test.lua           vinyl           [ fail ]
[016]
[016] Test failed! Result content mismatch:
[016] --- sql/gh-3613-idx-alter-update.result	Wed Aug 15 00:41:34 2018
[016] +++ sql/gh-3613-idx-alter-update.reject	Wed Aug 15 00:42:19 2018
[016] @@ -32,6 +32,7 @@
[016]  box.sql.execute('DROP INDEX i ON j3')
[016]  ---
[016]  ...
[016] +-- Cleanup
[016]  box.sql.execute('DROP TABLE j3')
[016]  ---
[016]  ...
[016]


> diff --git a/src/box/sql.c b/src/box/sql.c
> index ae12cae..e2d3cc1 100644
> --- a/src/box/sql.c
> +++ b/src/box/sql.c
> @@ -840,6 +840,55 @@ rename_fail:
>   	return SQL_TARANTOOL_ERROR;
>   }
>   
> +int
> +sql_update_index_table_name(uint32_t space_id, uint32_t iid,
> +			    const char *new_tbl_name, char **sql_stmt)

4. I think, it would be enough to pass index_def (that you have
on the stack above). It has both index id and space id. And here
you use space_cache_find and struct space only to get index_def.

> +{
> +	assert(space_id != 0);
> +	assert(new_tbl_name != NULL);
> +	struct space *space = space_cache_find(space_id);
> +	assert(space != NULL);
> +	assert(iid < space->index_count);
> +	struct index_def *def = space->index[iid]->def;
> +
> +	bool is_quoted = false;
> +	*sql_stmt = rename_table(db, *sql_stmt, new_tbl_name, &is_quoted);
> +
> +	uint32_t key_len = mp_sizeof_uint(space_id) + mp_sizeof_uint(iid) +
> +			   mp_sizeof_array(2);
> +	uint32_t new_opts_sz = tarantoolSqlite3MakeIdxOpts(def->opts.is_unique,
> +							   *sql_stmt, NULL);
> +	uint32_t op_sz = mp_sizeof_array(1) + mp_sizeof_array(3) +
> +			 mp_sizeof_str(1) + mp_sizeof_uint(4) + new_opts_sz;
> +
> +	char *key_begin = (char*) region_alloc(&fiber()->gc, key_len + op_sz);
> +	if (key_begin == NULL) {
> +		diag_set(OutOfMemory, key_len, "region_alloc", "key_begin");
> +		return SQL_TARANTOOL_ERROR;
> +	}
> +	char *key = mp_encode_array(key_begin, 2);
> +	key = mp_encode_uint(key, space_id);
> +	key = mp_encode_uint(key, iid);
> +
> +	char *op_begin = key;
> +	char *op = mp_encode_array(op_begin, 1);
> +	op = mp_encode_array(op, 3);
> +	op = mp_encode_str(op, "=", 1);
> +	op = mp_encode_uint(op, 4);
> +	op += tarantoolSqlite3MakeIdxOpts(def->opts.is_unique, *sql_stmt, op);
> +
> +	box_tuple_t *res = NULL;

5. You do not need this dummy res. box_update (as all other DML
C API functions) is tolerant to NULL in the last parameter.

> +
> +	if (box_update(BOX_INDEX_ID, 0, key_begin, key, op_begin, op,
> +		       0, &res) != 0) {
> +		diag_set(ClientError, ER_UPDATE_FIELD, 4,
> +			 "while updating SQL field");

6. box_update has already set diag.

> +		return SQL_TARANTOOL_ERROR;
> +	}
> +
> +	return SQLITE_OK;
> +}
> +
>   int
>   tarantoolSqlite3IdxKeyCompare(struct BtCursor *cursor,
>   			      struct UnpackedRecord *unpacked)

  reply	other threads:[~2018-08-14 22:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-13  4:54 [tarantool-patches] " Kirill Yukhin
2018-08-14 22:22 ` Vladislav Shpilevoy [this message]
2018-08-15  8:54   ` [tarantool-patches] " Kirill Yukhin
2018-08-16 22:24     ` Vladislav Shpilevoy
2018-08-17  5:08       ` Kirill Yukhin
2018-08-17  8:31         ` Vladislav Shpilevoy
2018-08-17 13:48           ` Kirill Yukhin
2018-08-17 18:26             ` n.pettik
2018-08-20  5:21               ` 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=d65e21b3-a1ea-0133-7fa2-b641fbbd5772@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kyukhin@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH] sql: after table rename properly update 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