Repository navigation
Expand file tree
/
Copy patharrow_chunk.go
More file actions
141 lines (121 loc) · 4.05 KB
/
Copy patharrow_chunk.go
File metadata and controls
141 lines (121 loc) · 4.05 KB
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
// Copyright (c) 2020-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"encoding/base64"
"io"
"strconv"
"strings"
"time"
"github.com/apache/arrow/go/v14/arrow"
"github.com/apache/arrow/go/v14/arrow/ipc"
"github.com/apache/arrow/go/v14/arrow/memory"
)
type arrowResultChunk struct {
reader *ipc.Reader
rowCount int
loc *time.Location
allocator memory.Allocator
}
func (arc *arrowResultChunk) decodeArrowChunk(rowType []execResponseRowType, highPrec bool) ([]chunkRowType, error) {
logger.Debug("Arrow Decoder")
var chunkRows []chunkRowType
for arc.reader.Next() {
record := arc.reader.Record()
start := len(chunkRows)
numRows := int(record.NumRows())
columns := record.Columns()
chunkRows = append(chunkRows, make([]chunkRowType, numRows)...)
for i := start; i < start+numRows; i++ {
chunkRows[i].ArrowRow = make([]snowflakeValue, len(columns))
}
for colIdx, col := range columns {
values := make([]snowflakeValue, numRows)
if err := arrowToValue(values, rowType[colIdx], col, arc.loc, highPrec); err != nil {
return nil, err
}
for i := range values {
chunkRows[start+i].ArrowRow[colIdx] = values[i]
}
}
arc.rowCount += numRows
}
return chunkRows, arc.reader.Err()
}
func (arc *arrowResultChunk) decodeArrowBatch(scd *snowflakeChunkDownloader) (*[]arrow.Record, error) {
var records []arrow.Record
defer arc.reader.Release()
for arc.reader.Next() {
rawRecord := arc.reader.Record()
record, err := arrowToRecord(scd.ctx, rawRecord, arc.allocator, scd.RowSet.RowType, arc.loc)
if err != nil {
return nil, err
}
records = append(records, record)
}
return &records, arc.reader.Err()
}
// Note(Qing): Previously, the gosnowflake driver decodes the raw arrow chunks fetched from snowflake by
// calling the decodeArrowBatch() function above. Instead of decoding here, we directly pass the raw records
// to evaluator, along with neccesary metadata needed.
func (arc *arrowResultChunk) passRawArrowBatch(scd *snowflakeChunkDownloader) (*[]arrow.Record, error) {
var records []arrow.Record
for {
rawRecord, err := arc.reader.Read()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
// Here we check all metadata from snowflake are preserved, so evaluator can decode accordingly
for idx, field := range rawRecord.Schema().Fields() {
// NOTE(Qing): Sometimes we see the rowtype metadata specify nullable as false but then still
// reveive nullable arrow records. Given that, we do not check the nullability here. Also, no
// need to compare names.
if !checkMetadata(field.Metadata, scd.RowSet.RowType[idx]) {
logger.Error("Lack or mismatch of necessary metadata to decode fetched raw arrow records")
return nil, &SnowflakeError{
Message: "Lack or mismatch of necessary metadata to decode fetched raw arrow records",
}
}
}
rawRecord.Retain()
records = append(records, rawRecord)
}
return &records, nil
}
func checkMetadata(actual arrow.Metadata, expected execResponseRowType) bool {
// LogicalType seems to be the only REALLY necessary metadata.
var hasLogicalType bool
for idx, key := range actual.Keys() {
switch strings.ToUpper(key) {
case "LOGICALTYPE":
hasLogicalType = true
if !strings.EqualFold(actual.Values()[idx], expected.Type) {
return false
}
case "SCALE":
switch strings.ToUpper(expected.Type) {
case "FIXED", "TIME", "TIMESTAMP_LTZ", "TIMESTAMP_NTZ":
if i64, err := strconv.ParseInt(actual.Values()[idx], 10, 64); err != nil || i64 != expected.Scale {
return false
}
default:
}
default:
}
}
return hasLogicalType
}
// Build arrow chunk based on RowSet of base64
func buildFirstArrowChunk(rowsetBase64 string, loc *time.Location, alloc memory.Allocator) arrowResultChunk {
rowSetBytes, err := base64.StdEncoding.DecodeString(rowsetBase64)
if err != nil {
return arrowResultChunk{}
}
rr, err := ipc.NewReader(bytes.NewReader(rowSetBytes), ipc.WithAllocator(alloc))
if err != nil {
return arrowResultChunk{}
}
return arrowResultChunk{rr, 0, loc, alloc}
}