aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/clickhouse-go/rows.go
blob: 30e844f6513f72c96f9c0b545c58a4dd2344c002 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package clickhouse

import (
	"database/sql/driver"
	"fmt"
	"io"
	"reflect"
	"sync"
	"time"

	"github.com/ClickHouse/clickhouse-go/lib/column"
	"github.com/ClickHouse/clickhouse-go/lib/data"
	"github.com/ClickHouse/clickhouse-go/lib/protocol"
)

type rows struct {
	ch           *clickhouse
	err          error
	mutex        sync.RWMutex
	finish       func()
	offset       int
	block        *data.Block
	totals       *data.Block
	extremes     *data.Block
	stream       chan *data.Block
	columns      []string
	blockColumns []column.Column
}

func (rows *rows) Columns() []string {
	return rows.columns
}

func (rows *rows) ColumnTypeScanType(idx int) reflect.Type {
	return rows.blockColumns[idx].ScanType()
}

func (rows *rows) ColumnTypeDatabaseTypeName(idx int) string {
	return rows.blockColumns[idx].CHType()
}

func (rows *rows) Next(dest []driver.Value) error {
	if rows.block == nil || int(rows.block.NumRows) <= rows.offset {
		switch block, ok := <-rows.stream; true {
		case !ok:
			if err := rows.error(); err != nil {
				return err
			}
			return io.EOF
		default:
			rows.block = block
			rows.offset = 0
		}
	}
	for i := range dest {
		dest[i] = rows.block.Values[i][rows.offset]
	}
	rows.offset++
	return nil
}

func (rows *rows) HasNextResultSet() bool {
	return rows.totals != nil || rows.extremes != nil
}

func (rows *rows) NextResultSet() error {
	switch {
	case rows.totals != nil:
		rows.block = rows.totals
		rows.offset = 0
		rows.totals = nil
	case rows.extremes != nil:
		rows.block = rows.extremes
		rows.offset = 0
		rows.extremes = nil
	default:
		return io.EOF
	}
	return nil
}

func (rows *rows) receiveData() error {
	defer close(rows.stream)
	var (
		err         error
		packet      uint64
		progress    *progress
		profileInfo *profileInfo
	)
	for {
		if packet, err = rows.ch.decoder.Uvarint(); err != nil {
			return rows.setError(err)
		}
		switch packet {
		case protocol.ServerException:
			rows.ch.logf("[rows] <- exception")
			return rows.setError(rows.ch.exception())
		case protocol.ServerProgress:
			if progress, err = rows.ch.progress(); err != nil {
				return rows.setError(err)
			}
			rows.ch.logf("[rows] <- progress: rows=%d, bytes=%d, total rows=%d",
				progress.rows,
				progress.bytes,
				progress.totalRows,
			)
		case protocol.ServerProfileInfo:
			if profileInfo, err = rows.ch.profileInfo(); err != nil {
				return rows.setError(err)
			}
			rows.ch.logf("[rows] <- profiling: rows=%d, bytes=%d, blocks=%d", profileInfo.rows, profileInfo.bytes, profileInfo.blocks)
		case protocol.ServerData, protocol.ServerTotals, protocol.ServerExtremes:
			var (
				block *data.Block
				begin = time.Now()
			)
			if block, err = rows.ch.readBlock(); err != nil {
				return rows.setError(err)
			}
			rows.ch.logf("[rows] <- data: packet=%d, columns=%d, rows=%d, elapsed=%s", packet, block.NumColumns, block.NumRows, time.Since(begin))
			if block.NumRows == 0 {
				continue
			}
			switch packet {
			case protocol.ServerData:
				rows.stream <- block
			case protocol.ServerTotals:
				rows.totals = block
			case protocol.ServerExtremes:
				rows.extremes = block
			}
		case protocol.ServerEndOfStream:
			rows.ch.logf("[rows] <- end of stream")
			return nil
		default:
			rows.ch.conn.Close()
			rows.ch.logf("[rows] unexpected packet [%d]", packet)
			return rows.setError(fmt.Errorf("[rows] unexpected packet [%d] from server", packet))
		}
	}
}

func (rows *rows) Close() error {
	rows.ch.logf("[rows] close")
	rows.columns = nil
	for range rows.stream {
	}
	rows.finish()
	return nil
}

func (rows *rows) error() error {
	rows.mutex.RLock()
	defer rows.mutex.RUnlock()
	return rows.err
}

func (rows *rows) setError(err error) error {
	rows.mutex.Lock()
	rows.err = err
	rows.mutex.Unlock()
	return err
}

func (rows *rows) ColumnTypeNullable(idx int) (nullable, ok bool) {
	_, ok = rows.blockColumns[idx].(*column.Nullable)
	return ok, true
}

func (rows *rows) ColumnTypePrecisionScale(idx int) (precision, scale int64, ok bool) {
	decimalVal, ok := rows.blockColumns[idx].(*column.Decimal)
	if !ok {
		if nullable, nullOk := rows.blockColumns[idx].(*column.Nullable); nullOk {
			decimalVal, ok = nullable.GetColumn().(*column.Decimal)
		}
	}
	if ok {
		return int64(decimalVal.GetPrecision()), int64(decimalVal.GetScale()), ok

	}
	return 0, 0, false
}