Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v2 1/1] sql: remove ephemeral Expr from AddDefaultValue
@ 2018-04-17 15:01 Kirill Shcherbatov
  2018-04-17 18:45 ` [tarantool-patches] " Vladislav Shpilevoy
  2018-04-19 12:47 ` [tarantool-patches] [PATCH v2 1/1] sql: remove zName and nColumn from SQL Kirill Shcherbatov
  0 siblings, 2 replies; 8+ messages in thread
From: Kirill Shcherbatov @ 2018-04-17 15:01 UTC (permalink / raw)
  To: tarantool-patches; +Cc: v.shpilevoy, Kirill Shcherbatov

As we don't need Expr tree in DDL we could store only
string representation that's better to understand.

Needed for #3051.
---
 src/box/sql.c       | 10 ++++------
 src/box/sql/build.c | 32 ++++++++++++++------------------
 2 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/src/box/sql.c b/src/box/sql.c
index 6bc82c2..4df9eeb 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -1467,7 +1467,7 @@ int tarantoolSqlite3MakeTableFormat(Table *pTable, void *buf)
 		const char *t;
 		struct coll *coll = NULL;
 		struct field_def *field = &pTable->def->fields[i];
-		struct Expr *def = field->default_value_expr;
+		const char *zToken = field->default_value;
 		if (aCol[i].zColl != NULL &&
 		    strcasecmp(aCol[i].zColl, "binary") != 0) {
 			coll = sqlite3FindCollSeq(aCol[i].zColl);
@@ -1475,7 +1475,7 @@ int tarantoolSqlite3MakeTableFormat(Table *pTable, void *buf)
 		int base_len = 4;
 		if (coll != NULL)
 			base_len += 1;
-		if (def != NULL)
+		if (zToken != NULL)
 			base_len += 1;
 		p = enc->encode_map(p, base_len);
 		p = enc->encode_str(p, "name", 4);
@@ -1499,11 +1499,9 @@ int tarantoolSqlite3MakeTableFormat(Table *pTable, void *buf)
 			p = enc->encode_str(p, "collation", strlen("collation"));
 			p = enc->encode_uint(p, coll->id);
 		}
-		if (def != NULL) {
-		        assert((def->flags & EP_IntValue) == 0);
-			assert(def->u.zToken != NULL);
+		if (zToken != NULL) {
 			p = enc->encode_str(p, "default", strlen("default"));
-			p = enc->encode_str(p, def->u.zToken, strlen(def->u.zToken));
+			p = enc->encode_str(p, zToken, strlen(zToken));
 		}
 	}
 	return (int)(p - base);
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 394ee3a..4406955 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -399,6 +399,8 @@ deleteTable(sqlite3 * db, Table * pTable)
 	if (pTable->def) {
 		/* fields has been allocated on separate region */
 		struct field_def *fields = pTable->def->fields;
+		for (uint32_t i = 0; i < pTable->def->field_count; ++i)
+			sqlite3DbFree(sql_get(), fields[i].default_value);
 		space_def_delete(pTable->def);
 		sqlite3DbFree(db, fields);
 	}
@@ -889,27 +891,21 @@ sqlite3AddDefaultValue(Parse * pParse, ExprSpan * pSpan)
 					"default value of column [%s] is not constant",
 					pCol->zName);
 		} else {
-			/* A copy of pExpr is used instead of the original, as pExpr contains
-			 * tokens that point to volatile memory. The 'span' of the expression
-			 * is required by pragma table_info.
-			 */
-			Expr x;
 			assert(p->def);
 			struct field_def *field =
 				&p->def->fields[p->nCol - 1];
-			sql_expr_free(db, field->default_value_expr, false);
-
-			memset(&x, 0, sizeof(x));
-			x.op = TK_SPAN;
-			x.u.zToken = sqlite3DbStrNDup(db, (char *)pSpan->zStart,
-						      (int)(pSpan->zEnd -
-							    pSpan->zStart));
-			x.pLeft = pSpan->pExpr;
-			x.flags = EP_Skip;
-
-			field->default_value_expr =
-				sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
-			sqlite3DbFree(db, x.u.zToken);
+
+			sqlite3DbFree(db, field->default_value);
+			field->default_value =
+				sqlite3DbStrNDup(db, (char *)pSpan->zStart,
+						 (int)(pSpan->zEnd -
+						       pSpan->zStart));
+			if (field->default_value == NULL) {
+				assert(db->mallocFailed);
+				pParse->rc = SQLITE_NOMEM_BKPT;
+				pParse->nErr++;
+				return;
+			}
 		}
 	}
 	sql_expr_free(db, pSpan->pExpr, false);
-- 
2.7.4

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-04-23 10:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-17 15:01 [tarantool-patches] [PATCH v2 1/1] sql: remove ephemeral Expr from AddDefaultValue Kirill Shcherbatov
2018-04-17 18:45 ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-19 12:47 ` [tarantool-patches] [PATCH v2 1/1] sql: remove zName and nColumn from SQL Kirill Shcherbatov
2018-04-19 15:25   ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-23 10:20     ` [tarantool-patches] [PATCH v2 0/3] " Kirill Shcherbatov
2018-04-23 10:20       ` [tarantool-patches] [PATCH v2 1/3] sql: Fixed disgusting code format in sqlite3Pragma Kirill Shcherbatov
2018-04-23 10:20       ` [tarantool-patches] [PATCH v2 2/3] sql: remove zName and nColumn from SQL Kirill Shcherbatov
2018-04-23 10:20       ` [tarantool-patches] [PATCH v2 3/3] sql: removed type field in server Kirill Shcherbatov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox