[tarantool-patches] [PATCH 2/8] sql: use field type instead of affinity for type_def
Nikita Pettik
korablev at tarantool.org
Fri Dec 28 12:34:46 MSK 2018
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
More information about the Tarantool-patches
mailing list