Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 2/8] sql: use field type instead of affinity for type_def
Date: Fri, 28 Dec 2018 11:34:46 +0200	[thread overview]
Message-ID: <ecb93e536df9617dd6c80dda06c453087726429b.1545987214.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1545987214.git.korablev@tarantool.org>
In-Reply-To: <cover.1545987214.git.korablev@tarantool.org>

Also, this allows to delay affinity assignment to field def until
encoding of table format.

Part of #3698
---
 src/box/sql.c           |  4 +++-
 src/box/sql/build.c     | 23 +++++++++++++++++++++--
 src/box/sql/parse.y     | 26 +++++++++++++-------------
 src/box/sql/sqliteInt.h |  5 ++++-
 test/sql/types.result   |  4 ++--
 5 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/src/box/sql.c b/src/box/sql.c
index 8c7607d84..a06c50dca 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -1006,7 +1006,9 @@ sql_encode_table(struct region *region, struct Table *table, uint32_t *size)
 		       action_is_nullable(def->fields[i].nullable_action));
 		mpstream_encode_str(&stream, field_type_strs[field->type]);
 		mpstream_encode_str(&stream, "affinity");
-		mpstream_encode_uint(&stream, def->fields[i].affinity);
+		enum affinity_type aff =
+			sql_field_type_to_affinity(def->fields[i].type);
+		mpstream_encode_uint(&stream, aff);
 		mpstream_encode_str(&stream, "is_nullable");
 		mpstream_encode_bool(&stream, def->fields[i].is_nullable);
 		mpstream_encode_str(&stream, "nullable_action");
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 49b90b5d0..beaafe1bc 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -501,6 +501,26 @@ sql_affinity_to_field_type(enum affinity_type affinity)
 	}
 }
 
+enum affinity_type
+sql_field_type_to_affinity(enum field_type field_type)
+{
+	switch (field_type) {
+		case FIELD_TYPE_INTEGER:
+		case FIELD_TYPE_UNSIGNED:
+			return AFFINITY_INTEGER;
+		case FIELD_TYPE_NUMBER:
+			return AFFINITY_REAL;
+		case FIELD_TYPE_STRING:
+			return AFFINITY_TEXT;
+		case FIELD_TYPE_SCALAR:
+			return AFFINITY_BLOB;
+		case FIELD_TYPE_ANY:
+			return AFFINITY_UNDEFINED;
+		default:
+			unreachable();
+	}
+}
+
 /*
  * Add a new column to the table currently being constructed.
  *
@@ -563,8 +583,7 @@ sqlite3AddColumn(Parse * pParse, Token * pName, struct type_def *type_def)
 	 */
 	column_def->nullable_action = ON_CONFLICT_ACTION_DEFAULT;
 	column_def->is_nullable = true;
-	column_def->affinity = type_def->type;
-	column_def->type = sql_affinity_to_field_type(column_def->affinity);
+	column_def->type = type_def->type;
 	p->def->field_count++;
 	pParse->constraintName.n = 0;
 }
diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index b664a4101..50bc25152 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -898,7 +898,7 @@ expr(A) ::= expr(A) COLLATE id(C). {
 expr(A) ::= CAST(X) LP expr(E) AS typedef(T) RP(Y). {
   spanSet(&A,&X,&Y); /*A-overwrites-X*/
   A.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, 0, 1);
-  A.pExpr->affinity = T.type;
+  A.pExpr->affinity = sql_field_type_to_affinity(T.type);
   sqlite3ExprAttachSubtrees(pParse->db, A.pExpr, E.pExpr, 0);
 }
 %endif  SQLITE_OMIT_CAST
@@ -1475,17 +1475,17 @@ wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
 }
 %endif  SQLITE_OMIT_CTE
 
-/* Primitive types. */
+////////////////////////////// TYPE DECLARATION ///////////////////////////////
 %type typedef {struct type_def}
-typedef(A) ::= TEXT . { A.type = AFFINITY_TEXT; }
-typedef(A) ::= BLOB . { A.type = AFFINITY_BLOB; }
-typedef(A) ::= DATE . { A.type = AFFINITY_REAL; }
-typedef(A) ::= TIME . { A.type = AFFINITY_REAL; }
-typedef(A) ::= DATETIME . { A.type = AFFINITY_REAL; }
+typedef(A) ::= TEXT . { A.type = FIELD_TYPE_STRING; }
+typedef(A) ::= BLOB . { A.type = FIELD_TYPE_SCALAR; }
+typedef(A) ::= DATE . { A.type = FIELD_TYPE_NUMBER; }
+typedef(A) ::= TIME . { A.type = FIELD_TYPE_NUMBER; }
+typedef(A) ::= DATETIME . { A.type = FIELD_TYPE_NUMBER; }
 
 %type char_len {int}
 typedef(A) ::= CHAR . {
-  A.type = AFFINITY_TEXT;
+  A.type = FIELD_TYPE_STRING;
 }
 
 char_len(A) ::= LP INTEGER(B) RP . {
@@ -1494,23 +1494,23 @@ char_len(A) ::= LP INTEGER(B) RP . {
 }
 
 typedef(A) ::= CHAR char_len(B) . {
-  A.type = AFFINITY_TEXT;
+  A.type = FIELD_TYPE_STRING;
   (void) B;
 }
 
 typedef(A) ::= VARCHAR char_len(B) . {
-  A.type = AFFINITY_TEXT;
+  A.type = FIELD_TYPE_STRING;
   (void) B;
 }
 
 %type number_typedef {struct type_def}
 typedef(A) ::= number_typedef(A) .
-number_typedef(A) ::= FLOAT|REAL|DOUBLE . { A.type = AFFINITY_REAL; }
-number_typedef(A) ::= INT|INTEGER . { A.type = AFFINITY_INTEGER; }
+number_typedef(A) ::= FLOAT|REAL|DOUBLE . { A.type = FIELD_TYPE_NUMBER; }
+number_typedef(A) ::= INT|INTEGER . { A.type = FIELD_TYPE_INTEGER; }
 
 %type number_len_typedef {struct type_def}
 number_typedef(A) ::= DECIMAL|NUMERIC|NUM number_len_typedef(B) . {
-  A.type = AFFINITY_REAL;
+  A.type = FIELD_TYPE_NUMBER;
   (void) B;
 }
 
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index 4110a5991..b7403d650 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -1634,7 +1634,7 @@ struct sqlite3 {
  * VARCHAR(<number of chars>).
  */
 struct type_def {
-	enum affinity_type type;
+	enum field_type type;
 };
 
 /*
@@ -3436,6 +3436,9 @@ sql_create_view(struct Parse *parse_context, struct Token *begin,
 enum field_type
 sql_affinity_to_field_type(enum affinity_type affinity);
 
+enum affinity_type
+sql_field_type_to_affinity(enum field_type field_type);
+
 /**
  * Compile view, i.e. create struct Select from
  * 'CREATE VIEW...' string, and assign cursors to each table from
diff --git a/test/sql/types.result b/test/sql/types.result
index 1daeb7a8c..915a6341a 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -44,8 +44,8 @@ box.sql.execute("CREATE VIEW v1 AS SELECT b + a, b - a FROM t1;")
 ...
 box.space.V1:format()
 ---
-- [{'affinity': 67, 'type': 'number', 'nullable_action': 'none', 'name': 'b + a',
-    'is_nullable': true}, {'affinity': 67, 'type': 'number', 'nullable_action': 'none',
+- [{'affinity': 69, 'type': 'number', 'nullable_action': 'none', 'name': 'b + a',
+    'is_nullable': true}, {'affinity': 69, 'type': 'number', 'nullable_action': 'none',
     'name': 'b - a', 'is_nullable': true}]
 ...
 -- gh-2494: index's part also features correct declared type.
-- 
2.15.1

  parent reply	other threads:[~2018-12-28  9:34 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-28  9:34 [tarantool-patches] [PATCH 0/8] Eliminate affinity from source code Nikita Pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 1/8] sql: remove SQLITE_ENABLE_UPDATE_DELETE_LIMIT define Nikita Pettik
2018-12-29 17:42   ` [tarantool-patches] " Vladislav Shpilevoy
2019-01-16 14:25     ` n.pettik
2018-12-28  9:34 ` Nikita Pettik [this message]
2018-12-29 17:42   ` [tarantool-patches] Re: [PATCH 2/8] sql: use field type instead of affinity for type_def Vladislav Shpilevoy
2019-01-16 14:26     ` n.pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 3/8] sql: remove numeric affinity Nikita Pettik
2018-12-29  9:01   ` [tarantool-patches] " Konstantin Osipov
2018-12-29 17:42   ` Vladislav Shpilevoy
2019-01-09  8:26     ` Konstantin Osipov
2019-01-16 14:26     ` n.pettik
2019-01-22 15:41       ` Vladislav Shpilevoy
2019-01-28 16:39         ` n.pettik
2019-01-30 13:04           ` Vladislav Shpilevoy
2019-02-01 16:39             ` n.pettik
2019-01-09  8:20   ` Konstantin Osipov
2018-12-28  9:34 ` [tarantool-patches] [PATCH 4/8] sql: replace affinity with field type for func Nikita Pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 5/8] sql: replace field type with affinity for VDBE runtime Nikita Pettik
2018-12-29 17:42   ` [tarantool-patches] " Vladislav Shpilevoy
2019-01-16 14:26     ` n.pettik
2019-01-22 15:41       ` Vladislav Shpilevoy
2019-01-28 16:39         ` n.pettik
2019-01-30 13:04           ` Vladislav Shpilevoy
2019-02-01 16:39             ` n.pettik
2019-02-05 15:08               ` Vladislav Shpilevoy
2019-02-05 17:46                 ` n.pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 6/8] sql: replace affinity with field type in struct Expr Nikita Pettik
2018-12-29 17:42   ` [tarantool-patches] " Vladislav Shpilevoy
2019-01-16 14:26     ` n.pettik
2019-01-22 15:41       ` Vladislav Shpilevoy
2019-01-28 16:39         ` n.pettik
2019-01-30 13:04           ` Vladislav Shpilevoy
2019-02-01 16:39             ` n.pettik
2019-02-05 15:08               ` Vladislav Shpilevoy
2019-02-05 17:46                 ` n.pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 7/8] sql: clean-up affinity from SQL source code Nikita Pettik
2018-12-29 17:42   ` [tarantool-patches] " Vladislav Shpilevoy
2019-01-16 14:26     ` n.pettik
2019-01-22 15:41       ` Vladislav Shpilevoy
2019-01-28 16:40         ` n.pettik
2019-01-30 13:04           ` Vladislav Shpilevoy
2019-02-01 16:39             ` n.pettik
2019-02-05 15:08               ` Vladislav Shpilevoy
2019-02-05 17:46                 ` n.pettik
2018-12-28  9:34 ` [tarantool-patches] [PATCH 8/8] Remove affinity from field definition Nikita Pettik
2019-02-05 19:41 ` [tarantool-patches] Re: [PATCH 0/8] Eliminate affinity from source code Vladislav Shpilevoy
2019-02-08 13:37 ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ecb93e536df9617dd6c80dda06c453087726429b.1545987214.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH 2/8] sql: use field type instead of affinity for type_def' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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