Skip to content

Commit

Permalink
* Fixed incorrect query results on array values (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamansky committed Oct 26, 2021
1 parent 64aad03 commit 0e7d084
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
21 changes: 21 additions & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

cd ./build/src/tests

set follow-fork-mode parent
set detach-on-fork on
set print elements 4096


define lb
set breakpoint pending on
source ~/.breakpoints
set breakpoint pending auto
echo breakpoints loaded\n
end

define sb
save breakpoints ~/.breakpoints
echo breakpoints saved\n
end


1 change: 0 additions & 1 deletion .lvimrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let &l:makeprg = 'cd '.g:build_dir.' && make -j4'
packadd termdebug
let g:termdebug_wide=1

tnoremap <A-Esc> <C-\><C-n>
nnoremap <leader>c :call asyncrun#run('',
\ {'mode':'terminal','cwd':g:build_dir,'save':2},
\ 'cmake .. -DCMAKE_BUILD_TYPE='.g:build_type.' -DBUILD_TESTS='.g:build_tests
Expand Down
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ejdb2 (2.62) UNRELEASED; urgency=medium

* Fixed incorrect query results on array values (#331)
* Fixed error in parsing FP JSON value (#333)
* Java: com.softmotions.ejdb2.JSON.ValueType.getTypeOf set to be public
* Flutter: Fixed #305
Expand Down
43 changes: 25 additions & 18 deletions src/jql/jql.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct MFCTX {
JQP_FILTER *qpf;
} MFCTX;

static JQP_NODE *_jql_match_node(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp);
static JQP_NODE* _jql_match_node(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp);

IW_INLINE void _jql_jqval_destroy(JQP_STRING *pv) {
JQVAL *qv = pv->opaque;
Expand Down Expand Up @@ -59,7 +59,7 @@ IW_INLINE void _jql_jqval_destroy(JQP_STRING *pv) {
}
}

static JQVAL *_jql_find_placeholder(JQL q, const char *name) {
static JQVAL* _jql_find_placeholder(JQL q, const char *name) {
JQP_AUX *aux = q->aux;
for (JQP_STRING *pv = aux->start_placeholder; pv; pv = pv->placeholder_next) {
if (!strcmp(pv->value, name)) {
Expand All @@ -69,7 +69,7 @@ static JQVAL *_jql_find_placeholder(JQL q, const char *name) {
return 0;
}

JQVAL *jql_find_placeholder(JQL q, const char *name) {
JQVAL* jql_find_placeholder(JQL q, const char *name) {
return _jql_find_placeholder(q, name);
}

Expand Down Expand Up @@ -99,7 +99,7 @@ static iwrc _jql_set_placeholder(JQL q, const char *placeholder, int index, JQVA

iwrc jql_set_json2(
JQL q, const char *placeholder, int index, JBL_NODE val,
void (*freefn)(void*, void*), void *op) {
void (*freefn) (void*, void*), void *op) {
JQVAL *qv = malloc(sizeof(*qv));
if (!qv) {
return iwrc_set_errno(IW_ERROR_ALLOC, errno);
Expand Down Expand Up @@ -161,7 +161,7 @@ iwrc jql_set_f64(JQL q, const char *placeholder, int index, double val) {

iwrc jql_set_str2(
JQL q, const char *placeholder, int index, const char *val,
void (*freefn)(void*, void*), void *op) {
void (*freefn) (void*, void*), void *op) {
JQVAL *qv = malloc(sizeof(*qv));
if (!qv) {
return iwrc_set_errno(IW_ERROR_ALLOC, errno);
Expand Down Expand Up @@ -191,7 +191,7 @@ iwrc jql_set_bool(JQL q, const char *placeholder, int index, bool val) {

iwrc jql_set_regexp2(
JQL q, const char *placeholder, int index, const char *expr,
void (*freefn)(void*, void*), void *op) {
void (*freefn) (void*, void*), void *op) {
struct re *rx = lwre_new(expr);
if (!rx) {
return iwrc_set_errno(IW_ERROR_ALLOC, errno);
Expand Down Expand Up @@ -359,7 +359,7 @@ size_t jql_estimate_allocated_size(JQL q) {
return ret;
}

const char *jql_collection(JQL q) {
const char* jql_collection(JQL q) {
return q->coll;
}

Expand Down Expand Up @@ -598,8 +598,15 @@ static int _jql_cmp_jqval_pair(const JQVAL *left, const JQVAL *right, iwrc *rcp)
return lv->vbool - (rv->vi64 != 0L);
case JQVAL_F64:
return lv->vbool - (rv->vf64 != 0.0); // -V550
case JQVAL_STR:
return lv->vbool - !strcmp(rv->vstr, "true");
case JQVAL_STR: {
if (strcmp(rv->vstr, "true") == 0) {
return lv->vbool - 1;
} else if (strcmp(rv->vstr, "false") == 0) {
return lv->vbool;
} else {
return -1;
}
}
case JQVAL_NULL:
return lv->vbool;
default:
Expand Down Expand Up @@ -1010,7 +1017,7 @@ bool jql_match_jqval_pair(
return _jql_match_jqval_pair(aux, left, jqop, right, rcp);
}

static JQVAL *_jql_unit_to_jqval(JQP_AUX *aux, JQPUNIT *unit, iwrc *rcp) {
static JQVAL* _jql_unit_to_jqval(JQP_AUX *aux, JQPUNIT *unit, iwrc *rcp) {
*rcp = 0;
switch (unit->type) {
case JQP_STRING_TYPE: {
Expand Down Expand Up @@ -1105,7 +1112,7 @@ static JQVAL *_jql_unit_to_jqval(JQP_AUX *aux, JQPUNIT *unit, iwrc *rcp) {
}
}

JQVAL *jql_unit_to_jqval(JQP_AUX *aux, JQPUNIT *unit, iwrc *rcp) {
JQVAL* jql_unit_to_jqval(JQP_AUX *aux, JQPUNIT *unit, iwrc *rcp) {
return _jql_unit_to_jqval(aux, unit, rcp);
}

Expand Down Expand Up @@ -1238,7 +1245,7 @@ IW_INLINE bool _jql_match_node_field(MCTX *mctx, JQP_NODE *n, iwrc *rcp) {
return (strcmp(n->value->string.value, mctx->key) == 0);
}

IW_INLINE JQP_NODE *_jql_match_node_anys(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp) {
IW_INLINE JQP_NODE* _jql_match_node_anys(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp) {
if (n->start < 0) {
n->start = mctx->lvl;
}
Expand All @@ -1257,7 +1264,7 @@ IW_INLINE JQP_NODE *_jql_match_node_anys(MCTX *mctx, JQP_NODE *n, bool *res, iwr
return n;
}

static JQP_NODE *_jql_match_node(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp) {
static JQP_NODE* _jql_match_node(MCTX *mctx, JQP_NODE *n, bool *res, iwrc *rcp) {
switch (n->ntype) {
case JQP_NODE_FIELD:
*res = _jql_match_node_field(mctx, n, rcp);
Expand Down Expand Up @@ -1413,15 +1420,15 @@ iwrc jql_matched(JQL q, JBL jbl, bool *out) {
return rc;
}

const char *jql_error(JQL q) {
const char* jql_error(JQL q) {
if (q && q->aux) {
return iwxstr_ptr(q->aux->xerr);
} else {
return "";
}
}

const char *jql_first_anchor(JQL q) {
const char* jql_first_anchor(JQL q) {
return q->aux->first_anchor;
}

Expand Down Expand Up @@ -1516,7 +1523,7 @@ static bool _jql_proj_matched(
}
if (proj->pos + 1 == lvl) {
JQP_STRING *ps = proj->value;
for (int i = 0; i < lvl; ps = ps->next, ++i) ; // -V529
for (int i = 0; i < lvl; ps = ps->next, ++i); // -V529
assert(ps);
if (ps->flavour & JQP_STR_PROJFIELD) {
for (JQP_STRING *sn = ps; sn; sn = sn->subnext) {
Expand Down Expand Up @@ -1556,7 +1563,7 @@ static bool _jql_proj_join_matched(
const char *pv, *spos;
bool ret = false;
JQP_STRING *ps = proj->value;
for (int i = 0; i < lvl; ps = ps->next, ++i) ; // -V529
for (int i = 0; i < lvl; ps = ps->next, ++i); // -V529
assert(ps);

if (ps->flavour & JQP_STR_PROJFIELD) {
Expand Down Expand Up @@ -1783,7 +1790,7 @@ iwrc jql_apply_and_project(JQL q, JBL jbl, JBL_NODE *out, void *exec_ctx, IWPOOL
return rc;
}

static const char *_ecodefn(locale_t locale, uint32_t ecode) {
static const char* _ecodefn(locale_t locale, uint32_t ecode) {
if (!((ecode > _JQL_ERROR_START) && (ecode < _JQL_ERROR_END))) {
return 0;
}
Expand Down
34 changes: 33 additions & 1 deletion src/tests/ejdb_test3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,37 @@ void ejdb_test3_8(void) {
iwpool_destroy(pool);
}

static void ejdb_test3_9(void) {
EJDB_OPTS opts = {
.kv = {
.path = "ejdb_test3_9",
.oflags = IWKV_TRUNC
},
.no_wal = true
};
EJDB db;
EJDB_LIST list = 0;
iwrc rc = ejdb_open(&opts, &db);
CU_ASSERT_EQUAL_FATAL(rc, 0)

rc = put_json(db, "zzz", "[[\"one\",\"two\"]]");
CU_ASSERT_EQUAL_FATAL(rc, 0);

rc = put_json(db, "zzz", "[[\"red\",\"brown\"],[false]]");
CU_ASSERT_EQUAL_FATAL(rc, 0);

rc = ejdb_list3(db, "zzz", "/*/[** = one]", 0, 0, &list);
CU_ASSERT_EQUAL_FATAL(rc, 0);

CU_ASSERT_PTR_NOT_NULL(list->first);
CU_ASSERT_PTR_NULL(list->first->next);

ejdb_list_destroy(&list);

rc = ejdb_close(&db);
CU_ASSERT_EQUAL_FATAL(rc, 0);
}

int main() {
CU_pSuite pSuite = NULL;
if (CUE_SUCCESS != CU_initialize_registry()) {
Expand All @@ -1136,7 +1167,8 @@ int main() {
|| (NULL == CU_add_test(pSuite, "ejdb_test3_5", ejdb_test3_5))
|| (NULL == CU_add_test(pSuite, "ejdb_test3_6", ejdb_test3_6))
|| (NULL == CU_add_test(pSuite, "ejdb_test3_7", ejdb_test3_7))
|| (NULL == CU_add_test(pSuite, "ejdb_test3_8", ejdb_test3_8))) {
|| (NULL == CU_add_test(pSuite, "ejdb_test3_8", ejdb_test3_8))
|| (NULL == CU_add_test(pSuite, "ejdb_test3_9", ejdb_test3_9))) {
CU_cleanup_registry();
return CU_get_error();
}
Expand Down

0 comments on commit 0e7d084

Please sign in to comment.