|
| 1 | +# JSON Scalar Index Validation - LanceDB v0.22.3 Retest |
| 2 | + |
| 3 | +## Executive Summary |
| 4 | + |
| 5 | +**Tested Version**: LanceDB v0.22.3 (upgraded from v0.20) |
| 6 | +**Test Date**: 2025-11-10 |
| 7 | +**Result**: **JSON querying BROKEN - Blocker WORSENED** |
| 8 | + |
| 9 | +## Critical Finding |
| 10 | + |
| 11 | +⚠️ **BREAKING CHANGE**: LanceDB v0.22.3 has **BROKEN** the `json_extract()` functionality that worked in v0.20. |
| 12 | + |
| 13 | +### Phase 2 (v0.20) Status |
| 14 | +- ✅ `json_extract(properties, '$.field')` worked with `DataType::Utf8` (string) storage |
| 15 | +- ✅ Query syntax: `properties != '' AND cast(json_extract(properties, '$.status') as text) = 'done'` |
| 16 | +- ⚠️ No scalar indexes on JSON fields, but basic querying functioned |
| 17 | + |
| 18 | +### v0.22.3 Status |
| 19 | +- ❌ **REGRESSION**: `json_extract()` function signature changed |
| 20 | +- ❌ **NEW REQUIREMENT**: Properties must be stored as `LargeBinary` with JSONB format |
| 21 | +- ❌ **API INCOMPATIBILITY**: Can't cast `Utf8` to `LargeBinary` in SQL |
| 22 | +- ❌ **FORMAT REQUIREMENT**: Expects JSONB (binary JSON format), not UTF-8 JSON strings |
| 23 | + |
| 24 | +## Upgrade Notes |
| 25 | + |
| 26 | +### Breaking API Changes |
| 27 | + |
| 28 | +1. **Function Signature Change**: |
| 29 | + ``` |
| 30 | + v0.20: json_extract(Utf8, Utf8) → Utf8 |
| 31 | + v0.22.3: json_extract(LargeBinary, Utf8) → ??? (requires JSONB input) |
| 32 | + ``` |
| 33 | + |
| 34 | +2. **DataType Cast Changes**: |
| 35 | + - v0.20: `cast(... as text)` supported |
| 36 | + - v0.22.3: Must use `cast(... as string)` - `text` type no longer exists |
| 37 | + |
| 38 | +3. **Null Check Changes**: |
| 39 | + - v0.20: `properties != ''` worked for Utf8 columns |
| 40 | + - v0.22.3: Must use `properties IS NOT NULL` for LargeBinary columns |
| 41 | + |
| 42 | +### Compilation Issues Encountered |
| 43 | + |
| 44 | +1. **Arrow Version Mismatch** (FIXED): |
| 45 | + - LanceDB 0.22.3 requires Arrow 56.x (was using 55.x) |
| 46 | + - **Solution**: Updated `arrow-array`, `arrow-schema`, `arrow-data` to `56.0` |
| 47 | + |
| 48 | +2. **Schema Type Mismatch** (ATTEMPTED FIX): |
| 49 | + - Changed `properties` column from `DataType::Utf8` → `DataType::LargeBinary` |
| 50 | + - Updated array types from `StringArray` → `LargeBinaryArray` |
| 51 | + - **Result**: Compilation successful, but runtime errors |
| 52 | + |
| 53 | +### Schema Migration Attempt |
| 54 | + |
| 55 | +**Goal**: Store properties as LargeBinary to match v0.22.3 `json_extract()` requirements |
| 56 | + |
| 57 | +**Changes Made**: |
| 58 | +```rust |
| 59 | +// OLD (v0.20): |
| 60 | +Field::new("properties", DataType::Utf8, true) |
| 61 | +Arc::new(StringArray::from(properties)) |
| 62 | + |
| 63 | +// NEW (v0.22.3 attempt): |
| 64 | +Field::new("properties", DataType::LargeBinary, true) |
| 65 | +Arc::new(LargeBinaryArray::from(properties_refs)) |
| 66 | + |
| 67 | +// Conversion: |
| 68 | +let properties_bytes: Vec<Vec<u8>> = nodes |
| 69 | + .iter() |
| 70 | + .map(|n| n.properties.as_ref().map(|v| v.to_string().into_bytes()).unwrap_or_default()) |
| 71 | + .collect(); |
| 72 | +``` |
| 73 | + |
| 74 | +**Result**: ❌ **FAILED** - `InvalidJsonb` error |
| 75 | + |
| 76 | +## Test Results |
| 77 | + |
| 78 | +### Test 1: json_extract() with Binary Storage |
| 79 | +**Status**: ❌ FAILED |
| 80 | +**Syntax tested**: |
| 81 | +```sql |
| 82 | +properties IS NOT NULL AND cast(json_extract(properties, '$.status') as string) = 'done' |
| 83 | +``` |
| 84 | +**Error**: |
| 85 | +``` |
| 86 | +Failed to select values from path '$.status': InvalidJsonb |
| 87 | +``` |
| 88 | + |
| 89 | +**Root Cause**: LanceDB v0.22.3 expects JSONB format (binary JSON), not UTF-8 encoded JSON strings. |
| 90 | + |
| 91 | +### JSONB Format Requirement |
| 92 | + |
| 93 | +LanceDB v0.22.3 uses **JSONB** (binary JSON format, similar to PostgreSQL's JSONB): |
| 94 | +- Not just UTF-8 bytes of JSON string |
| 95 | +- Requires specialized binary encoding with type tags and offsets |
| 96 | +- Different from simple `serde_json::to_string().as_bytes()` |
| 97 | + |
| 98 | +**Implication**: Requires substantial schema and encoding changes to support v0.22.3. |
| 99 | + |
| 100 | +### Tests Not Completed |
| 101 | + |
| 102 | +Due to the JSONB format requirement, the following tests could not be completed: |
| 103 | +- ❌ Multi-level nested property filtering |
| 104 | +- ❌ Deep nesting (5 levels) |
| 105 | +- ❌ Sparse property behavior |
| 106 | +- ❌ Performance benchmarks |
| 107 | +- ❌ Scalar index creation |
| 108 | + |
| 109 | +## Technical Analysis |
| 110 | + |
| 111 | +### Why v0.22.3 Is Worse Than v0.20 |
| 112 | + |
| 113 | +1. **Loss of Functionality**: |
| 114 | + - v0.20: Basic JSON querying worked (even without indexes) |
| 115 | + - v0.22.3: JSON querying completely broken without JSONB encoding |
| 116 | + |
| 117 | +2. **Increased Complexity**: |
| 118 | + - v0.20: Store JSON as simple UTF-8 string → works |
| 119 | + - v0.22.3: Must implement JSONB encoder/decoder → non-trivial |
| 120 | + |
| 121 | +3. **Migration Barrier**: |
| 122 | + - v0.20 → v0.22.3 requires: |
| 123 | + - Schema migration (Utf8 → LargeBinary) |
| 124 | + - Data migration (re-encode all JSON as JSONB) |
| 125 | + - Query updates (fix cast syntax, null checks) |
| 126 | + - JSONB encoder implementation |
| 127 | + |
| 128 | +### JSONB Implementation Requirements |
| 129 | + |
| 130 | +To support v0.22.3, we would need to: |
| 131 | + |
| 132 | +1. **Implement JSONB Encoder**: |
| 133 | + ```rust |
| 134 | + fn encode_jsonb(value: &serde_json::Value) -> Vec<u8> { |
| 135 | + // Convert serde_json::Value to JSONB binary format |
| 136 | + // This is non-trivial - requires understanding JSONB spec |
| 137 | + } |
| 138 | + ``` |
| 139 | + |
| 140 | +2. **Implement JSONB Decoder**: |
| 141 | + ```rust |
| 142 | + fn decode_jsonb(bytes: &[u8]) -> Result<serde_json::Value> { |
| 143 | + // Parse JSONB binary back to serde_json::Value |
| 144 | + } |
| 145 | + ``` |
| 146 | + |
| 147 | +3. **Update All Storage Operations**: |
| 148 | + - Insert: JSON → JSONB encoding |
| 149 | + - Query: JSONB → JSON decoding |
| 150 | + - Update: Re-encode modified JSON |
| 151 | + |
| 152 | +**Complexity Estimate**: 2-3 days of development + testing |
| 153 | + |
| 154 | +## Updated Recommendation |
| 155 | + |
| 156 | +### 🔴 **STRONGLY DO NOT MIGRATE to LanceDB v0.22.3** |
| 157 | + |
| 158 | +The Phase 2 recommendation to **not migrate** is now **STRENGTHENED**: |
| 159 | + |
| 160 | +**v0.20 Issues**: |
| 161 | +- ⚠️ No scalar indexes on JSON fields |
| 162 | +- ⚠️ Full table scan performance penalty |
| 163 | +- ✅ But basic querying works |
| 164 | + |
| 165 | +**v0.22.3 Issues**: |
| 166 | +- ❌ JSON querying completely broken |
| 167 | +- ❌ Requires JSONB format implementation |
| 168 | +- ❌ Breaking API changes with no migration path |
| 169 | +- ❌ Increased complexity without any benefits |
| 170 | +- ❌ All Phase 2 limitations PLUS new blockers |
| 171 | + |
| 172 | +### Comparison to Phase 2 Findings |
| 173 | + |
| 174 | +| Aspect | v0.20 (Phase 2) | v0.22.3 (Current) | |
| 175 | +|--------|----------------|-------------------| |
| 176 | +| JSON Querying | ✅ Works | ❌ Broken | |
| 177 | +| Scalar Indexes | ❌ Not supported | ❌ Not supported | |
| 178 | +| Query Performance | ⚠️ Full scan | ❌ Can't query | |
| 179 | +| Migration Effort | Medium | **Very High** | |
| 180 | +| Recommendation | Do Not Migrate | **STRONGLY Do Not Migrate** | |
| 181 | + |
| 182 | +## Recommendation: Stay on Turso |
| 183 | + |
| 184 | +**Turso Advantages** (reinforced by v0.22.3 findings): |
| 185 | +- ✅ Native JSON/JSONB support with SQLite's json_extract() |
| 186 | +- ✅ Scalar indexes work on nested JSON fields |
| 187 | +- ✅ Mature, stable API with backward compatibility |
| 188 | +- ✅ No breaking changes between versions |
| 189 | +- ✅ Excellent query performance with indexes |
| 190 | + |
| 191 | +**LanceDB v0.22.3 Disadvantages**: |
| 192 | +- ❌ Breaking changes with no migration guide |
| 193 | +- ❌ Lost functionality compared to v0.20 |
| 194 | +- ❌ Requires custom JSONB implementation |
| 195 | +- ❌ Still no scalar indexes after all this effort |
| 196 | +- ❌ Immature API undergoing frequent breaking changes |
| 197 | + |
| 198 | +## Next Steps |
| 199 | + |
| 200 | +1. **Abandon LanceDB for Full Node Storage**: |
| 201 | + - Do NOT attempt to implement JSONB encoder |
| 202 | + - Do NOT upgrade beyond v0.20 (if ever used) |
| 203 | + - Maintain Turso as primary datastore |
| 204 | + |
| 205 | +2. **Proceed with Final Evaluation Report (#459)**: |
| 206 | + - Document v0.22.3 regression as additional evidence |
| 207 | + - Update recommendation to "STRONGLY Do Not Migrate" |
| 208 | + - Emphasize API stability concerns |
| 209 | + |
| 210 | +3. **Consider Hybrid Architecture (if embedding search needed)**: |
| 211 | + - Turso: Full node storage with JSON querying |
| 212 | + - LanceDB: Vector embeddings ONLY (not properties) |
| 213 | + - Keep datastores separate and focused |
| 214 | + |
| 215 | +## Files Modified |
| 216 | + |
| 217 | +- `packages/desktop-app/src-tauri/Cargo.toml`: |
| 218 | + - lancedb: 0.20 → 0.22.3 |
| 219 | + - arrow-*: 55.0 → 56.0 |
| 220 | + |
| 221 | +- `packages/desktop-app/src-tauri/src/datastore/lance/store.rs`: |
| 222 | + - Schema: properties DataType::Utf8 → DataType::LargeBinary |
| 223 | + - Arrays: StringArray → LargeBinaryArray (incomplete, reverted) |
| 224 | + |
| 225 | +- `packages/desktop-app/src-tauri/src/datastore/lance/tests/`: |
| 226 | + - json_index_tests.rs: Updated query syntax (`text` → `string`, `''` → `IS NOT NULL`) |
| 227 | + - json_index_v22_retest.rs: New comprehensive test suite (tests failed) |
| 228 | + |
| 229 | +## Conclusion |
| 230 | + |
| 231 | +LanceDB v0.22.3 has **WORSENED** the situation compared to v0.20: |
| 232 | +- Lost basic JSON querying capability |
| 233 | +- Introduced breaking API changes |
| 234 | +- Added JSONB format requirement |
| 235 | +- Still no scalar indexes on JSON fields |
| 236 | + |
| 237 | +**The Phase 2 blocker persists and is now WORSE**. NodeSpace should **STRONGLY avoid** LanceDB v0.22.3 and remain on Turso for all node storage needs. |
| 238 | + |
| 239 | +### Rollback Recommendation |
| 240 | + |
| 241 | +Revert all v0.22.3 changes: |
| 242 | +```bash |
| 243 | +git checkout packages/desktop-app/src-tauri/Cargo.toml |
| 244 | +git checkout packages/desktop-app/src-tauri/src/datastore/lance/ |
| 245 | +``` |
| 246 | + |
| 247 | +Or if keeping for documentation: |
| 248 | +- Keep test files for reference |
| 249 | +- Revert Cargo.toml to lancedb = "0.20" |
| 250 | +- Document findings in final evaluation report |
0 commit comments