Skip to content

Commit 9ee5cf8

Browse files
Gerold103kyukhin
authored andcommitted
tuple: implement update by field name
Tuple fields can be named, accessed by name, indexed by name, but till this commit field names couldn't be used in update operations. Now it is possible. This patch is a teaser of updates by JSON path. Part of #1261
1 parent 6d4430d commit 9ee5cf8

File tree

18 files changed

+247
-97
lines changed

18 files changed

+247
-97
lines changed

src/box/errcode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ struct errcode_record {
205205
/*150 */_(ER_CANT_CREATE_COLLATION, "Failed to initialize collation: %s.") \
206206
/*151 */_(ER_WRONG_COLLATION_OPTIONS, "Wrong collation options (field %u): %s") \
207207
/*152 */_(ER_NULLABLE_PRIMARY, "Primary index of space '%s' can not contain nullable parts") \
208-
/*153 */_(ER_NO_SUCH_FIELD_NAME, "Field '%s' was not found in space '%s' format") \
208+
/*153 */_(ER_NO_SUCH_FIELD_NAME_IN_SPACE, "Field '%s' was not found in space '%s' format") \
209209
/*154 */_(ER_TRANSACTION_YIELD, "Transaction has been aborted by a fiber yield") \
210210
/*155 */_(ER_NO_SUCH_GROUP, "Replication group '%s' does not exist") \
211211
/*156 */_(ER_SQL_BIND_VALUE, "Bind value for parameter %s is out of range for type %s") \
@@ -253,6 +253,7 @@ struct errcode_record {
253253
/*198 */_(ER_FUNC_INDEX_FUNC, "Failed to build a key for functional index '%s' of space '%s': %s") \
254254
/*199 */_(ER_FUNC_INDEX_FORMAT, "Key format doesn't match one defined in functional index '%s' of space '%s': %s") \
255255
/*200 */_(ER_FUNC_INDEX_PARTS, "Wrong functional index definition: %s") \
256+
/*201 */_(ER_NO_SUCH_FIELD_NAME, "Field '%s' was not found in the tuple") \
256257

257258
/*
258259
* !IMPORTANT! Please follow instructions at start of the file

src/box/lua/tuple.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ lbox_tuple_transform(struct lua_State *L)
439439
const char *old_data = tuple_data_range(tuple, &bsize);
440440
struct region *region = &fiber()->gc;
441441
size_t used = region_used(region);
442+
struct tuple_format *format = tuple_format(tuple);
442443
struct tuple *new_tuple = NULL;
443444
/*
444445
* Can't use box_tuple_update() since transform must reset
@@ -449,8 +450,8 @@ lbox_tuple_transform(struct lua_State *L)
449450
*/
450451
const char *new_data =
451452
tuple_update_execute(buf->buf, buf->buf + ibuf_used(buf),
452-
old_data, old_data + bsize, &new_size, 1,
453-
NULL);
453+
old_data, old_data + bsize, format->dict,
454+
&new_size, 1, NULL);
454455
if (new_data != NULL)
455456
new_tuple = tuple_new(box_tuple_format_default(),
456457
new_data, new_data + new_size);

src/box/memtx_space.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,16 @@ memtx_space_execute_update(struct space *space, struct txn *txn,
388388

389389
/* Update the tuple; legacy, request ops are in request->tuple */
390390
uint32_t new_size = 0, bsize;
391+
struct tuple_format *format = space->format;
391392
const char *old_data = tuple_data_range(old_tuple, &bsize);
392393
const char *new_data =
393394
tuple_update_execute(request->tuple, request->tuple_end,
394-
old_data, old_data + bsize,
395+
old_data, old_data + bsize, format->dict,
395396
&new_size, request->index_base, NULL);
396397
if (new_data == NULL)
397398
return -1;
398399

399-
stmt->new_tuple = memtx_tuple_new(space->format, new_data,
400+
stmt->new_tuple = memtx_tuple_new(format, new_data,
400401
new_data + new_size);
401402
if (stmt->new_tuple == NULL)
402403
return -1;
@@ -442,6 +443,7 @@ memtx_space_execute_upsert(struct space *space, struct txn *txn,
442443
if (index_get(index, key, part_count, &old_tuple) != 0)
443444
return -1;
444445

446+
struct tuple_format *format = space->format;
445447
if (old_tuple == NULL) {
446448
/**
447449
* Old tuple was not found. A write optimized
@@ -460,11 +462,11 @@ memtx_space_execute_upsert(struct space *space, struct txn *txn,
460462
* @sa https://github.com/tarantool/tarantool/issues/1156
461463
*/
462464
if (tuple_update_check_ops(request->ops, request->ops_end,
465+
format->dict,
463466
request->index_base) != 0) {
464467
return -1;
465468
}
466-
stmt->new_tuple = memtx_tuple_new(space->format,
467-
request->tuple,
469+
stmt->new_tuple = memtx_tuple_new(format, request->tuple,
468470
request->tuple_end);
469471
if (stmt->new_tuple == NULL)
470472
return -1;
@@ -482,12 +484,13 @@ memtx_space_execute_upsert(struct space *space, struct txn *txn,
482484
const char *new_data =
483485
tuple_upsert_execute(request->ops, request->ops_end,
484486
old_data, old_data + bsize,
485-
&new_size, request->index_base,
486-
false, &column_mask);
487+
format->dict, &new_size,
488+
request->index_base, false,
489+
&column_mask);
487490
if (new_data == NULL)
488491
return -1;
489492

490-
stmt->new_tuple = memtx_tuple_new(space->format, new_data,
493+
stmt->new_tuple = memtx_tuple_new(format, new_data,
491494
new_data + new_size);
492495
if (stmt->new_tuple == NULL)
493496
return -1;

src/box/space.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ space_before_replace(struct space *space, struct txn *txn,
397397
old_data_end = old_data + old_size;
398398
new_data = tuple_update_execute(request->tuple,
399399
request->tuple_end, old_data,
400-
old_data_end, &new_size,
400+
old_data_end,
401+
space->format->dict, &new_size,
401402
request->index_base, NULL);
402403
if (new_data == NULL)
403404
return -1;
@@ -420,6 +421,7 @@ space_before_replace(struct space *space, struct txn *txn,
420421
new_data_end = request->tuple_end;
421422
if (tuple_update_check_ops(request->ops,
422423
request->ops_end,
424+
space->format->dict,
423425
request->index_base) != 0)
424426
return -1;
425427
break;
@@ -428,8 +430,9 @@ space_before_replace(struct space *space, struct txn *txn,
428430
old_data_end = old_data + old_size;
429431
new_data = tuple_upsert_execute(request->ops, request->ops_end,
430432
old_data, old_data_end,
431-
&new_size, request->index_base,
432-
false, NULL);
433+
space->format->dict, &new_size,
434+
request->index_base, false,
435+
NULL);
433436
new_data_end = new_data + new_size;
434437
break;
435438
default:

src/box/sql/insert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ sqlInsert(Parse * pParse, /* Parser context */
373373
}
374374
}
375375
if (j >= (int) space_def->field_count) {
376-
diag_set(ClientError, ER_NO_SUCH_FIELD_NAME,
376+
diag_set(ClientError,
377+
ER_NO_SUCH_FIELD_NAME_IN_SPACE,
377378
pColumn->a[i].zName,
378379
pTabList->a[0].zName);
379380
pParse->is_aborted = true;

src/box/sql/resolve.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ lookupName(Parse * pParse, /* The parsing context */
431431
if (zTab == NULL) {
432432
diag_set(ClientError, ER_SQL_CANT_RESOLVE_FIELD, zCol);
433433
} else {
434-
diag_set(ClientError, ER_NO_SUCH_FIELD_NAME, zCol,
435-
zTab);
434+
diag_set(ClientError, ER_NO_SUCH_FIELD_NAME_IN_SPACE,
435+
zCol, zTab);
436436
}
437437
pParse->is_aborted = true;
438438
pTopNC->nErr++;

src/box/sql/update.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ sqlUpdate(Parse * pParse, /* The parser context */
167167
}
168168
}
169169
if (j >= (int)def->field_count) {
170-
diag_set(ClientError, ER_NO_SUCH_FIELD_NAME,
170+
diag_set(ClientError, ER_NO_SUCH_FIELD_NAME_IN_SPACE,
171171
pChanges->a[i].zName, def->name);
172172
pParse->is_aborted = true;
173173
goto update_cleanup;

src/box/tuple.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,15 +707,15 @@ box_tuple_update(box_tuple_t *tuple, const char *expr, const char *expr_end)
707707
const char *old_data = tuple_data_range(tuple, &bsize);
708708
struct region *region = &fiber()->gc;
709709
size_t used = region_used(region);
710+
struct tuple_format *format = tuple_format(tuple);
710711
const char *new_data =
711712
tuple_update_execute(expr, expr_end, old_data, old_data + bsize,
712-
&new_size, 1, NULL);
713+
format->dict, &new_size, 1, NULL);
713714
if (new_data == NULL) {
714715
region_truncate(region, used);
715716
return NULL;
716717
}
717-
struct tuple *ret = tuple_new(tuple_format(tuple), new_data,
718-
new_data + new_size);
718+
struct tuple *ret = tuple_new(format, new_data, new_data + new_size);
719719
region_truncate(region, used);
720720
if (ret != NULL)
721721
return tuple_bless(ret);
@@ -729,16 +729,16 @@ box_tuple_upsert(box_tuple_t *tuple, const char *expr, const char *expr_end)
729729
const char *old_data = tuple_data_range(tuple, &bsize);
730730
struct region *region = &fiber()->gc;
731731
size_t used = region_used(region);
732+
struct tuple_format *format = tuple_format(tuple);
732733
const char *new_data =
733734
tuple_upsert_execute(expr, expr_end, old_data, old_data + bsize,
734-
&new_size, 1, false, NULL);
735+
format->dict, &new_size, 1, false, NULL);
735736
if (new_data == NULL) {
736737
region_truncate(region, used);
737738
return NULL;
738739
}
739740

740-
struct tuple *ret = tuple_new(tuple_format(tuple),
741-
new_data, new_data + new_size);
741+
struct tuple *ret = tuple_new(format, new_data, new_data + new_size);
742742
region_truncate(region, used);
743743
if (ret != NULL)
744744
return tuple_bless(ret);

0 commit comments

Comments
 (0)