forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunker_test.cc
More file actions
383 lines (347 loc) · 11.2 KB
/
Copy pathchunker_test.cc
File metadata and controls
383 lines (347 loc) · 11.2 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <cstdint>
#include <memory>
#include <numeric>
#include <string>
#include <gtest/gtest.h>
#include "arrow/buffer.h"
#include "arrow/csv/chunker.h"
#include "arrow/csv/options.h"
#include "arrow/csv/test_common.h"
#include "arrow/testing/gtest_util.h"
namespace arrow {
namespace csv {
void AssertChunkSize(Chunker& chunker, const std::string& str, uint32_t chunk_size) {
std::shared_ptr<Buffer> block, whole, partial;
block = std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(str.data()),
static_cast<int64_t>(str.size()));
ASSERT_OK(chunker.Process(block, &whole, &partial));
ASSERT_EQ(block->size(), whole->size() + partial->size());
auto actual_chunk_size = static_cast<uint32_t>(whole->size());
ASSERT_EQ(actual_chunk_size, chunk_size);
}
template <typename IntContainer>
void AssertChunking(Chunker& chunker, const std::string& str,
const IntContainer& expected_lengths) {
uint32_t expected_chunk_size;
// First chunkize whole CSV block
expected_chunk_size = static_cast<uint32_t>(
std::accumulate(expected_lengths.begin(), expected_lengths.end(), 0ULL));
AssertChunkSize(chunker, str, expected_chunk_size);
// Then chunkize incomplete substrings of the block
expected_chunk_size = 0;
for (const auto length : expected_lengths) {
AssertChunkSize(chunker, str.substr(0, expected_chunk_size + length - 1),
expected_chunk_size);
expected_chunk_size += static_cast<uint32_t>(length);
AssertChunkSize(chunker, str.substr(0, expected_chunk_size), expected_chunk_size);
}
}
class BaseChunkerTest : public ::testing::TestWithParam<bool> {
protected:
void SetUp() override {
options_ = ParseOptions::Defaults();
options_.newlines_in_values = GetParam();
}
void MakeChunker() { chunker_ = ::arrow::csv::MakeChunker(options_); }
void AssertSkip(const std::string& str, int64_t count, int64_t rem_count,
int64_t rest_size) {
MakeChunker();
{
auto test_count = count;
auto partial = std::make_shared<Buffer>("");
auto block = std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(str.data()),
static_cast<int64_t>(str.size()));
std::shared_ptr<Buffer> rest;
ASSERT_OK(chunker_->ProcessSkip(partial, block, true, &test_count, &rest));
ASSERT_EQ(rem_count, test_count);
ASSERT_EQ(rest_size, rest->size());
AssertBufferEqual(*SliceBuffer(block, block->size() - rest_size), *rest);
}
{
auto test_count = count;
auto split = static_cast<int64_t>(str.find_first_of('\n'));
auto partial =
std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(str.data()), split);
auto block =
std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(str.data() + split),
static_cast<int64_t>(str.size()) - split);
std::shared_ptr<Buffer> rest;
ASSERT_OK(chunker_->ProcessSkip(partial, block, true, &test_count, &rest));
ASSERT_EQ(rem_count, test_count);
ASSERT_EQ(rest_size, rest->size());
AssertBufferEqual(*SliceBuffer(block, block->size() - rest_size), *rest);
}
}
ParseOptions options_;
std::unique_ptr<Chunker> chunker_;
};
INSTANTIATE_TEST_SUITE_P(ChunkerTest, BaseChunkerTest, ::testing::Values(true));
INSTANTIATE_TEST_SUITE_P(NoNewlineChunkerTest, BaseChunkerTest, ::testing::Values(false));
TEST_P(BaseChunkerTest, Basics) {
auto csv = MakeCSVData({"ab,c,\n", "def,,gh\n", ",ij,kl\n"});
auto lengths = {6, 8, 7};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
TEST_P(BaseChunkerTest, Empty) {
MakeChunker();
{
auto csv = MakeCSVData({"\n"});
auto lengths = {1};
AssertChunking(*chunker_, csv, lengths);
}
{
auto csv = MakeCSVData({"\n\n"});
auto lengths = {1, 1};
AssertChunking(*chunker_, csv, lengths);
}
{
auto csv = MakeCSVData({",\n"});
auto lengths = {2};
AssertChunking(*chunker_, csv, lengths);
}
{
auto csv = MakeCSVData({",\n,\n"});
auto lengths = {2, 2};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, Newlines) {
auto check_csv = [&](const std::string& csv) {
AssertChunkSize(*chunker_, csv, static_cast<uint32_t>(csv.size()));
// Trailing \n after \r is optional
AssertChunkSize(*chunker_, csv.substr(0, csv.size() - 1),
static_cast<uint32_t>(csv.size() - 1));
};
MakeChunker();
{
ARROW_SCOPED_TRACE("short values");
check_csv(MakeCSVData({"a\n", "b\r", "c,d\r\n"}));
ARROW_SCOPED_TRACE("long values");
check_csv(MakeCSVData(
{"aaaaaaaaaaaaaaa\n", "bbbbbbbbbbbbb\r", "cccccccccccccc,ddddddddddd\r\n"}));
}
}
TEST_P(BaseChunkerTest, QuotingSimple) {
auto check_csv = [&](const std::string& csv) {
auto lengths = {csv.size()};
AssertChunking(*chunker_, csv, lengths);
};
auto csv_short_values = MakeCSVData({"1,\",3,\",5\n"});
auto csv_long_values = MakeCSVData({"111111111111,\",3333333333333,\",55555555555\n"});
for (auto quoting : {true, false}) {
options_.quoting = quoting;
MakeChunker();
ARROW_SCOPED_TRACE("short values");
check_csv(csv_short_values);
ARROW_SCOPED_TRACE("long values");
check_csv(csv_long_values);
}
}
TEST_P(BaseChunkerTest, QuotingNewline) {
auto csv = MakeCSVData({"a,\"c \n d\",e\n"});
if (options_.newlines_in_values) {
MakeChunker();
auto lengths = {12};
AssertChunking(*chunker_, csv, lengths);
}
{
options_.quoting = false;
MakeChunker();
auto lengths = {6, 6};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, QuotingUnbalanced) {
// Quote introduces a quoted field that doesn't end
auto csv = MakeCSVData({"a,b\n", "1,\",3,,5\n", "c,d\n"});
if (options_.newlines_in_values) {
MakeChunker();
auto lengths = {4};
AssertChunking(*chunker_, csv, lengths);
}
{
options_.quoting = false;
MakeChunker();
auto lengths = {4, 9, 4};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, QuotingEmpty) {
MakeChunker();
{
auto csv = MakeCSVData({"\"\"\n", "a\n"});
auto lengths = {3, 2};
AssertChunking(*chunker_, csv, lengths);
}
{
auto csv = MakeCSVData({",\"\"\n", "a\n"});
auto lengths = {4, 2};
AssertChunking(*chunker_, csv, lengths);
}
{
auto csv = MakeCSVData({"\"\",\n", "a\n"});
auto lengths = {4, 2};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, QuotingDouble) {
{
MakeChunker();
// 4 quotes is a quoted quote
auto csv = MakeCSVData({"\"\"\"\"\n", "a\n"});
auto lengths = {5, 2};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, QuotesSpecial) {
// Some non-trivial cases
{
MakeChunker();
auto csv = MakeCSVData({"a,b\"c,d\n", "e\n"});
auto lengths = {8, 2};
AssertChunking(*chunker_, csv, lengths);
}
{
MakeChunker();
auto csv = MakeCSVData({"a,\"b\" \"c\",d\n", "e\n"});
auto lengths = {12, 2};
AssertChunking(*chunker_, csv, lengths);
}
}
TEST_P(BaseChunkerTest, Escaping) {
{
auto csv = MakeCSVData({"a\\b,c\n", "d\n"});
auto lengths = {6, 2};
{
options_.escaping = false;
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
{
options_.escaping = true;
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
}
{
auto csv = MakeCSVData({"a\\,b,c\n", "d\n"});
auto lengths = {7, 2};
{
options_.escaping = false;
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
{
options_.escaping = true;
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
}
}
TEST_P(BaseChunkerTest, EscapingNewline) {
if (options_.newlines_in_values) {
auto csv = MakeCSVData({"a\\\nb\n", "c\n"});
{
auto lengths = {3, 2, 2};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
options_.escaping = true;
{
auto lengths = {5, 2};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
}
}
TEST_P(BaseChunkerTest, EscapingAndQuoting) {
if (options_.newlines_in_values) {
{
auto csv = MakeCSVData({"\"a\\\"\n", "\"b\\\"\n"});
{
options_.quoting = true;
options_.escaping = true;
auto lengths = {10};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
{
options_.quoting = true;
options_.escaping = false;
auto lengths = {5, 5};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
}
{
auto csv = MakeCSVData({"\"a\\\n\"\n"});
{
options_.quoting = false;
options_.escaping = true;
auto lengths = {6};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
{
options_.quoting = false;
options_.escaping = false;
auto lengths = {4, 2};
MakeChunker();
AssertChunking(*chunker_, csv, lengths);
}
}
}
}
TEST_P(BaseChunkerTest, ParseSkip) {
{
auto csv = MakeCSVData({"ab,c,\n", "def,,gh\n", ",ij,kl\n"});
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 1, 0, 15));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 2, 0, 7));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 3, 0, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 4, 1, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 6, 3, 0));
}
// Test with no trailing new line
{
auto csv = MakeCSVData({"ab,c,\n", "def,,gh\n", ",ij,kl"});
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 2, 0, 6));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 3, 0, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 4, 1, 0));
}
// Test skip with new lines in values
{
auto csv = MakeCSVData({"ab,\"c\n\",\n", "\"d\nef\",,gh\n", ",ij,\"nkl\"\n"});
options_.newlines_in_values = true;
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 1, 0, 21));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 2, 0, 10));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 3, 0, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 4, 1, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 6, 3, 0));
}
// Test with no trailing new line and new lines in values
{
auto csv = MakeCSVData({"ab,\"c\n\",\n", "\"d\nef\",,gh\n", ",ij,\"nkl\""});
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 2, 0, 9));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 3, 0, 0));
ASSERT_NO_FATAL_FAILURE(AssertSkip(csv, 4, 1, 0));
}
}
} // namespace csv
} // namespace arrow