* [tarantool-patches] [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint
@ 2019-07-19 8:38 imeevma
2019-07-22 11:22 ` [tarantool-patches] " n.pettik
2019-07-24 14:10 ` Kirill Yukhin
0 siblings, 2 replies; 3+ messages in thread
From: imeevma @ 2019-07-19 8:38 UTC (permalink / raw)
To: korablev; +Cc: tarantool-patches
If a CHECK constraint was added using an ALTER TABLE statement,
row_count should be increased. Note that row_count does not
increase if a CHECK constraint is added during the execution of a
CREATE TABLE statement.
For example:
box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(id > 0);')
Should return:
- row_count: 1
However it was '0' before the patch.
Closes #4363
---
https://github.com/tarantool/tarantool/issues/4363
https://github.com/tarantool/tarantool/tree/imeevma/gh-4363-increase-row_count-on-alter-table
src/box/sql/build.c | 8 ++++++++
test/sql/row-count.result | 16 ++++++++++++++++
test/sql/row-count.test.lua | 7 +++++++
3 files changed, 31 insertions(+)
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 2aefa2a..183f875 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -1124,6 +1124,14 @@ vdbe_emit_ck_constraint_create(struct Parse *parser,
return;
sqlVdbeAddOp3(v, OP_SInsert, BOX_CK_CONSTRAINT_ID, 0,
ck_constraint_reg + 5);
+ /*
+ * In case constraint was added using ALTER TABLE
+ * statement we should increase row_count.
+ */
+ if (parser->create_table_def.new_space == NULL) {
+ sqlVdbeCountChanges(v);
+ sqlVdbeChangeP5(v, OPFLAG_NCHANGE);
+ }
save_record(parser, BOX_CK_CONSTRAINT_ID, ck_constraint_reg, 2,
v->nOp - 1, true);
VdbeComment((v, "Create CK constraint %s", ck_def->name));
diff --git a/test/sql/row-count.result b/test/sql/row-count.result
index e7841ca..fcea2cf 100644
--- a/test/sql/row-count.result
+++ b/test/sql/row-count.result
@@ -335,3 +335,19 @@ box.execute("DROP TABLE t1;")
---
- row_count: 1
...
+--
+-- gh-4363: make sure that row_count has increased in the case of
+-- ALTER TABLE <table> ADD CONSTRAINT <constraint> CHECK(<expr>);
+--
+box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
+---
+- row_count: 1
+...
+box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(id > 0);')
+---
+- row_count: 1
+...
+box.execute('DROP TABLE t1;')
+---
+- row_count: 1
+...
diff --git a/test/sql/row-count.test.lua b/test/sql/row-count.test.lua
index 9f5215c..7b43db7 100644
--- a/test/sql/row-count.test.lua
+++ b/test/sql/row-count.test.lua
@@ -73,3 +73,10 @@ box.execute("DROP TABLE t2;")
box.execute("DROP TABLE t3;")
box.execute("DROP TABLE t1;")
+--
+-- gh-4363: make sure that row_count has increased in the case of
+-- ALTER TABLE <table> ADD CONSTRAINT <constraint> CHECK(<expr>);
+--
+box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
+box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(id > 0);')
+box.execute('DROP TABLE t1;')
--
2.7.4
^ permalink raw reply [flat|nested] 3+ messages in thread
* [tarantool-patches] Re: [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint
2019-07-19 8:38 [tarantool-patches] [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint imeevma
@ 2019-07-22 11:22 ` n.pettik
2019-07-24 14:10 ` Kirill Yukhin
1 sibling, 0 replies; 3+ messages in thread
From: n.pettik @ 2019-07-22 11:22 UTC (permalink / raw)
To: tarantool-patches; +Cc: Imeev Mergen
> On 19 Jul 2019, at 11:38, imeevma@tarantool.org wrote:
>
> If a CHECK constraint was added using an ALTER TABLE statement,
> row_count should be increased. Note that row_count does not
> increase if a CHECK constraint is added during the execution of a
> CREATE TABLE statement.
> For example:
>
> box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
> box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(id > 0);')
>
> Should return:
> - row_count: 1
>
> However it was '0' before the patch.
>
> Closes #4363
> —
I’ve pushed this fix:
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 183f87505..a1a71174c 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -772,9 +772,13 @@ sql_create_check_contraint(struct Parse *parser)
return;
}
int space_id_reg = ++parser->nMem;
- sqlVdbeAddOp2(sqlGetVdbe(parser), OP_Integer, space->def->id,
+ struct Vdbe *v = sqlGetVdbe(parser);
+ sqlVdbeAddOp2(v, OP_Integer, space->def->id,
space_id_reg);
vdbe_emit_ck_constraint_create(parser, ck_def, space_id_reg);
+ assert(sqlVdbeGetOp(v, v->nOp)->opcode == OP_SInsert);
+ sqlVdbeCountChanges(v);
+ sqlVdbeChangeP5(v, OPFLAG_NCHANGE);
} else {
rlist_add_entry(&parser->create_table_def.new_check, ck_parse,
link);
@@ -1124,14 +1128,6 @@ vdbe_emit_ck_constraint_create(struct Parse *parser,
return;
sqlVdbeAddOp3(v, OP_SInsert, BOX_CK_CONSTRAINT_ID, 0,
ck_constraint_reg + 5);
- /*
- * In case constraint was added using ALTER TABLE
- * statement we should increase row_count.
- */
- if (parser->create_table_def.new_space == NULL) {
- sqlVdbeCountChanges(v);
- sqlVdbeChangeP5(v, OPFLAG_NCHANGE);
- }
save_record(parser, BOX_CK_CONSTRAINT_ID, ck_constraint_reg, 2,
v->nOp - 1, true);
VdbeComment((v, "Create CK constraint %s", ck_def->name));
^ permalink raw reply [flat|nested] 3+ messages in thread
* [tarantool-patches] Re: [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint
2019-07-19 8:38 [tarantool-patches] [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint imeevma
2019-07-22 11:22 ` [tarantool-patches] " n.pettik
@ 2019-07-24 14:10 ` Kirill Yukhin
1 sibling, 0 replies; 3+ messages in thread
From: Kirill Yukhin @ 2019-07-24 14:10 UTC (permalink / raw)
To: tarantool-patches; +Cc: korablev
Hello,
On 19 Jul 11:38, imeevma@tarantool.org wrote:
> If a CHECK constraint was added using an ALTER TABLE statement,
> row_count should be increased. Note that row_count does not
> increase if a CHECK constraint is added during the execution of a
> CREATE TABLE statement.
> For example:
>
> box.execute('CREATE TABLE t1(id INTEGER PRIMARY KEY);')
> box.execute('ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(id > 0);')
>
> Should return:
> - row_count: 1
>
> However it was '0' before the patch.
>
> Closes #4363
> ---
> https://github.com/tarantool/tarantool/issues/4363
> https://github.com/tarantool/tarantool/tree/imeevma/gh-4363-increase-row_count-on-alter-table
I've checked your patch into master.
--
Regards, Kirill Yukhin
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-07-24 14:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-19 8:38 [tarantool-patches] [PATCH v1 1/1] sql: increase row_count when adding CHECK constraint imeevma
2019-07-22 11:22 ` [tarantool-patches] " n.pettik
2019-07-24 14:10 ` Kirill Yukhin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox