Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cypher): parse and apply the full multi-key ORDER BY list, keeping LIMIT (#1334)

Martin Vogel0a0ef516d664
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 6b7fa0acb..a0fca4071 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -1655,7 +1655,6 @@ static int parse_return_item(parser_t *p, cbm_return_item_t *item) {
return 0;
}
-/* Parse ORDER BY field into r->order_by and r->order_dir */
/* Parse aggregate function call for ORDER BY */
static void parse_order_by_agg(parser_t *p, char *buf, size_t buf_sz) {
const char *fn = agg_func_name(peek(p)->type);
@@ -1696,16 +1695,29 @@ static char *parse_order_by_expr(parser_t *p, char *buf, size_t buf_sz) {
return buf;
}
-static void parse_order_by_clause(parser_t *p, cbm_return_clause_t *r) {
+/* Parse the full comma-separated ORDER BY key list (#1334). Consuming only the
+ * first key left ", key2 ... LIMIT n" unparsed, which silently dropped the
+ * LIMIT and flooded the caller with the whole result set. Returns 0 on
+ * success, CBM_NOT_FOUND when the key list exceeds the modeled maximum. */
+static int parse_order_by_clause(parser_t *p, cbm_return_clause_t *r) {
expect(p, TOK_BY);
- char order_buf[CBM_SZ_256];
- parse_order_by_expr(p, order_buf, sizeof(order_buf));
- r->order_by = heap_strdup(order_buf);
- if (match(p, TOK_ASC)) {
- r->order_dir = heap_strdup("ASC");
- } else if (match(p, TOK_DESC)) {
- r->order_dir = heap_strdup("DESC");
- }
+ do {
+ if (r->order_key_count >= CBM_CYPHER_ORDER_KEYS_MAX) {
+ return CBM_NOT_FOUND;
+ }
+ char order_buf[CBM_SZ_256];
+ parse_order_by_expr(p, order_buf, sizeof(order_buf));
+ bool desc = false;
+ if (match(p, TOK_ASC)) {
+ desc = false;
+ } else if (match(p, TOK_DESC)) {
+ desc = true;
+ }
+ r->order_keys[r->order_key_count] = heap_strdup(order_buf);
+ r->order_descs[r->order_key_count] = desc;
+ r->order_key_count++;
+ } while (match(p, TOK_COMMA));
+ return 0;
}
/* Parse RETURN/WITH clause (shared logic) */
@@ -1767,7 +1779,14 @@ static int parse_return_or_with(parser_t *p, cbm_return_clause_t **out, bool is_
tail:
/* Optional ORDER BY */
if (match(p, TOK_ORDER)) {
- parse_order_by_clause(p, r);
+ if (parse_order_by_clause(p, r) < 0) {
+ for (int k = 0; k < r->order_key_count; k++) {
+ safe_str_free(&r->order_keys[k]);
+ }
+ free(r->items);
+ free(r);
+ return CBM_NOT_FOUND;
+ }
}
/* Optional SKIP */
@@ -2100,8 +2119,9 @@ static void free_return_clause(cbm_return_clause_t *r) {
free(r->items[i].args);
}
free(r->items);
- safe_str_free(&r->order_by);
- safe_str_free(&r->order_dir);
+ for (int k = 0; k < r->order_key_count; k++) {
+ safe_str_free(&r->order_keys[k]);
+ }
free(r);
}
@@ -3308,16 +3328,17 @@ static void expand_pattern_rels(cbm_store_t *store, cbm_pattern_t *pat, binding_
/* ── Result postprocessing helpers ─────────────────────────────── */
-/* Find the column index for ORDER BY, checking both column names and aliases.
- * Returns -1 if not found. */
-static int rb_find_order_column(const result_builder_t *rb, const cbm_return_clause_t *ret) {
+/* Find the column index for one ORDER BY key, checking both column names and
+ * aliases. Returns -1 if not found. */
+static int rb_find_order_column(const result_builder_t *rb, const cbm_return_clause_t *ret,
+ const char *key) {
for (int ci = 0; ci < rb->col_count; ci++) {
- if (strcmp(rb->columns[ci], ret->order_by) == 0) {
+ if (strcmp(rb->columns[ci], key) == 0) {
return ci;
}
}
for (int ci = 0; ci < ret->count; ci++) {
- if (ret->items[ci].alias && strcmp(ret->items[ci].alias, ret->order_by) == 0) {
+ if (ret->items[ci].alias && strcmp(ret->items[ci].alias, key) == 0) {
return ci;
}
}
@@ -3345,26 +3366,44 @@ static bool rb_is_numeric_column(const result_builder_t *rb, int col) {
}
static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *ret) {
- if (!ret->order_by) {
+ if (ret->order_key_count == 0) {
return;
}
- int order_col = rb_find_order_column(rb, ret);
- if (order_col < 0) {
+ /* Resolve every key up front; an unresolvable key is skipped, matching the
+ * single-key forgiveness (sorting on what can be resolved beats dropping
+ * the whole ORDER BY). */
+ int cols[CBM_CYPHER_ORDER_KEYS_MAX];
+ bool numeric[CBM_CYPHER_ORDER_KEYS_MAX];
+ bool descs[CBM_CYPHER_ORDER_KEYS_MAX];
+ int keys = 0;
+ for (int k = 0; k < ret->order_key_count; k++) {
+ int order_col = rb_find_order_column(rb, ret, ret->order_keys[k]);
+ if (order_col < 0) {
+ continue;
+ }
+ cols[keys] = order_col;
+ numeric[keys] = rb_is_numeric_column(rb, order_col);
+ descs[keys] = ret->order_descs[k];
+ keys++;
+ }
+ if (keys == 0) {
return;
}
-
- bool desc = ret->order_dir && strcmp(ret->order_dir, "DESC") == 0;
- bool numeric = rb_is_numeric_column(rb, order_col);
for (int i = 0; i < rb->row_count - SKIP_ONE; i++) {
for (int j = 0; j < rb->row_count - i - SKIP_ONE; j++) {
- int cmp;
- if (numeric) {
- cmp = (int)strtol(rb->rows[j][order_col], NULL, CBM_DECIMAL_BASE) -
- (int)strtol(rb->rows[j + SKIP_ONE][order_col], NULL, CBM_DECIMAL_BASE);
- } else {
- cmp = strcmp(rb->rows[j][order_col], rb->rows[j + SKIP_ONE][order_col]);
+ int cmp = 0;
+ for (int k = 0; k < keys && cmp == 0; k++) {
+ if (numeric[k]) {
+ cmp = (int)strtol(rb->rows[j][cols[k]], NULL, CBM_DECIMAL_BASE) -
+ (int)strtol(rb->rows[j + SKIP_ONE][cols[k]], NULL, CBM_DECIMAL_BASE);
+ } else {
+ cmp = strcmp(rb->rows[j][cols[k]], rb->rows[j + SKIP_ONE][cols[k]]);
+ }
+ if (descs[k]) {
+ cmp = -cmp;
+ }
}
- if (desc ? cmp < 0 : cmp > 0) {
+ if (cmp > 0) {
const char **tmp = rb->rows[j];
rb->rows[j] = rb->rows[j + SKIP_ONE];
rb->rows[j + SKIP_ONE] = tmp;
@@ -3674,18 +3713,26 @@ static void distinct_list_add(char ***list, int *count, const char *val) {
(*list)[idx] = heap_strdup(val);
}
-/* Sort bindings by a virtual variable using bubble sort */
-static void sort_bindings(binding_t *vbindings, int count, const char *key, bool desc) {
+/* Sort bindings by the ORDER BY key list (virtual variables) using bubble
+ * sort; later keys break ties, direction is per key (#1334). */
+static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clause_t *wc) {
for (int i = 0; i < count - SKIP_ONE; i++) {
for (int j = 0; j < count - i - SKIP_ONE; j++) {
- const char *va = binding_get_virtual(&vbindings[j], key, NULL);
- const char *vb2 = binding_get_virtual(&vbindings[j + SKIP_ONE], key, NULL);
- char *ea = NULL;
- char *eb = NULL;
- double da = strtod(va, &ea);
- double db = strtod(vb2, &eb);
- int cmp = (ea != va && eb != vb2) ? ((da > db) - (da < db)) : strcmp(va, vb2);
- if (desc ? cmp < 0 : cmp > 0) {
+ int cmp = 0;
+ for (int k = 0; k < wc->order_key_count && cmp == 0; k++) {
+ const char *va = binding_get_virtual(&vbindings[j], wc->order_keys[k], NULL);
+ const char *vb2 =
+ binding_get_virtual(&vbindings[j + SKIP_ONE], wc->order_keys[k], NULL);
+ char *ea = NULL;
+ char *eb = NULL;
+ double da = strtod(va, &ea);
+ double db = strtod(vb2, &eb);
+ cmp = (ea != va && eb != vb2) ? ((da > db) - (da < db)) : strcmp(va, vb2);
+ if (wc->order_descs[k]) {
+ cmp = -cmp;
+ }
+ }
+ if (cmp > 0) {
binding_t tmp = vbindings[j];
vbindings[j] = vbindings[j + SKIP_ONE];
vbindings[j + SKIP_ONE] = tmp;
@@ -3718,9 +3765,8 @@ static void bindings_skip_limit(binding_t *vbindings, int *count, int skip, int
/* Sort, skip, and limit binding array in-place */
static void with_sort_skip_limit(const cbm_return_clause_t *wc, binding_t *vbindings, int *vcount) {
- if (wc->order_by) {
- bool wdesc = wc->order_dir && strcmp(wc->order_dir, "DESC") == 0;
- sort_bindings(vbindings, *vcount, wc->order_by, wdesc);
+ if (wc->order_key_count > 0) {
+ sort_bindings(vbindings, *vcount, wc);
}
bindings_skip_limit(vbindings, vcount, wc->skip, wc->limit);
}
@@ -4390,7 +4436,7 @@ static void build_return_columns(result_builder_t *rb, cbm_return_clause_t *ret)
static void execute_return_simple(cbm_return_clause_t *ret, binding_t *bindings, int bind_count,
int max_rows, result_builder_t *rb) {
int proj_cap = max_rows;
- if (ret->limit > 0 && !ret->distinct && !ret->order_by && ret->skip <= 0) {
+ if (ret->limit > 0 && !ret->distinct && ret->order_key_count == 0 && ret->skip <= 0) {
proj_cap = ret->limit;
}
for (int bi = 0; bi < bind_count && rb->row_count < proj_cap; bi++) {
diff --git a/src/cypher/cypher.h b/src/cypher/cypher.h
index 6ad140e65..a1e4462ab 100644
--- a/src/cypher/cypher.h
+++ b/src/cypher/cypher.h
@@ -262,15 +262,23 @@ typedef struct {
int arg_count;
} cbm_return_item_t;
+/* Upper bound on ORDER BY sort keys. Queries with more keys are rejected at
+ * parse time: an unmodeled key must be a loud error, never a silently dropped
+ * remainder (#1334 - the unconsumed tail swallowed the LIMIT clause). */
+#define CBM_CYPHER_ORDER_KEYS_MAX 8
+
typedef struct {
cbm_return_item_t *items;
int count;
bool distinct;
- bool star; /* RETURN * */
- const char *order_by; /* "variable.property" or "COUNT(var)" or alias */
- const char *order_dir; /* "ASC" or "DESC", NULL = default */
- int skip; /* SKIP N, 0 = none */
- int limit; /* 0 = default */
+ bool star; /* RETURN * */
+ /* ORDER BY key list, in priority order. Each key is "variable.property",
+ * "COUNT(var)" or an alias; direction is per key (Cypher semantics). */
+ const char *order_keys[CBM_CYPHER_ORDER_KEYS_MAX];
+ bool order_descs[CBM_CYPHER_ORDER_KEYS_MAX]; /* false = ASC (default) */
+ int order_key_count; /* 0 = no ORDER BY */
+ int skip; /* SKIP N, 0 = none */
+ int limit; /* 0 = default */
} cbm_return_clause_t;
/* Full query AST */
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index 73a0455ec..f103d2a50 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -378,14 +378,48 @@ TEST(cypher_parse_return_order_limit) {
int rc =
cbm_cypher_parse("MATCH (f:Function) RETURN f.name ORDER BY f.name DESC LIMIT 5", &q, &err);
ASSERT_EQ(rc, 0);
- ASSERT_NOT_NULL(q->ret->order_by);
- ASSERT_STR_EQ(q->ret->order_dir, "DESC");
+ ASSERT_EQ(q->ret->order_key_count, 1);
+ ASSERT_STR_EQ(q->ret->order_keys[0], "f.name");
+ ASSERT(q->ret->order_descs[0]);
ASSERT_EQ(q->ret->limit, 5);
cbm_query_free(q);
PASS();
}
+/* #1334: every ORDER BY key is parsed (per-key direction) and the LIMIT that
+ * follows the key list is consumed instead of silently dropped. */
+TEST(cypher_parse_multikey_order_by_issue1334) {
+ cbm_query_t *q = NULL;
+ char *err = NULL;
+ int rc = cbm_cypher_parse(
+ "MATCH (f:Function) RETURN f.name ORDER BY f.complexity DESC, f.name ASC LIMIT 5", &q,
+ &err);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(q->ret->order_key_count, 2);
+ ASSERT_STR_EQ(q->ret->order_keys[0], "f.complexity");
+ ASSERT(q->ret->order_descs[0]);
+ ASSERT_STR_EQ(q->ret->order_keys[1], "f.name");
+ ASSERT_FALSE(q->ret->order_descs[1]);
+ ASSERT_EQ(q->ret->limit, 5);
+
+ cbm_query_free(q);
+ PASS();
+}
+
+/* #1334: more keys than the modeled maximum is a loud parse error - the old
+ * failure mode (ignore the remainder, drop the LIMIT) must never come back. */
+TEST(cypher_parse_order_by_over_cap_rejected_issue1334) {
+ cbm_query_t *q = NULL;
+ char *err = NULL;
+ int rc = cbm_cypher_parse("MATCH (f:Function) RETURN f.name ORDER BY "
+ "f.a, f.b, f.c, f.d, f.e, f.f, f.g, f.h, f.i LIMIT 5",
+ &q, &err);
+ ASSERT(rc != 0);
+ free(err);
+ PASS();
+}
+
TEST(cypher_parse_return_distinct) {
cbm_query_t *q = NULL;
char *err = NULL;
@@ -2648,6 +2682,65 @@ TEST(cypher_exec_skip_limit) {
PASS();
}
+/* Regression for #1334: a LIMIT must survive a multi-key ORDER BY. The parser
+ * used to stop at the first sort key, leaving ", key2 ... LIMIT n" unconsumed —
+ * the whole result set came back (6326 rows instead of 5 on the reporter's
+ * graph: a token-flood into agent context). */
+TEST(cypher_exec_multikey_order_by_keeps_limit_issue1334) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ /* start_lines: HandleOrder=10, ValidateOrder=5, SubmitOrder=0, LogError=0 */
+ int rc = cbm_cypher_execute(
+ s,
+ "MATCH (f:Function) RETURN f.name, f.start_line "
+ "ORDER BY f.start_line DESC, f.name ASC LIMIT 2",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 2);
+ ASSERT_STR_EQ(r.rows[0][0], "HandleOrder");
+ ASSERT_STR_EQ(r.rows[1][0], "ValidateOrder");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+/* #1334: the secondary key must actually break ties, per-key direction. */
+TEST(cypher_exec_multikey_order_by_tiebreak_issue1334) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ /* start_line ASC puts the two 0-line functions first; name DESC breaks the
+ * tie: SubmitOrder before LogError. */
+ int rc = cbm_cypher_execute(
+ s,
+ "MATCH (f:Function) RETURN f.name, f.start_line "
+ "ORDER BY f.start_line ASC, f.name DESC LIMIT 2",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 2);
+ ASSERT_STR_EQ(r.rows[0][0], "SubmitOrder");
+ ASSERT_STR_EQ(r.rows[1][0], "LogError");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+/* #1334: the WITH-clause pipeline has the same multi-key ORDER BY contract. */
+TEST(cypher_exec_with_multikey_order_by_keeps_limit_issue1334) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s,
+ "MATCH (f:Function) WITH f.name AS n, f.start_line AS sl "
+ "ORDER BY sl DESC, n ASC LIMIT 2 RETURN n",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 2);
+ ASSERT_STR_EQ(r.rows[0][0], "HandleOrder");
+ ASSERT_STR_EQ(r.rows[1][0], "ValidateOrder");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_exec_sum) {
cbm_store_t *s = setup_cypher_store();
cbm_cypher_result_t r = {0};
@@ -3563,6 +3656,8 @@ SUITE(cypher) {
RUN_TEST(cypher_parse_return_simple);
RUN_TEST(cypher_parse_return_count);
RUN_TEST(cypher_parse_return_order_limit);
+ RUN_TEST(cypher_parse_multikey_order_by_issue1334);
+ RUN_TEST(cypher_parse_order_by_over_cap_rejected_issue1334);
RUN_TEST(cypher_parse_return_distinct);
RUN_TEST(cypher_parse_inline_props);
RUN_TEST(cypher_parse_error);
@@ -3677,6 +3772,9 @@ SUITE(cypher) {
/* Phase 4: SKIP + aggregation */
RUN_TEST(cypher_exec_skip);
RUN_TEST(cypher_exec_skip_limit);
+ RUN_TEST(cypher_exec_multikey_order_by_keeps_limit_issue1334);
+ RUN_TEST(cypher_exec_multikey_order_by_tiebreak_issue1334);
+ RUN_TEST(cypher_exec_with_multikey_order_by_keeps_limit_issue1334);
RUN_TEST(cypher_exec_sum);
RUN_TEST(cypher_exec_avg);
RUN_TEST(cypher_exec_min);