Skip to content

Stack Overflow Fix — Technical Summary

Problem

During hash-based extraction from the Web UI, the server crashed with:

thread 'tokio-runtime-worker' has overflowed its stack

Root Cause

The MergeDeltaWriter was generating massive OR-based predicates for batch deletes:

WHERE (id=1) OR (id=2) OR (id=3) OR ... OR (id=10000)

With 10,000-row batches, this created predicates with 10,000+ OR conditions. The datafusion query planner's recursive descent parser would blow the stack during predicate parsing and optimization.

Solution Progression

Phase 1: Initial Mitigations (Incomplete)

  • Refactored Arrow builder code to reduce stack frames
  • Simplified string handling (closures → match statements)
  • Increased tokio worker stack: 2MB → 4MB → 16MB
  • Added panic recovery with catch_unwind

Result: Server stayed up (didn't crash), but extraction still failed with stack overflow.

Phase 2: Predicate Chunking (Workaround)

Implemented PREDICATE_CHUNK_SIZE = 500 to split large predicates into smaller chunks:

// Process 10,000 rows in 20 chunks of 500
for chunk in rows.chunks(500) {
    let predicate = build_or_predicate(chunk); // 500 OR conditions
    table.delete().with_predicate(predicate).await?;
}

Result: Worked, but created 20 delete transactions instead of 1 for a 10K batch.

Phase 3: IN Clause Predicates (Optimal Solution) ✅

Replaced string-based OR predicates with type-safe IN clause predicates via datafusion Expression API:

use deltalake::datafusion::prelude::{col, lit};
use deltalake::datafusion::logical_expr::Expr;

// Build IN expression
let in_expr = col("id").in_list(vec![
    lit(1), lit(2), lit(3), ..., lit(10000)
], false);

// For composite keys: AND multiple IN clauses
let predicate = pk1_in_expr.and(pk2_in_expr).and(pk3_in_expr);

table.delete().with_predicate(predicate).await?;

Key Advantages: - Single delete operation per batch (no chunking) - Datafusion query planner properly optimizes IN clauses - Type-safe (no string concatenation) - Handles composite primary keys elegantly

Performance Impact

Scenario Before (OR predicates) After (IN clauses) Improvement
Hash-based extraction (10K rows) 21 transactions
(20 chunked deletes + 1 insert)
2 transactions
(1 delete + 1 insert)
90% reduction
CDC/CT upserts (10K rows) 21 transactions 2 transactions 90% reduction
CDC/CT deletes (10K rows) 20 transactions 1 transaction 95% reduction

Code Changes

Updated Functions (src/writers/delta_writer.rs)

  1. apply_hash_based_upserts() — Hash-based merge with IN clause deletes
  2. apply_upserts_static() — CDC/CT upsert with IN clause deletes
  3. apply_deletes_static() — CDC/CT delete with IN clause predicates

Removed Code

  • const PREDICATE_CHUNK_SIZE: usize = 500; — No longer needed
  • All chunking loops — Single transaction per batch now
  • String-based predicate building — Replaced with Expression API

Added Imports

use deltalake::datafusion::prelude::{col, lit};
use deltalake::datafusion::logical_expr::Expr;

Testing

All 15 integration tests pass:

cargo test --features mssql,delta -- --nocapture

Validated scenarios: - ✅ Single primary key (id IN (1, 2, 3, ...)) - ✅ Composite primary keys ((pk1 IN (...)) AND (pk2 IN (...))) - ✅ Hash-based extraction with 10K+ rows - ✅ CDC extraction with upserts/deletes - ✅ Change Tracking extraction

Future Optimization: Native MERGE API

The IN clause solution gives us 90% of the optimization potential. The remaining 10% would come from native MERGE API:

Approach Transactions per 10K batch Notes
OR predicates (original) 21 Stack overflow with large batches
OR predicates chunked 21 No stack overflow, but many transactions
IN clauses (current) 2 ✅ Excellent performance
Native MERGE (future) 1 Would require deltalake API maturity

Decision: Defer native MERGE implementation until: - deltalake 0.31+ stabilizes the MERGE API - User reports transaction overhead as performance bottleneck - Need for atomic guarantees becomes critical

See FUTURE_OPTIMIZATIONS.md for details.

Lessons Learned

  1. Datafusion has stack limits for deeply nested expression trees (OR chains)
  2. IN clause predicates are properly optimized in datafusion's query planner
  3. Type-safe Expression API is more reliable than string concatenation
  4. 90% optimization is often sufficient — perfect can be enemy of good
  5. Tokio stack size (16MB) still matters as a safety net for edge cases

References