Skip to content

Commit a65cfa6

Browse files
committed
sql: replace BLOB as column type with SCALAR
BLOB column type is represented by SCALAR field type in terms of NoSQL. We attempted at emulating BLOB behaviour, but such efforts turn out to be not decent enough. For this reason, we've decided to abandon these attempts and fairly replace it with SCALAR column type. SCALAR column type acts in the same way as it does in NoSQL: it is aggregator-type for INTEGER, NUMBER and STRING types. So, column declared with this type can contain values of these three (available in SQL) types. For instance: CREATE TABLE t1 (a SCALAR PRIMARY KEY); INSERT INTO t1 VALUES ('1'); SELECT * FROM t1 WHERE a = 1; [] -- Result is empty set since column "a" contains string literal value '1', not integer value 1. It is worth noting that CAST operator in this case does nothing: CAST(123 AS SCALAR); -- Returns 123 (integer) CAST('abc' AS SCALAR); -- Returns 'abc' (string) Still, we consider BLOB values as entries encoded in msgpack with MP_BIN format. To make this happen, values to be operated should be represented in BLOB form x'...' (e.g. x'000000'). What is more, there are two built-in functions returning BLOBs: randomblob() and zeroblob(). Closes #4019
1 parent 165f831 commit a65cfa6

27 files changed

+84
-111
lines changed

extra/mkkeywordhash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ static Keyword aKeywordTable[] = {
109109
{ "BEFORE", "TK_BEFORE", TRIGGER, false },
110110
{ "BEGIN", "TK_BEGIN", TRIGGER, true },
111111
{ "BETWEEN", "TK_BETWEEN", ALWAYS, true },
112-
{ "BLOB", "TK_BLOB_KW", RESERVED, true },
113112
{ "BY", "TK_BY", ALWAYS, true },
114113
{ "CASCADE", "TK_CASCADE", FKEY, false },
115114
{ "CASE", "TK_CASE", ALWAYS, true },
@@ -193,6 +192,7 @@ static Keyword aKeywordTable[] = {
193192
{ "ROLLBACK", "TK_ROLLBACK", ALWAYS, true },
194193
{ "ROW", "TK_ROW", TRIGGER, true },
195194
{ "SAVEPOINT", "TK_SAVEPOINT", ALWAYS, true },
195+
{ "SCALAR", "TK_SCALAR", ALWAYS, true },
196196
{ "SELECT", "TK_SELECT", ALWAYS, true },
197197
{ "SET", "TK_SET", ALWAYS, true },
198198
{ "SIMPLE", "TK_SIMPLE", ALWAYS, true },

src/box/sql/parse.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ wqlist(A) ::= wqlist(A) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. {
14771477
////////////////////////////// TYPE DECLARATION ///////////////////////////////
14781478
%type typedef {struct type_def}
14791479
typedef(A) ::= TEXT . { A.type = FIELD_TYPE_STRING; }
1480-
typedef(A) ::= BLOB_KW . { A.type = FIELD_TYPE_SCALAR; }
1480+
typedef(A) ::= SCALAR . { A.type = FIELD_TYPE_SCALAR; }
14811481
/**
14821482
* Time-like types are temporary disabled, until they are
14831483
* implemented as a native Tarantool types (gh-3694).

src/box/sql/vdbe.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ mem_apply_type(struct Mem *record, enum field_type type)
364364
record->flags &= ~(MEM_Real | MEM_Int);
365365
return 0;
366366
case FIELD_TYPE_SCALAR:
367-
if (record->flags & (MEM_Str | MEM_Blob))
368-
record->flags |= MEM_Blob;
369367
return 0;
370368
default:
371369
return -1;

src/box/sql/vdbemem.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -617,19 +617,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type)
617617
}
618618
switch (type) {
619619
case FIELD_TYPE_SCALAR:
620-
if (pMem->flags & MEM_Blob)
621-
return SQL_OK;
622-
if (pMem->flags & MEM_Str) {
623-
MemSetTypeFlag(pMem, MEM_Blob);
624-
return SQL_OK;
625-
}
626-
if (pMem->flags & MEM_Int || pMem->flags & MEM_Real) {
627-
if (sqlVdbeMemStringify(pMem, 1) != 0)
628-
return -1;
629-
MemSetTypeFlag(pMem, MEM_Blob);
630-
return 0;
631-
}
632-
return SQL_ERROR;
620+
return 0;
633621
case FIELD_TYPE_INTEGER:
634622
if ((pMem->flags & MEM_Blob) != 0) {
635623
if (sql_atoi64(pMem->z, (int64_t *) &pMem->u.i,

test/sql-tap/analyze9.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ test:do_execsql_test(
391391
INSERT INTO t1 VALUES(null, 4, 4);
392392
INSERT INTO t1 VALUES(null, 5, 5);
393393
ANALYZE;
394-
CREATE TABLE x1(tbl TEXT, idx TEXT , neq TEXT, nlt TEXT, ndlt TEXT, sample BLOB, PRIMARY KEY(tbl, idx, sample));
394+
CREATE TABLE x1(tbl TEXT, idx TEXT , neq TEXT, nlt TEXT, ndlt TEXT, sample SCALAR, PRIMARY KEY(tbl, idx, sample));
395395
INSERT INTO x1 SELECT * FROM "_sql_stat4";
396396
DELETE FROM "_sql_stat4";
397397
INSERT INTO "_sql_stat4" SELECT * FROM x1;

test/sql-tap/blob.test.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ test:do_test(
170170
"blob-2.0",
171171
function()
172172
test:execsql [[
173-
CREATE TABLE t1(a BLOB primary key, b BLOB);
173+
CREATE TABLE t1(a SCALAR primary key, b SCALAR);
174174
INSERT INTO t1 VALUES(X'123456', x'7890ab');
175175
INSERT INTO t1 VALUES(X'CDEF12', x'345678');
176176
]]
@@ -186,7 +186,7 @@ test:do_test(
186186
-- </blob-2.0>
187187
})
188188

189-
-- An index on a blob column
189+
-- An index on a SCALAR column
190190
test:do_test(
191191
"blob-2.1",
192192
function()

test/sql-tap/boundary1.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test:do_test(
2626
"boundary1-1.1",
2727
function()
2828
return test:execsql([[
29-
CREATE TABLE t1(rowid INT primary key, a INT ,x BLOB);
29+
CREATE TABLE t1(rowid INT primary key, a INT ,x SCALAR);
3030
INSERT INTO t1(rowid,a,x) VALUES(-8388609,1,'ffffffffff7fffff');
3131
INSERT INTO t1(rowid,a,x) VALUES(-36028797018963969,2,'ff7fffffffffffff');
3232
INSERT INTO t1(rowid,a,x) VALUES(9223372036854775807,3,'7fffffffffffffff');

test/sql-tap/boundary3.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test:do_test(
2626
"boundary3-1.1",
2727
function()
2828
return test:execsql([[
29-
CREATE TABLE t1(rowid INT primary key, a FLOAT ,x BLOB);
29+
CREATE TABLE t1(rowid INT primary key, a FLOAT ,x SCALAR);
3030
INSERT INTO t1(rowid,a,x) VALUES(-8388609,1,'ffffffffff7fffff');
3131
INSERT INTO t1(rowid,a,x) VALUES(-36028797018963969,2,'ff7fffffffffffff');
3232
INSERT INTO t1(rowid,a,x) VALUES(9223372036854775806,3,'7fffffffffffffff');

test/sql-tap/cast.test.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test:plan(82)
2222
-- Only run these tests if the build includes the CAST operator
2323

2424

25-
-- Tests for the CAST( AS blob), CAST( AS text) and CAST( AS numeric) built-ins
25+
-- Tests for the CAST( AS SCALAR), CAST( AS text) and CAST( AS numeric) built-ins
2626
--
2727
test:do_execsql_test(
2828
"cast-1.1",
@@ -77,7 +77,7 @@ test:do_catchsql_test(
7777
test:do_execsql_test(
7878
"cast-1.7",
7979
[[
80-
SELECT CAST(x'616263' AS blob)
80+
SELECT CAST(x'616263' AS SCALAR)
8181
]], {
8282
-- <cast-1.7>
8383
"abc"
@@ -87,7 +87,7 @@ test:do_execsql_test(
8787
test:do_execsql_test(
8888
"cast-1.8",
8989
[[
90-
SELECT typeof(CAST(x'616263' AS blob))
90+
SELECT typeof(CAST(x'616263' AS SCALAR))
9191
]], {
9292
-- <cast-1.8>
9393
"blob"
@@ -167,7 +167,7 @@ test:do_execsql_test(
167167
test:do_execsql_test(
168168
"cast-1.17",
169169
[[
170-
SELECT CAST(NULL AS blob)
170+
SELECT CAST(NULL AS SCALAR)
171171
]], {
172172
-- <cast-1.17>
173173
""
@@ -177,7 +177,7 @@ test:do_execsql_test(
177177
test:do_execsql_test(
178178
"cast-1.18",
179179
[[
180-
SELECT typeof(CAST(NULL AS blob))
180+
SELECT typeof(CAST(NULL AS SCALAR))
181181
]], {
182182
-- <cast-1.18>
183183
"null"
@@ -267,20 +267,20 @@ test:do_execsql_test(
267267
test:do_execsql_test(
268268
"cast-1.27",
269269
[[
270-
SELECT CAST(123 AS blob)
270+
SELECT CAST(123 AS SCALAR)
271271
]], {
272272
-- <cast-1.27>
273-
"123"
273+
123
274274
-- </cast-1.27>
275275
})
276276

277277
test:do_execsql_test(
278278
"cast-1.28",
279279
[[
280-
SELECT typeof(CAST(123 AS blob))
280+
SELECT typeof(CAST(123 AS SCALAR))
281281
]], {
282282
-- <cast-1.28>
283-
"blob"
283+
"integer"
284284
-- </cast-1.28>
285285
})
286286

@@ -367,20 +367,20 @@ test:do_execsql_test(
367367
test:do_execsql_test(
368368
"cast-1.37",
369369
[[
370-
SELECT CAST(123.456 AS blob)
370+
SELECT CAST(123.456 AS SCALAR)
371371
]], {
372372
-- <cast-1.37>
373-
"123.456"
373+
123.456
374374
-- </cast-1.37>
375375
})
376376

377377
test:do_execsql_test(
378378
"cast-1.38",
379379
[[
380-
SELECT typeof(CAST(123.456 AS blob))
380+
SELECT typeof(CAST(123.456 AS SCALAR))
381381
]], {
382382
-- <cast-1.38>
383-
"blob"
383+
"real"
384384
-- </cast-1.38>
385385
})
386386

@@ -457,10 +457,10 @@ test:do_catchsql_test(
457457
test:do_execsql_test(
458458
"cast-1.48",
459459
[[
460-
SELECT typeof(CAST('123abc' AS blob))
460+
SELECT typeof(CAST('123abc' AS SCALAR))
461461
]], {
462462
-- <cast-1.48>
463-
"blob"
463+
"text"
464464
-- </cast-1.48>
465465
})
466466

test/sql-tap/default.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test:do_execsql_test(
2828
CREATE TABLE t1(
2929
rowid INTEGER PRIMARY KEY AUTOINCREMENT,
3030
a INTEGER,
31-
b BLOB DEFAULT x'6869'
31+
b SCALAR DEFAULT x'6869'
3232
);
3333
INSERT INTO t1(a) VALUES(1);
3434
SELECT a, b from t1;

test/sql-tap/delete4.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test:do_execsql_test(
5656
2.1,
5757
[[
5858
DROP TABLE IF EXISTS t1;
59-
CREATE TABLE t1(x INTEGER PRIMARY KEY, y INT , z BLOB);
59+
CREATE TABLE t1(x INTEGER PRIMARY KEY, y INT , z SCALAR);
6060
INSERT INTO t1 VALUES(1, 0, randomblob(200));
6161
INSERT INTO t1 VALUES(2, 1, randomblob(200));
6262
INSERT INTO t1 VALUES(3, 0, randomblob(200));

test/sql-tap/distinct.test.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ if (1 > 0) then
222222
---------------------------------------------------------------------------
223223
-- Ticket [fccbde530a6583bf2748400919f1603d5425995c] (2014-01-08)
224224
-- The logic that computes DISTINCT sometimes thinks that a zeroblob()
225-
-- and a blob of all zeros are different when they should be the same.
225+
-- and a blob of all zeros are different when they should be the same.
226226
--
227227
test:do_execsql_test(
228228
4.1,
@@ -236,13 +236,8 @@ if (1 > 0) then
236236
INSERT INTO t1 VALUES(4,2);
237237
INSERT INTO t1 VALUES(5,3);
238238
INSERT INTO t1 VALUES(6,1);
239-
CREATE TABLE t2(x BLOB primary key);
240-
INSERT INTO t2
241-
SELECT DISTINCT
242-
CASE a WHEN 1 THEN x'0000000000'
243-
WHEN 2 THEN zeroblob(5)
244-
ELSE 'xyzzy' END
245-
FROM t1;
239+
CREATE TABLE t2(x SCALAR primary key);
240+
INSERT INTO t2 SELECT DISTINCT CASE a WHEN 1 THEN x'0000000000' WHEN 2 THEN zeroblob(5) ELSE 'xyzzy' END FROM t1;
246241
SELECT quote(x) FROM t2 ORDER BY 1;
247242
]], {
248243
-- <4.1>

test/sql-tap/e_expr.test.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,15 +3034,13 @@ test:do_execsql_test(
30343034
--
30353035
do_expr_test("e_expr-27.2.1", " CAST(NULL AS integer) ", "null", "")
30363036
do_expr_test("e_expr-27.2.2", " CAST(NULL AS text) ", "null", "")
3037-
do_expr_test("e_expr-27.2.3", " CAST(NULL AS blob) ", "null", "")
3037+
do_expr_test("e_expr-27.2.3", " CAST(NULL AS SCALAR) ", "null", "")
30383038
do_expr_test("e_expr-27.2.4", " CAST(NULL AS numeric) ", "null", "")
3039-
-- EVIDENCE-OF: R-22956-37754 Casting to a BLOB consists of first casting
3040-
-- the value to TEXT in the encoding of the database connection, then
3041-
-- interpreting the resulting byte sequence as a BLOB instead of as TEXT.
3039+
-- Casting to a SCALAR doesn't affect original value.
30423040
--
3043-
do_qexpr_test("e_expr-27.4.1", " CAST('ghi' AS blob) ", "X'676869'")
3044-
do_qexpr_test("e_expr-27.4.2", " CAST(456 AS blob) ", "X'343536'")
3045-
do_qexpr_test("e_expr-27.4.3", " CAST(1.78 AS blob) ", "X'312E3738'")
3041+
do_qexpr_test("e_expr-27.4.1", " CAST('ghi' AS SCALAR) ", "X'676869'")
3042+
do_qexpr_test("e_expr-27.4.2", " CAST(456 AS SCALAR) ", "X'343536'")
3043+
do_qexpr_test("e_expr-27.4.3", " CAST(1.78 AS SCALAR) ", "X'312E3738'")
30463044

30473045
-- EVIDENCE-OF: R-22235-47006 Casting an INTEGER or REAL value into TEXT
30483046
-- renders the value as if via sql_snprintf() except that the
@@ -3170,7 +3168,7 @@ do_expr_test("e_expr-32.2.4", [[
31703168
CAST(9223372036854775807 AS NUMERIC)
31713169
]], "real", 9223372036854775807)
31723170
-- EVIDENCE-OF: R-64550-29191 Note that the result from casting any
3173-
-- non-BLOB value into a BLOB and the result from casting any BLOB value
3171+
-- non-BLOB value into a SCALAR and the result from casting any SCALAR value
31743172
-- into a non-BLOB value may be different depending on whether the
31753173
-- database encoding is UTF-8
31763174
--

test/sql-tap/in3.test.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,12 @@ test:do_test(
271271
DROP TABLE IF EXISTS t1;
272272
DROP TABLE IF EXISTS t1;
273273
274-
CREATE TABLE t1(id INT primary key, a BLOB, b FLOAT ,c TEXT);
274+
CREATE TABLE t1(id INT primary key, a SCALAR, b FLOAT ,c TEXT);
275275
CREATE UNIQUE INDEX t1_i1 ON t1(a); /* no affinity */
276276
CREATE UNIQUE INDEX t1_i2 ON t1(b); /* numeric affinity */
277277
CREATE UNIQUE INDEX t1_i3 ON t1(c); /* text affinity */
278278
279-
CREATE TABLE t2(id INT primary key, x BLOB, y FLOAT, z TEXT);
279+
CREATE TABLE t2(id INT primary key, x SCALAR, y FLOAT, z TEXT);
280280
CREATE UNIQUE INDEX t2_i1 ON t2(x); /* no affinity */
281281
CREATE UNIQUE INDEX t2_i2 ON t2(y); /* numeric affinity */
282282
CREATE UNIQUE INDEX t2_i3 ON t2(z); /* text affinity */
@@ -318,9 +318,8 @@ test:do_test(
318318
test:do_test(
319319
"in3-3.4",
320320
function()
321-
-- BLOB is compatible with TEXT, however index can't
322-
-- be used since under the hood BLOB is SCALAR (which
323-
-- can contain not only STRING values) and TEXT is STRING.
321+
-- SCALAR is compatible with TEXT, however index can't
322+
-- be used since SCALAR can accept not only string values.
324323
return exec_neph(" SELECT x IN (SELECT c FROM t1) FROM t2 ")
325324
end, {
326325
-- <in3-3.4>

test/sql-tap/index4.test.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ testprefix = "index4"
2222
test:do_execsql_test(
2323
1.1,
2424
[[
25-
CREATE TABLE t1(x BLOB primary key);
25+
CREATE TABLE t1(x SCALAR primary key);
2626
START TRANSACTION;
2727
INSERT INTO t1 VALUES(randomblob(102));
2828
INSERT INTO t1 SELECT randomblob(102) FROM t1; -- 2
@@ -78,7 +78,7 @@ test:do_execsql_test(
7878
1.6,
7979
[[
8080
DROP TABLE t1;
81-
CREATE TABLE t1(x BLOB primary key);
81+
CREATE TABLE t1(x SCALAR primary key);
8282
START TRANSACTION;
8383
INSERT INTO t1 VALUES('a');
8484
INSERT INTO t1 VALUES('b');

test/sql-tap/substr.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test:plan(93)
2525
--
2626
test:execsql [[
2727
CREATE TABLE t1(id integer primary key --autoincrement
28-
, t text, b blob)
28+
, t text, b SCALAR)
2929
]]
3030

3131
local function substr_test(id, string, i1, i2, result)

test/sql-tap/table.test.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ test:do_catchsql_test(
495495
key int,
496496
"14_vac" int,
497497
fuzzy_dog_12 varchar(10),
498-
beginn blob,
499-
endd blob
498+
beginn SCALAR,
499+
endd SCALAR
500500
)
501501
]=], {
502502
-- <table-7.1>
@@ -551,8 +551,8 @@ test:do_execsql2_test(
551551
key int,
552552
"14_vac" int,
553553
fuzzy_dog_12 varchar(10),
554-
beginn blob,
555-
endd blob
554+
beginn SCALAR,
555+
endd SCALAR
556556
);
557557
INSERT INTO t2 SELECT * from weird;
558558
SELECT * FROM t2;
@@ -704,7 +704,7 @@ test:do_catchsql_test(
704704
test:do_catchsql_test(
705705
"table-9.2",
706706
[[
707-
CREATE TABLE t6(a varchar(100) primary key, b blob, a integer);
707+
CREATE TABLE t6(a varchar(100) primary key, b SCALAR, a integer);
708708
]], {
709709
-- <table-9.2>
710710
1, "Space field 'A' is duplicate"
@@ -895,8 +895,8 @@ test:do_execsql_test(
895895
b FLOAT,
896896
c VARCHAR(8),
897897
d VARCHAR(9),
898-
e blob,
899-
f BLOB,
898+
e SCALAR,
899+
f SCALAR,
900900
g Text,
901901
h text
902902
);
@@ -1142,7 +1142,7 @@ test:do_test(
11421142
-- DROP TABLE IF EXISTS t1;
11431143
-- BEGIN;
11441144
-- CREATE TABLE t1 AS SELECT zeroblob(2e20);
1145-
-- } {1 {string or blob too big}}
1145+
-- } {1 {string or SCALAR too big}}
11461146
-- do_execsql_test table-18.2 {
11471147
-- COMMIT;
11481148
-- PRAGMA integrity_check;

test/sql-tap/tkt1443.test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test:do_test(
8585
CREATE TABLE Users (
8686
userId INTEGER PRIMARY KEY,
8787
"user" TEXT UNIQUE,
88-
salt BLOB,
88+
salt SCALAR,
8989
password TEXT
9090
);
9191
INSERT INTO Users VALUES(1, 'test', 'Šæ%s',

0 commit comments

Comments
 (0)