From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 6D91D469719 for ; Tue, 22 Sep 2020 18:59:22 +0300 (MSK) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.1\)) From: Roman Khabibov In-Reply-To: <20200916133728.GD10599@tarantool.org> Date: Tue, 22 Sep 2020 18:59:20 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <07DA8937-EB39-477C-B41F-72EE0088E812@tarantool.org> References: <20200911215115.6622-1-roman.habibov@tarantool.org> <20200911215115.6622-6-roman.habibov@tarantool.org> <20200916133728.GD10599@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH v4 5/6] box: disallow to modify format of a view List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Nikita Pettik Cc: tarantool-patches@dev.tarantool.org, v.shpilevoy@tarantool.org Hi! Thanks for the review. > On Sep 16, 2020, at 16:37, Nikita Pettik = wrote: >=20 > On 12 Sep 00:51, Roman Khabibov wrote: >> Disallow to modify format and field_count of a view. >=20 > Please add rationale 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 >>=20 >> 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 !=3D >> + old_space->def->field_count)) { >> + diag_set(ClientError, ER_ALTER_SPACE, >> + space_name(old_space), >> + "can not modify format or field_count = of a " >=20 > field_count -> field count Done. > What if field count count is the same, but we modify column's type? You=E2=80=99re right. See new version of the patch. >=20 >> + "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 >=20 > Why not put the test case into sql/view.test.lua? Because it is a bug. As far as I know, we make tests for bugs in separate files. commit 7214d872e94693378d79f648dfef04d367914be9 Author: Roman Khabibov Date: Fri Sep 11 09:33:39 2020 +0300 box: disallow to modify format of a view =20 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; } =20 +/** + * 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 =3D tuple_field(old_tuple, = BOX_SPACE_FIELD_FORMAT); + const char *new_format =3D tuple_field(new_tuple, = BOX_SPACE_FIELD_FORMAT); + assert(old_format !=3D NULL && new_format !=3D NULL); + const char *end =3D old_format; + mp_next(&end); + uint32_t length =3D end - old_format; + end =3D new_format; + mp_next(&end); + if (length !=3D end - new_format) + return true; + if (memcmp(old_format, new_format, length) !=3D 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 !=3D + 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 =3D box.space._space.index[2]:select('V')[1]:totable() + | --- + | ... +view_format =3D box.space.V:format() + | --- + | ... +f =3D {type =3D 'string', nullable_action =3D 'none', name =3D 'B', = is_nullable =3D true} + | --- + | ... +table.insert(view_format, f) + | --- + | ... +view[5] =3D 2 + | --- + | ... +view[7] =3D 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 =3D box.space.V + | --- + | ... +view:format{} + | --- + | - error: 'Can''t modify space ''V'': can not modify field count of a = view' + | ... + +view_format =3D box.space.V:format() + | --- + | ... +view_format[1].name =3D '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 =3D box.space._space.index[2]:select('V')[1]:totable() +view_format =3D box.space.V:format() +f =3D {type =3D 'string', nullable_action =3D 'none', name =3D 'B', = is_nullable =3D true} +table.insert(view_format, f) +view[5] =3D 2 +view[7] =3D view_format +box.space._space:replace(view) + +-- +-- Try to modify format only. +-- +view =3D box.space.V +view:format{} + +view_format =3D box.space.V:format() +view_format[1].name =3D 'B' +view:format(view_format) + +box.execute("DROP VIEW v;") +box.execute("DROP TABLE t1;")