gortiz opened a new pull request, #20464:
URL: https://github.com/apache/pinot/pull/20464
## Summary
A filter predicate on an expression over one dictionary-encoded column, such
as `BITAND(flags, 33) = 0` or `SUBSTR(code, 0, 3) = 'C50'`, now runs the
expression once per dictionary value instead of once per row. The server keeps
the dictionary ids that match and filters the column with them, like a
predicate on the column itself. The filter can then use the sorted index, the
inverted index or a scan of dictionary ids. When the predicate is true or false
for every dictionary value, the filter reads no rows.
The PR has two commits:
1. The feature and its tests.
2. `BenchmarkDictionaryExpressionFilter`, a JMH benchmark. See
[Benchmark](#benchmark) for the results.
## Why
Today `FilterPlanNode` turns any predicate on a function into an
`ExpressionFilterOperator`. This operator reads the forward index and the
dictionary for each candidate row and runs the transform once per row. It also
runs last in an AND, so it cannot use an index.
Many columns that users filter with an expression have few distinct values
per segment: bit sets, codes, enum-like strings, numbers stored as strings. For
these columns, the work per dictionary value is much smaller than the work per
row. See [Benchmark](#benchmark) for the numbers.
## How
```
FilterPlanNode (predicate on a function)
└─ constructDictionaryExpressionFilterOperator
├─ DictionaryExpressionPredicateEvaluatorFactory null →
ExpressionFilterOperator (as today)
│ ├─ checks: one SV column, dictionary-encoded forward index,
dictionary length <= threshold,
│ │ deterministic and null-intolerant functions
│ ├─ runs the TransformFunction over DictionaryValueBlock (ranges
of dictionary ids)
│ └─ applies the value-based PredicateEvaluator → BitSet of
matching dictionary ids
├─ DictIdSetPredicateEvaluator(BitSet)
└─ FilterOperatorUtils.getDictionaryExpressionLeafFilterOperator
→ sorted index / inverted index / scan / always true / always
false
```
- **`DictionaryValueBlock`** is a `ValueBlock` over a range of dictionary
ids. Its `BlockValSet` returns the ids and reads the typed values from the
dictionary. The factory makes a new block for each range of 10,000 ids, because
`ScalarTransformFunctionWrapper` caches its result per block.
- **`DictIdSetPredicateEvaluator`** keeps the matching ids in a `BitSet`. It
reports the predicate type `IN`, or `NOT_IN` when fewer ids fail than match. It
never reports `RANGE` or `EQ`, because the range index operator casts
evaluators of these types to `IntRange` or `IntValue`. `getPredicate()` keeps
the original predicate, so `EXPLAIN` shows it.
- **When the new path applies.** The left side of the predicate reads
exactly one single-value column with a dictionary-encoded forward index. The
dictionary has at most `expressionDictSizeThreshold` values. The predicate type
is `EQ`, `NOT_EQ`, `IN`, `NOT_IN`, `RANGE` or `REGEXP_LIKE`. The segment is
immutable: the dictionary of a consuming segment grows during the query. In all
other cases, and when the computation fails, the server uses the per-row path
as today.
- **Fallback for a scan on a nullable column.** In `auto` mode, a column
with null rows under null handling and no sorted or inverted index keeps the
per-row path. The `ScanBasedFilterOperator` of such a column removes the null
rows with an AND of the scan and the non-null rows
(`BaseColumnFilterOperator.excludeNulls`). A parent AND cannot restrict that
inner AND to its own candidates, so the scan reads the whole segment, while
`ExpressionFilterOperator` evaluates only the candidates. This also affects
plain column predicates today. A separate PR will make the scan skip the null
rows itself, and then this fallback can go.
### Nulls
A null row stores the default null value of the column in the dictionary.
The new path evaluates that value like any other value. With null handling on,
the column filter operators remove the null rows
(`BaseColumnFilterOperator.excludeNulls`).
This is correct only when the expression is null for a null input and gives
the same result each time. So the factory accepts only these functions:
- Scalar functions whose `FunctionInfo` is deterministic and has no nullable
parameters.
- Transform functions from an allow list: arithmetic, rounding,
single-parameter math, `CAST`, time conversion, `DATETRUNC`, `EXTRACT` and the
date-part functions.
`COALESCE`, `CASE`, `IS NULL`, JSON functions and other functions keep the
per-row path. Under null handling, if the expression returns null for a
non-null dictionary value, the factory also falls back to the per-row path.
### New settings
| Setting | Values | Default |
|---|---|---|
| Query option `expressionDictSizeThreshold` | Maximum dictionary length.
`0` turns the feature off. | `10000` |
| Server config `pinot.server.query.executor.expression.dict.size.threshold`
| Default of the query option for that server. | `10000` |
| Query option `expressionDictFilterMode` | `off` (per row, as today),
`auto`, `inverted` (inverted index when the column has one and is not sorted),
`scan` (scan of dictionary ids) | `auto` |
## Compatibility
The feature changes only the plan inside one server. It adds no wire format,
segment format or broker change. Mixed-version clusters are not affected. To
get the old behavior, set `expressionDictFilterMode=off` on the query or set
the server threshold to `0`.
`EXPLAIN` output changes for these predicates. For example, `CONCAT(...) =
'...'` on a sorted column now shows `FILTER_SORTED_INDEX(... operator:IN ...)`
instead of `FILTER_EXPRESSION`. The PR updates the expectations in
`ExplainPlanQueriesTest`.
## Tests
- `DictIdSetPredicateEvaluatorTest`: inclusive and exclusive forms, MV
columns (never the exclusive form), always true, always false and batch apply.
- `DictionaryValueBlockTest`: values and ids for INT, LONG, FLOAT, DOUBLE,
BIG_DECIMAL, STRING and BYTES dictionaries, over more than one block, and
rejected ranges.
- `DictionaryExpressionFilterQueriesTest`: 40 predicates, with null handling
on and off, in every mode and with two thresholds. Each result must be equal to
the result of the per-row path. Other tests count null rows, make sure that an
always-true segment scans no entries, and make sure that each case gets the
expected filter operator (including the cases that must keep the per-row path).
- `ExplainPlanQueriesTest`: new expectations, and a copy of the old query
with `expressionDictFilterMode='off'`.
- `BitFunctions.json`: multi-stage cases for `BITAND` and `BITEXTRACT` on a
nullable LONG column, with and without null handling, and with the mode `off`.
## Benchmark
`BenchmarkDictionaryExpressionFilter` (second commit), full matrix on a
24-core Linux machine, JDK 25 with jemalloc, 4 segments, `-wi 5 -i 5 -f 2 -prof
gc`. The bit-set column is a nullable LONG with a dictionary and an inverted
index. Its values follow a Zipf-Mandelbrot distribution, and `BITAND(col, 33) =
0` fails for `failShare` of the rows. `selectivity` is the share of rows that a
`CODE IN (...)` filter on an indexed INT column keeps before the expression.
Null handling is on. Values are ms per query, 4 segments of 2.5M rows each,
3,000 distinct bit sets.
| Query | failShare | selectivity | `off` (per row) | `auto` | `auto` /
`off` |
|---|---|---|---|---|---|
| `COUNT(*)` with `BITAND` | 0 | 1.0 | 168.5 | 0.34 | 0.002 |
| `COUNT(*)` with `BITAND` | 0.2 | 1.0 | 168.5 | 4.4 | 0.03 |
| `COUNT(*)` with `BITAND` | 0.2 | 0.08 | 20.3 | 6.3 | 0.31 |
| `COUNT(*)` with `BITAND` | 0.2 | 0.008 | 3.5 | 3.9 | 1.12 |
| `GROUP BY` two columns with `BITAND` | 0.2 | 1.0 | 311 | 164 | 0.53 |
| `SUBSTR(DIAG, 0, 3) = 'C50'` | - | 1.0 | 328 | 2.1 | 0.006 |
| `CAST(STR_NUM AS INT) BETWEEN 0 AND 15` | - | 1.0 | 142 | 0.36 | 0.003 |
- Allocation follows latency: for `SUBSTR` without another filter it drops
from 1.5 GB to 3.7 MB per query.
- With 100,000 distinct bit sets the dictionary is above the threshold, and
`auto` is within ±3% of `off` in every case. The check that falls back costs
nothing visible.
- `auto` is slower than `off` when the other filter keeps few rows and the
bit set is not always true:
- `COUNT(*)`, `SUBSTR` and `CAST` at selectivity 0.008: +5% to +48% on 1M
rows, which is 0.1 to 0.7 ms on queries of 1.3 to 3.5 ms. On 2.5M rows,
`SUBSTR` and `CAST` are faster than `off`, and `COUNT(*)` with `BITAND` is up
to 12% slower.
- `GROUP BY` with `failShare` 0.2 at selectivity 0.008 and 0.08 on 1M
rows: +1% to +8%.
In these cases the OR of the inverted-index bitmaps of the matching values
costs more than the evaluation of the few candidate rows. A follow-up can
compare the dictionary length with the number of candidate rows before it picks
the path.
To run it:
```
./mvnw package -pl pinot-perf -am -DskipTests
java -cp "pinot-perf/target/pinot-perf-pkg/lib/*" org.openjdk.jmh.Main
BenchmarkDictionaryExpressionFilter \
-bm avgt -tu ms -wi 5 -i 5 -f 2 -prof gc -rf json -rff results.json
```
The full run was before the fallback for a scan on a nullable column. The
new query `COUNT_NO_INDEX` measures that case: with the fallback, `auto` is
equal to `off` (0.75 against 0.77 ms at selectivity 0.008, 1M rows), where a
scan of the dictionary ids takes 12 ms.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]