rocketmq-filter
SQL92 message filtering, pluggable filter SPI, expression evaluation, and Bloom filter utilities for RocketMQ-Rust.
rocketmq-filter provides the filtering foundation used by RocketMQ-Rust broker-side and client-facing message
selection paths. It compiles SQL92-style subscription expressions into reusable expression trees, evaluates them against
message property contexts, exposes a thread-safe filter registry, and includes Bloom filter primitives compatible with
RocketMQ consume-queue filtering metadata.
The crate is intentionally focused: it does not run a RocketMQ service by itself. It supplies reusable filtering, expression, and bitset utilities for higher-level crates.
Capabilities
| Area | What it provides |
|---|---|
| SQL92 filtering | SqlFilter compiles SQL92-style property expressions into reusable Expression objects. |
| Filter SPI | Filter, FilterSpi, and FilterFactory provide a pluggable filter registration and lookup model. |
| Expression runtime | Expression, Value, EvaluationContext, and MessageEvaluationContext evaluate message properties with typed results. |
| Boolean expressions | Ready-to-use boolean primitives such as AlwaysTrueExpression, AlwaysFalseExpression, PropertyEqualsExpression, AndExpression, OrExpression, and NotExpression. |
| Bloom filter utilities | Java-compatible Bloom filter generation and hit checks using byte-aligned bit arrays and MurmurHash3-based double hashing. |
| Hot-path benchmarks | Criterion benchmarks for SQL expression compilation, expression evaluation, and Bloom filter byte-slice hit checks. |
Architecture
subscription expression
|
v
FilterRegistrySnapshot -> SqlFilter -> SQL runtime parser -> Expression tree
|
message properties -> MessageEvaluationContext ---+
|
v
Value::Boolean / Value::Null / error
consumer filter metadata -> BloomFilterData -> BloomFilter -> BitsArray / raw bytes
FilterRegistryBuilder validates registrations and builds immutable FilterRegistrySnapshot values. Duplicate types and a registry without SQL92 are rejected. Compiled filters retain their registry ownership; changing the legacy global FilterFactory does not modify an existing Broker snapshot. SqlFilter delegates parsing to the SQL runtime and returns object-safe expression instances that
can be cached by callers. MessageEvaluationContext supplies string properties for evaluation, while the runtime
coerces numeric and boolean literals where the SQL expression requires them.
Crate Layout
| Path | Purpose |
|---|---|
src/lib.rs |
Public module exports for constants, expressions, filters, and utilities. |
src/filter.rs |
Filter module facade and public exports. |
src/filter/filter_spi.rs |
Core Filter trait and Java-compatible FilterSpi alias. |
src/filter/filter_factory.rs |
Global filter registry backed by DashMap, with default SQL92 registration. |
src/filter/filter_sql_filter.rs |
Stateless SQL92 filter implementation. |
src/filter/sql_runtime.rs |
SQL expression lexer, parser, evaluator, and three-valued evaluation behavior. |
src/expression.rs |
Expression traits, values, evaluation errors, and expression exports. |
src/expression/evaluation_context.rs |
Message property evaluation context with serde support. |
src/expression/boolean_expression.rs |
Boolean expression primitives used by simple filtering paths and tests. |
src/utils/bits_array.rs |
Bit array wrapper with bitwise operations and bounds-checked access. |
src/utils/bloom_filter.rs |
Bloom filter implementation compatible with RocketMQ filtering metadata. |
src/utils/bloom_filter_data.rs |
Serializable Bloom filter metadata model. |
benches/sql_filter_benchmark.rs |
Criterion benchmarks for SQL and Bloom filter hot paths. |
The legacy global FilterFactory is lazily initialized. get_sql_filter panics if SQL92 has been removed; try_get_sql_filter provides a fallback. Prefer an explicit registry for service composition. See registry.rs.
Requirements
- Stable Rust
1.95.0, using the pinned repository toolchain. - The repository toolchain from
../rust-toolchain.toml. - No running RocketMQ broker or NameServer is required for local tests, expression evaluation, or Bloom filter usage.
Installation
Inside this workspace:
[]
= { = "../rocketmq-filter" }
For external consumers:
[]
= "1.0.0"
Quick Start
Compile and evaluate a SQL92-style subscription expression:
use ;
use ;
Typed filter compilation
Implementations and callers use Filter::try_compile and handle FilterCompileError. Its kind, stage, optional raw UTF-8 byte
offset, and SQL92 source classification are stable and redaction-safe. Compilation failures use the
protocol.filter.invalid catalog descriptor.
Custom Filter implementations provide the same structured method directly:
use Arc;
use ;
use ;
use SqlFilter;
;
Use Bloom filter metadata to pre-check candidate messages:
use BitsArray;
use BloomFilter;
SQL92 Expression Support
The current SQL runtime supports:
| Category | Supported syntax |
|---|---|
| Comparison | =, !=, <>, >, >=, <, <= |
| Logical operators | AND, OR, NOT, parentheses |
| Null checks | IS NULL, IS NOT NULL |
| Set and range checks | IN, NOT IN, BETWEEN, NOT BETWEEN |
| String matching | CONTAINS, NOT CONTAINS, STARTSWITH, ENDSWITH |
| Literals | Single-quoted strings, escaped quotes with '', integers, floating-point numbers, TRUE, FALSE, NULL, NOW |
| Property names | ASCII identifiers with letters, digits, _, $, and . after the first character |
Message properties are supplied as strings. The evaluator coerces numeric and boolean values when needed for comparison.
Missing properties evaluate as NULL, so callers should handle Value::Null when using SQL expressions directly.
Bloom Filter Utilities
BloomFilter follows the RocketMQ Java design closely:
create_by_fn(f, n)derives the hash count and byte-aligned bit length from false-positive percentagefand expected element countn.generateproducesBloomFilterDatawith calculated bit positions and bit count.hash_to,hash_to_str, andhash_to_positionsset Bloom filter bits.is_hit,is_hit_str,is_hit_positions, andis_hit_bytescheck candidate hits.is_hit_bytesavoids building a temporaryBitsArrayon hot consume-queue paths.
Validation
Focused checks for this crate:
Select additional checks for this crate from the repository root:
Benchmarks
Run the filter benchmark suite from the workspace root:
Criterion reports are written under target/criterion-filter. Keep benchmark comparisons on the same toolchain,
feature set, and hardware when evaluating changes to SQL parsing, expression evaluation, or Bloom filter hit checks.
License
RocketMQ-Rust is licensed under the Apache License 2.0. See ../LICENSE-APACHE.