forked from xitongsys/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.go
170 lines (151 loc) · 5.87 KB
/
chunk.go
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
package layout
import (
"github.com/apache/thrift/lib/go/thrift"
"github.com/xitongsys/parquet-go/common"
"github.com/xitongsys/parquet-go/encoding"
"github.com/xitongsys/parquet-go/types"
"github.com/xitongsys/parquet-go/schema"
"github.com/xitongsys/parquet-go/parquet"
)
//Chunk stores the ColumnChunk in parquet file
type Chunk struct {
Pages []*Page
ChunkHeader *parquet.ColumnChunk
}
//Convert several pages to one chunk
func PagesToChunk(pages []*Page) *Chunk {
ln := len(pages)
var numValues int64 = 0
var totalUncompressedSize int64 = 0
var totalCompressedSize int64 = 0
var maxVal interface{} = pages[0].MaxVal
var minVal interface{} = pages[0].MinVal
pT, cT := types.TypeNameToParquetType(pages[0].Info.Type, pages[0].Info.BaseType)
for i := 0; i < ln; i++ {
if pages[i].Header.DataPageHeader != nil {
numValues += int64(pages[i].Header.DataPageHeader.NumValues)
} else {
numValues += int64(pages[i].Header.DataPageHeaderV2.NumValues)
}
totalUncompressedSize += int64(pages[i].Header.UncompressedPageSize) + int64(len(pages[i].RawData)) - int64(pages[i].Header.CompressedPageSize)
totalCompressedSize += int64(len(pages[i].RawData))
maxVal = common.Max(maxVal, pages[i].MaxVal, pT, cT)
minVal = common.Min(minVal, pages[i].MinVal, pT, cT)
}
chunk := new(Chunk)
chunk.Pages = pages
chunk.ChunkHeader = parquet.NewColumnChunk()
metaData := parquet.NewColumnMetaData()
metaData.Type = pages[0].DataType
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_RLE)
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_BIT_PACKED)
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_PLAIN)
//metaData.Encodings = append(metaData.Encodings, parquet.Encoding_DELTA_BINARY_PACKED)
metaData.Codec = pages[0].CompressType
metaData.NumValues = numValues
metaData.TotalCompressedSize = totalCompressedSize
metaData.TotalUncompressedSize = totalUncompressedSize
metaData.PathInSchema = pages[0].Path
metaData.Statistics = parquet.NewStatistics()
if maxVal != nil && minVal != nil {
tmpBufMax := encoding.WritePlain([]interface{}{maxVal}, *pT)
tmpBufMin := encoding.WritePlain([]interface{}{minVal}, *pT)
if (cT != nil && *cT == parquet.ConvertedType_UTF8) ||
(cT != nil && *cT == parquet.ConvertedType_DECIMAL && *pT == parquet.Type_BYTE_ARRAY) {
tmpBufMax = tmpBufMax[4:]
tmpBufMin = tmpBufMin[4:]
}
metaData.Statistics.Max = tmpBufMax
metaData.Statistics.Min = tmpBufMin
}
chunk.ChunkHeader.MetaData = metaData
return chunk
}
//Convert several pages to one chunk with dict page first
func PagesToDictChunk(pages []*Page) *Chunk {
if len(pages) < 2 {
return nil
}
var numValues int64 = 0
var totalUncompressedSize int64 = 0
var totalCompressedSize int64 = 0
var maxVal interface{} = pages[1].MaxVal
var minVal interface{} = pages[1].MinVal
pT, cT := types.TypeNameToParquetType(pages[1].Info.Type,
pages[1].Info.BaseType)
for i := 0; i < len(pages); i++ {
if pages[i].Header.DataPageHeader != nil {
numValues += int64(pages[i].Header.DataPageHeader.NumValues)
} else if pages[i].Header.DataPageHeaderV2 != nil {
numValues += int64(pages[i].Header.DataPageHeaderV2.NumValues)
}
totalUncompressedSize += int64(pages[i].Header.UncompressedPageSize) + int64(len(pages[i].RawData)) - int64(pages[i].Header.CompressedPageSize)
totalCompressedSize += int64(len(pages[i].RawData))
if i > 0 {
maxVal = common.Max(maxVal, pages[i].MaxVal, pT, cT)
minVal = common.Min(minVal, pages[i].MinVal, pT, cT)
}
}
chunk := new(Chunk)
chunk.Pages = pages
chunk.ChunkHeader = parquet.NewColumnChunk()
metaData := parquet.NewColumnMetaData()
metaData.Type = pages[1].DataType
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_RLE)
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_BIT_PACKED)
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_PLAIN)
metaData.Encodings = append(metaData.Encodings, parquet.Encoding_PLAIN_DICTIONARY)
metaData.Codec = pages[1].CompressType
metaData.NumValues = numValues
metaData.TotalCompressedSize = totalCompressedSize
metaData.TotalUncompressedSize = totalUncompressedSize
metaData.PathInSchema = pages[1].Path
metaData.Statistics = parquet.NewStatistics()
if maxVal != nil && minVal != nil {
tmpBufMax := encoding.WritePlain([]interface{}{maxVal}, *pT)
tmpBufMin := encoding.WritePlain([]interface{}{minVal}, *pT)
if (cT != nil && *cT == parquet.ConvertedType_UTF8) ||
(cT != nil && *cT == parquet.ConvertedType_DECIMAL && *pT == parquet.Type_BYTE_ARRAY) {
tmpBufMax = tmpBufMax[4:]
tmpBufMin = tmpBufMin[4:]
}
metaData.Statistics.Max = tmpBufMax
metaData.Statistics.Min = tmpBufMin
}
chunk.ChunkHeader.MetaData = metaData
return chunk
}
//Decode a dict chunk
func DecodeDictChunk(chunk *Chunk) {
dictPage := chunk.Pages[0]
numPages := len(chunk.Pages)
for i := 1; i < numPages; i++ {
numValues := len(chunk.Pages[i].DataTable.Values)
for j := 0; j < numValues; j++ {
if chunk.Pages[i].DataTable.Values[j] != nil {
index := chunk.Pages[i].DataTable.Values[j].(int64)
chunk.Pages[i].DataTable.Values[j] = dictPage.DataTable.Values[index]
}
}
}
chunk.Pages = chunk.Pages[1:] // delete the head dict page
}
//Read one chunk from parquet file (Deprecated)
func ReadChunk(thriftReader *thrift.TBufferedTransport, schemaHandler *schema.SchemaHandler, chunkHeader *parquet.ColumnChunk) (*Chunk, error) {
chunk := new(Chunk)
chunk.ChunkHeader = chunkHeader
var readValues int64 = 0
var numValues int64 = chunkHeader.MetaData.GetNumValues()
for readValues < numValues {
page, cnt, _, err := ReadPage(thriftReader, schemaHandler, chunkHeader.GetMetaData())
if err != nil {
return nil, err
}
chunk.Pages = append(chunk.Pages, page)
readValues += cnt
}
if len(chunk.Pages) > 0 && chunk.Pages[0].Header.GetType() == parquet.PageType_DICTIONARY_PAGE {
DecodeDictChunk(chunk)
}
return chunk, nil
}