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.

feat(cypher): scalar + entity-introspection functions (suite tier 1)

Martin Vogel71a6c5714ea2
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index cc2866415..760fc86a0 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -206,6 +206,56 @@ if [ "$CD_ROWS" -ne 1 ]; then
fi
echo "OK: query_graph count(DISTINCT f.label) returned 1 aggregate row"
+# 3d-funcs: scalar / introspection functions (full Cypher suite, Tier 1)
+cyp_first_cell() {
+ # $1 = query; echoes rows[0][0] (or empty)
+ cli query_graph "{\"project\":\"$PROJECT\",\"query\":\"$1\"}" |
+ python3 -c "import json,sys; d=json.loads(sys.stdin.read()); rows=d.get('rows',[]); print(rows[0][0] if rows and rows[0] else '')" 2>/dev/null || echo ""
+}
+
+# labels(n) → JSON list like ["Function"]
+LBLV=$(cyp_first_cell 'MATCH (f:Function) RETURN labels(f) AS l LIMIT 1')
+case "$LBLV" in
+ '['*) echo "OK: query_graph labels(f) = $LBLV" ;;
+ *) echo "FAIL: query_graph labels(f) returned '$LBLV' (expected a [\"...\"] list)"; exit 1 ;;
+esac
+
+# type(r) → relationship type
+TYPV=$(cyp_first_cell 'MATCH (f:Function)-[r]->(g) RETURN type(r) AS t LIMIT 1')
+if [ -z "$TYPV" ]; then
+ echo "FAIL: query_graph type(r) returned empty"; exit 1
+fi
+echo "OK: query_graph type(r) = $TYPV"
+
+# id(n) → numeric identity
+IDV=$(cyp_first_cell 'MATCH (f:Function) RETURN id(f) AS i LIMIT 1')
+case "$IDV" in
+ ''|*[!0-9]*) echo "FAIL: query_graph id(f) returned non-numeric '$IDV'"; exit 1 ;;
+ *) echo "OK: query_graph id(f) = $IDV" ;;
+esac
+
+# properties(n) → JSON object
+PROPV=$(cyp_first_cell 'MATCH (f:Function) RETURN properties(f) AS p LIMIT 1')
+case "$PROPV" in
+ '{'*) echo "OK: query_graph properties(f) is a JSON object" ;;
+ *) echo "FAIL: query_graph properties(f) returned '$PROPV'"; exit 1 ;;
+esac
+
+# toInteger() cast in projection
+TIV=$(cyp_first_cell 'MATCH (f:Function) RETURN toInteger(f.start_line) AS n LIMIT 1')
+case "$TIV" in
+ ''|*[!0-9-]*) echo "FAIL: query_graph toInteger(f.start_line) returned non-integer '$TIV'"; exit 1 ;;
+ *) echo "OK: query_graph toInteger(f.start_line) = $TIV" ;;
+esac
+
+# =~ regex match in WHERE
+CYPHER_RX=$(cli query_graph "{\"project\":\"$PROJECT\",\"query\":\"MATCH (f:Function) WHERE f.name =~ \\\".+\\\" RETURN f.name\"}")
+RX_ROWS=$(echo "$CYPHER_RX" | python3 -c "import json,sys; d=json.loads(sys.stdin.read()); print(len(d.get('rows',[])))" 2>/dev/null || echo "0")
+if [ "$RX_ROWS" -lt 1 ]; then
+ echo "FAIL: query_graph WHERE =~ regex returned 0 rows"; echo "$CYPHER_RX"; exit 1
+fi
+echo "OK: query_graph WHERE f.name =~ regex returned $RX_ROWS row(s)"
+
# 3e: delete_project cleanup
cli delete_project "{\"project\":\"$PROJECT\"}" > /dev/null
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 02d069e14..0989ef57c 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -1246,6 +1246,34 @@ static cbm_case_expr_t *parse_case_expr(parser_t *p) {
/* Parse a single RETURN/WITH item (aggregate, string func, CASE, or plain var.prop).
* Returns 0 on success, -1 on error. */
/* Parse var[.prop] into item->variable and item->property. Returns -1 on error. */
+/* ASCII case-insensitive string equality. */
+static bool cyp_ci_eq(const char *a, const char *b) {
+ if (!a || !b) {
+ return false;
+ }
+ for (; *a && *b; a++, b++) {
+ if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) {
+ return false;
+ }
+ }
+ return *a == '\0' && *b == '\0';
+}
+
+/* Canonical name for a single-argument scalar / entity-introspection function
+ * invoked by identifier — labels/type/id/keys/properties and the numeric/bool
+ * casts toInteger/toFloat/toBoolean — or NULL if unrecognised (case-insensitive).
+ * toLower/toUpper/toString are separate keyword tokens handled elsewhere. */
+static const char *scalar_func_canonical(const char *s) {
+ static const char *const names[] = {"labels", "type", "id", "keys", "properties",
+ "toInteger", "toFloat", "toBoolean", NULL};
+ for (int i = 0; names[i]; i++) {
+ if (cyp_ci_eq(s, names[i])) {
+ return names[i];
+ }
+ }
+ return NULL;
+}
+
static int parse_var_dot_prop(parser_t *p, cbm_return_item_t *item) {
const cbm_token_t *var = expect(p, TOK_IDENT);
if (!var) {
@@ -1261,6 +1289,31 @@ static int parse_var_dot_prop(parser_t *p, cbm_return_item_t *item) {
return 0;
}
+/* True if the cursor is at `IDENT(` where IDENT is a supported scalar function. */
+static bool is_named_func_call(parser_t *p) {
+ if (!check(p, TOK_IDENT) || p->pos + SKIP_ONE >= p->count) {
+ return false;
+ }
+ if (p->tokens[p->pos + SKIP_ONE].type != TOK_LPAREN) {
+ return false;
+ }
+ return scalar_func_canonical(peek(p)->text) != NULL;
+}
+
+/* Parse a single-argument scalar / introspection call: labels(n), type(r),
+ * id(n), keys(n), properties(n), toInteger(n.start_line), ... */
+static int parse_named_func_item(parser_t *p, cbm_return_item_t *item) {
+ const char *canon = scalar_func_canonical(peek(p)->text);
+ advance(p); /* consume the function name */
+ expect(p, TOK_LPAREN);
+ if (parse_var_dot_prop(p, item) < 0) {
+ return CBM_NOT_FOUND;
+ }
+ expect(p, TOK_RPAREN);
+ item->func = heap_strdup(canon);
+ return 0;
+}
+
/* Parse aggregate function call: COUNT(var.prop) */
static int parse_aggregate_item(parser_t *p, cbm_return_item_t *item) {
cbm_token_type_t ft = peek(p)->type;
@@ -1304,6 +1357,8 @@ static int parse_return_item(parser_t *p, cbm_return_item_t *item) {
rc = parse_aggregate_item(p, item);
} else if (is_string_func_tok(peek(p)->type)) {
rc = parse_string_func_item(p, item);
+ } else if (is_named_func_call(p)) {
+ rc = parse_named_func_item(p, item);
} else {
rc = parse_var_dot_prop(p, item);
}
@@ -2339,6 +2394,39 @@ static const char *apply_string_func(const char *func, const char *val, char *bu
if (strcmp(func, "toString") == 0) {
return val; /* already strings */
}
+ if (strcmp(func, "toInteger") == 0) {
+ char *end = NULL;
+ long long v = strtoll(val, &end, CBM_DECIMAL_BASE);
+ if (end == val) {
+ /* Not an integer literal — accept a float string and truncate. */
+ char *fend = NULL;
+ double d = strtod(val, &fend);
+ if (fend == val) {
+ return ""; /* non-numeric → null */
+ }
+ v = (long long)d;
+ }
+ snprintf(buf, buf_sz, "%lld", v);
+ return buf;
+ }
+ if (strcmp(func, "toFloat") == 0) {
+ char *end = NULL;
+ double d = strtod(val, &end);
+ if (end == val) {
+ return ""; /* non-numeric → null */
+ }
+ snprintf(buf, buf_sz, "%g", d);
+ return buf;
+ }
+ if (strcmp(func, "toBoolean") == 0) {
+ if (cyp_ci_eq(val, "true")) {
+ return "true";
+ }
+ if (cyp_ci_eq(val, "false")) {
+ return "false";
+ }
+ return ""; /* not a boolean → null */
+ }
return val;
}
@@ -2778,14 +2866,96 @@ static void rb_free(result_builder_t *rb) {
/* ── Get projection value for a binding + return item ─────────── */
+/* Build a JSON list of a node's non-null property keys: keys(n). */
+static const char *node_keys_list(const cbm_node_t *n, char *buf, size_t buf_sz) {
+ const struct {
+ const char *k;
+ bool present;
+ } ks[] = {
+ {"name", n->name && n->name[0]},
+ {"qualified_name", n->qualified_name && n->qualified_name[0]},
+ {"label", n->label && n->label[0]},
+ {"file_path", n->file_path && n->file_path[0]},
+ {"start_line", n->start_line > 0},
+ {"end_line", n->end_line > 0},
+ };
+ size_t pos = 0;
+ bool first = true;
+ if (pos < buf_sz - SKIP_ONE) {
+ buf[pos++] = '[';
+ }
+ for (size_t i = 0; i < sizeof(ks) / sizeof(ks[0]) && pos < buf_sz - SKIP_ONE; i++) {
+ if (!ks[i].present) {
+ continue;
+ }
+ int w = snprintf(buf + pos, buf_sz - pos, "%s\"%s\"", first ? "" : ",", ks[i].k);
+ if (w < 0 || (size_t)w >= buf_sz - pos) {
+ break;
+ }
+ pos += (size_t)w;
+ first = false;
+ }
+ if (pos < buf_sz - SKIP_ONE) {
+ buf[pos++] = ']';
+ }
+ buf[pos] = '\0';
+ return buf;
+}
+
static const char *project_item(binding_t *b, cbm_return_item_t *item, char *func_buf,
size_t buf_sz) {
if (item->kase) {
return eval_case_expr(item->kase, b);
}
+ /* Entity-introspection functions operate on the bound node/edge itself,
+ * not on a scalar property value. */
+ if (item->func) {
+ if (strcmp(item->func, "labels") == 0) {
+ cbm_node_t *n = binding_get(b, item->variable);
+ if (n && n->label) {
+ snprintf(func_buf, buf_sz, "[\"%s\"]", n->label);
+ return func_buf;
+ }
+ return "[]";
+ }
+ if (strcmp(item->func, "type") == 0) {
+ cbm_edge_t *e = binding_get_edge(b, item->variable);
+ return (e && e->type) ? e->type : "";
+ }
+ if (strcmp(item->func, "id") == 0) {
+ cbm_node_t *n = binding_get(b, item->variable);
+ if (n) {
+ snprintf(func_buf, buf_sz, "%lld", (long long)n->id);
+ return func_buf;
+ }
+ cbm_edge_t *e = binding_get_edge(b, item->variable);
+ if (e) {
+ snprintf(func_buf, buf_sz, "%lld", (long long)e->id);
+ return func_buf;
+ }
+ return "";
+ }
+ if (strcmp(item->func, "keys") == 0) {
+ cbm_node_t *n = binding_get(b, item->variable);
+ return n ? node_keys_list(n, func_buf, buf_sz) : "[]";
+ }
+ if (strcmp(item->func, "properties") == 0) {
+ cbm_node_t *n = binding_get(b, item->variable);
+ if (n) {
+ return n->properties_json ? n->properties_json : "{}";
+ }
+ cbm_edge_t *e = binding_get_edge(b, item->variable);
+ if (e) {
+ return e->properties_json ? e->properties_json : "{}";
+ }
+ return "{}";
+ }
+ }
const char *raw = binding_get_virtual(b, item->variable, item->property);
- if (item->func && (strcmp(item->func, "toLower") == 0 || strcmp(item->func, "toUpper") == 0 ||
- strcmp(item->func, "toString") == 0)) {
+ if (item->func &&
+ (strcmp(item->func, "toLower") == 0 || strcmp(item->func, "toUpper") == 0 ||
+ strcmp(item->func, "toString") == 0 || strcmp(item->func, "toInteger") == 0 ||
+ strcmp(item->func, "toFloat") == 0 || strcmp(item->func, "toBoolean") == 0)) {
return apply_string_func(item->func, raw, func_buf, buf_sz);
}
return raw;
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index c47a9d2b4..8dd9c6424 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -561,6 +561,91 @@ TEST(cypher_exec_return_properties) {
PASS();
}
+/* ── Scalar / introspection functions (full-suite Tier 1) ──────── */
+
+TEST(cypher_func_labels) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(
+ s, "MATCH (f:Function) WHERE f.name = \"HandleOrder\" RETURN labels(f)", "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_STR_EQ(r.rows[0][0], "[\"Function\"]");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(cypher_func_type) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(
+ s, "MATCH (f:Function)-[r:CALLS]->(g:Function) RETURN type(r) LIMIT 1", "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_STR_EQ(r.rows[0][0], "CALLS");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(cypher_func_id) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s, "MATCH (f:Function) WHERE f.name = \"HandleOrder\" RETURN id(f)",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ /* id is a non-empty numeric string */
+ ASSERT_TRUE(r.rows[0][0][0] >= '0' && r.rows[0][0][0] <= '9');
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(cypher_func_keys) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(
+ s, "MATCH (f:Function) WHERE f.name = \"HandleOrder\" RETURN keys(f)", "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_TRUE(strstr(r.rows[0][0], "\"name\"") != NULL);
+ ASSERT_TRUE(strstr(r.rows[0][0], "\"qualified_name\"") != NULL);
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(cypher_func_properties) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(
+ s, "MATCH (f:Function) WHERE f.name = \"HandleOrder\" RETURN properties(f)", "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_EQ(r.rows[0][0][0], '{'); /* a JSON object */
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(cypher_func_tointeger_tofloat) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s,
+ "MATCH (f:Function) WHERE f.name = \"HandleOrder\" "
+ "RETURN toInteger(f.start_line), toFloat(f.start_line)",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_STR_EQ(r.rows[0][0], "10"); /* start_line = 10 */
+ ASSERT_STR_EQ(r.rows[0][1], "10");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_exec_calls_relationship) {
cbm_store_t *s = setup_cypher_store();
cbm_cypher_result_t r = {0};
@@ -2339,6 +2424,12 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_where_contains);
RUN_TEST(cypher_exec_where_starts_with);
RUN_TEST(cypher_exec_return_properties);
+ RUN_TEST(cypher_func_labels);
+ RUN_TEST(cypher_func_type);
+ RUN_TEST(cypher_func_id);
+ RUN_TEST(cypher_func_keys);
+ RUN_TEST(cypher_func_properties);
+ RUN_TEST(cypher_func_tointeger_tofloat);
RUN_TEST(cypher_exec_calls_relationship);
RUN_TEST(cypher_exec_calls_with_where);
RUN_TEST(cypher_exec_inbound);