Tarantool development patches archive
 help / color / mirror / Atom feed
From: Roman Khabibov <roman.habibov@tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v4 5/6] box: disallow to modify format of a view
Date: Tue, 22 Sep 2020 18:59:38 +0300	[thread overview]
Message-ID: <F9D2D648-9FB9-4B7A-A365-ABEBB4D5CDEB@tarantool.org> (raw)
In-Reply-To: <cb7440f1-dca4-3709-e7d2-b464891c5686@tarantool.org>

Hi! Thanks for the review.

> On Sep 17, 2020, at 18:01, Vladislav Shpilevoy <v.shpilevoy@tarantool.org> wrote:
> 
> Thanks for the patch!
> 
> On 11.09.2020 23:51, Roman Khabibov wrote:
>> Disallow to modify format and field_count of a view.
> 
> See 4 comments below in addition to Nikita's comments.
> 
> 1. That should be in a different branch, to emphasize that
> it is a separate bug, which we will need to merge into 2.4
> and 2.5 as well.
Done.

>> Needed for #3075
>> ---
>> src/box/alter.cc                          |  8 ++++
>> test/box/view-dont-modify-format.result   | 51 +++++++++++++++++++++++
>> test/box/view-dont-modify-format.test.lua | 18 ++++++++
>> 3 files changed, 77 insertions(+)
>> create mode 100644 test/box/view-dont-modify-format.result
>> create mode 100644 test/box/view-dont-modify-format.test.lua
>> 
>> diff --git a/src/box/alter.cc b/src/box/alter.cc
>> index ba96d9c62..3bfbb690a 100644
>> --- a/src/box/alter.cc
>> +++ b/src/box/alter.cc
>> @@ -2441,6 +2441,14 @@ on_replace_dd_space(struct trigger * /* trigger */, void *event)
>> 				  "view");
>> 			return -1;
>> 		}
>> +		if (def->opts.is_view && (def->field_count !=
>> +		    old_space->def->field_count)) {
> 
> 2. The () around != is not needed.
Removed.

>> +			diag_set(ClientError, ER_ALTER_SPACE,
>> +				  space_name(old_space),
>> +				  "can not modify format or field_count of a "
>> +				  "view");
>> +			return -1;
>> +		}
>> 		/*
>> 		 * Allow change of space properties, but do it
>> 		 * in WAL-error-safe mode.
>> diff --git a/test/box/view-dont-modify-format.result b/test/box/view-dont-modify-format.result
>> new file mode 100644
>> index 000000000..c130db49c
>> --- /dev/null
>> +++ b/test/box/view-dont-modify-format.result
>> @@ -0,0 +1,51 @@
>> +-- test-run result file version 2
>> +--
>> +-- Make sure we can't modify a view format.
>> +--
>> +box.execute("CREATE TABLE t1 (a INT PRIMARY KEY);")
>> + | ---
>> + | - row_count: 1
>> + | ...
>> +box.execute("CREATE VIEW v AS SELECT * FROM t1;")
>> + | ---
>> + | - row_count: 1
>> + | ...
>> +view = box.space._space.index[2]:select('V')[1]:totable()
> 
> 3. You could use box.space.V:format().
Done.

>> +f = {type = 'string', nullable_action = 'none', name = 'C', is_nullable = true}
>> + | ---
>> + | ...
>> +table.insert(view_format, f)
>> + | ---
>> + | ...
>> +view[5] = 3
>> + | ---
>> + | ...
>> +view[7] = view_format
> 
> 4. You can use space:alter() method instead of raw modification
> of _space. Although it is not available on 2.4.
If we should merge it into 2.4, I decided not to use space:alter().

>> + | ---
>> + | ...
>> +box.space._space:replace(view)
>> + | ---
>> + | - error: 'Can''t modify space ''V'': can not modify format or field_count of a view'
>> + | ...
>> +
>> +view = box.space.V
>> + | ---
>> + | ...
>> +view:format({})
>> + | ---
>> + | - error: 'Can''t modify space ''V'': can not modify format or field_count of a view'
>> + | ...
>> +
>> +box.execute("DROP VIEW v;")
>> + | ---
>> + | - row_count: 1
>> + | ...
>> +box.execute("DROP TABLE t1;")
>> + | ---
>> + | - row_count: 1
>> + | …

commit 7214d872e94693378d79f648dfef04d367914be9
Author: Roman Khabibov <roman.habibov@tarantool.org>
Date:   Fri Sep 11 09:33:39 2020 +0300

    box: disallow to modify format of a view
    
    Ban ability to modify format or field count of a view. Since a
    view is a named select, and not a space, in fact, changing view
    format is not a valid operation.

diff --git a/src/box/alter.cc b/src/box/alter.cc
index ba96d9c62..4d0da347a 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -2086,6 +2086,32 @@ update_view_references(struct Select *select, int update_value,
 	return 0;
 }
 
+/**
+ * Check whether space format is changed during UPDATE/REPLACE.
+ *
+ * Used to disallow view's format modification.
+ *
+ * @param old_tuple Tuple of a space before UPDATE/REPLACE.
+ * @param new_tuple Tuple of a space after UPDATE/REPLACE.
+ */
+static bool
+format_is_changed(struct tuple *old_tuple, struct tuple *new_tuple)
+{
+	const char *old_format = tuple_field(old_tuple, BOX_SPACE_FIELD_FORMAT);
+	const char *new_format = tuple_field(new_tuple, BOX_SPACE_FIELD_FORMAT);
+	assert(old_format != NULL && new_format != NULL);
+	const char *end = old_format;
+	mp_next(&end);
+	uint32_t length = end - old_format;
+	end = new_format;
+	mp_next(&end);
+	if (length != end - new_format)
+		return true;
+	if (memcmp(old_format, new_format, length) != 0)
+		return true;
+	return false;
+}
+
 /**
  * Trigger which is fired to commit creation of new SQL view.
  * Its purpose is to release memory of SELECT.
@@ -2441,6 +2467,20 @@ on_replace_dd_space(struct trigger * /* trigger */, void *event)
 				  "view");
 			return -1;
 		}
+		if (def->opts.is_view && old_space->def->field_count !=
+		    def->field_count) {
+			diag_set(ClientError, ER_ALTER_SPACE,
+				 space_name(old_space),
+				 "can not modify field count of a view");
+			return -1;
+		}
+		if (def->opts.is_view && format_is_changed(old_tuple,
+							   new_tuple)) {
+			diag_set(ClientError, ER_ALTER_SPACE,
+				 space_name(old_space),
+				 "can not modify format of a view");
+			return -1;
+		}
 		/*
 		 * Allow change of space properties, but do it
 		 * in WAL-error-safe mode.
diff --git a/test/box/view-dont-modify-format.result b/test/box/view-dont-modify-format.result
new file mode 100644
index 000000000..0d8c50601
--- /dev/null
+++ b/test/box/view-dont-modify-format.result
@@ -0,0 +1,69 @@
+-- test-run result file version 2
+--
+-- Make sure we can't modify a view format.
+--
+box.execute("CREATE TABLE t1 (a INT PRIMARY KEY);")
+ | ---
+ | - row_count: 1
+ | ...
+box.execute("CREATE VIEW v AS SELECT * FROM t1;")
+ | ---
+ | - row_count: 1
+ | ...
+
+--
+-- Try to add a field.
+--
+view = box.space._space.index[2]:select('V')[1]:totable()
+ | ---
+ | ...
+view_format = box.space.V:format()
+ | ---
+ | ...
+f = {type = 'string', nullable_action = 'none', name = 'B', is_nullable = true}
+ | ---
+ | ...
+table.insert(view_format, f)
+ | ---
+ | ...
+view[5] = 2
+ | ---
+ | ...
+view[7] = view_format
+ | ---
+ | ...
+box.space._space:replace(view)
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify field count of a view'
+ | ...
+
+--
+-- Try to modify format only.
+--
+view = box.space.V
+ | ---
+ | ...
+view:format{}
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify field count of a view'
+ | ...
+
+view_format = box.space.V:format()
+ | ---
+ | ...
+view_format[1].name = 'B'
+ | ---
+ | ...
+view:format(view_format)
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify format of a view'
+ | ...
+
+box.execute("DROP VIEW v;")
+ | ---
+ | - row_count: 1
+ | ...
+box.execute("DROP TABLE t1;")
+ | ---
+ | - row_count: 1
+ | ...
diff --git a/test/box/view-dont-modify-format.test.lua b/test/box/view-dont-modify-format.test.lua
new file mode 100644
index 000000000..e553e7012
--- /dev/null
+++ b/test/box/view-dont-modify-format.test.lua
@@ -0,0 +1,29 @@
+--
+-- Make sure we can't modify a view format.
+--
+box.execute("CREATE TABLE t1 (a INT PRIMARY KEY);")
+box.execute("CREATE VIEW v AS SELECT * FROM t1;")
+
+--
+-- Try to add a field.
+--
+view = box.space._space.index[2]:select('V')[1]:totable()
+view_format = box.space.V:format()
+f = {type = 'string', nullable_action = 'none', name = 'B', is_nullable = true}
+table.insert(view_format, f)
+view[5] = 2
+view[7] = view_format
+box.space._space:replace(view)
+
+--
+-- Try to modify format only.
+--
+view = box.space.V
+view:format{}
+
+view_format = box.space.V:format()
+view_format[1].name = 'B'
+view:format(view_format)
+
+box.execute("DROP VIEW v;")
+box.execute("DROP TABLE t1;")

  reply	other threads:[~2020-09-22 15:59 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-11 21:51 [Tarantool-patches] [PATCH v4 0/6] Support column addition Roman Khabibov
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 1/6] sql: rename TK_COLUMN to TK_COLUMN_NAME Roman Khabibov
2020-09-16 13:17   ` Nikita Pettik
2020-09-28 23:29     ` Roman Khabibov
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 2/6] sql: refactor create_table_def and parse Roman Khabibov
2020-09-16 13:27   ` Nikita Pettik
2020-09-17 14:43     ` Vladislav Shpilevoy
2020-09-18 12:31       ` Nikita Pettik
2020-09-18 13:21         ` Roman Khabibov
2020-09-28 23:29     ` Roman Khabibov
2020-09-17 14:43   ` Vladislav Shpilevoy
2020-10-02 22:08     ` Vladislav Shpilevoy
2020-10-03 21:37     ` Roman Khabibov
2020-10-04 13:45       ` Vladislav Shpilevoy
2020-10-04 21:44         ` Roman Khabibov
2020-10-05 21:22           ` Vladislav Shpilevoy
2020-10-07 13:53             ` Roman Khabibov
2020-10-07 22:35               ` Vladislav Shpilevoy
2020-10-08 10:32                 ` Roman Khabibov
2020-09-17 15:16   ` Vladislav Shpilevoy
2020-10-02 22:08     ` Vladislav Shpilevoy
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 3/6] schema: add box_space_field_MAX Roman Khabibov
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 4/6] sql: use parser's region of "index" array Roman Khabibov
2020-09-16 13:30   ` Nikita Pettik
2020-09-28 23:29     ` Roman Khabibov
2020-09-17 14:53   ` Vladislav Shpilevoy
2020-09-23 14:25     ` Roman Khabibov
2020-09-24 21:30       ` Vladislav Shpilevoy
2020-10-05 21:22   ` Vladislav Shpilevoy
2020-10-07 13:53     ` Roman Khabibov
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 5/6] box: disallow to modify format of a view Roman Khabibov
2020-09-16 13:37   ` Nikita Pettik
2020-09-22 15:59     ` Roman Khabibov
2020-09-17 15:01   ` Vladislav Shpilevoy
2020-09-22 15:59     ` Roman Khabibov [this message]
2020-09-11 21:51 ` [Tarantool-patches] [PATCH v4 6/6] sql: support column addition Roman Khabibov
2020-09-16 20:18   ` Nikita Pettik
2020-09-17 15:19     ` Vladislav Shpilevoy
2020-09-18 12:59       ` Nikita Pettik
2020-09-28 23:28         ` Roman Khabibov
2020-10-02 22:08           ` Vladislav Shpilevoy
2020-10-03 21:37             ` Roman Khabibov
2020-09-17 15:45   ` Vladislav Shpilevoy
2020-10-04 13:45   ` Vladislav Shpilevoy
2020-10-04 21:44     ` Roman Khabibov
2020-09-11 22:00 ` [Tarantool-patches] [PATCH v4 0/6] Support " Roman Khabibov
2020-10-08 22:07 ` Vladislav Shpilevoy

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=F9D2D648-9FB9-4B7A-A365-ABEBB4D5CDEB@tarantool.org \
    --to=roman.habibov@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v4 5/6] box: disallow to modify format of a view' \
    /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