|
| 1 | +package cypher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/orneryd/nornicdb/pkg/storage" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// TestBug_CountDirectReturnVsWithReturn reproduces the bug where: |
| 13 | +// - MATCH (n) RETURN count(n) returns 0 (WRONG) |
| 14 | +// - MATCH (n) WITH n RETURN count(n) returns correct count (CORRECT) |
| 15 | +func TestBug_CountDirectReturnVsWithReturn(t *testing.T) { |
| 16 | + store := storage.NewMemoryEngine() |
| 17 | + defer store.Close() |
| 18 | + exec := NewStorageExecutor(store) |
| 19 | + ctx := context.Background() |
| 20 | + |
| 21 | + // Create 10 nodes |
| 22 | + for i := 0; i < 10; i++ { |
| 23 | + _, err := exec.Execute(ctx, "CREATE (n:CountTest {idx: $idx})", map[string]any{"idx": i}) |
| 24 | + require.NoError(t, err) |
| 25 | + } |
| 26 | + |
| 27 | + // Verify nodes exist |
| 28 | + result, err := exec.Execute(ctx, "MATCH (n:CountTest) RETURN n", nil) |
| 29 | + require.NoError(t, err) |
| 30 | + t.Logf("MATCH (n) RETURN n: %d rows", len(result.Rows)) |
| 31 | + require.Len(t, result.Rows, 10, "Should have 10 nodes") |
| 32 | + |
| 33 | + // Test 1: Direct count in RETURN (this is the broken query) |
| 34 | + t.Run("direct_count_in_return", func(t *testing.T) { |
| 35 | + result, err := exec.Execute(ctx, "MATCH (n:CountTest) RETURN count(n) as cnt", nil) |
| 36 | + require.NoError(t, err) |
| 37 | + require.Len(t, result.Rows, 1) |
| 38 | + |
| 39 | + cnt := result.Rows[0][0] |
| 40 | + t.Logf("MATCH (n) RETURN count(n): %v (type: %T)", cnt, cnt) |
| 41 | + |
| 42 | + var countVal int64 |
| 43 | + switch v := cnt.(type) { |
| 44 | + case int64: |
| 45 | + countVal = v |
| 46 | + case int: |
| 47 | + countVal = int64(v) |
| 48 | + case float64: |
| 49 | + countVal = int64(v) |
| 50 | + } |
| 51 | + assert.Equal(t, int64(10), countVal, "MATCH (n) RETURN count(n) should return 10") |
| 52 | + }) |
| 53 | + |
| 54 | + // Test 2: Count with WITH clause (this works correctly) |
| 55 | + t.Run("count_with_with_clause", func(t *testing.T) { |
| 56 | + result, err := exec.Execute(ctx, "MATCH (n:CountTest) WITH n RETURN count(n) as cnt", nil) |
| 57 | + require.NoError(t, err) |
| 58 | + require.Len(t, result.Rows, 1) |
| 59 | + |
| 60 | + cnt := result.Rows[0][0] |
| 61 | + t.Logf("MATCH (n) WITH n RETURN count(n): %v (type: %T)", cnt, cnt) |
| 62 | + |
| 63 | + var countVal int64 |
| 64 | + switch v := cnt.(type) { |
| 65 | + case int64: |
| 66 | + countVal = v |
| 67 | + case int: |
| 68 | + countVal = int64(v) |
| 69 | + case float64: |
| 70 | + countVal = int64(v) |
| 71 | + } |
| 72 | + assert.Equal(t, int64(10), countVal, "MATCH (n) WITH n RETURN count(n) should return 10") |
| 73 | + }) |
| 74 | + |
| 75 | + // Test 3: count(*) direct |
| 76 | + t.Run("count_star_direct", func(t *testing.T) { |
| 77 | + result, err := exec.Execute(ctx, "MATCH (n:CountTest) RETURN count(*) as cnt", nil) |
| 78 | + require.NoError(t, err) |
| 79 | + require.Len(t, result.Rows, 1) |
| 80 | + |
| 81 | + cnt := result.Rows[0][0] |
| 82 | + t.Logf("MATCH (n) RETURN count(*): %v (type: %T)", cnt, cnt) |
| 83 | + |
| 84 | + var countVal int64 |
| 85 | + switch v := cnt.(type) { |
| 86 | + case int64: |
| 87 | + countVal = v |
| 88 | + case int: |
| 89 | + countVal = int64(v) |
| 90 | + case float64: |
| 91 | + countVal = int64(v) |
| 92 | + } |
| 93 | + assert.Equal(t, int64(10), countVal, "MATCH (n) RETURN count(*) should return 10") |
| 94 | + }) |
| 95 | + |
| 96 | + // Test 4: count without label filter |
| 97 | + t.Run("count_all_nodes_direct", func(t *testing.T) { |
| 98 | + result, err := exec.Execute(ctx, "MATCH (n) RETURN count(n) as cnt", nil) |
| 99 | + require.NoError(t, err) |
| 100 | + require.Len(t, result.Rows, 1) |
| 101 | + |
| 102 | + cnt := result.Rows[0][0] |
| 103 | + t.Logf("MATCH (n) RETURN count(n) [all nodes]: %v (type: %T)", cnt, cnt) |
| 104 | + |
| 105 | + var countVal int64 |
| 106 | + switch v := cnt.(type) { |
| 107 | + case int64: |
| 108 | + countVal = v |
| 109 | + case int: |
| 110 | + countVal = int64(v) |
| 111 | + case float64: |
| 112 | + countVal = int64(v) |
| 113 | + } |
| 114 | + assert.Equal(t, int64(10), countVal, "MATCH (n) RETURN count(n) [all nodes] should return 10") |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +// TestBug_CountAfterDeleteRecreate tests count behavior after delete/recreate |
| 119 | +func TestBug_CountAfterDeleteRecreate(t *testing.T) { |
| 120 | + store := storage.NewMemoryEngine() |
| 121 | + defer store.Close() |
| 122 | + exec := NewStorageExecutor(store) |
| 123 | + ctx := context.Background() |
| 124 | + |
| 125 | + // Create initial nodes |
| 126 | + for i := 0; i < 5; i++ { |
| 127 | + _, err := exec.Execute(ctx, "CREATE (n:Cycle {idx: $idx})", map[string]any{"idx": i}) |
| 128 | + require.NoError(t, err) |
| 129 | + } |
| 130 | + |
| 131 | + // Verify initial count |
| 132 | + result, err := exec.Execute(ctx, "MATCH (n:Cycle) RETURN count(n) as cnt", nil) |
| 133 | + require.NoError(t, err) |
| 134 | + t.Logf("Initial count: %v", result.Rows[0][0]) |
| 135 | + |
| 136 | + // Delete all |
| 137 | + _, err = exec.Execute(ctx, "MATCH (n:Cycle) DELETE n", nil) |
| 138 | + require.NoError(t, err) |
| 139 | + |
| 140 | + // Verify delete count |
| 141 | + result, err = exec.Execute(ctx, "MATCH (n:Cycle) RETURN count(n) as cnt", nil) |
| 142 | + require.NoError(t, err) |
| 143 | + t.Logf("After delete count: %v", result.Rows[0][0]) |
| 144 | + |
| 145 | + // Recreate |
| 146 | + for i := 0; i < 3; i++ { |
| 147 | + _, err := exec.Execute(ctx, "CREATE (n:Cycle {idx: $idx, recreated: true})", map[string]any{"idx": i + 100}) |
| 148 | + require.NoError(t, err) |
| 149 | + } |
| 150 | + |
| 151 | + // Verify recreate count |
| 152 | + result, err = exec.Execute(ctx, "MATCH (n:Cycle) RETURN count(n) as cnt", nil) |
| 153 | + require.NoError(t, err) |
| 154 | + cnt := result.Rows[0][0] |
| 155 | + t.Logf("After recreate count: %v", cnt) |
| 156 | + |
| 157 | + var countVal int64 |
| 158 | + switch v := cnt.(type) { |
| 159 | + case int64: |
| 160 | + countVal = v |
| 161 | + case int: |
| 162 | + countVal = int64(v) |
| 163 | + case float64: |
| 164 | + countVal = int64(v) |
| 165 | + } |
| 166 | + assert.Equal(t, int64(3), countVal, "Count after recreate should be 3") |
| 167 | +} |
0 commit comments