Skip to content

Commit

Permalink
DRQS 177610804: Enable record truncation for index verify
Browse files Browse the repository at this point in the history
Signed-off-by: lzhu286 <[email protected]>
  • Loading branch information
lzhu286 committed Jan 31, 2025
1 parent 2dc04c8 commit 0fcb916
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 6 deletions.
21 changes: 15 additions & 6 deletions db/sqlglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ int mem_to_ondisk(void *outbuf, struct field *f, struct mem_info *info,
// if string is longer than field
// and find-by-truncate is enabled
convopts->step = (f->flags & INDEX_DESCEND) ? 0 : 1;
bias_info->truncated = 1;
if (bias_info) bias_info->truncated = 1;
}
rc = CLIENT_to_SERVER(m->z, m->n, CLIENT_PSTR2, null,
(struct field_conv_opts *)convopts,
Expand Down Expand Up @@ -1371,7 +1371,7 @@ int mem_to_ondisk(void *outbuf, struct field *f, struct mem_info *info,
/* if the SQLite BLOB is longer than the bytearray field
and find-by-truncate is enabled. */
convopts->step = (f->flags & INDEX_DESCEND) ? 0 : 1;
bias_info->truncated = 1;
if (bias_info) bias_info->truncated = 1;
}

rc =
Expand Down Expand Up @@ -12834,9 +12834,12 @@ unsigned long long verify_indexes(struct dbtable *db, uint8_t *rec,

static inline void build_indexes_expressions_query(strbuf *sql,
struct schema *sc,
char *tblname, char *expr)
char *tblname, struct field *fld)
{
int i;
int max_expr_size = 256;
char expr[max_expr_size];

strbuf_clear(sql);
strbuf_appendf(sql, "WITH \"%s\"(\"%s\"", tblname, sc->member[0].name);
for (i = 1; i < sc->nmembers; i++) {
Expand All @@ -12846,6 +12849,13 @@ static inline void build_indexes_expressions_query(strbuf *sql,
for (i = 1; i < sc->nmembers; i++) {
strbuf_appendf(sql, ", @%s", sc->member[i].name);
}
switch(fld->type) {
case SERVER_BCSTR:
snprintf(expr, max_expr_size, "substr(%s,1,%u)", fld->name, fld->len - 1);
break;
default:
snprintf(expr, max_expr_size, "%s", fld->name);
}
strbuf_appendf(sql, ") SELECT (%s) FROM \"%s\"", expr, tblname);
}

Expand Down Expand Up @@ -12913,10 +12923,9 @@ int indexes_expressions_data(const struct dbtable *tbl, struct schema *sc,
}
}

build_indexes_expressions_query(sql, sc, "expridx_temp", f->name);
build_indexes_expressions_query(sql, sc, "expridx_temp", f);

rc =
run_verify_indexes_query((char *)strbuf_buf(sql), sc, m, &mout, &exist);
rc = run_verify_indexes_query((char *)strbuf_buf(sql), sc, m, &mout, &exist);
if (rc || !exist) {
logmsg(LOGMSG_ERROR, "%s: failed to run internal query, rc %d\n",
__func__, rc);
Expand Down
8 changes: 8 additions & 0 deletions tests/verify_index_expressions.test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ifeq ($(TESTSROOTDIR),)
include ../testcase.mk
else
include $(TESTSROOTDIR)/testcase.mk
endif
ifeq ($(TEST_TIMEOUT),)
export TEST_TIMEOUT=3m
endif
2 changes: 2 additions & 0 deletions tests/verify_index_expressions.test/lrl.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
table names names.csc2
legacy_schema off
12 changes: 12 additions & 0 deletions tests/verify_index_expressions.test/names.csc2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tag ondisk
{
cstring name[5]
int id
vutf8 desc[25]
}

keys
{
"P_KEY" = name + id
dup "SHORT_DESC" = (cstring[17])"desc" { where id < 2 }
}
22 changes: 22 additions & 0 deletions tests/verify_index_expressions.test/runit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
bash -n "$0" | exit 1

DBNAME=$1
TESTNAME="verify_index_expressions"
SQL="$TESTNAME.sql"
OUTPUT="$TESTNAME.out"
EXPECTED="$TESTNAME.expected"

rm -f $OUTPUT

cdb2sql ${CDB2_OPTIONS} -f $SQL $DBNAME default > $OUTPUT
cdb2sql ${CDB2_OPTIONS} $DBNAME default "explain select * from names where id < 2" >> $OUTPUT
cdb2sql ${CDB2_OPTIONS} $DBNAME default "exec procedure sys.cmd.verify('names')" >> $OUTPUT
diff $OUTPUT $EXPECTED > /dev/null
rc=$?
if [[ $rc -ne 0 ]]; then
echo "Failed $TESTNAME test"
echo diff $(pwd)/$OUTPUT $(pwd)/$EXPECTED
fi

exit $rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[BEGIN] rc 0
[INSERT INTO names VALUES("nick", 0, "nicholas")] rc 0
[INSERT INTO names VALUES("nick", 1, "nicholas the 1st")] rc 0
[INSERT INTO names VALUES("nick", 2, "nicholas the 2nd.")] rc 0
[INSERT INTO names VALUES("nick", 3, "nicholas the 3rd & final")] rc 0
[COMMIT] rc 0
(addr=0, opcode='Init', p1=0, p2=16, p3=0, p4='', p5='00', comment='Start at 16')
(addr=1, opcode='OpenRead', p1=0, p2=8, p3=0, p4='3', p5='02', comment='root=8 iDb=0; names')
(addr=2, opcode='ColumnsUsed', p1=0, p2=0, p3=0, p4='7', p5='00', comment='')
(addr=3, opcode='OpenRead', p1=1, p2=10, p3=0, p4='k(2,,)', p5='00', comment='root=10 iDb=0; $SHORT_DESC_C53B9974')
(addr=4, opcode='ColumnsUsed', p1=1, p2=0, p3=0, p4='1', p5='00', comment='')
(addr=5, opcode='Explain', p1=5, p2=0, p3=0, p4='SCAN TABLE names USING INDEX $SHORT_DESC_C53B9974 (~491520 rows)', p5='00', comment='')
(addr=6, opcode='Rewind', p1=1, p2=13, p3=1, p4='0', p5='00', comment='')
(addr=7, opcode='DeferredSeek', p1=1, p2=0, p3=0, p4='', p5='00', comment='Move 0 to 1.rowid if needed')
(addr=8, opcode='Column', p1=0, p2=0, p3=1, p4='', p5='00', comment='r[1]=names.name')
(addr=9, opcode='Column', p1=0, p2=1, p3=2, p4='', p5='00', comment='r[2]=names.id')
(addr=10, opcode='Column', p1=1, p2=0, p3=3, p4='', p5='00', comment='r[3]=names.desc')
(addr=11, opcode='ResultRow', p1=1, p2=3, p3=0, p4='', p5='00', comment='output=r[1..3]')
(addr=12, opcode='Next', p1=1, p2=7, p3=0, p4='', p5='01', comment='')
(addr=13, opcode='Close', p1=0, p2=0, p3=0, p4='', p5='00', comment='')
(addr=14, opcode='Close', p1=1, p2=0, p3=0, p4='', p5='00', comment='')
(addr=15, opcode='Halt', p1=0, p2=0, p3=0, p4='', p5='00', comment='')
(addr=16, opcode='Transaction', p1=0, p2=0, p3=0, p4='0', p5='01', comment='usesStmtJournal=0')
(addr=17, opcode='Goto', p1=0, p2=1, p3=0, p4='', p5='00', comment='')
(out='Verify succeeded.')
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

BEGIN
INSERT INTO names VALUES("nick", 0, "nicholas")
INSERT INTO names VALUES("nick", 1, "nicholas the 1st")
INSERT INTO names VALUES("nick", 2, "nicholas the 2nd.")
INSERT INTO names VALUES("nick", 3, "nicholas the 3rd & final")
COMMIT

0 comments on commit 0fcb916

Please sign in to comment.