[tarantool-patches] Re: [PATCH v2 1/5] sql: introduce structs assembling DDL arguments during parsing

n.pettik korablev at tarantool.org
Tue Jan 29 22:03:44 MSK 2019



> On 24 Jan 2019, at 15:30, Konstantin Osipov <kostja at tarantool.org> wrote:
> 
> * n.pettik <korablev at tarantool.org> [19/01/24 13:51]:
>>>> +	 * One of parse_def structures which are used to
>>>> +	 * assemble and carry arguments of DDL routines
>>>> +	 * from parse.y
>>>> +	 */
>>>> +	void *alter_entity_def;
>>> 
>>> Please consider adding a type code to the base entity def, so that
>>> you can use a base struct reference rather than void *. 
>> 
>> Please, look at code and explain why do we need type
>> code? At first I added it, but it turned out that such code
>> was completely useless. The only real usage was to
>> assert that type of structure matches with its code.
> 
> To assert the structure matches with its code.
> 
> -- 
> Konstantin Osipov, Moscow, Russia, +7 903 626 22 32
> http://tarantool.io - www.twitter.com/kostja_osipov

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index d0e19407a..dfaa6b505 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -717,6 +717,7 @@ sqlite3AddPrimaryKey(Parse * pParse,        /* Parsing context */
        struct create_entity_def create_def = {};
        struct create_constraint_def constr_def = {};
        struct create_index_def idx_def = {};
+       alter_def.entity_type = ENTITY_TYPE_INDEX;
        create_def.base = &alter_def;
        constr_def.base = &create_def;
        idx_def.base = &constr_def;
@@ -1715,6 +1716,7 @@ sql_drop_table(struct Parse *parse_context, bool is_view)
 {
        struct drop_entity_def *drop_def = parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TABLE);
        struct SrcList *table_name_list = alter_def->entity_name;
        struct Vdbe *v = sqlite3GetVdbe(parse_context);
        struct sqlite3 *db = parse_context->db;
@@ -1821,6 +1823,7 @@ sql_create_foreign_key(struct Parse *parse_context)
        struct create_constraint_def *create_constr_def = create_fk_def->base;
        struct create_entity_def *create_def = create_constr_def->base;
        struct alter_entity_def *alter_def = create_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_FK);
        /*
         * When this function is called second time during
         * <CREATE TABLE ...> statement (i.e. at VDBE runtime),
@@ -2041,6 +2044,7 @@ sql_drop_foreign_key(struct Parse *parse_context)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_FK);
        const char *table_name = alter_def->entity_name->a[0].zName;
        assert(table_name != NULL);
        struct space *child = space_by_name(table_name);
@@ -2257,6 +2261,7 @@ sql_create_index(struct Parse *parse) {
        struct create_constraint_def *create_constr_def = create_idx_def->base;
        struct create_entity_def *create_entity_def = create_constr_def->base;
        struct alter_entity_def *alter_entity_def = create_entity_def->base;
+       assert(alter_entity_def->entity_type == ENTITY_TYPE_INDEX);
        /*
         * Get list of columns to be indexed. It will be NULL if
         * this is a primary key or unique-constraint on the most
@@ -2580,6 +2585,7 @@ sql_drop_index(struct Parse *parse_context)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parse_context->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_INDEX);
        struct Vdbe *v = sqlite3GetVdbe(parse_context);
        assert(v != NULL);
        struct sqlite3 *db = parse_context->db;
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index b0327c27a..1d7f13122 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -408,7 +408,8 @@ drop_table ::= ifexists(E) fullname(X) . {
   if (alter_def == NULL)
     break;
   struct drop_entity_def *drop_def =
-    drop_entity_def_new(pParse, alter_def, (struct Token){}, E);
+    drop_entity_def_new(pParse, alter_def, ENTITY_TYPE_TABLE,
+                        (struct Token){}, E);
   if (drop_def == NULL)
     break;
   pParse->alter_entity_def = (void *) drop_def;
@@ -1284,6 +1285,7 @@ cmd ::= createkw uniqueflag(U) INDEX ifnotexists(NE) nm(X)
   struct create_entity_def create_def = {};
   struct create_constraint_def constr_def = {};
   struct create_index_def idx_def = {};
+  alter_def.entity_type = ENTITY_TYPE_INDEX;
   alter_def.entity_name = sqlite3SrcListAppend(pParse->db,0,&Y);
   create_def.base = &alter_def;
   create_def.name = X;
@@ -1358,6 +1360,7 @@ collate(C) ::= COLLATE id.   {C = 1;}
 cmd ::= DROP INDEX ifexists(E) nm(X) ON fullname(Y).   {
   struct alter_entity_def alter_def = {};
   struct drop_entity_def drop_def = {};
+  alter_def.entity_type = ENTITY_TYPE_INDEX;
   alter_def.entity_name = Y;
   drop_def.base = &alter_def;
   drop_def.if_exist = E;
@@ -1417,6 +1420,7 @@ trigger_decl(A) ::= TRIGGER ifnotexists(NOERR) nm(B)
   struct alter_entity_def alter_def = {};
   struct create_entity_def create_def = {};
   struct create_trigger_def trigger_def = {};
+  alter_def.entity_type = ENTITY_TYPE_TRIGGER;
   alter_def.entity_name = E;
   create_def.base = &alter_def;
   create_def.name = B;
@@ -1539,6 +1543,7 @@ raisetype(A) ::= FAIL.      {A = ON_CONFLICT_ACTION_FAIL;}
 cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). {
   struct alter_entity_def alter_def = {};
   struct drop_entity_def drop_def = {};
+  alter_def.entity_type = ENTITY_TYPE_TRIGGER;
   alter_def.entity_name = X;
   drop_def.base = &alter_def;
   drop_def.if_exist = NOERR;
@@ -1600,7 +1605,7 @@ cmd ::= ALTER TABLE fullname(X) DROP CONSTRAINT nm(Z). {
   if (alter_def == NULL)
     break;
   struct drop_entity_def *drop_def =
-    drop_entity_def_new(pParse, alter_def, Z, false);
+    drop_entity_def_new(pParse, alter_def, ENTITY_TYPE_FK, Z, false);
   if (drop_def == NULL)
     break;
   pParse->alter_entity_def = (void *) drop_def;
diff --git a/src/box/sql/parse_def.c b/src/box/sql/parse_def.c
index 7f241636a..d3581e4bb 100644
--- a/src/box/sql/parse_def.c
+++ b/src/box/sql/parse_def.c
@@ -41,6 +41,7 @@ alter_entity_def_new(struct Parse *parse, struct SrcList *name)
                parse->nErr++;
                return NULL;
        }
+       alter_def->entity_type = ENTITY_TYPE_TABLE;
        alter_def->entity_name = name;
        return alter_def;
 }
@@ -82,7 +83,8 @@ create_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
 
 struct drop_entity_def *
 drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
-                   struct Token entity_name, bool if_exist)
+                   enum entity_type type, struct Token entity_name,
+                   bool if_exist)
 {
        size_t sz = sizeof(struct drop_entity_def);
        struct drop_entity_def *drop_def = region_alloc(&parse->region, sz);
@@ -92,6 +94,7 @@ drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->entity_type = type;
        drop_def->base = base;
        drop_def->name = entity_name;
        drop_def->if_exist = if_exist;
@@ -129,6 +132,7 @@ create_fk_def_new(struct Parse *parse, struct create_constraint_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->base->base->entity_type = ENTITY_TYPE_FK;
        fk_def->base = base;
        fk_def->child_cols = child_cols;
        fk_def->parent_name = parent_name;
@@ -150,6 +154,7 @@ create_index_def_new(struct Parse *parse, struct create_constraint_def *base,
                parse->nErr++;
                return NULL;
        }
+       base->base->base->entity_type = ENTITY_TYPE_INDEX;
        idx_def->base = base;
        idx_def->cols = cols;
        idx_def->idx_type = idx_type;
diff --git a/src/box/sql/parse_def.h b/src/box/sql/parse_def.h
index 2f6d3d047..4ed7c977a 100644
--- a/src/box/sql/parse_def.h
+++ b/src/box/sql/parse_def.h
@@ -66,7 +66,18 @@
  * CREATE TRIGGER tr1 ...
  * ALTER *TABLE* -> CREATE ENTITY -> CREATE TRIGGER
  */
+
+enum entity_type {
+       ENTITY_TYPE_TABLE = 0,
+       ENTITY_TYPE_INDEX,
+       ENTITY_TYPE_TRIGGER,
+       ENTITY_TYPE_FK,
+       entity_type_MAX
+};
+
 struct alter_entity_def {
+       /** Type of topmost entity. */
+       enum entity_type entity_type;
        /** As a rule it is a name of table to be altered. */
        struct SrcList *entity_name;
 };
@@ -153,7 +164,7 @@ create_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
 
 struct drop_entity_def *
 drop_entity_def_new(struct Parse *parse, struct alter_entity_def *base,
-                   struct Token name, bool if_exist);
+                   enum entity_type type, struct Token name, bool if_exist);
 
 struct create_constraint_def *
 create_constraint_def_new(struct Parse *parse, struct create_entity_def *base,
diff --git a/src/box/sql/trigger.c b/src/box/sql/trigger.c
index 685343ce9..c6c9c0393 100644
--- a/src/box/sql/trigger.c
+++ b/src/box/sql/trigger.c
@@ -71,6 +71,7 @@ sql_trigger_begin(struct Parse *parse)
        struct create_trigger_def *trigger_def = parse->alter_entity_def;
        struct create_entity_def *create_def = trigger_def->base;
        struct alter_entity_def *alter_def = create_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TRIGGER);
 
        char *trigger_name = NULL;
        if (alter_def->entity_name == NULL || db->mallocFailed)
@@ -426,6 +427,7 @@ sql_drop_trigger(struct Parse *parser)
        struct drop_entity_def *drop_def =
                (struct drop_entity_def *) parser->alter_entity_def;
        struct alter_entity_def *alter_def = drop_def->base;
+       assert(alter_def->entity_type == ENTITY_TYPE_TRIGGER);
        struct SrcList *name = alter_def->entity_name;
        bool no_err = drop_def->if_exist;
        sqlite3 *db = parser->db;





More information about the Tarantool-patches mailing list