Parser for predicate expressions using NimbleParsec.
Parses SQL-like filter predicates such as:
"book_id = 123""status != 'Pending'""publication_date >= '2024-01-01'""list_price > 19.99"
Returns tuples in the format {operator, column_name, value} where:
- operator:
:eq,:neq,:gt,:lt,:gte,:lte - column_name: string
- value: parsed value (integer, float, boolean, nil, or string)
Summary
Functions
Normalize a filter value based on the column type.
Parse a predicate string into a tuple.
Parses the given binary as predicate.
Functions
Normalize a filter value based on the column type.
date: Converts ISO8601 date strings to Date structs when the column type is :date, otherwise raises ArgumentError for invalid date strings.
other types: Returns the value unchanged.
Examples
iex> DeltaQuery.PredicateParser.normalize_value(:date, "2025-01-15")
~D[2025-01-15]
iex> DeltaQuery.PredicateParser.normalize_value(:string, "hello")
"hello"
Parse a predicate string into a tuple.
Returns {:ok, {operator, column, value}} on success,
or {:error, reason} on failure.
Examples
iex> DeltaQuery.PredicateParser.parse_predicate("book_id = 123")
{:ok, {:eq, "book_id", 123}}
iex> DeltaQuery.PredicateParser.parse_predicate("status != 'Pending'")
{:ok, {:neq, "status", "Pending"}}
iex> DeltaQuery.PredicateParser.parse_predicate("list_price >= 19.99")
{:ok, {:gte, "list_price", 19.99}}
iex> DeltaQuery.PredicateParser.parse_predicate("available = true")
{:ok, {:eq, "available", true}}
@spec predicate(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: non_neg_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary as predicate.
Returns {:ok, [token], rest, context, position, byte_offset} or
{:error, reason, rest, context, line, byte_offset} where position
describes the location of the predicate (start position) as {line, offset_to_start_of_line}.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.
Options
:byte_offset- the byte offset for the whole binary, defaults to 0:line- the line and the byte offset into that line, defaults to{1, byte_offset}:context- the initial context value. It will be converted to a map