Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 02/10] sql: remove string of fields collation from Table
Date: Sun, 12 Aug 2018 17:12:58 +0300	[thread overview]
Message-ID: <f5dc43f4f2c2a5fd33fa227660ac924740a29def.1534001739.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1534001739.git.korablev@tarantool.org>
In-Reply-To: <cover.1534001739.git.korablev@tarantool.org>

Part of #3561
---
 src/box/sql/build.c     |  1 -
 src/box/sql/insert.c    | 71 +++++++++++--------------------------------------
 src/box/sql/sqliteInt.h | 14 +++++++---
 src/box/sql/update.c    |  2 +-
 4 files changed, 28 insertions(+), 60 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index f3546b794..48ced1766 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -344,7 +344,6 @@ deleteTable(sqlite3 * db, Table * pTable)
 	/* Delete the Table structure itself.
 	 */
 	sqlite3HashClear(&pTable->idxHash);
-	sqlite3DbFree(db, pTable->zColAff);
 	assert(pTable->def != NULL);
 	/* Do not delete pTable->def allocated on region. */
 	if (!pTable->def->opts.is_temporary)
diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index 13cb61ecf..50c30fd82 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -124,59 +124,21 @@ sql_index_affinity_str(struct sqlite3 *db, struct index_def *def)
 	return aff;
 }
 
-/*
- * Compute the affinity string for table pTab, if it has not already been
- * computed.  As an optimization, omit trailing AFFINITY_BLOB affinities.
- *
- * If the affinity exists (if it is no entirely AFFINITY_BLOB values) and
- * if iReg>0 then code an OP_Affinity opcode that will set the affinities
- * for register iReg and following.  Or if affinities exists and iReg==0,
- * then just set the P4 operand of the previous opcode (which should  be
- * an OP_MakeRecord) to the affinity string.
- *
- * A column affinity string has one character per column:
- *
- *  Character      Column affinity
- *  ------------------------------
- *  'A'            BLOB
- *  'B'            TEXT
- *  'C'            NUMERIC
- *  'D'            INTEGER
- *  'E'            REAL
- */
 void
-sqlite3TableAffinity(Vdbe * v, Table * pTab, int iReg)
+sql_emit_table_affinity(struct Vdbe *v, struct space_def *def, int reg)
 {
-	int i;
-	char *zColAff = pTab->zColAff;
-	if (zColAff == 0) {
-		sqlite3 *db = sqlite3VdbeDb(v);
-		zColAff =
-			(char *)sqlite3DbMallocRaw(0,
-						   pTab->def->field_count + 1);
-		if (!zColAff) {
-			sqlite3OomFault(db);
-			return;
-		}
-
-		for (i = 0; i < (int)pTab->def->field_count; i++) {
-			char affinity = pTab->def->fields[i].affinity;
-			zColAff[i] = affinity;
-		}
-		do {
-			zColAff[i--] = 0;
-		} while (i >= 0 && zColAff[i] == AFFINITY_BLOB);
-		pTab->zColAff = zColAff;
-	}
-	i = sqlite3Strlen30(zColAff);
-	if (i) {
-		if (iReg) {
-			sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff,
-					  i);
-		} else {
-			sqlite3VdbeChangeP4(v, -1, zColAff, i);
-		}
+	assert(reg > 0);
+	sqlite3 *db = sqlite3VdbeDb(v);
+	uint32_t field_count = def->field_count;
+	char *colls_aff = (char *)sqlite3DbMallocZero(db, field_count + 1);
+	if (colls_aff == NULL) {
+		sqlite3OomFault(db);
+		return;
 	}
+	for (uint32_t i = 0; i < field_count; ++i)
+		colls_aff[i] = def->fields[i].affinity;
+	sqlite3VdbeAddOp4(v, OP_Affinity, reg, field_count, 0, colls_aff,
+			  P4_DYNAMIC);
 }
 
 /**
@@ -704,9 +666,8 @@ sqlite3Insert(Parse * pParse,	/* Parser context */
 		 * If this is a real table, attempt conversions as required by the
 		 * table column affinities.
 		 */
-		if (!is_view) {
-			sqlite3TableAffinity(v, pTab, regCols + 1);
-		}
+		if (!is_view)
+			sql_emit_table_affinity(v, pTab->def, regCols + 1);
 
 		/* Fire BEFORE or INSTEAD OF triggers */
 		vdbe_code_row_trigger(pParse, trigger, TK_INSERT, 0,
@@ -1188,7 +1149,7 @@ sqlite3GenerateConstraintChecks(Parse * pParse,		/* The parser context */
 		if (aRegIdx[ix] == 0)
 			continue;	/* Skip indices that do not change */
 		if (bAffinityDone == 0) {
-			sqlite3TableAffinity(v, pTab, regNewData+1);
+			sql_emit_table_affinity(v, pTab->def, regNewData + 1);
 			bAffinityDone = 1;
 		}
 		iThisCur = iIdxCur + ix;
@@ -1239,7 +1200,7 @@ sqlite3GenerateConstraintChecks(Parse * pParse,		/* The parser context */
 					pIdx->def->key_def->parts[0].fieldno;
 				reg_pk = regNewData + 1 + fieldno;
 
-				if (pTab->zColAff[fieldno] ==
+				if (pTab->def->fields[fieldno].affinity ==
 				    AFFINITY_INTEGER) {
 					int skip_if_null = sqlite3VdbeMakeLabel(v);
 					if ((pTab->tabFlags & TF_Autoincrement) != 0) {
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index ddedfbcb4..9c861518f 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -1874,8 +1874,6 @@ struct Savepoint {
  */
 struct Table {
 	Index *pIndex;		/* List of SQL indexes on this table. */
-	char *zColAff;		/* String defining the affinity of each column */
-	/*   ... also used as column name list in a VIEW */
 	Hash idxHash;		/* All (named) indices indexed by name */
 	u32 nTabRef;		/* Number of pointers to this Table */
 	i16 iAutoIncPKey;	/* If PK is marked INTEGER PRIMARY KEY AUTOINCREMENT, store
@@ -4318,7 +4316,17 @@ const char *sqlite3IndexAffinityStr(sqlite3 *, Index *);
 char *
 sql_index_affinity_str(struct sqlite3 *db, struct index_def *def);
 
-void sqlite3TableAffinity(Vdbe *, Table *, int);
+/**
+ * Code an OP_Affinity opcode that will set affinities
+ * for given range of register starting from @reg.
+ *
+ * @param v VDBE.
+ * @param def Definition of table to be used.
+ * @param reg Register where affinities will be placed.
+ */
+void
+sql_emit_table_affinity(struct Vdbe *v, struct space_def *def, int reg);
+
 char sqlite3CompareAffinity(Expr * pExpr, char aff2);
 int sqlite3IndexAffinityOk(Expr * pExpr, char idx_affinity);
 
diff --git a/src/box/sql/update.c b/src/box/sql/update.c
index 9e9fa3a79..e2324c354 100644
--- a/src/box/sql/update.c
+++ b/src/box/sql/update.c
@@ -494,7 +494,7 @@ sqlite3Update(Parse * pParse,		/* The parser context */
 	 * verified. One could argue that this is wrong.
 	 */
 	if (tmask & TRIGGER_BEFORE) {
-		sqlite3TableAffinity(v, pTab, regNew);
+		sql_emit_table_affinity(v, pTab->def, regNew);
 		vdbe_code_row_trigger(pParse, trigger, TK_UPDATE, pChanges,
 				      TRIGGER_BEFORE, pTab, regOldPk,
 				      on_error, labelContinue);
-- 
2.15.1

  parent reply	other threads:[~2018-08-12 14:13 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-12 14:12 [tarantool-patches] [PATCH 00/10] sql: cleanup in struct Index and struct Table Nikita Pettik
2018-08-12 14:12 ` [tarantool-patches] [PATCH 01/10] sql: remove suport of ALTER TABLE ADD COLUMN Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-12 14:12 ` Nikita Pettik [this message]
2018-08-13 20:24   ` [tarantool-patches] Re: [PATCH 02/10] sql: remove string of fields collation from Table Vladislav Shpilevoy
2018-08-12 14:12 ` [tarantool-patches] [PATCH 03/10] sql: remove index hash from struct Table Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-12 14:13 ` [tarantool-patches] [PATCH 04/10] sql: remove flags " Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-12 14:13 ` [tarantool-patches] [PATCH 05/10] sql: remove affinity string of columns from Index Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-21 16:31     ` n.pettik
2018-08-24 21:04       ` Vladislav Shpilevoy
2018-08-26 19:45         ` n.pettik
2018-08-12 14:13 ` [tarantool-patches] [PATCH 06/10] sql: completely remove support of partial indexes Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-21 16:31     ` n.pettik
2018-08-24 21:04       ` Vladislav Shpilevoy
2018-08-26 19:44         ` n.pettik
2018-08-12 14:13 ` [tarantool-patches] [PATCH 07/10] sql: remove index type from struct Index Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-21 16:31     ` n.pettik
2018-08-12 14:13 ` [tarantool-patches] [PATCH 08/10] sql: use secondary indexes to process OP_Delete Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-12 14:13 ` [tarantool-patches] [PATCH 09/10] sql: disable ON CONFLICT actions for indexes Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-21 16:31     ` n.pettik
2018-08-24 21:04       ` Vladislav Shpilevoy
2018-08-26 19:44         ` n.pettik
2018-08-27 17:24           ` Vladislav Shpilevoy
2018-08-12 14:13 ` [tarantool-patches] [PATCH 10/10] sql: move autoincrement field number to server Nikita Pettik
2018-08-13 20:24   ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-21 16:31     ` n.pettik
2018-08-24 21:03       ` Vladislav Shpilevoy
2018-08-26 19:44         ` n.pettik
2018-08-27 17:24           ` Vladislav Shpilevoy
2018-08-27 17:24 ` [tarantool-patches] Re: [PATCH 00/10] sql: cleanup in struct Index and struct Table Vladislav Shpilevoy
2018-08-29 14:11 ` 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=f5dc43f4f2c2a5fd33fa227660ac924740a29def.1534001739.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH 02/10] sql: remove string of fields collation from Table' \
    /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