Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@dev.tarantool.org
Cc: v.shpilevoy@tarantool.org
Subject: [Tarantool-patches] [PATCH 5/6] sql: extend result set with autoincrement
Date: Wed, 27 Nov 2019 15:15:45 +0300	[thread overview]
Message-ID: <659c12232934c069ae8d71ed1e2bc8115d36f6e0.1574846892.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1574846892.git.korablev@tarantool.org>
In-Reply-To: <cover.1574846892.git.korablev@tarantool.org>

If result set contains column which features attached sequence
(AUTOINCREMENT in terms of SQL) then meta-information will contain
corresponding field ('autoicrement' : boolean) in response.

Part of #4407
---
 src/box/execute.c          | 18 +++++++++++++++---
 src/box/iproto_constants.h |  3 ++-
 src/box/lua/execute.c      |  5 +++++
 src/box/lua/net_box.c      | 10 +++++++---
 src/box/sql/select.c       |  6 ++++++
 src/box/sql/sqlInt.h       |  3 +++
 src/box/sql/vdbe.h         |  3 +++
 src/box/sql/vdbeInt.h      |  2 ++
 src/box/sql/vdbeapi.c      |  8 ++++++++
 src/box/sql/vdbeaux.c      |  7 +++++++
 10 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/src/box/execute.c b/src/box/execute.c
index 98812ae1e..8305f6f3b 100644
--- a/src/box/execute.c
+++ b/src/box/execute.c
@@ -270,7 +270,7 @@ error:
 
 static size_t
 metadata_map_sizeof(const char *name, const char *type, const char *coll,
-		    int nullable)
+		    int nullable, bool is_autoincrement)
 {
 	uint32_t members_count = 2;
 	size_t map_size = 0;
@@ -284,6 +284,11 @@ metadata_map_sizeof(const char *name, const char *type, const char *coll,
 		map_size += mp_sizeof_uint(IPROTO_FIELD_NULLABLE);
 		map_size += mp_sizeof_bool(nullable);
 	}
+	if (is_autoincrement) {
+		members_count++;
+		map_size += mp_sizeof_uint(IPROTO_FIELD_AUTOINCREMENT);
+		map_size += mp_sizeof_bool(true);
+	}
 	map_size += mp_sizeof_uint(IPROTO_FIELD_NAME);
 	map_size += mp_sizeof_uint(IPROTO_FIELD_TYPE);
 	map_size += mp_sizeof_str(strlen(name));
@@ -319,6 +324,7 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
 		const char *name = sql_column_name(stmt, i);
 		const char *type = sql_column_datatype(stmt, i);
 		int nullable = sql_column_is_nullable(stmt, i);
+		bool is_autoincrement = sql_column_is_autoincrement(stmt, i);
 		/*
 		 * Can not fail, since all column names and types
 		 * are preallocated during prepare phase and the
@@ -326,13 +332,15 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
 		 */
 		assert(name != NULL);
 		assert(type != NULL);
-		size = metadata_map_sizeof(name, type, coll, nullable);
+		size = metadata_map_sizeof(name, type, coll, nullable,
+					   is_autoincrement);
 		char *pos = (char *) obuf_alloc(out, size);
 		if (pos == NULL) {
 			diag_set(OutOfMemory, size, "obuf_alloc", "pos");
 			return -1;
 		}
-		uint32_t map_sz = 2 + (coll != NULL) + (nullable != -1);
+		uint32_t map_sz = 2 + (coll != NULL) + (nullable != -1) +
+				  is_autoincrement;
 		pos = mp_encode_map(pos, map_sz);
 		pos = mp_encode_uint(pos, IPROTO_FIELD_NAME);
 		pos = mp_encode_str(pos, name, strlen(name));
@@ -346,6 +354,10 @@ sql_get_metadata(struct sql_stmt *stmt, struct obuf *out, int column_count)
 			pos = mp_encode_uint(pos, IPROTO_FIELD_NULLABLE);
 			pos = mp_encode_bool(pos, nullable);
 		}
+		if (is_autoincrement) {
+			pos = mp_encode_uint(pos, IPROTO_FIELD_AUTOINCREMENT);
+			pos = mp_encode_bool(pos, true);
+		}
 	}
 	return 0;
 }
diff --git a/src/box/iproto_constants.h b/src/box/iproto_constants.h
index 030c25531..4d43583b0 100644
--- a/src/box/iproto_constants.h
+++ b/src/box/iproto_constants.h
@@ -132,7 +132,8 @@ enum iproto_metadata_key {
 	IPROTO_FIELD_NAME = 0,
 	IPROTO_FIELD_TYPE = 1,
 	IPROTO_FIELD_COLL = 2,
-	IPROTO_FIELD_NULLABLE = 3
+	IPROTO_FIELD_NULLABLE = 3,
+	IPROTO_FIELD_AUTOINCREMENT = 4,
 };
 
 enum iproto_ballot_key {
diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c
index 3d7ca710c..d7dd9432f 100644
--- a/src/box/lua/execute.c
+++ b/src/box/lua/execute.c
@@ -25,6 +25,7 @@ lua_sql_get_metadata(struct sql_stmt *stmt, struct lua_State *L,
 		const char *name = sql_column_name(stmt, i);
 		const char *type = sql_column_datatype(stmt, i);
 		int nullable = sql_column_is_nullable(stmt, i);
+		bool is_autoincrement = sql_column_is_autoincrement(stmt, i);
 		/*
 		 * Can not fail, since all column names are
 		 * preallocated during prepare phase and the
@@ -44,6 +45,10 @@ lua_sql_get_metadata(struct sql_stmt *stmt, struct lua_State *L,
 			lua_pushboolean(L, nullable);
 			lua_setfield(L, -2, "is_nullable");
 		}
+		if (is_autoincrement) {
+			lua_pushboolean(L, true);
+			lua_setfield(L, -2, "autoincrement");
+		}
 		lua_rawseti(L, -2, i + 1);
 	}
 }
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 3e93cbc75..3be644785 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -651,11 +651,15 @@ decode_metadata_optional(struct lua_State *L, const char **data,
 			const char *coll = mp_decode_str(data, &len);
 			lua_pushlstring(L, coll, len);
 			lua_setfield(L, -2, "collation");
-		} else {
-			assert(key == IPROTO_FIELD_NULLABLE);
+		} else if (key == IPROTO_FIELD_NULLABLE) {
 			bool is_nullable = mp_decode_bool(data);
 			lua_pushboolean(L, is_nullable);
 			lua_setfield(L, -2, "is_nullable");
+		} else {
+			assert(key == IPROTO_FIELD_AUTOINCREMENT);
+			bool autoincrement = mp_decode_bool(data);
+			lua_pushboolean(L, autoincrement);
+			lua_setfield(L, -2, "autoincrement");
 		}
 	}
 }
@@ -672,7 +676,7 @@ netbox_decode_metadata(struct lua_State *L, const char **data)
 	lua_createtable(L, count, 0);
 	for (uint32_t i = 0; i < count; ++i) {
 		uint32_t map_size = mp_decode_map(data);
-		assert(map_size >= 2 && map_size <= 4);
+		assert(map_size >= 2 && map_size <= 5);
 		(void) map_size;
 		uint32_t key = mp_decode_uint(data);
 		assert(key == IPROTO_FIELD_NAME);
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index b772bcead..f40178194 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -1841,6 +1841,12 @@ generate_column_metadata(struct Parse *pParse, struct SrcList *pTabList,
 			if (p->op == TK_COLUMN)
 				vdbe_set_metadata_col_nullability(v, i,
 								  is_nullable);
+			if (pTabList->a[j].space->sequence != NULL) {
+				int afno =
+					pTabList->a[j].space->sequence_fieldno;
+				if (afno == iCol)
+					vdbe_set_metadata_col_autoincrement(v, i);
+			}
 		} else {
 			const char *z = pEList->a[i].zSpan;
 			if (z == NULL)
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 89920b3d1..590791648 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -582,6 +582,9 @@ sql_column_coll(sql_stmt *stmt, int n);
 int
 sql_column_is_nullable(sql_stmt *stmt, int n);
 
+bool
+sql_column_is_autoincrement(sql_stmt *stmt, int n);
+
 int
 sql_initialize(void);
 
diff --git a/src/box/sql/vdbe.h b/src/box/sql/vdbe.h
index 0f315b660..4e1a67416 100644
--- a/src/box/sql/vdbe.h
+++ b/src/box/sql/vdbe.h
@@ -260,6 +260,9 @@ vdbe_set_metadata_col_collation(struct Vdbe *p, int idx, const char *coll,
 void
 vdbe_set_metadata_col_nullability(struct Vdbe *p, int idx, int nullable);
 
+void
+vdbe_set_metadata_col_autoincrement(struct Vdbe *p, int idx);
+
 void sqlVdbeCountChanges(Vdbe *);
 sql *sqlVdbeDb(Vdbe *);
 void sqlVdbeSetSql(Vdbe *, const char *z, int n, int);
diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h
index 0e54e42a5..63afb8777 100644
--- a/src/box/sql/vdbeInt.h
+++ b/src/box/sql/vdbeInt.h
@@ -355,6 +355,8 @@ struct sql_column_metadata {
 	 * columns: all other expressions are nullable by default.
 	 */
 	int8_t nullable : 2;
+	/** True if column features autoincrement property. */
+	int8_t actoincrement : 1;
 };
 
 /*
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index ea8c7c438..ece803262 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -761,6 +761,14 @@ sql_column_is_nullable(sql_stmt *stmt, int n)
 	return p->metadata[n].nullable;
 }
 
+bool
+sql_column_is_autoincrement(sql_stmt *stmt, int n)
+{
+	struct Vdbe *p = (struct Vdbe *) stmt;
+	assert(n < sql_column_count(stmt) && n >= 0);
+	return p->metadata[n].actoincrement;
+}
+
 /******************************* sql_bind_  **************************
  *
  * Routines used to attach values to wildcards in a compiled SQL statement.
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index 6c3523ba4..34a0c1267 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1911,6 +1911,13 @@ vdbe_set_metadata_col_nullability(struct Vdbe *p, int idx, int nullable)
 	p->metadata[idx].nullable = nullable;
 }
 
+void
+vdbe_set_metadata_col_autoincrement(struct Vdbe *p, int idx)
+{
+	assert(idx < p->nResColumn);
+	p->metadata[idx].actoincrement = 1;
+}
+
 /*
  * This routine checks that the sql.nVdbeActive count variable
  * matches the number of vdbe's in the list sql.pVdbe that are
-- 
2.15.1

  parent reply	other threads:[~2019-11-27 12:15 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-27 12:15 [Tarantool-patches] [PATCH 0/6] sql: extend response metadata Nikita Pettik
2019-11-27 12:15 ` [Tarantool-patches] [PATCH 1/6] sql: refactor resulting set metadata Nikita Pettik
2019-11-28 22:41   ` Vladislav Shpilevoy
2019-12-05 11:39     ` Nikita Pettik
2019-12-05 23:58       ` Vladislav Shpilevoy
2019-12-06 12:48         ` Nikita Pettik
2019-12-17 13:23   ` Sergey Ostanevich
2019-11-27 12:15 ` [Tarantool-patches] [PATCH 2/6] sql: fix possible null dereference in sql_expr_coll() Nikita Pettik
2019-11-28 22:42   ` Vladislav Shpilevoy
2019-12-05 11:40     ` Nikita Pettik
2019-12-05 23:59       ` Vladislav Shpilevoy
2019-12-06 12:48         ` Nikita Pettik
2019-12-17 13:30           ` Sergey Ostanevich
2019-12-17 14:44             ` Nikita Pettik
2019-12-17 19:53               ` Nikita Pettik
2019-11-27 12:15 ` [Tarantool-patches] [PATCH 3/6] sql: extend result set with collation Nikita Pettik
2019-11-28 22:41   ` Vladislav Shpilevoy
2019-12-05 11:50     ` Nikita Pettik
2019-12-18 11:08   ` Sergey Ostanevich
2019-12-24  0:44     ` Nikita Pettik
2019-11-27 12:15 ` [Tarantool-patches] [PATCH 4/6] sql: extend result set with nullability Nikita Pettik
2019-11-28 22:41   ` Vladislav Shpilevoy
2019-12-05 11:50     ` Nikita Pettik
2019-12-06  0:00       ` Vladislav Shpilevoy
2019-12-06 12:49         ` Nikita Pettik
2019-12-18 13:31   ` Sergey Ostanevich
2019-11-27 12:15 ` Nikita Pettik [this message]
2019-11-28 22:41   ` [Tarantool-patches] [PATCH 5/6] sql: extend result set with autoincrement Vladislav Shpilevoy
2019-12-05 11:51     ` Nikita Pettik
2019-12-18 15:17   ` Sergey Ostanevich
2019-12-24  0:47     ` Nikita Pettik
2019-11-27 12:15 ` [Tarantool-patches] [PATCH 6/6] sql: extend result set with alias Nikita Pettik
2019-11-28 22:41   ` Vladislav Shpilevoy
2019-12-05 11:51     ` Nikita Pettik
2019-12-06  0:02       ` Vladislav Shpilevoy
2019-12-06 12:50         ` Nikita Pettik
2019-12-06 21:52           ` Vladislav Shpilevoy
2019-12-19 15:17   ` Sergey Ostanevich
2019-12-24  0:27     ` Nikita Pettik
2019-11-28 22:55 ` [Tarantool-patches] [PATCH 0/6] sql: extend response metadata 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=659c12232934c069ae8d71ed1e2bc8115d36f6e0.1574846892.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 5/6] sql: extend result set with autoincrement' \
    /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