Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Block stream read process would be terminated by empty block with zero rows #1104

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clickhouse_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ next:
r.err = err
return false
}
goto next
case block := <-r.stream:
if block == nil {
return false
Expand All @@ -63,6 +62,7 @@ next:
}
r.row, r.block = 0, block
}
goto next
}
r.row++
return r.row <= r.block.Rows()
Expand Down
116 changes: 116 additions & 0 deletions clickhouse_rows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package clickhouse

import (
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)

func TestReadWithEmptyBlock(t *testing.T) {
blockInitFunc := func() *proto.Block {
retVal := &proto.Block{
Packet: 0,
Columns: nil,
Timezone: nil,
}
retVal.AddColumn("col1", ("Int64"))
retVal.AddColumn("col2", ("String"))
return retVal
}

testCases := map[string]struct {
actual func() rows
expected int
}{
"none empty": {
func() rows {
firstBlock := blockInitFunc()
firstBlock.Append(int64(0), strconv.Itoa(0))
blockChan := make(chan *proto.Block)
go func() {
for i := 1; i < 10; i++ {
block := blockInitFunc()
block.Append(int64(i), strconv.Itoa(i))
blockChan <- block
}
close(blockChan)
}()
return rows{
err: nil,
row: 0,
block: firstBlock,
totals: nil,
errors: nil,
stream: blockChan,
columns: nil,
structMap: nil,
}
},
10,
},
"all empty": {
func() rows {
firstBlock := blockInitFunc()
blockChan := make(chan *proto.Block)
go func() {
for i := 1; i < 10; i++ {
block := blockInitFunc()
blockChan <- block
}
close(blockChan)
}()
return rows{
err: nil,
row: 0,
block: firstBlock,
totals: nil,
errors: nil,
stream: blockChan,
columns: nil,
structMap: nil,
}
},
0,
},
"some empty": {
func() rows {
firstBlock := blockInitFunc()
blockChan := make(chan *proto.Block)
go func() {
for i := 1; i < 10; i++ {
block := blockInitFunc()
if i%2 == 0 {
block.Append(int64(i), strconv.Itoa(i))
}
blockChan <- block
}
close(blockChan)
}()
return rows{
err: nil,
row: 0,
block: firstBlock,
totals: nil,
errors: nil,
stream: blockChan,
columns: nil,
structMap: nil,
}
},
4,
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
actual := testCase.actual()

rowCnt := 0
for actual.Next() {
rowCnt++
}
assert.Equal(t, testCase.expected, rowCnt)
})
}
}
Loading