<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <br>
    Thank you for the review! Sorry for all these code style errors.<br>
    I consider review changes minor, so I put diff b/w first and<br>
    second version of patch at the end of the letter.<br>
    (second commit on the branch 'review fixes' would be squashed)<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">Completely removes struct Table. Also the
patch simplifies memory management as in
many cases struct space (which replaces
struct Table) is allocated on region
and shouldn't be explicitly freed.

</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">Feel free to use up to 72 chars in commit message body.

sql: remove struct Table

Completely removes struct Table. Also the patch simplifies memory
management as in many cases struct space (which replaces struct Table)
is allocated on region and shouldn't be explicitly freed.

Note that after commit subject's prefix we use lower-case.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

Also, for some reason content of this letter looks very strange:
at least it contains unusual fonts… I am not complaining but
are you sure that you set up git-email correctly?

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">Closes #3235
---
Branch: 
<a class="moz-txt-link-freetext" href="https://github.com/tarantool/tarantool/tree/sudobobo/gh-3235-repl-Table-w-space">https://github.com/tarantool/tarantool/tree/sudobobo/gh-3235-repl-Table-w-space</a>

Issue: 
<a class="moz-txt-link-freetext" href="https://github.com/tarantool/tarantool/issues/3235">https://github.com/tarantool/tarantool/issues/3235</a>


 src/box/sql.c              |  64 +++-----
 src/box/sql.h              |  25 +--
 src/box/sql/analyze.c      |   8 +-
 src/box/sql/build.c        | 390 +++++++++++++++++++++------------------------
 src/box/sql/delete.c       |  75 ++++-----
 src/box/sql/expr.c         |  28 ++--
 src/box/sql/fkey.c         |  67 ++++----
 src/box/sql/insert.c       |  97 ++++++-----
 src/box/sql/resolve.c      |  47 +++---
 src/box/sql/select.c       | 297 ++++++++++++++++------------------
 src/box/sql/sqliteInt.h    | 129 +++++++--------
 src/box/sql/tarantoolInt.h |   8 +-
 src/box/sql/tokenize.c     |  10 +-
 src/box/sql/treeview.c     |   4 +-
 src/box/sql/trigger.c      |  36 ++---
 src/box/sql/update.c       |  52 +++---
 src/box/sql/where.c        |  59 ++++---
 src/box/sql/wherecode.c    |   8 +-
 src/box/sql/whereexpr.c    |  13 +-
 19 files changed, 635 insertions(+), 782 deletions(-)

diff --git a/src/box/sql.c b/src/box/sql.c
index 387da7b3d..978ad8310 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -972,7 +972,7 @@ cursor_advance(BtCursor *pCur, int *pRes)
  */
 
 char *
-sql_encode_table(struct region *region, struct Table *table, uint32_t *size)
+sql_encode_table(struct region *region, struct space *space, uint32_t *size)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
In this function we use only space_def, so lets pass
directly space_def instead of whole space.</pre>
    </blockquote>
    Ok, fixed here and in other similar places.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -1033,7 +1033,7 @@ sql_encode_table(struct region *region, struct Table *table, uint32_t *size)
 }
 
 char *
-sql_encode_table_opts(struct region *region, struct Table *table,
+sql_encode_table_opts(struct region *region, struct space *space,
                      const char *sql, uint32_t *size)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
The same is here.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -1258,49 +1258,26 @@ sql_ephemeral_space_def_new(struct Parse *parser, const char *name)
        return def;
 }
 
-Table *
-sql_ephemeral_table_new(Parse *parser, const char *name)
+struct space *
+sql_ephemeral_space_new(Parse *parser, const char *name)
 {
-       sqlite3 *db = parser->db;
-       struct space_def *def = NULL;
-       Table *table = sqlite3DbMallocZero(db, sizeof(Table));
-       if (table != NULL)
-               def = sql_ephemeral_space_def_new(parser, name);
-       if (def == NULL) {
-               sqlite3DbFree(db, table);
-               return NULL;
-       }
-       table->space = (struct space *) region_alloc(&parser->region,
-                                                    sizeof(struct space));
-       if (table->space == NULL) {
+       struct space *space =
+               (struct space *) region_alloc(&parser->region,
+                                             sizeof(struct space));
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Smth wrong with indentation here: we use as much tabs as we can
and fill the rest with spaces.

NIt: consider this variant:

diff --git a/src/box/sql.c b/src/box/sql.c
index 978ad8310..933dfe059 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -1261,22 +1261,18 @@ sql_ephemeral_space_def_new(struct Parse *parser, const char *name)
 struct space *
 sql_ephemeral_space_new(Parse *parser, const char *name)
 {
-       struct space *space =
-               (struct space *) region_alloc(&parser->region,
-                                             sizeof(struct space));
+       size_t sz = sizeof(struct space);
+       struct space *space = (struct space *) region_alloc(&parser->region, sz);
        if (space == NULL) {
-               diag_set(OutOfMemory, sizeof(struct space), "region", "space");
+               diag_set(OutOfMemory, sz, "region", "space");
                parser->rc = SQL_TARANTOOL_ERROR;
                parser->nErr++;
                return NULL;
        }
-
-       memset(space, 0, sizeof(struct space));
+       memset(space, 0, sz);
        space->def = sql_ephemeral_space_def_new(parser, name);
-       if (space->def == NULL) {
+       if (space->def == NULL)
                return NULL;
-       }
-
        return space;
</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+  if (space == NULL) {
                diag_set(OutOfMemory, sizeof(struct space), "region", "space");
                parser->rc = SQL_TARANTOOL_ERROR;
                parser->nErr++;
-               sqlite3DbFree(db, table);
                return NULL;
        }
-       memset(table->space, 0, sizeof(struct space));
-       table->def = def;
-       return table;
-}
 
-int
-sql_table_def_rebuild(struct sqlite3 *db, struct Table *pTable)
-{
-       struct space_def *old_def = pTable->def;
-       struct space_def *new_def = NULL;
-       new_def = space_def_new(old_def->id, old_def->uid,
-                               old_def->field_count, old_def->name,
-                               strlen(old_def->name), old_def->engine_name,
-                               strlen(old_def->engine_name), &old_def->opts,
-                               old_def->fields, old_def->field_count);
-       if (new_def == NULL) {
-               sqlite3OomFault(db);
-               return -1;
+       memset(space, 0, sizeof(struct space));
+       space->def = sql_ephemeral_space_def_new(parser, name);
+       if (space->def == NULL) {
+               return NULL;
        }
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
We don’t wrap one-line if body in brackets.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -1357,11 +1334,10 @@ sql_checks_resolve_space_def_reference(ExprList *expr_list,
        sql_parser_create(&parser, sql_get());
        parser.parse_only = true;
 
-       Table dummy_table;
-       memset(&dummy_table, 0, sizeof(dummy_table));
-       dummy_table.def = def;
+       struct space dummy_space;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Objects created on stack are not supposed to be zeroed.
This time we get lucky and it is filled with 0,
but generally speaking - it is not true. You can use memset.
Another way of initialisation with zeroes is:

struct space dummy_space = { 0 };

I guess both memset and {0} are represented in the
same way in terms of machine codes, so choose any option.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+  dummy_space.def = def;
 
-       sql_resolve_self_reference(&parser, &dummy_table, NC_IsCheck, NULL,
+       sql_resolve_self_reference(&parser, &dummy_space, NC_IsCheck, NULL,
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
In both cases where this function appears you pass
space allocated on stack. Hence, you can pass here
space_def and inside this function wrap it in dummy
space.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">diff --git a/src/box/sql.h b/src/box/sql.h
index 028a15245..0d9ecd0ce 100644
--- a/src/box/sql.h
+++ b/src/box/sql.h
@@ -225,36 +225,25 @@ void
 sql_expr_delete(struct sqlite3 *db, struct Expr *expr, bool extern_alloc);
 
 /**
- * Create and initialize a new ephemeral SQL Table object.
+ * Create and initialize a new ephemeral space object.
  * @param parser SQL Parser object.
- * @param name Table to create name.
+ * @param name Space to create name.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Rephrase comment pls, can’t parse.
Guess it should be “Name of space to be created”.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
 
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> /**
  * Create and initialize a new ephemeral space_def object.
  * @param parser SQL Parser object.
- * @param name Table to create name.
+ * @param name Space to create name.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
The same (ofc it is initially not your comment but anyway).</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">  * @retval NULL on memory allocation error, Parser state changed.
  * @retval not NULL on success.
  */
 struct space_def *
 sql_ephemeral_space_def_new(struct Parse *parser, const char *name);
 
 void
-sqlite3DeleteTable(sqlite3 * db, Table * pTable)
+sql_space_delete(struct sqlite3 *db, struct space *space)
 {
-       /* Do not delete the table until the reference count reaches zero. */
-       if (!pTable)
-               return;
-       if (((!db || db->pnBytesFreed == 0) && (--pTable->nTabRef) > 0))
+       if (!space || !db || db->pnBytesFreed == 0)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
We use explicit != NULL comparison.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">           return;
-       table_delete(db, pTable);
+
+       if (space->def->opts.is_temporary) {
+               for (uint32_t i = 0; i < space->index_count; ++i)
+                       index_def_delete(space->index[i]->def);
+               /**
+                * Do not delete space and space->def allocated
+                * on region.
+                */
+               sql_expr_list_delete(db, space->def->opts.checks);
+       }
 }
 
 /*
@@ -343,15 +310,15 @@ sqlite3CheckIdentifierName(Parse *pParse, char *zName)
 }
 
 struct index *
-sql_table_primary_key(const struct Table *tab)
+sql_table_primary_key(const struct space *space)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Its not longer table.</pre>
    </blockquote>
    Naming is fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> /**
- * Create and initialize a new SQL Table object.
+ * Create and initialize a new SQL space object.
  * All memory except table object itself is allocated on region.
  * @param parser SQL Parser object.
  * @param name Table to create name.
@@ -359,18 +326,17 @@ sql_table_primary_key(const struct Table *tab)
  *         changed.
  * @retval not NULL on success.
  */
-static Table *
+static struct space *
 sql_table_new(Parse *parser, char *name)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Inline this function, it seems to be too insignificant.</pre>
    </blockquote>
    Ok, done.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> {
-       struct Table *table = sql_ephemeral_table_new(parser, name);
-       if (table == NULL)
+       struct space *space = sql_ephemeral_space_new(parser, name);
+       if (space == NULL)
                return NULL;
 
-       strcpy(table->def->engine_name,
+       strcpy(space->def->engine_name,
               sql_storage_engine_strs[current_session()->sql_default_engine]);
 
-       table->nTabRef = 1;
-       return table;
+       return space;
 }
 
 /*
@@ -396,7 +362,6 @@ sql_table_new(Parse *parser, char *name)
 void
 sqlite3StartTable(Parse *pParse, Token *pName, int noErr)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Update comment to this function: there is no pNewTable anymore.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> {
-       Table *pTable;
        char *zName = 0;        /* The name of the new table */
        sqlite3 *db = pParse->db;
        struct Vdbe *v = sqlite3GetVdbe(pParse);
@@ -423,12 +388,12 @@ sqlite3StartTable(Parse *pParse, Token *pName, int noErr)
                goto cleanup;
        }
 
-       pTable = sql_table_new(pParse, zName);
-       if (pTable == NULL)
+       struct space *new_space = sql_table_new(pParse, zName);
+       if (new_space == NULL)
                goto cleanup;
 
-       assert(pParse->pNewTable == 0);
-       pParse->pNewTable = pTable;
+       assert(pParse->new_space == 0);
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
== NULL</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">
@@ -513,27 +478,28 @@ void
 sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
 {
        assert(type_def != NULL);
-       Table *p;
        int i;
        char *z;
        sqlite3 *db = pParse->db;
-       if ((p = pParse->pNewTable) == 0)
+       if ((pParse->new_space) == 0)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
== NULL

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">           return;
+       struct space *space = pParse->new_space;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
You use only def in this function, so fetch space_def
instead of whole space.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+
 #if SQLITE_MAX_COLUMN
-       if ((int)p->def->field_count + 1 > db->aLimit[SQLITE_LIMIT_COLUMN]) {
+       if ((int)space->def->field_count + 1 > db->aLimit[SQLITE_LIMIT_COLUMN]) {
                sqlite3ErrorMsg(pParse, "too many columns on %s",
-                               p->def->name);
+                               space->def->name);
                return;
        }
 #endif
-       /*
+       /**
         * As sql_field_retrieve will allocate memory on region
-        * ensure that p->def is also temporal and would be rebuilded or
+        * ensure that p->space->def is also temporal and would be rebuilded or
         * dropped.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
You have removed rebuild function, so I guess there is no opportunities
to rebuild anymore.</pre>
    </blockquote>
    Yes, it's true. Fixed the comment.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">    */
-       assert(p->def->opts.is_temporary);
-       if (sql_field_retrieve(pParse, p,
-                              (uint32_t) p->def->field_count) == NULL)
+       assert(space->def->opts.is_temporary);
+       if (sql_field_retrieve(pParse, space->def,
+                             (uint32_t) space->def->field_count) == NULL)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
field_count is already of type uint32_t.</pre>
    </blockquote>
    Removed type cast.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">           return;
        struct region *region = &pParse->region;
        z = region_alloc(region, pName->n + 1);
@@ -547,13 +513,14 @@ sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
        memcpy(z, pName->z, pName->n);
        z[pName->n] = 0;
        sqlite3NormalizeName(z);
-       for (i = 0; i < (int)p->def->field_count; i++) {
-               if (strcmp(z, p->def->fields[i].name) == 0) {
+       for (i = 0; i < (int)space->def->field_count; i++) {
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
for (uint32_t i = 0; space->def->field_count; ...)</pre>
    </blockquote>
    Ok, fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+          if (strcmp(z, space->def->fields[i].name) == 0) {
                        sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
                        return;
                }
        }

 
@@ -573,17 +540,18 @@ void
 sql_column_add_nullable_action(struct Parse *parser,
                               enum on_conflict_action nullable_action)
 {
-       struct Table *p = parser->pNewTable;
-       if (p == NULL || NEVER(p->def->field_count < 1))
+       if (parser->new_space == NULL ||
+               NEVER(parser->new_space->def->field_count < 1))
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Wrong indentation.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">           return;
-       struct field_def *field = &p->def->fields[p->def->field_count - 1];
+       struct space *space = parser->new_space;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
struct space_def *def = parser->new_space->def;</pre>
    </blockquote>
    Ok, fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -608,20 +576,20 @@ sql_column_add_nullable_action(struct Parse *parser,
 void
 sqlite3AddDefaultValue(Parse * pParse, ExprSpan * pSpan)
 {
-       Table *p;
        sqlite3 *db = pParse->db;
-       p = pParse->pNewTable;
-       assert(p->def->opts.is_temporary);
-       if (p != 0) {
+       assert(pParse->new_space->def->opts.is_temporary);
+       if (pParse->new_space != 0) {
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
!= NULL (please, fix the rest of similar places).</pre>
    </blockquote>
    Ok, fixed here and in another places.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+          struct space_def *space_def = pParse->new_space->def;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Call it simply “def” and you won’t have to carry line below.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">           if (!sqlite3ExprIsConstantOrFunction
                    (pSpan->pExpr, db->init.busy)) {
                        sqlite3ErrorMsg(pParse,
                                        "default value of column [%s] is not constant",
-                                       p->def->fields[p->def->field_count - 1].name);
+                                       space_def->
+                                       fields[space_def->field_count - 1].name);


@@ -714,14 +682,14 @@ sqlite3AddPrimaryKey(Parse * pParse,      /* Parsing context */
                }
        }
        if (nTerm == 1 && iCol != -1 &&
-           pTab->def->fields[iCol].type == FIELD_TYPE_INTEGER &&
-           sortOrder != SORT_ORDER_DESC) {
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Wrong indentation.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+          space->def->fields[iCol].type == FIELD_TYPE_INTEGER &&
+               sortOrder != SORT_ORDER_DESC) {



@@ -1229,7 +1197,7 @@ resolve_link(struct Parse *parse_context, const struct space_def *def,
  * This routine is called to report the final ")" that terminates
  * a CREATE TABLE statement.
  *
- * The table structure that other action routines have been building
+ * The space structure that other action routines have been building
  * is added to the internal hash tables, assuming no errors have
  * occurred.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
There is no internal hash of tables anymore. Fix comment pls.</pre>
    </blockquote>
    Comment is fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">  *
@@ -1238,12 +1206,12 @@ resolve_link(struct Parse *parse_context, const struct space_def *def,
  *     space and all necessary Tarantool indexes is emitted
  *  2. When db->init.busy == 1. This means that byte code for creation
  *     of new table is executing right now, and it's time to add new entry
- *     for the table into SQL memory represenation
+ *     for the table into SQL memory representation
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
The same.</pre>
    </blockquote>
    Comment also fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> sqlite3EndTable(Parse * pParse,   /* Parse context */
@@ -1251,24 +1219,24 @@ sqlite3EndTable(Parse * pParse, /* Parse context */
                Select * pSelect        /* Select from a "CREATE ... AS SELECT" */
     )
 {
-       Table *p;               /* The new table */
        sqlite3 *db = pParse->db;    /* The database connection */
 
        if (pEnd == 0 && pSelect == 0) {
                return;
        }
        assert(!db->mallocFailed);
-       p = pParse->pNewTable;
-       if (p == 0)
+       if (pParse->new_space == 0)
                return;
 
+       struct space *new_space = pParse->new_space;
+
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Don’t abuse new lines.</pre>
    </blockquote>
    Ok<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -1820,13 +1788,15 @@ sql_create_foreign_key(struct Parse *parse_context, struct SrcList *child,
        char *parent_name = NULL;
        char *constraint_name = NULL;
        bool is_self_referenced = false;
-       /*
-        * Table under construction during CREATE TABLE
+       /**
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
/** -> /*
</pre>
    </blockquote>
    Fixed<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+   * Space under construction during CREATE TABLE
         * processing. NULL for ALTER TABLE statement handling.
         */
-       struct Table *new_tab = parse_context->pNewTable;
-       /* Whether we are processing ALTER TABLE or CREATE TABLE. */
-       bool is_alter = new_tab == NULL;
+       bool is_alter = (parse_context->new_space == NULL);
+       struct space *space;
+       if (!is_alter)
+               space = parse_context->new_space;
+
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index cae0b3f6e..4b7eef988 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -1792,11 +1792,8 @@ sql_create_foreign_key(struct Parse *parse_context, struct SrcList *child,
         * Space under construction during CREATE TABLE
         * processing. NULL for ALTER TABLE statement handling.
         */
-       bool is_alter = (parse_context->new_space == NULL);
-       struct space *space;
-       if (!is_alter)
-               space = parse_context->new_space;
-
+       struct space *space = parse_context->new_space;
+       bool is_alter = space == NULL;

</pre>
    </blockquote>
    Ok<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -2249,7 +2219,6 @@ sql_create_index(struct Parse *parse, struct Token *token,
         * Return early if not found.
         */
        struct space *space = NULL;
-       struct space_def *def = NULL;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Do you really need this diff? It results only in other extra diffs.
def makes handling shorter.</pre>
    </blockquote>
    Ok, put it back.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">   if (tbl_name != NULL) {
                assert(token != NULL && token->z != NULL);
                const char *name = tbl_name->a[0].zName;
@@ -2262,17 +2231,16 @@ sql_create_index(struct Parse *parse, struct Token *token,
                        }
                        goto exit_create_index;
                }
-               def = space->def;
        } else {
-               if (parse->pNewTable == NULL)
+               if (parse->new_space == NULL)
                        goto exit_create_index;
                assert(token == NULL);
                assert(start == NULL);
-               space = parse->pNewTable->space;
-               def = parse->pNewTable->def;
+               space = parse->new_space;
+               space->def = parse->new_space->def;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Why do you assign parse->new_space->def to parse->new_space->def ?</pre>
    </blockquote>
    Sorry, line removed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -2883,7 +2851,7 @@ sqlite3SrcListDelete(sqlite3 * db, SrcList * pList)
                        sqlite3DbFree(db, pItem->u1.zIndexedBy);
                if (pItem->fg.isTabFunc)
                        sql_expr_list_delete(db, pItem->u1.pFuncArg);
-               sqlite3DeleteTable(db, pItem->pTab);
+               sql_space_delete(db, pItem->space);
                sql_select_delete(db, pItem->pSelect);
                sql_expr_delete(db, pItem->pOn, false);
                sqlite3IdListDelete(db, pItem->pUsing);
diff --git a/src/box/sql/delete.c b/src/box/sql/delete.c
index f9c42fdec..b687ff8f4 100644
--- a/src/box/sql/delete.c
+++ b/src/box/sql/delete.c
@@ -53,16 +53,10 @@ sql_lookup_table(struct Parse *parse, struct SrcList_item *tbl_name)
                parse->nErr++;
                return NULL;
        }
-       struct Table *table = sqlite3DbMallocZero(parse->db, sizeof(*table));
-       if (table == NULL)
-               return NULL;
-       table->def = space->def;
-       table->space = space;
-       table->nTabRef = 1;
-       tbl_name->pTab = table;
-       if (sqlite3IndexedByLookup(parse, tbl_name) != 0)
-               table = NULL;
-       return table;
+       space_name->space = space;
+       if (sqlite3IndexedByLookup(parse, space_name) != 0)
+               space = NULL;
+       return space;
 }
 

@@ -435,7 +428,7 @@ sql_table_delete_from(struct Parse *parse, struct SrcList *tab_list,
 }
 
 void
-sql_generate_row_delete(struct Parse *parse, struct Table *table,
+sql_generate_row_delete(struct Parse *parse, struct space *space,
                        struct sql_trigger *trigger_list, int cursor,
                        int reg_pk, short npk, bool need_update_count,
                        enum on_conflict_action onconf, u8 mode,
@@ -464,31 +457,31 @@ sql_generate_row_delete(struct Parse *parse, struct Table *table,
        /* If there are any triggers to fire, allocate a range of registers to
         * use for the old.* references in the triggers.
         */
-       if (table != NULL &&
-           (fkey_is_required(table->def->id, NULL) || trigger_list != NULL)) {
+       if (space != NULL &&
+               (fkey_is_required(space->def->id, NULL) ||
+                       trigger_list != NULL)) {
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Broken indentation.</pre>
    </blockquote>
    Fixed now.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">diff --git a/src/box/sql/fkey.c b/src/box/sql/fkey.c
index 3033cfcbb..93f98b9db 100644
--- a/src/box/sql/fkey.c
+++ b/src/box/sql/fkey.c
@@ -302,20 +302,23 @@ fkey_lookup_parent(struct Parse *parse_context, struct space *parent,
        sqlite3VdbeAddOp1(v, OP_Close, cursor);
 }
 
-/*
- * Return an Expr object that refers to a memory register corresponding
- * to column iCol of table pTab.
+/**
+ * Return an Expr object that refers to a memory register
+ * corresponding to column iCol of given space.
  *
- * regBase is the first of an array of register that contains the data
- * for pTab.  regBase+1 holds the first column.
+ * regBase is the first of an array of register that contains
+ * the data for given space.  regBase+1 holds the first column.
  * regBase+2 holds the second column, and so forth.
+ *
+ * @param pParse Parsing and code generating context.
+ * @param space  The space whose content is at r[regBase]...
+ * @param regBase Contents of table pTab.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Not pTab.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+ * @param iCol Which column of pTab is desired.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Same.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+ * @return an Expr object that refers to a memory register
+ *         corresponding to column iCol of given space.
  */
 static Expr *
-exprTableRegister(Parse * pParse,      /* Parsing and code generating context */
-                 Table * pTab, /* The table whose content is at r[regBase]... */
-                 int regBase,  /* Contents of table pTab */
-                 i16 iCol      /* Which column of pTab is desired */
-    )
+exprTableRegister(Parse * pParse, struct space * space, int regBase, i16 iCol)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Lets rename it to space_field_register() or space_field_get_register()
(not sure if we use _get_ prefix for getters).

You also can pass here just def. </pre>
    </blockquote>
    Ok, done<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index f147f6a50..c45bcc21b 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -117,14 +117,14 @@ sql_space_autoinc_fieldno(struct space *space)
  * SELECT.
  *
  * @param parser Parse context.
- * @param table Table AST object.
- * @retval  true if the table table in database or any of its
+ * @param space_def Space definition.
+ * @retval  true if the space (given by space_id) in database or any of its
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Nit: out of 66 chars.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -311,32 +310,32 @@ sqlite3Insert(Parse * pParse,        /* Parser context */

        /* Figure out if we have any triggers and if the table being
         * inserted into is a view
         */
-       trigger = sql_triggers_exist(pTab, TK_INSERT, NULL, &tmask);
-       bool is_view = pTab->def->opts.is_view;
+       trigger = sql_triggers_exist(space->def, TK_INSERT, NULL, &tmask);
+       struct space_def *space_def = space->def;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Swap this and previous lines pls, so that we pass
space_def to sql_triggers_exits (not space->def).</pre>
    </blockquote>
    Ok.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+  space_id = space->def->id;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Here you need space id only to pass it to space_column_default_expr(),
which in turn again fetches space from cache using space_by_id().
Lets remove this redundant function and simply get default value
by space->def->fields…</pre>
    </blockquote>
    Done.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -218,7 +218,7 @@ lookupName(Parse * pParse,     /* The parsing context */
        struct SrcList_item *pMatch = 0;        /* The matching pSrcList item */
        NameContext *pTopNC = pNC;      /* First namecontext in the list */
        int isTrigger = 0;      /* True if resolved to a trigger column */
-       Table *pTab = 0;        /* Table hold the row */
+       struct space_def *space_def;
 
        assert(pNC);            /* the name context cannot be NULL. */
        assert(zCol);           /* The Z in X.Y.Z cannot be NULL */
@@ -237,9 +237,10 @@ lookupName(Parse * pParse, /* The parsing context */
                if (pSrcList) {
                        for (i = 0, pItem = pSrcList->a; i < pSrcList->nSrc;
                             i++, pItem++) {
-                               pTab = pItem->pTab;
-                               assert(pTab != 0 && pTab->def->name != NULL);
-                               assert(pTab->def->field_count > 0);
+                               assert(pItem->space != NULL &&
+                                       pItem->space->def->name != NULL);
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Broken indentation a bit.</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -311,32 +312,32 @@ lookupName(Parse * pParse,   /* The parsing context */
                 * it is a new.* or old.* trigger argument reference
                 */
                if (zTab != 0 && cntTab == 0
-                   && pParse->pTriggerTab != 0) {
+                   && pParse->trigger_space != 0) {
                        int op = pParse->eTriggerOp;
                        assert(op == TK_DELETE || op == TK_UPDATE
                               || op == TK_INSERT);
                        if (op != TK_DELETE && sqlite3StrICmp("new", zTab) == 0) {
                                pExpr->iTable = 1;
-                               pTab = pParse->pTriggerTab;
+                               space_def = pParse->trigger_space->def;
                        } else if (op != TK_INSERT
                                   && sqlite3StrICmp("old", zTab) == 0) {
                                pExpr->iTable = 0;
-                               pTab = pParse->pTriggerTab;
+                               space_def = pParse->trigger_space->def;
                        } else {
-                               pTab = 0;
+                               space_def = 0;
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
NULL (and in other places too).</pre>
    </blockquote>
    Fixed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">
@@ -1934,20 +1932,28 @@ cleanup:
 }
 
 /*
+
+ */
+
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Garbage diff.</pre>
    </blockquote>
    Removed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+/**
  * Add type and collation information to a column list based on
  * a SELECT statement.
  *
- * The column list presumably came from selectColumnNamesFromExprList().
- * The column list has only names, not types or collations.  This
- * routine goes through and adds the types and collations.
+ * The column list presumably came from
+ * selectColumnNamesFromExprList(). The column list has only
+ * names, not types or collations.  This routine goes through
+ * and adds the types and collations.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Why did you change this comment?</pre>
    </blockquote>
    Because the length of it's lines exceeded 65 chars.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">  *
  * This routine requires that all identifiers in the SELECT
  * statement be resolved.
+ *
+ * @param pParse Parsing contexts.
+ * @param space Add column type information to this table.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
To this space.</pre>
    </blockquote>
    Ok.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">+ * @param pSelect SELECT used to determine types and collations.
  */
 void
-sqlite3SelectAddColumnTypeAndCollation(Parse * pParse,         /* Parsing contexts */
-                                      Table * pTab,            /* Add column type information to this table */
-                                      Select * pSelect)        /* SELECT used to determine types and collations */
+sqlite3SelectAddColumnTypeAndCollation(Parse * pParse, struct space * space, 
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Extra space after asterisk.</pre>
    </blockquote>
    Removed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -2000,23 +2006,20 @@ sqlite3ResultSetOfSelect(Parse * pParse, Select * pSelect)
        while (pSelect->pPrior)
                pSelect = pSelect->pPrior;
        user_session->sql_flags = savedFlags;
-       Table *table = sql_ephemeral_table_new(pParse, NULL);
-       if (table == NULL)
+       struct space *space = sql_ephemeral_space_new(pParse, NULL);
+
+       if (space == NULL)
                return 0;
-       /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
+       /* The sqlite3ResultSetOfSelect() is only used in contexts where lookaside
         * is disabled
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Extra diff.</pre>
    </blockquote>
    Empty line is removed.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">    */
        assert(db->lookaside.bDisable);
-       table->nTabRef = 1;
-       table->tuple_log_count = DEFAULT_TUPLE_LOG_COUNT;
-       assert(sqlite3LogEst(DEFAULT_TUPLE_COUNT) == DEFAULT_TUPLE_LOG_COUNT);
-       sqlite3ColumnsFromExprList(pParse, pSelect->pEList, table);
-       sqlite3SelectAddColumnTypeAndCollation(pParse, table, pSelect);
+       sqlite3ColumnsFromExprList(pParse, pSelect->pEList, space->def);
+       sqlite3SelectAddColumnTypeAndCollation(pParse, space, pSelect);
        if (db->mallocFailed) {
-               sqlite3DeleteTable(db, table);
                return 0;
        }
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Remove extra brackets.
</pre>
    </blockquote>
    Removed. <br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -4803,39 +4781,36 @@ selectExpander(Walker * pWalker, Select * p)
                         * unique identifier.
                         */
                        const char *name = "sqlite_sq_DEADBEAFDEADBEAF";
-                       pFrom->pTab = pTab =
-                               sql_ephemeral_table_new(pParse, name);
-                       if (pTab == NULL)
+                       struct space *space;
+                       pFrom->space = space =
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Avoid using double assignment at one time.</pre>
    </blockquote>
    Ok.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -5673,28 +5649,29 @@ sqlite3Select(Parse * pParse,              /* The parser context */
                                              pItem->regReturn);
                        pItem->addrFillSub = topAddr + 1;
                        if (pItem->fg.isCorrelated == 0) {
-                               /* If the subquery is not correlated and if we are not inside of
-                                * a trigger, then we only need to compute the value of the subquery
-                                * once.
+                               /* If the subquery is not
+                                * correlated and if we are not
+                                * inside of a trigger, then
+                                * we only need to compute the
+                                * value of the subquery once.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
It is not necessary to fix this comment.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> /*
  * Each foreign key constraint is an instance of the following structure.

@@ -2733,7 +2712,7 @@ struct Parse {
        int nSelect;            /* Number of SELECT statements seen */
        int nSelectIndent;      /* How far to indent SELECTTRACE() output */
        Parse *pToplevel;       /* Parse structure for main program (or NULL) */
-       Table *pTriggerTab;     /* Table triggers are being coded for */
+       struct space *trigger_space;    /* Space triggers are being coded for */
        u32 nQueryLoop;         /* Est number of iterations of a query (10*log2(N)) */
        u32 oldmask;            /* Mask of old.* columns referenced */
        u32 newmask;            /* Mask of new.* columns referenced */
@@ -2776,8 +2755,7 @@ struct Parse {
        VList *pVList;          /* Mapping between variable names and numbers */
        Vdbe *pReprepare;       /* VM being reprepared (sqlite3Reprepare()) */
        const char *zTail;      /* All SQL text past the last semicolon parsed */
-       Table *pNewTable;       /* A table being constructed by CREATE TABLE */
-       Table *pZombieTab;      /* List of Table objects to delete after code gen */
+       struct space *new_space;        /* A space being constructed by CREATE TABLE */
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Now we have trigger_space and new_space members. And it seems
that we can’t use both of the at the same time. So, lets throw away
one member. If you want to save names, mb it is worth to use union.</pre>
    </blockquote>
    I've thrown away one member and renamed another into
    'updated_space'.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

Moreover, I still see several pNewTable references in comments.</pre>
    </blockquote>
    Removed now.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
 
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap=""> /**
  * Generate code for a DELETE FROM statement.
@@ -3603,10 +3581,11 @@ int sqlite3WhereOkOnePass(WhereInfo *, int *);
 
 /**
  * Generate code that will extract the iColumn-th column from
- * table pTab and store the column value in a register.
+ * table defined by space_def and store the column value in a
+ * register.
  *
  * An effort is made to store the column value in register iReg.
- * This is not garanteeed for GetColumn() - the result can be
+ * This is not guaranteed for GetColumn() - the result can be
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Extra diff.</pre>
    </blockquote>
    This diff fixes typo 'garanteeed' -> 'guaranteed'<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">@@ -3985,19 +3964,21 @@ vdbe_code_drop_trigger(struct Parse *parser, const char *trigger_name,
                       bool account_changes);
 
 /**
- * Return a list of all triggers on table pTab if there exists at
- * least one trigger that must be fired when an operation of type
- * 'op' is performed on the table, and, if that operation is an
- * UPDATE, if at least one of the columns in changes_list is being
- * modified.
- *
- * @param table The table the contains the triggers.
+ * Return a list of all triggers on space (represented with
+ * space_def) if there exists at least one trigger that must be
+ * fired when an operation of type 'op' is performed on the
+ * table, and, if that operation is an UPDATE, if at least one
+ * of the columns in changes_list is being modified.
+ *
+ * @param space_def The definition of the space that contains
+ *        the triggers.
  * @param op operation one of TK_DELETE, TK_INSERT, TK_UPDATE.
  * @param changes_list Columns that change in an UPDATE statement.
  * @param[out] pMask Mask of TRIGGER_BEFORE|TRIGGER_AFTER
  */
 struct sql_trigger *
-sql_triggers_exist(struct Table *table, int op, struct ExprList *changes_list,
+sql_triggers_exist(struct space_def *space_def, int op,
+                  struct ExprList *changes_list,
                   int *mask_ptr);
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
You can fit two args in one line.</pre>
    </blockquote>
    Ok, done.<br>
    <blockquote type="cite"
      cite="mid:A67773AC-65C8-41A2-AD19-B9CB59136912@tarantool.org">
      <pre class="moz-quote-pre" wrap="">

Finally, I see a lot of space_by_id() usage through code.
Could you please validate them and remove redundant
ones. You may move this part to a separate patch,
if you want to.


</pre>
    </blockquote>
    Also done. A few usages like spave_by_id(def->id) are removed.<br>
    <br>
    <pre>---
 src/box/schema.cc          |   6 +-
 src/box/sql.c              |  27 +++----
 src/box/sql.h              |   8 +-
 src/box/sql/build.c        | 196 ++++++++++++++++++---------------------------
 src/box/sql/delete.c       |   6 +-
 src/box/sql/expr.c         |   4 +-
 src/box/sql/fkey.c         |  49 ++++++------
 src/box/sql/insert.c       |  26 +++---
 src/box/sql/resolve.c      |  27 ++++---
 src/box/sql/select.c       |  70 ++++++++--------
 src/box/sql/sqliteInt.h    |  18 ++---
 src/box/sql/tarantoolInt.h |   8 +-
 src/box/sql/tokenize.c     |   4 +-
 src/box/sql/treeview.c     |   2 +-
 src/box/sql/trigger.c      |   7 +-
 src/box/sql/update.c       |   6 +-
 src/box/sql/where.c        |   4 +-
 17 files changed, 209 insertions(+), 259 deletions(-)

diff --git a/src/box/schema.cc b/src/box/schema.cc
index 8625d92ea..2d12f01d0 100644
--- a/src/box/schema.cc
+++ b/src/box/schema.cc
@@ -193,8 +193,8 @@ space_cache_replace(struct space *old_space, struct space *new_space)
                        mh_strnptr_del(spaces_by_name, k, NULL);
                }
                /*
-                * Insert @new_space into @spaces cache, replacing
-                * @old_space if it's not NULL.
+                * Insert @updated_space into @spaces cache,
+                * replacing @old_space if it's not NULL.
                 */
                const struct mh_i32ptr_node_t node_p = { space_id(new_space),
                                                         new_space };
@@ -209,7 +209,7 @@ space_cache_replace(struct space *old_space, struct space *new_space)
                assert(old_space_by_id == old_space);
                (void)old_space_by_id;
                /*
-                * Insert @new_space into @spaces_by_name cache.
+                * Insert @updated_space into @spaces_by_name cache.
                 */
                const char *name = space_name(new_space);
                uint32_t name_len = strlen(name);
diff --git a/src/box/sql.c b/src/box/sql.c
index 978ad8310..3bf21cb55 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -972,7 +972,7 @@ cursor_advance(BtCursor *pCur, int *pRes)
  */
 
 char *
-sql_encode_table(struct region *region, struct space *space, uint32_t *size)
+sql_encode_table(struct region *region, struct space_def *def, uint32_t *size)
 {
        size_t used = region_used(region);
        struct mpstream stream;
@@ -980,7 +980,6 @@ sql_encode_table(struct region *region, struct space *space, uint32_t *size)
        mpstream_init(&stream, region, region_reserve_cb, region_alloc_cb,
                      set_encode_error, &is_error);
 
-       const struct space_def *def = space->def;
        assert(def != NULL);
        uint32_t field_count = def->field_count;
        mpstream_encode_array(&stream, field_count);
@@ -1033,7 +1032,7 @@ sql_encode_table(struct region *region, struct space *space, uint32_t *size)
 }
 
 char *
-sql_encode_table_opts(struct region *region, struct space *space,
+sql_encode_table_opts(struct region *region, struct space_def *def,
                      const char *sql, uint32_t *size)
 {
        size_t used = region_used(region);
@@ -1043,8 +1042,8 @@ sql_encode_table_opts(struct region *region, struct space *space,
                      set_encode_error, &is_error);
        int checks_cnt = 0;
        struct ExprList_item *a;
-       bool is_view = space->def->opts.is_view;
-       struct ExprList *checks = space->def->opts.checks;
+       bool is_view = def->opts.is_view;
+       struct ExprList *checks = def->opts.checks;
        if (checks != NULL) {
                checks_cnt = checks->nExpr;
                a = checks->a;
@@ -1261,21 +1260,19 @@ sql_ephemeral_space_def_new(struct Parse *parser, const char *name)
 struct space *
 sql_ephemeral_space_new(Parse *parser, const char *name)
 {
-       struct space *space =
-               (struct space *) region_alloc(&parser->region,
-                                             sizeof(struct space));
+       size_t sz = sizeof(struct space);
+       struct space *space = (struct space *) region_alloc(&parser->region, sz);
        if (space == NULL) {
-               diag_set(OutOfMemory, sizeof(struct space), "region", "space");
+               diag_set(OutOfMemory, sz, "region", "space");
                parser->rc = SQL_TARANTOOL_ERROR;
                parser->nErr++;
                return NULL;
        }
 
-       memset(space, 0, sizeof(struct space));
+       memset(space, 0, sz);
        space->def = sql_ephemeral_space_def_new(parser, name);
-       if (space->def == NULL) {
+       if (space->def == NULL)
                return NULL;
-       }
 
        return space;
 }
@@ -1334,11 +1331,7 @@ sql_checks_resolve_space_def_reference(ExprList *expr_list,
        sql_parser_create(&parser, sql_get());
        parser.parse_only = true;
 
-       struct space dummy_space;
-       dummy_space.def = def;
-
-       sql_resolve_self_reference(&parser, &dummy_space, NC_IsCheck, NULL,
-                                  expr_list);
+       sql_resolve_self_reference(&parser, def, NC_IsCheck, NULL, expr_list);
        int rc = 0;
        if (parser.rc != SQLITE_OK) {
                /* Tarantool error may be already set with diag. */
diff --git a/src/box/sql.h b/src/box/sql.h
index 0d9ecd0ce..d0b654e9c 100644
--- a/src/box/sql.h
+++ b/src/box/sql.h
@@ -227,7 +227,7 @@ sql_expr_delete(struct sqlite3 *db, struct Expr *expr, bool extern_alloc);
 /**
  * Create and initialize a new ephemeral space object.
  * @param parser SQL Parser object.
- * @param name Space to create name.
+ * @param name Name of space to be created.
  * @retval NULL on memory allocation error, Parser state changed.
  * @retval not NULL on success.
  */
@@ -237,7 +237,7 @@ sql_ephemeral_space_new(struct Parse *parser, const char *name);
 /**
  * Create and initialize a new ephemeral space_def object.
  * @param parser SQL Parser object.
- * @param name Space to create name.
+ * @param name Name of space to be created.
  * @retval NULL on memory allocation error, Parser state changed.
  * @retval not NULL on success.
  */
@@ -290,13 +290,13 @@ sql_expr_list_append(struct sqlite3 *db, struct ExprList *expr_list,
  * the column number. Any errors cause an error message to be set
  * in parser.
  * @param parser Parsing context.
- * @param space The space being referenced.
+ * @param def The definition of space being referenced.
  * @param type NC_IsCheck or NC_IdxExpr.
  * @param expr Expression to resolve.  May be NULL.
  * @param expr_list Expression list to resolve.  May be NUL.
  */
 void
-sql_resolve_self_reference(struct Parse *parser, struct space *space, int type,
+sql_resolve_self_reference(struct Parse *parser, struct space_def *def, int type,
                           struct Expr *expr, struct ExprList *expr_list);
 
 /**
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index cae0b3f6e..599ece6d5 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -247,7 +247,7 @@ sql_space_column_is_in_pk(struct space *space, uint32_t column)
 void
 sql_space_delete(struct sqlite3 *db, struct space *space)
 {
-       if (!space || !db || db->pnBytesFreed == 0)
+       if (space  != NULL || db != NULL|| db->pnBytesFreed == 0)
                return;
 
        if (space->def->opts.is_temporary) {
@@ -310,35 +310,13 @@ sqlite3CheckIdentifierName(Parse *pParse, char *zName)
 }
 
 struct index *
-sql_table_primary_key(const struct space *space)
+sql_space_primary_key(const struct space *space)
 {
        if (space->index_count == 0 || space->index[0]->def->iid != 0)
                return NULL;
        return space->index[0];
 }
 
-/**
- * Create and initialize a new SQL space object.
- * All memory except table object itself is allocated on region.
- * @param parser SQL Parser object.
- * @param name Table to create name.
- * @retval NULL on memory allocation error, Parser state is
- *         changed.
- * @retval not NULL on success.
- */
-static struct space *
-sql_table_new(Parse *parser, char *name)
-{
-       struct space *space = sql_ephemeral_space_new(parser, name);
-       if (space == NULL)
-               return NULL;
-
-       strcpy(space->def->engine_name,
-              sql_storage_engine_strs[current_session()->sql_default_engine]);
-
-       return space;
-}
-
 /*
  * Begin constructing a new table representation in memory.  This is
  * the first of several action routines that get called in response
@@ -349,7 +327,7 @@ sql_table_new(Parse *parser, char *name)
  * when the "TEMP" or "TEMPORARY" keyword occurs in between
  * CREATE and TABLE.
  *
- * The new table record is initialized and put in pParse->pNewTable.
+ * The new table record is initialized and put in pParse->updated_space.
  * As more of the CREATE TABLE statement is parsed, additional action
  * routines will be called to add more information to this record.
  * At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
@@ -388,12 +366,15 @@ sqlite3StartTable(Parse *pParse, Token *pName, int noErr)
                goto cleanup;
        }
 
-       struct space *new_space = sql_table_new(pParse, zName);
+       struct space *new_space = sql_ephemeral_space_new(pParse, zName);
        if (new_space == NULL)
                goto cleanup;
 
-       assert(pParse->new_space == 0);
-       pParse->new_space = new_space;
+       strcpy(new_space->def->engine_name,
+              sql_storage_engine_strs[current_session()->sql_default_engine]);
+
+       assert(pParse->updated_space == NULL);
+       pParse->updated_space = new_space;
 
        if (!db->init.busy && (v = sqlite3GetVdbe(pParse)) != 0)
                sql_set_multi_write(pParse, true);
@@ -478,28 +459,25 @@ void
 sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
 {
        assert(type_def != NULL);
-       int i;
        char *z;
        sqlite3 *db = pParse->db;
-       if ((pParse->new_space) == 0)
+       if (pParse->updated_space == NULL)
                return;
-       struct space *space = pParse->new_space;
+       struct space_def *def = pParse->updated_space->def;
 
 #if SQLITE_MAX_COLUMN
-       if ((int)space->def->field_count + 1 > db->aLimit[SQLITE_LIMIT_COLUMN]) {
-               sqlite3ErrorMsg(pParse, "too many columns on %s",
-                               space->def->name);
+       if ((int)def->field_count + 1 > db->aLimit[SQLITE_LIMIT_COLUMN]) {
+               sqlite3ErrorMsg(pParse, "too many columns on %s", def->name);
                return;
        }
 #endif
        /**
         * As sql_field_retrieve will allocate memory on region
-        * ensure that p->space->def is also temporal and would be rebuilded or
+        * ensure that p->space->def is also temporal and would be
         * dropped.
         */
-       assert(space->def->opts.is_temporary);
-       if (sql_field_retrieve(pParse, space->def,
-                             (uint32_t) space->def->field_count) == NULL)
+       assert(def->opts.is_temporary);
+       if (sql_field_retrieve(pParse, def, def->field_count) == NULL)
                return;
        struct region *region = &pParse->region;
        z = region_alloc(region, pName->n + 1);
@@ -513,14 +491,13 @@ sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
        memcpy(z, pName->z, pName->n);
        z[pName->n] = 0;
        sqlite3NormalizeName(z);
-       for (i = 0; i < (int)space->def->field_count; i++) {
-               if (strcmp(z, space->def->fields[i].name) == 0) {
+       for (uint32_t i = 0; i < def->field_count; i++) {
+               if (strcmp(z, def->fields[i].name) == 0) {
                        sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
                        return;
                }
        }
-       struct field_def *column_def =
-               &space->def->fields[space->def->field_count];
+       struct field_def *column_def = &def->fields[def->field_count];
        memcpy(column_def, &field_def_default, sizeof(field_def_default));
        column_def->name = z;
        /*
@@ -532,7 +509,7 @@ sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
        column_def->is_nullable = true;
        column_def->affinity = type_def->type;
        column_def->type = sql_affinity_to_field_type(column_def->affinity);
-       space->def->field_count++;
+       def->field_count++;
        pParse->constraintName.n = 0;
 }
 
@@ -540,18 +517,18 @@ void
 sql_column_add_nullable_action(struct Parse *parser,
                               enum on_conflict_action nullable_action)
 {
-       if (parser->new_space == NULL ||
-               NEVER(parser->new_space->def->field_count < 1))
+       if (parser->updated_space == NULL ||
+           NEVER(parser->updated_space->def->field_count < 1))
                return;
-       struct space *space = parser->new_space;
-       struct field_def *field = &space->def->fields[space->def->field_count - 1];
+       struct space_def *def = parser->updated_space->def;
+       struct field_def *field = &def->fields[def->field_count - 1];
        if (field->nullable_action != ON_CONFLICT_ACTION_DEFAULT &&
            nullable_action != field->nullable_action) {
                /* Prevent defining nullable_action many times. */
                const char *err_msg =
                        tt_sprintf("NULL declaration for column '%s' of table "
                                   "'%s' has been already set to '%s'",
-                                  field->name, space->def->name,
+                                  field->name, def->name,
                                   on_conflict_action_strs[field->
                                                           nullable_action]);
                diag_set(ClientError, ER_SQL, err_msg);
@@ -577,19 +554,18 @@ void
 sqlite3AddDefaultValue(Parse * pParse, ExprSpan * pSpan)
 {
        sqlite3 *db = pParse->db;
-       assert(pParse->new_space->def->opts.is_temporary);
-       if (pParse->new_space != 0) {
-               struct space_def *space_def = pParse->new_space->def;
+       assert(pParse->updated_space->def->opts.is_temporary);
+       if (pParse->updated_space != NULL) {
+               struct space_def *def = pParse->updated_space->def;
                if (!sqlite3ExprIsConstantOrFunction
                    (pSpan->pExpr, db->init.busy)) {
                        sqlite3ErrorMsg(pParse,
                                        "default value of column [%s] is not constant",
-                                       space_def->
-                                       fields[space_def->field_count - 1].name);
+                                       def->fields[def->field_count - 1].name);
                } else {
-                       assert(space_def != NULL);
+                       assert(def != NULL);
                        struct field_def *field =
-                               &space_def->fields[space_def->field_count - 1];
+                               &def->fields[def->field_count - 1];
                        struct region *region = &pParse->region;
                        uint32_t default_length = (int)(pSpan->zEnd - pSpan->zStart);
                        field->default_value = region_alloc(region,
@@ -648,16 +624,16 @@ sqlite3AddPrimaryKey(Parse * pParse,      /* Parsing context */
 {
        int iCol = -1, i;
        int nTerm;
-       if (pParse->new_space == 0)
+       if (pParse->updated_space == NULL)
                goto primary_key_exit;
-       struct space *space = pParse->new_space;
-       if (sql_table_primary_key(space) != NULL) {
+       struct space *space = pParse->updated_space;
+       if (sql_space_primary_key(space) != NULL) {
                sqlite3ErrorMsg(pParse,
                                "table \"%s\" has more than one primary key",
                                space->def->name);
                goto primary_key_exit;
        }
-       if (pList == 0) {
+       if (pList == NULL) {
                iCol = space->def->field_count - 1;
                nTerm = 1;
        } else {
@@ -682,8 +658,8 @@ sqlite3AddPrimaryKey(Parse * pParse,        /* Parsing context */
                }
        }
        if (nTerm == 1 && iCol != -1 &&
-               space->def->fields[iCol].type == FIELD_TYPE_INTEGER &&
-               sortOrder != SORT_ORDER_DESC) {
+           space->def->fields[iCol].type == FIELD_TYPE_INTEGER &&
+           sortOrder != SORT_ORDER_DESC) {
                assert(autoInc == 0 || autoInc == 1);
                pParse->is_new_table_autoinc = autoInc;
                struct sqlite3 *db = pParse->db;
@@ -711,7 +687,7 @@ sqlite3AddPrimaryKey(Parse * pParse,        /* Parsing context */
                        goto primary_key_exit;
        }
 
-       struct index *pk = sql_table_primary_key(space);
+       struct index *pk = sql_space_primary_key(space);
        assert(pk != NULL);
        struct key_def *pk_key_def = pk->def->key_def;
        for (uint32_t i = 0; i < pk_key_def->part_count; i++) {
@@ -728,8 +704,8 @@ void
 sql_add_check_constraint(struct Parse *parser, struct ExprSpan *span)
 {
        struct Expr *expr = span->pExpr;
-       if (parser->new_space != NULL) {
-               struct space *space = parser->new_space;
+       if (parser->updated_space != NULL) {
+               struct space *space = parser->updated_space;
                expr->u.zToken =
                        sqlite3DbStrNDup(parser->db, (char *)span->zStart,
                                         (int)(span->zEnd - span->zStart));
@@ -742,7 +718,7 @@ sql_add_check_constraint(struct Parse *parser, struct ExprSpan *span)
                        sqlite3DbFree(parser->db, expr->u.zToken);
                        goto release_expr;
                }
-               if (parser->constraintName.n) {
+               if (parser->constraintName.n != 0) {
                        sqlite3ExprListSetName(parser, space->def->opts.checks,
                                               &parser->constraintName, 1);
                }
@@ -759,9 +735,9 @@ release_expr:
 void
 sqlite3AddCollateType(Parse * pParse, Token * pToken)
 {
-       if (pParse->new_space == NULL)
+       if (pParse->updated_space == NULL)
                return;
-       struct space *space= pParse->new_space;
+       struct space *space = pParse->updated_space;
        uint32_t i = space->def->field_count - 1;
        sqlite3 *db = pParse->db;
        char *zColl = sqlite3NameFromToken(db, pToken);
@@ -892,7 +868,7 @@ vdbe_emit_create_index(struct Parse *parse, struct space_def *def,
        memcpy(raw, index_parts, index_parts_sz);
        index_parts = raw;
 
-       if (parse->new_space != NULL) {
+       if (parse->updated_space != NULL) {
                sqlite3VdbeAddOp2(v, OP_SCopy, space_id_reg, entry_reg);
                sqlite3VdbeAddOp2(v, OP_Integer, idx_def->iid, entry_reg + 1);
        } else {
@@ -937,13 +913,13 @@ createSpace(Parse * pParse, int iSpaceId, char *zStmt)
        int iRecord = (pParse->nMem += 7);
        struct region *region = &pParse->region;
        uint32_t table_opts_stmt_sz = 0;
-       struct space *space = pParse->new_space;
-       char *table_opts_stmt = sql_encode_table_opts(region, space, zStmt,
+       struct space *space = pParse->updated_space;
+       char *table_opts_stmt = sql_encode_table_opts(region, space->def, zStmt,
                                                      &table_opts_stmt_sz);
        if (table_opts_stmt == NULL)
                goto error;
        uint32_t table_stmt_sz = 0;
-       char *table_stmt = sql_encode_table(region, space, &table_stmt_sz);
+       char *table_stmt = sql_encode_table(region, space->def, &table_stmt_sz);
        if (table_stmt == NULL)
                goto error;
        char *raw = sqlite3DbMallocRaw(pParse->db,
@@ -1082,14 +1058,14 @@ vdbe_emit_fkey_create(struct Parse *parse_context, const struct fkey_def *fk)
         * of <CREATE TABLE ...> statement, we don't have child
         * id, but we know register where it will be stored.
         */
-       if (parse_context->new_space != NULL) {
+       if (parse_context->updated_space != NULL) {
                sqlite3VdbeAddOp2(vdbe, OP_SCopy, fk->child_id,
                                  constr_tuple_reg + 1);
        } else {
                sqlite3VdbeAddOp2(vdbe, OP_Integer, fk->child_id,
                                  constr_tuple_reg + 1);
        }
-       if (parse_context->new_space != NULL && fkey_is_self_referenced(fk)) {
+       if (parse_context->updated_space != NULL && fkey_is_self_referenced(fk)) {
                sqlite3VdbeAddOp2(vdbe, OP_SCopy, fk->parent_id,
                                  constr_tuple_reg + 2);
        } else {
@@ -1152,7 +1128,7 @@ vdbe_emit_fkey_create(struct Parse *parse_context, const struct fkey_def *fk)
                          constr_tuple_reg + 9);
        sqlite3VdbeAddOp3(vdbe, OP_SInsert, BOX_FK_CONSTRAINT_ID, 0,
                          constr_tuple_reg + 9);
-       if (parse_context->new_space == NULL)
+       if (parse_context->updated_space == NULL)
                sqlite3VdbeChangeP5(vdbe, OPFLAG_NCHANGE);
        save_record(parse_context, BOX_FK_CONSTRAINT_ID, constr_tuple_reg, 2,
                    vdbe->nOp - 1);
@@ -1197,16 +1173,8 @@ resolve_link(struct Parse *parse_context, const struct space_def *def,
  * This routine is called to report the final ")" that terminates
  * a CREATE TABLE statement.
  *
- * The space structure that other action routines have been building
- * is added to the internal hash tables, assuming no errors have
- * occurred.
- *
- * Insert is performed in two passes:
- *  1. When db->init.busy == 0. Byte code for creation of new Tarantool
- *     space and all necessary Tarantool indexes is emitted
- *  2. When db->init.busy == 1. This means that byte code for creation
- *     of new table is executing right now, and it's time to add new entry
- *     for the table into SQL memory representation
+ * During this routine byte code for creation of new Tarantool
+ * space and all necessary Tarantool indexes is emitted.
  *
  * If the pSelect argument is not NULL, it means that this routine
  * was called to create a space generated from a
@@ -1221,19 +1189,18 @@ sqlite3EndTable(Parse * pParse, /* Parse context */
 {
        sqlite3 *db = pParse->db;    /* The database connection */
 
-       if (pEnd == 0 && pSelect == 0) {
+       if (pEnd == NULL && pSelect == NULL) {
                return;
        }
        assert(!db->mallocFailed);
-       if (pParse->new_space == 0)
+       if (pParse->updated_space == NULL)
                return;
 
-       struct space *new_space = pParse->new_space;
-
+       struct space *new_space = pParse->updated_space;
        assert(!db->init.busy);
 
        if (!new_space->def->opts.is_view) {
-               if (sql_table_primary_key(new_space) == NULL) {
+               if (sql_space_primary_key(new_space) == NULL) {
                        sqlite3ErrorMsg(pParse,
                                        "PRIMARY KEY missing on table %s",
                                        new_space->def->name);
@@ -1322,7 +1289,7 @@ sqlite3EndTable(Parse * pParse,   /* Parse context */
                        }
                        fk->parent_id = reg_space_id;
                } else if (fk_parse->is_self_referenced) {
-                       struct index *pk = sql_table_primary_key(new_space);
+                       struct index *pk = sql_space_primary_key(new_space);
                        if (pk->def->key_def->part_count != fk->field_count) {
                                diag_set(ClientError, ER_CREATE_FK_CONSTRAINT,
                                         fk->name, "number of columns in "\
@@ -1359,9 +1326,9 @@ sql_create_view(struct Parse *parse_context, struct Token *begin,
                goto create_view_fail;
        }
        sqlite3StartTable(parse_context, name, if_exists);
-       if (parse_context->new_space == NULL || parse_context->nErr != 0)
+       if (parse_context->updated_space == NULL || parse_context->nErr != 0)
                goto create_view_fail;
-       struct space *space = parse_context->new_space;
+       struct space *space = parse_context->updated_space;
 
        struct space *select_res_space =
                sqlite3ResultSetOfSelect(parse_context, select);
@@ -1376,7 +1343,7 @@ sql_create_view(struct Parse *parse_context, struct Token *begin,
                        goto create_view_fail;
                }
                sqlite3ColumnsFromExprList(parse_context, aliases, space->def);
-               sqlite3SelectAddColumnTypeAndCollation(parse_context, space,
+               sqlite3SelectAddColumnTypeAndCollation(parse_context, space->def,
                                                       select);
        } else {
                assert(select_res_space->def->opts.is_temporary);
@@ -1788,14 +1755,12 @@ sql_create_foreign_key(struct Parse *parse_context, struct SrcList *child,
        char *parent_name = NULL;
        char *constraint_name = NULL;
        bool is_self_referenced = false;
-       /**
+       /*
         * Space under construction during CREATE TABLE
         * processing. NULL for ALTER TABLE statement handling.
         */
-       bool is_alter = (parse_context->new_space == NULL);
-       struct space *space;
-       if (!is_alter)
-               space = parse_context->new_space;
+       struct space *space = parse_context->updated_space;
+       bool is_alter = space == NULL;
 
        uint32_t child_cols_count;
        if (child_cols == NULL) {
@@ -2118,12 +2083,10 @@ index_fill_def(struct Parse *parse, struct index *index,
                         "region", "key parts");
                goto tnt_error;
        }
-       struct space tmp_space;
-       tmp_space.def = space_def;
-       assert(tmp_space.def != NULL);
+
        for (int i = 0; i < expr_list->nExpr; i++) {
                struct Expr *expr = expr_list->a[i].pExpr;
-               sql_resolve_self_reference(parse, &tmp_space, NC_IdxExpr,
+               sql_resolve_self_reference(parse, space_def, NC_IdxExpr,
                                           expr, 0);
                if (parse->nErr > 0)
                        goto cleanup;
@@ -2232,15 +2195,15 @@ sql_create_index(struct Parse *parse, struct Token *token,
                        goto exit_create_index;
                }
        } else {
-               if (parse->new_space == NULL)
+               if (parse->updated_space == NULL)
                        goto exit_create_index;
                assert(token == NULL);
                assert(start == NULL);
-               space = parse->new_space;
-               space->def = parse->new_space->def;
+               space = parse->updated_space;
        }
+       struct space_def *def = space->def;
 
-       if (space->def->opts.is_view) {
+       if (def->opts.is_view) {
                sqlite3ErrorMsg(parse, "views can not be indexed");
                goto exit_create_index;
        }
@@ -2273,7 +2236,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
                        if (!if_not_exist) {
                                sqlite3ErrorMsg(parse,
                                                "index %s.%s already exists",
-                                               space->def->name, name);
+                                               def->name, name);
                        }
                        goto exit_create_index;
                }
@@ -2305,7 +2268,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
                uint32_t idx_count = space->index_count;
                if (constraint_name == NULL ||
                    strcmp(constraint_name, "") == 0) {
-                       name = sqlite3MPrintf(db, prefix, space->def->name,
+                       name = sqlite3MPrintf(db, prefix, def->name,
                                              idx_count + 1);
                } else {
                        name = sqlite3MPrintf(db, prefix,
@@ -2318,7 +2281,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
                goto exit_create_index;
 
        if (tbl_name != NULL && space_is_system(space)) {
-               diag_set(ClientError, ER_MODIFY_INDEX, name, space->def->name,
+               diag_set(ClientError, ER_MODIFY_INDEX, name, def->name,
                         "can't create index on system space");
                parse->nErr++;
                parse->rc = SQL_TARANTOOL_ERROR;
@@ -2333,8 +2296,8 @@ sql_create_index(struct Parse *parse, struct Token *token,
         */
        if (col_list == NULL) {
                struct Token prev_col;
-               uint32_t last_field = space->def->field_count - 1;
-               sqlite3TokenInit(&prev_col, space->def->fields[last_field].name);
+               uint32_t last_field = def->field_count - 1;
+               sqlite3TokenInit(&prev_col, def->fields[last_field].name);
                col_list = sql_expr_list_append(parse->db, NULL,
                                                sqlite3ExprAlloc(db, TK_ID,
                                                                 &prev_col, 0));
@@ -2366,7 +2329,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
                iid = space->index_id_max + 1;
        else
                iid = 0;
-       if (index_fill_def(parse, index, space->def, iid, name, strlen(name),
+       if (index_fill_def(parse, index, def, iid, name, strlen(name),
                           col_list, idx_type) != 0)
                goto exit_create_index;
        /*
@@ -2390,7 +2353,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
        }
        index->def->key_def->part_count = new_part_count;
 
-       if (!index_def_is_valid(index->def, space->def->name))
+       if (!index_def_is_valid(index->def, def->name))
                goto exit_create_index;
 
        /*
@@ -2422,7 +2385,7 @@ sql_create_index(struct Parse *parse, struct Token *token,
         * constraint, but has different onError (behavior on
         * constraint violation), then an error is raised.
         */
-       if (parse->new_space != NULL) {
+       if (parse->updated_space != NULL) {
                for (uint32_t i = 0; i < space->index_count; ++i) {
                        struct index *existing_idx = space->index[i];
                        uint32_t iid = existing_idx->def->iid;
@@ -2491,10 +2454,9 @@ sql_create_index(struct Parse *parse, struct Token *token,
                sqlite3VdbeChangeP5(vdbe, OPFLAG_SEEKEQ);
 
                assert(start != NULL);
-               int index_id = getNewIid(parse, space->def->id, cursor);
+               int index_id = getNewIid(parse, def->id, cursor);
                sqlite3VdbeAddOp1(vdbe, OP_Close, cursor);
-               vdbe_emit_create_index(parse, space->def, index->def,
-                                      space->def->id, index_id);
+               vdbe_emit_create_index(parse, def, index->def, def->id, index_id);
                sqlite3VdbeChangeP5(vdbe, OPFLAG_NCHANGE);
                sqlite3VdbeAddOp0(vdbe, OP_Expire);
        }
diff --git a/src/box/sql/delete.c b/src/box/sql/delete.c
index b687ff8f4..e8354f7e4 100644
--- a/src/box/sql/delete.c
+++ b/src/box/sql/delete.c
@@ -415,7 +415,7 @@ sql_table_delete_from(struct Parse *parse, struct SrcList *tab_list,
 
        /* Return the number of rows that were deleted. */
        if ((user_session->sql_flags & SQLITE_CountRows) != 0 &&
-           parse->trigger_space != NULL) {
+           parse->updated_space != NULL) {
                sqlite3VdbeAddOp2(v, OP_ResultRow, reg_count, 1);
                sqlite3VdbeSetNumCols(v, 1);
                sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted",
@@ -458,8 +458,8 @@ sql_generate_row_delete(struct Parse *parse, struct space *space,
         * use for the old.* references in the triggers.
         */
        if (space != NULL &&
-               (fkey_is_required(space->def->id, NULL) ||
-                       trigger_list != NULL)) {
+           (fkey_is_required(space->def->id, NULL) ||
+            trigger_list != NULL)) {
                /* Mask of OLD.* columns in use */
                /* TODO: Could use temporary registers here. */
                uint32_t mask =
diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c
index e2fbcedf8..b23f36731 100644
--- a/src/box/sql/expr.c
+++ b/src/box/sql/expr.c
@@ -2228,7 +2228,7 @@ isCandidateForInOpt(Expr * pX)
                return 0;       /* Single term in FROM clause */
        if (pSrc->a[0].pSelect)
                return 0;       /* FROM is not a subquery or view */
-       assert(pSrc->a[0].space != 0);
+       assert(pSrc->a[0].space != NULL);
        /* FROM clause is not a view */
        assert(!pSrc->a[0].space->def->opts.is_view);
        pEList = p->pEList;
@@ -4335,7 +4335,7 @@ sqlite3ExprCodeTarget(Parse * pParse, Expr * pExpr, int target)
                        break;
                }
        case TK_RAISE:
-               if (pParse->trigger_space == NULL) {
+               if (pParse->updated_space == NULL) {
                        sqlite3ErrorMsg(pParse, "RAISE() may only be used "
                                        "within a trigger-program");
                        return 0;
diff --git a/src/box/sql/fkey.c b/src/box/sql/fkey.c
index 93f98b9db..61cfb6fd7 100644
--- a/src/box/sql/fkey.c
+++ b/src/box/sql/fkey.c
@@ -311,14 +311,15 @@ fkey_lookup_parent(struct Parse *parse_context, struct space *parent,
  * regBase+2 holds the second column, and so forth.
  *
  * @param pParse Parsing and code generating context.
- * @param space  The space whose content is at r[regBase]...
- * @param regBase Contents of table pTab.
- * @param iCol Which column of pTab is desired.
+ * @param def Definition of space whose content is at r[regBase]...
+ * @param regBase Contents of table defined by def.
+ * @param iCol Which column of space is desired.
  * @return an Expr object that refers to a memory register
  *         corresponding to column iCol of given space.
  */
 static Expr *
-exprTableRegister(Parse * pParse, struct space * space, int regBase, i16 iCol)
+space_field_register(Parse *pParse, struct space_def *def, int regBase,
+                    i16 iCol)
 {
        Expr *pExpr;
        sqlite3 *db = pParse->db;
@@ -327,7 +328,7 @@ exprTableRegister(Parse * pParse, struct space * space, int regBase, i16 iCol)
        if (pExpr) {
                if (iCol >= 0) {
                        pExpr->iTable = regBase + iCol + 1;
-                       char affinity = space->def->fields[iCol].affinity;
+                       char affinity = def->fields[iCol].affinity;
                        pExpr->affinity = affinity;
                } else {
                        pExpr->iTable = regBase;
@@ -397,14 +398,15 @@ exprTableColumn(sqlite3 * db, struct space_def *def, int cursor, i16 column)
  * this file as "I.2" and "D.2".
  * @param parser SQL parser.
  * @param src The child table to be scanned.
- * @param tab Parent table.
+ * @param def Parent space definition.
  * @param fkey The foreign key linking src to tab.
  * @param reg_data Register from which parent row data starts.
  * @param incr_count Amount to increment deferred counter by.
  */
 static void
-fkey_scan_children(struct Parse *parser, struct SrcList *src, struct space *space,
-                  struct fkey_def *fkey, int reg_data, int incr_count)
+fkey_scan_children(struct Parse *parser, struct SrcList *src,
+                  struct space_def *def, struct fkey_def *fkey, int reg_data,
+                  int incr_count)
 {
        assert(incr_count == -1 || incr_count == 1);
        struct sqlite3 *db = parser->db;
@@ -435,7 +437,7 @@ fkey_scan_children(struct Parse *parser, struct SrcList *src, struct space *spac
        for (uint32_t i = 0; i < fkey->field_count; i++) {
                uint32_t fieldno = fkey->links[i].parent_field;
                struct Expr *pexpr =
-                       exprTableRegister(parser, space, reg_data, fieldno);
+                       space_field_register(parser, def, reg_data, fieldno);
                fieldno = fkey->links[i].child_field;
                const char *field_name = child_space->def->fields[fieldno].name;
                struct Expr *chexpr = sqlite3Expr(db, TK_ID, field_name);
@@ -452,14 +454,14 @@ fkey_scan_children(struct Parse *parser, struct SrcList *src, struct space *spac
         *     NOT( $current_a==a AND $current_b==b AND ... )
         *     The primary key is (a,b,...)
         */
-       if (space->def->id == fkey->child_id && incr_count > 0) {
+       if (def->id == fkey->child_id && incr_count > 0) {
                struct Expr *expr = NULL, *pexpr, *chexpr, *eq;
                for (uint32_t i = 0; i < fkey->field_count; i++) {
                        uint32_t fieldno = fkey->links[i].parent_field;
-                       pexpr = exprTableRegister(parser, space, reg_data,
-                                                 fieldno);
-                       chexpr = exprTableColumn(db, space->def,
-                                                src->a[0].iCursor, fieldno);
+                       pexpr = space_field_register(parser, def, reg_data,
+                                                    fieldno);
+                       chexpr = exprTableColumn(db, def, src->a[0].iCursor,
+                                                fieldno);
                        eq = sqlite3PExpr(parser, TK_EQ, pexpr, chexpr);
                        expr = sqlite3ExprAnd(db, expr, eq);
                }
@@ -625,13 +627,13 @@ fkey_emit_check(struct Parse *parser, struct space *space, int reg_old,
                item->iCursor = parser->nTab++;
 
                if (reg_new != 0) {
-                       fkey_scan_children(parser, src, space, fk->def, reg_new,
-                                          -1);
+                       fkey_scan_children(parser, src, space->def, fk->def,
+                                          reg_new, -1);
                }
                if (reg_old != 0) {
                        enum fkey_action action = fk_def->on_update;
-                       fkey_scan_children(parser, src, space, fk->def, reg_old,
-                                          1);
+                       fkey_scan_children(parser, src, space->def, fk->def,
+                                          reg_old, 1);
                        /*
                         * If this is a deferred FK constraint, or
                         * a CASCADE or SET NULL action applies,
@@ -731,7 +733,7 @@ fkey_is_required(uint32_t space_id, const int *changes)
  * foreign key object by fkey_delete().
  *
  * @param pParse Parse context.
- * @param space Space being updated or deleted from.
+ * @param def Definition of space being updated or deleted from.
  * @param fkey Foreign key to get action for.
  * @param is_update True if action is on update.
  *
@@ -739,8 +741,8 @@ fkey_is_required(uint32_t space_id, const int *changes)
  * @retval NULL on failure.
  */
 static struct sql_trigger *
-fkey_action_trigger(struct Parse *pParse, struct space *space, struct fkey *fkey,
-                   bool is_update)
+fkey_action_trigger(struct Parse *pParse, struct space_def *def,
+                   struct fkey *fkey, bool is_update)
 {
        struct sqlite3 *db = pParse->db;
        struct fkey_def *fk_def = fkey->def;
@@ -768,7 +770,7 @@ fkey_action_trigger(struct Parse *pParse, struct space *space, struct fkey *fkey
                struct field_def *child_fields = child_space->def->fields;
 
                uint32_t pcol = fk_def->links[i].parent_field;
-               sqlite3TokenInit(&t_to_col, space->def->fields[pcol].name);
+               sqlite3TokenInit(&t_to_col, def->fields[pcol].name);
 
                uint32_t chcol = fk_def->links[i].child_field;
                sqlite3TokenInit(&t_from_col, child_fields[chcol].name);
@@ -942,7 +944,8 @@ fkey_emit_actions(struct Parse *parser, struct space *space, int reg_old,
                    !fkey_is_modified(fk->def, FIELD_LINK_PARENT, changes))
                        continue;
                struct sql_trigger *pAct =
-                       fkey_action_trigger(parser, space, fk, changes != NULL);
+                       fkey_action_trigger(parser, space->def, fk,
+                                           changes != NULL);
                if (pAct == NULL)
                        continue;
                vdbe_code_row_trigger_direct(parser, pAct, space, reg_old,
diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index c45bcc21b..f28567dd1 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -118,9 +118,9 @@ sql_space_autoinc_fieldno(struct space *space)
  *
  * @param parser Parse context.
  * @param space_def Space definition.
- * @retval  true if the space (given by space_id) in database or any of its
- *          indices have been opened at any point in the VDBE
- *          program.
+ * @retval  true if the space (given by space_id) in database or
+ *          any of its indices have been opened at any point in
+ *          the VDBE program.
  * @retval  false else.
  */
 static bool
@@ -281,7 +281,6 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
        int regTupleid;         /* registers holding insert tupleid */
        int regData;            /* register holding first column to insert */
        int *aRegIdx = 0;       /* One register allocated to each index */
-       uint32_t space_id = 0;
        /* List of triggers on pTab, if required. */
        struct sql_trigger *trigger;
        int tmask;              /* Mask of trigger times */
@@ -318,9 +317,8 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
        /* Figure out if we have any triggers and if the table being
         * inserted into is a view
         */
-       trigger = sql_triggers_exist(space->def, TK_INSERT, NULL, &tmask);
        struct space_def *space_def = space->def;
-       space_id = space->def->id;
+       trigger = sql_triggers_exist(space_def, TK_INSERT, NULL, &tmask);
 
        bool is_view = space_def->opts.is_view;
        assert((trigger != NULL && tmask != 0) ||
@@ -356,7 +354,7 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
         *
         * This is the 2nd template.
         */
-       if (pColumn == 0 &&
+       if (pColumn == NULL &&
            xferOptimization(pParse, space, pSelect, on_error)) {
                assert(trigger == NULL);
                assert(pList == 0);
@@ -518,7 +516,8 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
                }
        }
 
-       if (pColumn == 0 && nColumn && nColumn != (int)space_def->field_count) {
+       if (pColumn == NULL && nColumn != 0 &&
+           nColumn != (int)space_def->field_count) {
                sqlite3ErrorMsg(pParse,
                                "table %S has %d columns but %d values were supplied",
                                pTabList, 0, space_def->field_count, nColumn);
@@ -589,9 +588,8 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
                                                          regCols + i + 1);
                                } else {
                                        struct Expr *dflt = NULL;
-                                       dflt = space_column_default_expr(
-                                               space_id,
-                                               i);
+                                       dflt = space_def->fields[i].
+                                               default_value_expr;
                                        sqlite3ExprCode(pParse,
                                                        dflt,
                                                        regCols + i + 1);
@@ -655,9 +653,7 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
                                        continue;
                                }
                                struct Expr *dflt = NULL;
-                               dflt = space_column_default_expr(
-                                       space_id,
-                                       i);
+                               dflt = space_def->fields[i].default_value_expr;
                                sqlite3ExprCodeFactorable(pParse,
                                                          dflt,
                                                          iRegStore);
@@ -797,7 +793,7 @@ sqlite3Insert(Parse * pParse,       /* Parser context */
 
        /* Return the number of rows inserted. */
        if ((user_session->sql_flags & SQLITE_CountRows) != 0 &&
-           pParse->trigger_space == NULL) {
+           pParse->updated_space == NULL) {
                sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
                sqlite3VdbeSetNumCols(v, 1);
                sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted",
diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c
index 226ed80d0..86d51280d 100644
--- a/src/box/sql/resolve.c
+++ b/src/box/sql/resolve.c
@@ -238,7 +238,7 @@ lookupName(Parse * pParse,  /* The parsing context */
                        for (i = 0, pItem = pSrcList->a; i < pSrcList->nSrc;
                             i++, pItem++) {
                                assert(pItem->space != NULL &&
-                                       pItem->space->def->name != NULL);
+                                      pItem->space->def->name != NULL);
                                struct space_def *space_def = pItem->space->def;
                                assert(space_def->field_count > 0);
                                if (pItem->pSelect
@@ -311,23 +311,23 @@ lookupName(Parse * pParse,        /* The parsing context */
                /* If we have not already resolved the name, then maybe
                 * it is a new.* or old.* trigger argument reference
                 */
-               if (zTab != 0 && cntTab == 0
-                   && pParse->trigger_space != 0) {
+               if (zTab != NULL && cntTab == 0
+                   && pParse->updated_space != NULL) {
                        int op = pParse->eTriggerOp;
                        assert(op == TK_DELETE || op == TK_UPDATE
                               || op == TK_INSERT);
                        if (op != TK_DELETE && sqlite3StrICmp("new", zTab) == 0) {
                                pExpr->iTable = 1;
-                               space_def = pParse->trigger_space->def;
+                               space_def = pParse->updated_space->def;
                        } else if (op != TK_INSERT
                                   && sqlite3StrICmp("old", zTab) == 0) {
                                pExpr->iTable = 0;
-                               space_def = pParse->trigger_space->def;
+                               space_def = pParse->updated_space->def;
                        } else {
-                               space_def = 0;
+                               space_def = NULL;
                        }
 
-                       if (space_def) {
+                       if (space_def != NULL) {
                                int iCol;
                                cntTab++;
                                for (iCol = 0; iCol <
@@ -1614,20 +1614,23 @@ sqlite3ResolveSelectNames(Parse * pParse,       /* The parser context */
 }
 
 void
-sql_resolve_self_reference(struct Parse *parser, struct space *space, int type,
+sql_resolve_self_reference(struct Parse *parser, struct space_def *def, int type,
                           struct Expr *expr, struct ExprList *expr_list)
 {
-       /* Fake SrcList for parser->pNewTable */
+       /* Fake SrcList for parser->updated_space */
        SrcList sSrc;
-       /* Name context for parser->pNewTable */
+       /* Name context for parser->updated_space */
        NameContext sNC;
 
        assert(type == NC_IsCheck || type == NC_IdxExpr);
        memset(&sNC, 0, sizeof(sNC));
        memset(&sSrc, 0, sizeof(sSrc));
        sSrc.nSrc = 1;
-       sSrc.a[0].zName = space->def->name;
-       sSrc.a[0].space = space;
+       sSrc.a[0].zName = def->name;
+       struct space tmp_space;
+       memset(&tmp_space, 0, sizeof(tmp_space));
+       tmp_space.def = def;
+       sSrc.a[0].space = &tmp_space;
        sSrc.a[0].iCursor = -1;
        sNC.pParse = parser;
        sNC.pSrcList = &sSrc;
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index 55739d96a..79b8eafca 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -419,11 +419,11 @@ sqlite3JoinType(Parse * pParse, Token * pA, Token * pB, Token * pC)
  * is not contained in the table.
  */
 static int
-columnIndex(struct space *space, const char *zCol)
+columnIndex(struct space_def *def, const char *zCol)
 {
        int i;
-       for (i = 0; i < (int)space->def->field_count; i++) {
-               if (strcmp(space->def->fields[i].name, zCol) == 0)
+       for (i = 0; i < (int)def->field_count; i++) {
+               if (strcmp(def->fields[i].name, zCol) == 0)
                        return i;
        }
        return -1;
@@ -450,7 +450,7 @@ tableAndColumnIndex(SrcList * pSrc, /* Array of tables to search */
 
        assert((piTab == 0) == (piCol == 0));   /* Both or neither are NULL */
        for (i = 0; i < N; i++) {
-               iCol = columnIndex(pSrc->a[i].space, zCol);
+               iCol = columnIndex(pSrc->a[i].space->def, zCol);
                if (iCol >= 0) {
                        if (piTab) {
                                *piTab = i;
@@ -490,8 +490,8 @@ addWhereTerm(Parse * pParse,        /* Parsing context */
 
        assert(iLeft < iRight);
        assert(pSrc->nSrc > iRight);
-       assert(pSrc->a[iLeft].space);
-       assert(pSrc->a[iRight].space);
+       assert(pSrc->a[iLeft].space != NULL);
+       assert(pSrc->a[iRight].space != NULL);
 
        pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
        pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
@@ -581,7 +581,7 @@ sqliteProcessJoin(Parse * pParse, Select * p)
                struct space *right_space = pRight->space;
                int isOuter;
 
-               if (NEVER(left_space == 0 || right_space == 0))
+               if (NEVER(left_space == NULL || right_space == NULL))
                        continue;
                isOuter = (pRight->fg.jointype & JT_OUTER) != 0;
 
@@ -645,7 +645,7 @@ sqliteProcessJoin(Parse * pParse, Select * p)
                                int iRightCol;  /* Column number of matching column on the right */
 
                                zName = pList->a[j].zName;
-                               iRightCol = columnIndex(right_space, zName);
+                               iRightCol = columnIndex(right_space->def, zName);
                                if (iRightCol < 0
                                    || !tableAndColumnIndex(pSrc, i + 1, zName,
                                                            &iLeft, &iLeftCol)
@@ -1931,10 +1931,6 @@ cleanup:
 
 }
 
-/*
-
- */
-
 /**
  * Add type and collation information to a column list based on
  * a SELECT statement.
@@ -1948,12 +1944,12 @@ cleanup:
  * statement be resolved.
  *
  * @param pParse Parsing contexts.
- * @param space Add column type information to this table.
+ * @param def Add column type information to this space.
  * @param pSelect SELECT used to determine types and collations.
  */
 void
-sqlite3SelectAddColumnTypeAndCollation(Parse * pParse, struct space * space,
-                                      Select * pSelect)
+sqlite3SelectAddColumnTypeAndCollation(Parse *pParse, struct space_def *def,
+                                      Select *pSelect)
 {
        sqlite3 *db = pParse->db;
        NameContext sNC;
@@ -1962,27 +1958,27 @@ sqlite3SelectAddColumnTypeAndCollation(Parse * pParse, struct space * space,
 
        assert(pSelect != 0);
        assert((pSelect->selFlags & SF_Resolved) != 0);
-       assert((int)space->def->field_count == pSelect->pEList->nExpr ||
+       assert((int)def->field_count == pSelect->pEList->nExpr ||
               db->mallocFailed);
        if (db->mallocFailed)
                return;
        memset(&sNC, 0, sizeof(sNC));
        sNC.pSrcList = pSelect->pSrc;
        a = pSelect->pEList->a;
-       for (uint32_t i = 0; i < space->def->field_count; i++) {
+       for (uint32_t i = 0; i < def->field_count; i++) {
                p = a[i].pExpr;
                char affinity = sqlite3ExprAffinity(p);
                if (affinity == 0)
                        affinity = AFFINITY_BLOB;
-               space->def->fields[i].affinity = affinity;
-               space->def->fields[i].type = sql_affinity_to_field_type(affinity);
+               def->fields[i].affinity = affinity;
+               def->fields[i].type = sql_affinity_to_field_type(affinity);
                bool is_found;
                uint32_t coll_id;
 
-               if (space->def->fields[i].coll_id == COLL_NONE &&
+               if (def->fields[i].coll_id == COLL_NONE &&
                    sql_expr_coll(pParse, p, &is_found, &coll_id) &&
                    coll_id != COLL_NONE)
-                       space->def->fields[i].coll_id = coll_id;
+                       def->fields[i].coll_id = coll_id;
        }
 }
 
@@ -2007,7 +2003,6 @@ sqlite3ResultSetOfSelect(Parse * pParse, Select * pSelect)
                pSelect = pSelect->pPrior;
        user_session->sql_flags = savedFlags;
        struct space *space = sql_ephemeral_space_new(pParse, NULL);
-
        if (space == NULL)
                return 0;
        /* The sqlite3ResultSetOfSelect() is only used in contexts where lookaside
@@ -2015,10 +2010,9 @@ sqlite3ResultSetOfSelect(Parse * pParse, Select * pSelect)
         */
        assert(db->lookaside.bDisable);
        sqlite3ColumnsFromExprList(pParse, pSelect->pEList, space->def);
-       sqlite3SelectAddColumnTypeAndCollation(pParse, space, pSelect);
-       if (db->mallocFailed) {
+       sqlite3SelectAddColumnTypeAndCollation(pParse, space->def, pSelect);
+       if (db->mallocFailed)
                return 0;
-       }
        return space;
 }
 
@@ -4378,7 +4372,7 @@ is_simple_count(struct Select *select, struct AggInfo *agg_info)
 int
 sqlite3IndexedByLookup(Parse * pParse, struct SrcList_item *pFrom)
 {
-       if (pFrom->space && pFrom->fg.isIndexedBy) {
+       if (pFrom->space != NULL && pFrom->fg.isIndexedBy) {
                struct space *space = pFrom->space;
                char *zIndexedBy = pFrom->u1.zIndexedBy;
                struct index *idx = NULL;
@@ -4576,7 +4570,7 @@ withExpand(Walker * pWalker, struct SrcList_item *pFrom)
        struct Cte *pCte;       /* Matched CTE (or NULL if no match) */
        With *pWith;            /* WITH clause that pCte belongs to */
 
-       assert(pFrom->space == 0);
+       assert(pFrom->space == NULL);
 
        pCte = searchWith(pParse->pWith, pFrom, &pWith);
        if (pCte) {
@@ -4598,7 +4592,7 @@ withExpand(Walker * pWalker, struct SrcList_item *pFrom)
                if (cannotBeFunction(pParse, pFrom))
                        return SQLITE_ERROR;
 
-               assert(pFrom->space == 0);
+               assert(pFrom->space == NULL);
                pFrom->space = sql_ephemeral_space_new(pParse, pCte->zName);
                if (pFrom->space == NULL)
                        return WRC_Abort;
@@ -4759,21 +4753,21 @@ selectExpander(Walker * pWalker, Select * p)
         * then create a transient space structure to describe the subquery.
         */
        for (i = 0, pFrom = pTabList->a; i < pTabList->nSrc; i++, pFrom++) {
-               assert(pFrom->fg.isRecursive == 0 || pFrom->space != 0);
+               assert(pFrom->fg.isRecursive == 0 || pFrom->space != NULL);
                if (pFrom->fg.isRecursive)
                        continue;
-               assert(pFrom->space == 0);
+               assert(pFrom->space == NULL);
 #ifndef SQLITE_OMIT_CTE
                if (withExpand(pWalker, pFrom))
                        return WRC_Abort;
-               if (pFrom->space) {
+               if (pFrom->space != NULL) {
                } else
 #endif
                if (pFrom->zName == 0) {
                        Select *pSel = pFrom->pSelect;
                        /* A sub-query in the FROM clause of a SELECT */
                        assert(pSel != 0);
-                       assert(pFrom->space == 0);
+                       assert(pFrom->space == NULL);
                        if (sqlite3WalkSelect(pWalker, pSel))
                                return WRC_Abort;
                        /*
@@ -4781,10 +4775,10 @@ selectExpander(Walker * pWalker, Select * p)
                         * unique identifier.
                         */
                        const char *name = "sqlite_sq_DEADBEAFDEADBEAF";
-                       struct space *space;
-                       pFrom->space = space =
+                       pFrom->space =
                            sql_ephemeral_space_new(sqlite3ParseToplevel(pParse),
                                                    name);
+                       struct space *space = pFrom->space;
 
                        if (space == NULL)
                                return WRC_Abort;
@@ -4907,7 +4901,7 @@ selectExpander(Walker * pWalker, Select * p)
                                        struct space *space = pFrom->space;
                                        Select *pSub = pFrom->pSelect;
                                        char *zTabName = pFrom->zAlias;
-                                       if (zTabName == 0)
+                                       if (zTabName == NULL)
                                                zTabName = space->def->name;
                                        if (db->mallocFailed)
                                                break;
@@ -4916,7 +4910,7 @@ selectExpander(Walker * pWalker, Select * p)
                                                selFlags & SF_NestedFrom) ==
                                            0) {
                                                pSub = 0;
-                                               if (zTName
+                                               if (zTName != NULL
                                                    && strcmp(zTName, zTabName)
                                                    != 0) {
                                                        continue;
@@ -5112,7 +5106,7 @@ selectAddSubqueryTypeInfo(Walker * pWalker, Select * p)
        pTabList = p->pSrc;
        for (i = 0, pFrom = pTabList->a; i < pTabList->nSrc; i++, pFrom++) {
                struct space *space = pFrom->space;
-               assert(space != 0);
+               assert(space != NULL);
                if (space->def->id == 0) {
                        /* A sub-query in the FROM clause of a SELECT */
                        Select *pSel = pFrom->pSelect;
@@ -5120,7 +5114,7 @@ selectAddSubqueryTypeInfo(Walker * pWalker, Select * p)
                                while (pSel->pPrior)
                                        pSel = pSel->pPrior;
                                sqlite3SelectAddColumnTypeAndCollation(pParse,
-                                                                      space,
+                                                                      space->def,
                                                                       pSel);
                        }
                }
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index 6ace02253..c9ec9b1ca 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -2712,7 +2712,6 @@ struct Parse {
        int nSelect;            /* Number of SELECT statements seen */
        int nSelectIndent;      /* How far to indent SELECTTRACE() output */
        Parse *pToplevel;       /* Parse structure for main program (or NULL) */
-       struct space *trigger_space;    /* Space triggers are being coded for */
        u32 nQueryLoop;         /* Est number of iterations of a query (10*log2(N)) */
        u32 oldmask;            /* Mask of old.* columns referenced */
        u32 newmask;            /* Mask of new.* columns referenced */
@@ -2755,7 +2754,8 @@ struct Parse {
        VList *pVList;          /* Mapping between variable names and numbers */
        Vdbe *pReprepare;       /* VM being reprepared (sqlite3Reprepare()) */
        const char *zTail;      /* All SQL text past the last semicolon parsed */
-       struct space *new_space;        /* A space being constructed by CREATE TABLE */
+       struct space *updated_space;    /* A space being constructed by CREATE TABLE
+                                          or space we are coding triggers for */
        TriggerPrg *pTriggerPrg;        /* Linked list of coded triggers */
        With *pWith;            /* Current WITH clause, or NULL */
        With *pWithToFree;      /* Free this WITH object at the end of the parse */
@@ -3327,14 +3327,15 @@ sql_space_column_is_in_pk(struct space *space, uint32_t);
 int sqlite3ColumnsFromExprList(Parse *parse, ExprList *expr_list,
                               struct space_def *space_def);
 
-void sqlite3SelectAddColumnTypeAndCollation(Parse *, struct space *, Select *);
+void
+sqlite3SelectAddColumnTypeAndCollation(Parse *, struct space_def *, Select *);
 struct space *sqlite3ResultSetOfSelect(Parse *, Select *);
 
 /**
  * Return the PRIMARY KEY index of a table.
  */
 struct index *
-sql_table_primary_key(const struct space *space);
+sql_space_primary_key(const struct space *space);
 
 void sqlite3StartTable(Parse *, Token *, int);
 void sqlite3AddColumn(Parse *, Token *, struct type_def *);
@@ -3476,8 +3477,8 @@ void sqlite3IdListDelete(sqlite3 *, IdList *);
  * index and tbl_name is the name of the table that is to be
  * indexed.  Both will be NULL for a primary key or an index that
  * is created to satisfy a UNIQUE constraint.  If tbl_name and
- * name are NULL, use parse->pNewTable as the table to be indexed.
- * parse->pNewTable is a table that is currently being
+ * name are NULL, use parse->updated_space as the table to be indexed.
+ * parse->updated_space is a space that is currently being
  * constructed by a CREATE TABLE statement.
  *
  * col_list is a list of columns to be indexed.  col_list will be
@@ -3486,7 +3487,7 @@ void sqlite3IdListDelete(sqlite3 *, IdList *);
  *
  * @param parse All information about this parse.
  * @param token Index name. May be NULL.
- * @param tbl_name Table to index. Use pParse->pNewTable ifNULL.
+ * @param tbl_name Table to index. Use pParse->updated_space ifNULL.
  * @param col_list A list of columns to be indexed.
  * @param start The CREATE token that begins this statement.
  * @param sort_order Sort order of primary key when pList==NULL.
@@ -3978,8 +3979,7 @@ vdbe_code_drop_trigger(struct Parse *parser, const char *trigger_name,
  */
 struct sql_trigger *
 sql_triggers_exist(struct space_def *space_def, int op,
-                  struct ExprList *changes_list,
-                  int *mask_ptr);
+                  struct ExprList *changes_list, int *mask_ptr);
 
 /**
  * This is called to code the required FOR EACH ROW triggers for
diff --git a/src/box/sql/tarantoolInt.h b/src/box/sql/tarantoolInt.h
index 4db541115..7f1c3b403 100644
--- a/src/box/sql/tarantoolInt.h
+++ b/src/box/sql/tarantoolInt.h
@@ -131,19 +131,19 @@ tarantoolSqlite3IncrementMaxid(uint64_t *space_max_id);
 /**
  * Encode format as entry to be inserted to _space on @region.
  * @param region Region to use.
- * @param table Space to encode.
+ * @param def Space definition to encode.
  * @param[out] size Size of result allocation.
  *
  * @retval NULL Error.
  * @retval not NULL Pointer to msgpack on success.
  */
 char *
-sql_encode_table(struct region *region, struct space *space, uint32_t *size);
+sql_encode_table(struct region *region, struct space_def *def, uint32_t *size);
 
 /**
  * Encode "opts" dictionary for _space entry on @region.
  * @param region Region to use.
- * @param space Space containing opts to encode.
+ * @param def Space definition containing opts to encode.
  * @param sql Source request to encode.
  * @param[out] size Size of result allocation.
  *
@@ -151,7 +151,7 @@ sql_encode_table(struct region *region, struct space *space, uint32_t *size);
  * @retval not NULL Pointer to msgpack on success.
  */
 char *
-sql_encode_table_opts(struct region *region, struct space *space,
+sql_encode_table_opts(struct region *region, struct space_def *def,
                      const char *sql, uint32_t *size);
 
 /**
diff --git a/src/box/sql/tokenize.c b/src/box/sql/tokenize.c
index 5be4c57f6..3102c75bd 100644
--- a/src/box/sql/tokenize.c
+++ b/src/box/sql/tokenize.c
@@ -449,7 +449,7 @@ sqlite3RunParser(Parse * pParse, const char *zSql, char **pzErrMsg)
                sqlite3OomFault(db);
                return SQLITE_NOMEM_BKPT;
        }
-       assert(pParse->new_space == 0);
+       assert(pParse->updated_space == NULL);
        assert(pParse->parsed_ast.trigger == NULL);
        assert(pParse->nVar == 0);
        assert(pParse->pVList == 0);
@@ -529,7 +529,7 @@ sqlite3RunParser(Parse * pParse, const char *zSql, char **pzErrMsg)
                sqlite3VdbeDelete(pParse->pVdbe);
                pParse->pVdbe = 0;
        }
-       sql_space_delete(db, pParse->new_space);
+       sql_space_delete(db, pParse->updated_space);
        if (pParse->pWithToFree)
                sqlite3WithDelete(db, pParse->pWithToFree);
        sqlite3DbFree(db, pParse->pVList);
diff --git a/src/box/sql/treeview.c b/src/box/sql/treeview.c
index a97ffdded..49b798c29 100644
--- a/src/box/sql/treeview.c
+++ b/src/box/sql/treeview.c
@@ -222,7 +222,7 @@ sqlite3TreeViewSelect(TreeView * pView, const Select * p, u8 moreToFollow)
                                if (pItem->zName) {
                                        sqlite3XPrintf(&x, " %s", pItem->zName);
                                }
-                               if (pItem->space) {
+                               if (pItem->space != NULL) {
                                        sqlite3XPrintf(&x, " tabname=%Q",
                                                       pItem->space->def->name);
                                }
diff --git a/src/box/sql/trigger.c b/src/box/sql/trigger.c
index 7f1d18342..e3c87c422 100644
--- a/src/box/sql/trigger.c
+++ b/src/box/sql/trigger.c
@@ -559,8 +559,7 @@ checkColumnOverlap(IdList * pIdList, ExprList * pEList)
 
 struct sql_trigger *
 sql_triggers_exist(struct space_def *space_def, int op,
-                  struct ExprList *changes_list,
-                  int *mask_ptr)
+                  struct ExprList *changes_list, int *mask_ptr)
 {
        int mask = 0;
        struct sql_trigger *trigger_list = NULL;
@@ -615,7 +614,7 @@ codeTriggerProgram(Parse * pParse,  /* The parser context */
        Vdbe *v = pParse->pVdbe;
        sqlite3 *db = pParse->db;
 
-       assert(pParse->trigger_space && pParse->pToplevel);
+       assert(pParse->updated_space != NULL && pParse->pToplevel != NULL);
        assert(pStepList);
        assert(v != 0);
 
@@ -800,7 +799,7 @@ sql_row_trigger_program(struct Parse *parser, struct sql_trigger *trigger,
        sql_parser_create(pSubParse, db);
        memset(&sNC, 0, sizeof(sNC));
        sNC.pParse = pSubParse;
-       pSubParse->trigger_space = space;
+       pSubParse->updated_space = space;
        pSubParse->pToplevel = pTop;
        pSubParse->eTriggerOp = trigger->op;
        pSubParse->nQueryLoop = parser->nQueryLoop;
diff --git a/src/box/sql/update.c b/src/box/sql/update.c
index 3b7122360..f7b7dc10d 100644
--- a/src/box/sql/update.c
+++ b/src/box/sql/update.c
@@ -295,7 +295,7 @@ sqlite3Update(Parse * pParse,               /* The parser context */
        /* Initialize the count of updated rows
         */
        if ((user_session->sql_flags & SQLITE_CountRows)
-           && !pParse->trigger_space) {
+           && pParse->updated_space == NULL) {
                regRowCount = ++pParse->nMem;
                sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
        }
@@ -503,7 +503,7 @@ sqlite3Update(Parse * pParse,               /* The parser context */
        /* Increment the row counter
         */
        if ((user_session->sql_flags & SQLITE_CountRows)
-           && !pParse->trigger_space) {
+           && pParse->updated_space == NULL) {
                sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
        }
 
@@ -525,7 +525,7 @@ sqlite3Update(Parse * pParse,               /* The parser context */
 
        /* Return the number of rows that were changed. */
        if (user_session->sql_flags & SQLITE_CountRows &&
-           pParse->trigger_space == NULL) {
+           pParse->updated_space == NULL) {
                sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
                sqlite3VdbeSetNumCols(v, 1);
                sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated",
diff --git a/src/box/sql/where.c b/src/box/sql/where.c
index c26661a20..fbbb05dff 100644
--- a/src/box/sql/where.c
+++ b/src/box/sql/where.c
@@ -4103,7 +4103,7 @@ where_loop_builder_shortcut(struct WhereLoopBuilder *builder)
                loop->rRun = 33;
        } else {
                assert(loop->aLTermSpace == loop->aLTerm);
-               struct space *space = space_by_id(space_def->id);
+               struct space *space = item->space;
                if (space != NULL) {
                        for (uint32_t i = 0; i < space->index_count; ++i) {
                                struct index_def *idx_def =
@@ -4834,7 +4834,7 @@ sqlite3WhereEnd(WhereInfo * pWInfo)
                int k, last;
                VdbeOp *pOp;
                struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
-               assert(pTabItem->space != 0);
+               assert(pTabItem->space != NULL);
                pLoop = pLevel->pWLoop;
 
                /* For a co-routine, change all OP_Column references to the table of
-- 
2.14.3 (Apple Git-98)
</pre>
  </body>
</html>