Expressions

group Expressions

Build DataFusion expressions and evaluate JSON predicates.

Functions

LanceDBExpr *lancedb_expr_column(const char *name)

Create a column reference expression.

Parameters:

name – - null-terminated C string containing the column name

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Caller must free with lancedb_expr_free()

LanceDBExpr *lancedb_expr_literal_string(const char *value)

Create a string literal expression.

Parameters:

value – - null-terminated C string containing the literal value

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Caller must free with lancedb_expr_free()

LanceDBExpr *lancedb_expr_literal_i64(int64_t value)

Create an integer literal expression (i64).

Parameters:

value – - the integer value

Returns:

Non-null pointer to LanceDBExpr Caller must free with lancedb_expr_free()

LanceDBExpr *lancedb_expr_literal_f64(double value)

Create a float literal expression (f64).

Parameters:

value – - the float value

Returns:

Non-null pointer to LanceDBExpr Caller must free with lancedb_expr_free()

LanceDBExpr *lancedb_expr_literal_bool(bool value)

Create a boolean literal expression.

Parameters:

value – - the boolean value

Returns:

Non-null pointer to LanceDBExpr Caller must free with lancedb_expr_free()

LanceDBExpr *lancedb_expr_binary(LanceDBExpr *left, LanceDBBinaryOp op, LanceDBExpr *right)

Create a binary expression (left op right).

Parameters:
  • left – - pointer to LanceDBExpr for left operand (consumed)

  • op – - binary operator

  • right – - pointer to LanceDBExpr for right operand (consumed)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both left and right are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_not(LanceDBExpr *expr)

Create a NOT expression.

Parameters:

expr – - pointer to LanceDBExpr (consumed)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The input expr is consumed; do not use or free it after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_is_null(LanceDBExpr *expr)

Create an IS NULL expression.

Parameters:

expr – - pointer to LanceDBExpr (consumed)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The input expr is consumed; do not use or free it after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_is_not_null(LanceDBExpr *expr)

Create an IS NOT NULL expression.

Parameters:

expr – - pointer to LanceDBExpr (consumed)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The input expr is consumed; do not use or free it after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_and(LanceDBExpr *left, LanceDBExpr *right)

Create an AND expression (convenience for binary AND).

Parameters:
Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both inputs are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_or(LanceDBExpr *left, LanceDBExpr *right)

Create an OR expression (convenience for binary OR).

Parameters:
Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both inputs are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_in_list(LanceDBExpr *expr, LanceDBExpr *const *list, size_t list_len, bool negated, char **error_message)

Create an IN list expression (expr IN (value1, value2, …)).

Parameters:
  • expr – - pointer to LanceDBExpr for the expression to check (consumed)

  • list – - array of pointers to LanceDBExpr for list values (all consumed)

  • list_len – - number of elements in the list

  • negated – - if true, creates NOT IN instead of IN

  • error_message – - optional pointer to receive detailed error message (NULL to ignore)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The input expr and all list elements are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_array_has(LanceDBExpr *array_expr, LanceDBExpr *value_expr, char **error_message)

Create an array_has expression: check if an array column contains a value.

Returns a boolean expression that is true when the array contains the element.

Parameters:
  • array_expr – - pointer to LanceDBExpr for the array column (consumed)

  • value_expr – - pointer to LanceDBExpr for the value to search for (consumed)

  • error_message – - optional pointer to receive detailed error message (NULL to ignore)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both inputs are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_starts_with(LanceDBExpr *str_expr, LanceDBExpr *prefix_expr, char **error_message)

Create a starts_with expression: check if a string expression starts with a prefix.

Returns a boolean expression that is true when str_expr starts with prefix_expr.

Parameters:
  • str_expr – - pointer to LanceDBExpr for the string column/expression (consumed)

  • prefix_expr – - pointer to LanceDBExpr for the prefix to check against (consumed)

  • error_message – - optional pointer to receive detailed error message (NULL to ignore)

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both inputs are consumed; do not use or free them after calling Caller must free result with lancedb_expr_free() If error_message is provided and an error occurs, the caller must free the message with lancedb_free_string()

LanceDBExpr *lancedb_expr_clone(const LanceDBExpr *expr)

Clone an expression (creates an independent copy).

Parameters:

expr – - pointer to LanceDBExpr to clone (not consumed)

Returns:

Non-null pointer to a new LanceDBExpr on success, NULL on failure The original expr remains valid and must still be freed separately Caller must free result with lancedb_expr_free()

void lancedb_expr_free(LanceDBExpr *expr)

Free an expression.

After calling this function, the expr pointer must not be used.

Parameters:

expr – - pointer to LanceDBExpr

LanceDBExpr *lancedb_expr_json_get_str(LanceDBExpr *json_expr, const char *const *path, size_t path_len)

Create a json_get_str expression: extract a string value from JSON by path.

For nested access, pass multiple path segments (e.g., [“user”, “name”] for {“user”:{“name”:”alice”}}).

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path

  • path_len – - number of path segments

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The json_expr is consumed; do not use or free it after calling. Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_json_get_int(LanceDBExpr *json_expr, const char *const *path, size_t path_len)

Create a json_get_int expression: extract an integer value from JSON by path.

For nested access, pass multiple path segments (e.g., [“stats”, “count”] for {“stats”:{“count”:42}}).

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path

  • path_len – - number of path segments

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The json_expr is consumed; do not use or free it after calling. Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_json_get_float(LanceDBExpr *json_expr, const char *const *path, size_t path_len)

Create a json_get_float expression: extract a float value from JSON by path.

For nested access, pass multiple path segments (e.g., [“stats”, “score”] for {“stats”:{“score”:4.2}}).

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path

  • path_len – - number of path segments

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The json_expr is consumed; do not use or free it after calling. Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_json_get_bool(LanceDBExpr *json_expr, const char *const *path, size_t path_len)

Create a json_get_bool expression: extract a boolean value from JSON by path.

For nested access, pass multiple path segments (e.g., [“user”, “active”] for {“user”:{“active”:true}}).

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path

  • path_len – - number of path segments

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The json_expr is consumed; do not use or free it after calling. Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_json_contains(LanceDBExpr *json_expr, const char *const *path, size_t path_len)

Create a json_contains expression: check if a key exists in JSON.

For nested access, pass multiple path segments (e.g., [“user”, “name”] to check if {“user”:{“name”:”alice”}} has the nested key).

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path

  • path_len – - number of path segments

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure The json_expr is consumed; do not use or free it after calling. Caller must free result with lancedb_expr_free()

LanceDBExpr *lancedb_expr_json_array_has(LanceDBExpr *json_expr, const char *const *path, size_t path_len, LanceDBExpr *value_expr, bool quote_value)

Create a json_array_has expression: check if a JSON array contains a value.

Navigates to a JSON array field using the path, then checks whether the given value expression appears as an element. The quote_value flag controls how the value is adapted:

  • true: for JSON string arrays: value is wrapped in JSON quotes for matching.

  • false: for JSON number/boolean arrays: value is cast to Utf8.

For arrays of non-scalar values (objects, nested arrays), it falls back to exact string matching against the raw JSON text, which is not JSON-aware

Parameters:
  • json_expr – - pointer to LanceDBExpr for the JSON column (consumed)

  • path – - array of null-terminated C strings forming the JSON path to the array

  • path_len – - number of path segments

  • value_expr – - pointer to LanceDBExpr for the value to search for (consumed)

  • quote_value – - true for string arrays, false for number/boolean arrays

Returns:

Non-null pointer to LanceDBExpr on success, NULL on failure Both json_expr and value_expr are consumed; do not use or free them after calling. Caller must free result with lancedb_expr_free()

LanceDBError lancedb_json_matches(FFI_ArrowArray **arrays, FFI_ArrowSchema *schema, size_t batch_count, LanceDBExpr *expr, bool **results_out, size_t *count_out, char **error_message)

Evaluate a JSON filter expression against Arrow RecordBatches.

Takes the Arrow FFI arrays and schema from lancedb_query_result_to_arrow and a filter expression. Returns a boolean array indicating which rows matched. The arrays are NOT consumed and remain valid after the call.

Must be called while the arrays are valid (before consuming them with arrow::ImportRecordBatch or freeing with lancedb_free_arrow_arrays).

Example: to filter rows where metadata.name = “alice”: col = lancedb_expr_column(“metadata”) path = (const char*[]){“name”} name = lancedb_expr_json_get_str(col, path, 1) val = lancedb_expr_literal_string(“alice”) expr = lancedb_expr_binary(name, LANCEDB_BINARY_OP_EQ, val) lancedb_json_matches(arrays, schema, count, expr,

&results, &nrows, &error_message)

Parameters:
  • arrays – - array of FFI_ArrowArray pointers (NOT consumed)

  • schema – - pointer to FFI_ArrowSchema (NOT consumed)

  • batch_count – - number of batches in the arrays

  • expr – - pointer to LanceDBExpr filter expression (consumed)

  • results_out – - pointer to receive allocated bool array (one per row)

  • count_out – - pointer to receive total row count across all batches

  • error_message – - optional pointer to receive detailed error message

Returns:

Error code indicating success or failure. The expr is consumed; do not use or free it after calling. Caller must free *results_out with lancedb_free_json_matches(). If error_message is set, caller must free with lancedb_free_string().

void lancedb_free_json_matches(bool *results)

Free results array returned by lancedb_json_matches.

Parameters:

results – - pointer returned by lancedb_json_matches, or NULL