forked from cmu-db/noisepage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.tpl
74 lines (66 loc) · 2.51 KB
/
update.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// UPDATE test_1 SET colA = 100000 + colA WHERE colA BETWEEN 495 AND 505
// Returns the tuples with values BETWEEN 100495 AND 100505 (11)
fun main(execCtx: *ExecutionContext) -> int32 {
var count = 0 // output count
// Init updater
var test1_oid = @testCatalogLookup(execCtx, "test_1", "")
var col_oids: [4]uint32
col_oids[0] = @testCatalogLookup(execCtx, "test_1", "colA")
col_oids[1] = @testCatalogLookup(execCtx, "test_1", "colB")
col_oids[2] = @testCatalogLookup(execCtx, "test_1", "colC")
col_oids[3] = @testCatalogLookup(execCtx, "test_1", "colD")
var updater: StorageInterface
@storageInterfaceInit(&updater, execCtx, test1_oid, col_oids, true)
// Init index iterator
var index : IndexIterator
var index1_oid = @testCatalogIndexLookup(execCtx, "index_1")
@indexIteratorInit(&index, execCtx, 1, test1_oid, index1_oid, col_oids)
// Set iteration bounds
var lo_index_pr = @indexIteratorGetLoPR(&index)
var hi_index_pr = @indexIteratorGetHiPR(&index)
@prSetInt(lo_index_pr, 0, @intToSql(495)) // Set colA in lo
@prSetInt(hi_index_pr, 0, @intToSql(505)) // Set colA in hi
// Iterate through rows with colA between 495 and 505
for (@indexIteratorScanAscending(&index, 0, 0); @indexIteratorAdvance(&index);) {
// Materialize the current match.
var table_pr = @indexIteratorGetTablePR(&index)
var colA = @prGetInt(table_pr, 0)
var slot = @indexIteratorGetSlot(&index)
// Delete + Insert on Table
if (!@tableDelete(&updater, &slot)) {
@indexIteratorFree(&index)
@storageInterfaceFree(&updater)
return 37
}
var insert_pr = @getTablePR(&updater)
@prSetInt(insert_pr, 0, colA + @intToSql(100000))
var insert_slot = @tableInsert(&updater)
// Delete + Index on Index
var index_pr = @getIndexPR(&updater, index1_oid)
@prSetInt(index_pr, 0, colA)
@indexDelete(&updater, &slot)
@prSetInt(index_pr, 0, colA + @intToSql(100000))
if (!@indexInsert(&updater)) {
@indexIteratorFree(&index)
@storageInterfaceFree(&updater)
return 38
}
}
@indexIteratorFree(&index)
@storageInterfaceFree(&updater)
// Count the number of updated tables
var tvi: TableVectorIterator
@tableIterInit(&tvi, execCtx, test1_oid, col_oids)
for (@tableIterAdvance(&tvi)) {
var vpi = @tableIterGetVPI(&tvi)
for (; @vpiHasNext(vpi); @vpiAdvance(vpi)) {
var cola = @vpiGetInt(vpi, 0)
if (cola >= 100495 and cola <= 100505) {
count = count + 1
}
}
@vpiReset(vpi)
}
@tableIterClose(&tvi)
return count
}