Skip to content

SIMD string kernels

Native Calc uses SIMD byte search and transformation for compatible string inputs. Search still returns Flink's 1-based character positions. Unicode inputs use UTF-8 character boundaries to interpret start positions and translate byte offsets. Binary inputs with malformed UTF-8 continue to use Flink's byte stepping rules.

LOCATE, POSITION, and INSTR use the existing memchr dependency for byte search. A literal needle prepares its substring finder once per batch, following DataFusion's strpos pattern. Short and mixed-text searches can check candidate offsets directly with memchr. Backward INSTR checks the last byte of each candidate with memrchr. Both paths bound the number of rejected candidates before switching to general substring search. INSTR retains overlapping matches. SPLIT_INDEX uses SIMD single-byte separator search and reuses the finder for a literal multi-byte separator. It consumes only the separator matches needed for the requested token. The empty string separator still uses Flink's Java whitespace rules.

ASCII LOWER and UPPER map bytes directly into the final Arrow value buffer. The represented case path also maps cached ASCII units when the worker's locale rules permit it. REVERSE writes each row in reverse into its final byte span, preserving the original offsets and null validity. The compiler vectorizes the ASCII byte loops. On x86-64 with AVX2, case conversion uses a wider loop for buffers of at least 256 bytes. On Unicode rows, reversal preserves complete code points and the JDK-compatible case path applies the worker's existing locale and cache rules.

A comparison of CHAR_LENGTH(TRIM(s)) with zero can scan the original bytes for a non-space without creating a trimmed string. This applies to default both-end trimming over direct string columns and preserves NULL. For eligible buffers the x86-64 path checks 32 bytes at a time with AVX2. Short strings and CPUs without AVX2 use the same early-exit byte scan. An isolated x86-64 correctness probe executed this kernel and the packed INT division kernel under QEMU 8.0.5 with AVX2 enabled. It checked unaligned buffers, individual non-space bytes, vector tails, and more than two million signed division cases.

The string projection pipeline benchmark combines this predicate with conditional concatenation, timestamp substrings, integer arithmetic and padding. It keeps the row source and sink, and the native run checks both transposes and Calc execution. Use the same input width and row count for the baseline, candidate and stock Flink release runs.

SF_BENCHMARK=true mvn -pl streamfusion-runtime -am test -Pbench,benchmarks \
  -Dtest=StringProjectionPipelineBenchmark -Dsurefire.failIfNoSpecifiedTests=false \
  -Dstring.projection.engine=native -Dstring.projection.width=256

Use -Dstring.projection.engine=flink for the stock control.

An Apple M4 run used Flink 2.2.1, JDK 17, release native libraries without RocksDB, parallelism one, 500,000 rows, and width 16. It ran stock Flink, two alternating native baseline/candidate pairs, then stock Flink again. Each fork discarded two warm-ups and timed three complete jobs. The baseline at 39500777a already included the trim predicate. The candidate added scalar-aware integer division.

Engine Range of fork medians Full measured range
Stock Flink 0.469–0.530 s 0.468–0.547 s
StreamFusion baseline 0.261–0.263 s 0.261–0.269 s
StreamFusion with scalar-aware division 0.264–0.266 s 0.255–0.270 s

The prepared-batch division improvement did not produce a measurable improvement in this ARM64 pipeline run.

A separate four-second stack sample of the warmed native pipeline at four million rows observed 193 stacks within native Calc. Of those, 108 included CASE evaluation, with Arrow byte filtering, string concatenation, and merge appearing in its branches. SUBSTR, LPAD, and integer parsing also appeared in the projection stacks.

Conditional concatenation

A one-branch CASE can write directly to one Arrow string array when its THEN expression is an input column and its ELSE expression is CONCAT of UTF-8 input columns and non-null string literals. The evaluator reads only the values from the chosen branch and reuses the input array when all rows choose THEN. A NULL condition chooses ELSE. A selected NULL string argument makes the corresponding output NULL.

Run the prepared-batch comparison with python3 bin/bench-native.py --bench calc_expressions --filter case_concat. The benchmark supplies the CASE predicate as a precomputed Boolean column. CONCAT uses a 128-byte payload plus two nullable fields of the same width. It verifies every output against a rowwise oracle. Apple M4 release builds with the counting system allocator and no RocksDB were measured in two alternating pairs against fb766a576.

Rows Field width Prior median Direct output median Paired speedup
16 16 B 3.77–3.81 µs 1.59–1.60 µs 2.36–2.39×
16 256 B 4.22–4.23 µs 1.74–1.83 µs 2.30–2.43×
1,024 16 B 37.9–45.4 µs 17.0–19.8 µs 2.22–2.29×
1,024 256 B 61.9–104.6 µs 22.7–31.2 µs 2.73–3.35×
16,384 16 B 952–978 µs 385–404 µs 2.42–2.47×
16,384 256 B 2.48–2.54 ms 0.702–0.773 ms 3.29–3.53×

For 16,384 rows and 256-byte fields, allocation requests fell from 44.0 MB to 14.1 MB, with the same 7.92 MB of new output buffers. The portable kernel reduces copies and has no CPU feature requirement.

The 500,000-row pipeline comparison used the same M4 setup and Flink 2.2.1. Two warm-up jobs preceded three measured jobs per fork, with the same stock/previous/candidate/candidate/previous/stock order and 16-byte host width. The full projection matched stock Flink in release-library SQL tests on Flink 1.18.1 and 2.2.1. The harness uses CHAR_LENGTH for the stock Flink character-length function. A separate warmed pipeline profile observed the direct conditional concatenation kernel on the native Calc stack.

Engine Range of fork medians Full measured range
Stock Flink 0.465–0.483 s 0.462–0.520 s
StreamFusion at fb766a576 0.254–0.264 s 0.253–0.279 s
StreamFusion with direct conditional concatenation 0.244–0.245 s 0.242–0.252 s

Literal substring and padding

Run python3 bin/bench-native.py --bench calc_expressions --filter string_literal_bounds for prepared Arrow batches. The fixture uses a NULL every seventh row, 14-byte ASCII timestamps for SUBSTR, and one- or two-digit minute strings for LPAD. Each case checks the complete result. Apple M4 release builds with the counting system allocator and no RocksDB were measured in two alternating pairs against d657aeef3.

Expression Rows Prior median Scalar literals median
SUBSTR(s, 0, 8) 16 0.812–0.813 µs 0.805–0.809 µs
1,024 6.46–6.47 µs 6.01–6.04 µs
16,384 91.7–92.6 µs 85.0–85.1 µs
SUBSTR(s, 11, 2) 16 0.800–0.812 µs 0.787–0.801 µs
1,024 6.17–6.20 µs 5.93–5.97 µs
16,384 88.6–88.7 µs 84.2–87.2 µs
LPAD(s, 2, '0') 16 1.00–1.03 µs 0.889–0.929 µs
1,024 12.10–12.11 µs 7.83–7.88 µs
16,384 183–187 µs 115–126 µs

At 16,384 rows, requested allocation fell from 397 kB to 266 kB for the date slice, from 397 kB to 201 kB for the minute slice, and from 294 kB to 102 kB for LPAD. The 500,000-row pipeline on the same M4 and Flink 2.2.1 showed no consistent incremental gain. Previous StreamFusion fork medians were 0.247–0.253 s (full range 0.234–0.259 s), and scalar-literal fork medians were 0.248–0.253 s (full range 0.230–0.256 s). The measured jobs kept both transposes. Correctness checks passed on Flink 1.18.1 and 2.2.1.

Native paths in the log projection

SQL expression Native path
IS NULL and OR Arrow validity and Boolean bitmaps
CHAR_LENGTH(TRIM(s)) = 0 One space scan with runtime AVX2 dispatch for eligible x86-64 inputs
CASE … ELSE CONCAT(…) END Direct conditional output for UTF-8 columns and literal separators
SUBSTR Scalar literal bounds and byte indexing for ASCII prefixes
String to INT AVX2 dispatch for dense two-digit batches, with Flink-compatible general parsing
/ 15 Scalar divisor with runtime packed division for eligible x86-64 INT batches
* 15 Arrow wrapping primitive multiplication
/ 15 * 15 Combined INT division and multiplication with runtime AVX2 dispatch
INT to VARCHAR Formats the original INT values directly into the Arrow value buffer
LPAD One prepared fill for short literal ASCII padding

Minute casts and arithmetic

python3 bin/bench-native.py --bench calc_expressions --filter minute_casts_and_arithmetic compares prepared Arrow batches. The cast fixtures use dense two-digit text and mixed text with NULLs and signed fractional strings. Integer inputs include NULLs and signed limits. The benchmark checks every output against its scalar result.

Two alternating release pairs on the M4 used Rust 1.95, the counting system allocator, and builds without RocksDB. The baseline was 112c40bdb. The table shows median ranges.

Expression Rows Baseline Candidate
Dense two-digit CAST to INT 1,024 4.57–4.80 µs 1.22–1.29 µs
16,384 69.4–69.5 µs 12.7–12.8 µs
Mixed CAST to INT 1,024 5.12–5.19 µs 4.52–4.81 µs
16,384 73.4–73.5 µs 64.1–65.0 µs
CAST INT to VARCHAR 1,024 6.72–6.81 µs 5.33–5.36 µs
16,384 117.1–124.5 µs 79.6–80.5 µs
v * 15 wrapping multiplication control 1,024 0.387–0.389 µs 0.388–0.434 µs
16,384 1.19–1.20 µs 1.24–1.28 µs
(v / 15) * 15 1,024 1.14–1.15 µs 0.947–0.951 µs
16,384 9.22–12.49 µs 8.59–8.63 µs

At 16,384 rows, requested allocation fell from 329 kB to 198 kB for formatting and from 133 kB to 67 kB for combined arithmetic. The mixed parser recognizes two-digit rows inside those batches and retains the original Flink parser for signed text, fractional text, ASCII-space trimming and error handling.

The isolated x86-64 correctness check ran the AVX2 digit and arithmetic kernels under QEMU 8.0.5. It checked both sides of the ASCII digit range, unaligned inputs, lane boundaries and vector tails. The arithmetic covered over two million signed cases with positive and negative divisors, including signed limits.

The conversion-inclusive comparison used JDK 17, released Flink 2.2.1, parallelism one, 500,000 source rows, width 16, two warm-up jobs and three measured jobs per fork. The native forks required both transposes, an empty JVM function list and positive Calc work. The comparison against stock and canonical preceded a separate alternating replay of the predecessor and final candidate under the same configuration.

Engine Series Range of fork medians Full measured range
Stock Flink Canonical comparison 0.462–0.479 s 0.448–0.490 s
Canonical StreamFusion at 50498f1f6 Canonical comparison 0.268–0.270 s 0.252–0.331 s
StreamFusion at 112c40bdb Final replay 0.242–0.249 s 0.227–0.252 s
StreamFusion with cast and arithmetic changes Final replay 0.237–0.243 s 0.225–0.249 s

The native fork medians were about 2–3% lower in the final replay, with overlap in the individual job times. A separate four-second warmed sample on four million rows showed 96 native Calc frame samples. The direct conditional concatenation and substring kernels appeared in 29 and 27 inclusive frame samples respectively. The pipeline timings are M4 measurements. Real x86-64 AVX2 throughput remains unmeasured.

SQL module coverage

Flink 1.18.1's core-only session rejects LENGTH on the original character inputs. With released Hive 3.1.3 loaded and USE MODULES core, hive, the original SQL matched the CHAR_LENGTH form in the UTF-8 parity fixture. For this Hive builtin, LENGTH(TRIM(s)) = 0 on a direct VARCHAR column uses the existing space scan on UTF-8 workers and Arrow UTF-8 input. The worker retains the generated Flink evaluator for other charsets or input layouts. The plan lists LENGTH in both the native and JVM function sets because it carries the fallback. Same-named user functions use their own evaluator. Flink module resolution

The Hive parity test exercises both UTF-8 and ISO-8859-1 JVMs with a string that produces different Hive results between the two encodings. It also checks NULLs, long strings of ASCII spaces, tabs, Unicode, and the complete projection.

On Apple M4, the exact Hive SQL benchmark used JDK 17 with UTF-8, Flink 1.18.1 with the released Hive 3.1.3 SQL connector, parallelism one, and 500,000 rows. Release native builds used the system allocator without RocksDB. Both native engines ran with the row source and sink and verified both transposes and positive native Calc work. Two warm-ups preceded three measured jobs per fork. Runs were ordered stock, predecessor, candidate, candidate, predecessor, stock for each host width. Paths include a leading slash. The predecessor was 5c215550b.

Host width Stock Flink median Prior StreamFusion median Direct blank check median
16 B 0.503–0.524 s 0.307–0.315 s 0.226–0.240 s
256 B 0.752–0.795 s 0.406–0.411 s 0.276–0.280 s

Complete measured ranges in the same order were 0.501–0.532 s, 0.305–0.318 s, and 0.223–0.243 s at width 16, and 0.749–0.799 s, 0.404–0.413 s, and 0.274–0.286 s at width 256. An initial candidate that requested extra Java string-cache columns took 0.363–0.374 s and 0.492–0.509 s in the two fixtures. The default trim examines its input bytes and produces a separate value, so the final certificate does not request these columns. A warmed four-second profile with three million rows and width 64 included 110 native Calc frame samples. SUBSTR and direct CASE concatenation appeared in 27 and 19 inclusive samples respectively. Real x86-64 AVX2 throughput remains unmeasured.

For the optional Hive test or benchmark, attach the released flink-sql-connector-hive-3.1.3_2.12:1.18.1 jar as -Dmaven.test.additionalClasspath="$STREAMFUSION_HIVE_TEST_JAR" with -Pflink-1.18. Run FlinkStringProjectionSqlHarnessTest with either UTF-8 or ISO-8859-1 as the worker JVM's -Dfile.encoding. On JDK 17 include --add-opens=java.base/java.net=ALL-UNNAMED in sf.extraJvmArgs for Hive. For StringProjectionPipelineBenchmark, add -Dstring.projection.hive=true to enable the original spelling. Surefire test classpath option

Matched projection measurements

The prepared projection_text cases isolate IS NULL, OR, TRIM, CHAR_LENGTH, CHAR_LENGTH(TRIM(s)) = 0, the two-column NULL/blank predicate, CASE, and CONCAT. Each case checks every result against a scalar oracle. The fixtures include NULLs, empty strings, tabs, Unicode, all-space values and a non-space byte at the end of a field. Width 33 exercises a 32-byte AVX2 chunk followed by a one-byte tail. The case_concat group measures the actual fused branch and string_literal_bounds covers all three SUBSTR offsets and LPAD.

On the controlled x86-64 host, record the CPU model, Rust and Java versions, Git revisions and Cargo flags. The benchmark prints PROJECTION_RUNTIME_AVX2 from the running Rust process. Use the same settings for both native revisions so the baseline binary can still exercise runtime feature dispatch.

Use canonical revision 50498f1f6 as the previous implementation for the combined optimization. Copy the candidate's native/engine/benches/calc_expressions.rs and streamfusion-runtime/src/benchmark/java/tech/streamfusion/StringProjectionPipelineBenchmark.java to a separate checkout of that revision. Build both revisions with the same release settings and use the same fixtures on both. A shared Cargo target directory can reuse dependencies across sequential builds.

python3 bin/bench-native.py --bench calc_expressions \
  --filter 'engine/(projection_text|case_concat|string_literal_bounds|integer_literal_division|minute_casts_and_arithmetic)'

Run two alternating native pairs. Keep each run's estimates and allocation report under a distinct output location. These are prepared Arrow measurements of the native revisions.

For the whole query, run the pipeline harness in this order for each width flink, previous, native, native, previous, flink. Use the canonical checkout for previous and the candidate checkout for native and flink. Use the same released Flink configuration, row count, width, warmup count and trial count in each fork. The Hive mode checks for the prior JVM LENGTH route in the previous run and the native LENGTH certificate in the native run. Both runs require positive native work and both transposes. Use the client-confirmed module and charset for the production comparison.

The pipeline harness also accepts -Dstring.projection.part=guard, message, date-hour, or minute. These select the host/path NULL and blank predicate, the complete CASE/CONCAT, the two timestamp slices, or the complete minute bucket. Each selection runs over the same source data and uses a row sink with the corresponding output schema. Use the same selection on all three engines and compare medians within that selection. Each run includes job setup and row conversion. Criterion measures kernel latency on prepared batches.

The full projection and its four components ran on the M4 with 500,000 rows and 256-byte hosts, Flink 1.18.1 with Hive 3.1.3, and JDK 17 with UTF-8. Both StreamFusion revisions used Rust 1.95 release builds with the system allocator and RocksDB disabled. The runs reused the Maven-resolved test classpath and JVM settings in fresh JUnit forks, with two warm-ups and three measured jobs in each fork. The table gives each engine's range of fork medians, in milliseconds.

Pipeline selection Stock Flink StreamFusion at 50498f1f6 Optimized StreamFusion
Full original Hive projection 747.7–749.9 460.0–467.7 274.1–278.8
Host/path NULL and blank predicate 598.3–612.5 293.5–293.6 152.8–158.8
CASE and CONCAT with the predicate 644.8–700.3 386.5–387.0 204.4–213.4
Date and hour SUBSTR 482.8–511.1 152.6–155.0 153.5–155.9
Minute slice, casts, arithmetic, and LPAD 496.2–498.3 132.4–138.5 126.7–132.8

The M4 prepared-batch comparison used Rust 1.95, the counting system allocator, release builds without RocksDB and two alternating pairs. Both revisions passed all 60 fixture checks across projection_text and string_literal_bounds. The table gives the range of fork medians in microseconds for 16,384 prepared rows.

Expression Canonical width 16 Candidate width 16 Canonical width 256 Candidate width 256
IS NULL 0.308–0.315 0.316–0.338 0.308–0.309 0.306–0.309
OR 0.679 0.675–0.681 0.669–0.682 0.678
TRIM 108–144 110–144 446–457 403–470
CHAR_LENGTH 21.5 21.4 122–124 123–124
Blank check 128–134 52.9–53.0 516–583 232–234
Complete host/path predicate 241–381 95.2–101.7 992–1,014 342–343
CASE with plain column branches 194–200 202–205 653–728 489–743
Standalone CONCAT 205–285 207 629–631 615–688

At width 256 the requested allocation fell from 8.53 MB to 26.2 kB for the blank check and from 17.1 MB to 69.0 kB for the host/path predicate. The isolated TRIM, CHAR_LENGTH, CASE, CONCAT and bitmap operations serve as controls. The hour SUBSTR fixture went from 89.4–89.9 µs to 79.7–82.4 µs with its 14-byte timestamp input. All three end-to-end Hive harness roles passed the 10,000-row smoke run. The prepared timing results are ARM64 measurements.

Release measurements

Run the scalar group on prepared Arrow inputs. Native Criterion methodology

python3 bin/bench-native.py --bench scalar_functions --filter string_simd

The release comparison used Rust 1.95.0 on one logical CPU of an Intel Core i7-12650H. Each invocation processed 4,096 Arrow rows with result allocation included. The nominal prefix widths were 12, 256, and 2,048 bytes, followed by short suffixes with hits and misses. The 256-byte Unicode fixture had one NULL every eight rows. The prior implementation came from 71dbaf767. Both implementations produced equivalent Arrow outputs for all 44 fixtures. The table shows the range of prior-to-current median latency ratios from two alternating pairs.

Operation Short ASCII 256-byte ASCII 256-byte Unicode with NULLs Long ASCII
LOCATE with a column needle 1.32–1.33× 1.23× 1.71× 1.02–1.03×
POSITION with a literal needle 0.99–1.01× 1.01× 0.96–1.00× 0.99–1.01×
POSITION with a column needle 1.25–1.26× 2.25–2.26× 1.02× 3.80–4.20×
Forward INSTR with a literal needle 2.96× 2.12–2.25× 1.81–1.86× 0.98–1.00×
Backward INSTR with a column needle 1.29–1.31× 1.76–1.77× 1.75–1.76× 1.82–1.87×
SPLIT_INDEX with a literal substring 1.56–1.57× 3.97–4.02× 2.85× 6.42–6.52×
SPLIT_INDEX with a literal byte 2.94–3.03× 2.46× 15.25–15.35× 2.58–2.93×
SPLIT_INDEX with a column separator 1.14–1.15× 1.40× 1.68–1.69× 1.59–1.60×
LOWER 1.00–1.27× 1.03–1.04× 0.99–1.00× 1.05–1.07×
UPPER 1.25–1.29× 0.96–1.02× 0.99× 1.04–1.05×
REVERSE 1.42–1.45× 1.59–1.98× 1.07–1.10× 1.22–1.30×

Literal POSITION continues to use DataFusion's prepared finder. The Unicode column-needle fixture continues to use its variable-needle path. The large single-byte split result for Unicode comes from a delimiter miss. Case results measure the direct UTF-8 registration. These timings cover kernel execution on prepared Arrow buffers.