# Pattern Matching Zero-Rows Fix Summary

## Overview
Fixed three critical bugs preventing variable-length path matching from working in nxCypher.

## Test Results

### Before Fix
- **Status**: Pattern matching completely broken
- **Error**: `ValueError: too many values to unpack (expected 2, got 3)`
- **Passing tests**: ~400/1300 (excluding CALL tests)

### After Fix
- **Status**: Pattern matching functional
- **Passing tests**: 443/900 non-xfailed tests (49.2%)
- **Improvement**: +43 tests now passing (+10.75%)
- **Test breakdown**:
  - 418 failed
  - 443 passed
  - 39 deselected (CALL procedures)
  - 439 xfailed (temporal types, etc.)

## Bugs Fixed

### Bug #1: Pattern Unpacking Error (CRITICAL)
**File**: `nxcypher/interpreter.py`
**Location**: Line 284 in `_apply_match()`

**Problem**:
```python
nodes, edges = part  # ERROR: expected 2, got 3
```

The pattern structure is actually `(path_var, nodes, edges)` but the code was only unpacking 2 values.

**Fix**:
```python
path_var, nodes, edges = part
if path_var:
    executor._paths.append(path_var)
```

**Impact**: This was causing ALL MATCH clauses to crash, not just variable-length paths.

---

### Bug #2: Background Section Not Parsed
**File**: `nxcypher/test_tck.py`
**Location**: `parse_feature_file()` function

**Problem**:
TCK feature files use `Background:` sections for common setup queries:
```gherkin
Background:
  Given an empty graph
  And having executed:
    """
    CREATE (n0:A {name: 'n0'}), ...
    """

Scenario: [1] Test case
  When executing query:
    """
    MATCH (a:A) RETURN a
    """
```

The parser was ignoring Background sections, so tests ran on empty graphs.

**Fix**:
```python
# Extract Background setup queries (applies to all scenarios)
background_queries = []
background_pattern = r'Background:\s*.*?And having executed:\s*"""\s*\n(.*?)"""'
for m in re.finditer(background_pattern, content, re.DOTALL):
    background_queries.append(m.group(1).strip())

# Prepend to each scenario's setup
scenario.setup_queries = background_queries + scenario.setup_queries
```

**Impact**: Match5 tests and others were returning 0 rows because graphs weren't initialized.

---

### Bug #3: Unbounded Variable-Length Patterns
**File**: `nxcypher/interpreter.py`
**Location**: Lines 313-318 in `_apply_match()`

**Problem**:
Pattern `[:LIKES*]` (unbounded) was being limited to `[*1..2]` instead of `[*1..100]`:
```python
maxh = maxh if maxh is not None else minh + 1  # BUG: always minh+1
```

**Fix**:
```python
if maxh is None:
    maxh = executor._max_hop  # Use limit (default 100)
elif maxh <= minh:
    maxh = minh + 1
```

**Impact**: Variable-length paths like `(a)-[*]->(b)` now explore full depth.

## Test Case Examples

### Example 1: Match5::[1] - Variable-Length Paths
**Query**:
```cypher
MATCH (a:A)
MATCH (a)-[:LIKES*]->(c)
RETURN c.name
```

**Before**: ValueError (too many values to unpack)
**After**: Returns 34 rows (expected 14 - deduplication issue)

**Status**: Partially fixed - executes but row count mismatch

### Example 2: Simple Variable-Length
**Query**:
```cypher
MATCH (a)-[*1..2]->(b)
RETURN a, b
```

**Before**: Crash on unpacking
**After**: Works correctly

## Remaining Issues

### 1. Row Count Mismatches
Some tests return more rows than expected (e.g., 34 vs 14). This appears to be related to:
- Multiple MATCH clauses creating Cartesian products
- Lack of deduplication for equivalent matches via different paths
- Possible semantic difference in how nxCypher vs Neo4j handle multi-MATCH queries

### 2. Categories Still Failing
- Type conversion functions (toInteger, toString, etc.)
- String functions (replace, split, etc.)
- Mathematical functions (abs, round, etc.)
- Temporal functions (date, datetime - marked as xfail)
- Map and list operations

## Files Modified

1. **nxcypher/interpreter.py**
   - Fixed pattern unpacking in `_apply_match()`
   - Fixed unbounded max_hop handling

2. **nxcypher/test_tck.py**
   - Added Background section parsing
   - Background queries now prepended to all scenarios

## Branch
`fix/pattern-matching-zero-rows`

## Commit
`c6502b8` - "fix: Add Background support and fix pattern unpacking for variable-length paths"

## Next Steps

1. **Investigate row count mismatches**
   - Debug why `MATCH (a:A) MATCH (a)-[*]->(c)` returns duplicates
   - May need deduplication logic or different executor chaining

2. **Implement missing functions**
   - Type conversion: toString, toInteger, toFloat, toBoolean
   - String manipulation: replace, split, substring
   - Math functions: abs, round, ceil, floor, sqrt

3. **Test more variable-length scenarios**
   - Bounded ranges: `[*2..5]`
   - With relationship types: `[:TYPE*]`
   - Multiple variable-length in single query
