|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/csv/chunker.h" |
| 19 | +#include "arrow/status.h" |
| 20 | +#include "arrow/util/logging.h" |
| 21 | + |
| 22 | +#include <sstream> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +namespace arrow { |
| 26 | +namespace csv { |
| 27 | + |
| 28 | +Chunker::Chunker(ParseOptions options, int32_t max_num_rows) |
| 29 | + : options_(options), max_num_rows_(max_num_rows) {} |
| 30 | + |
| 31 | +// NOTE: cvsmonkey (https://github.com/dw/csvmonkey) has optimization ideas |
| 32 | + |
| 33 | +template <bool quoting, bool escaping> |
| 34 | +inline const char* Chunker::ReadLine(const char* data, const char* data_end) { |
| 35 | + DCHECK_EQ(quoting, options_.quoting); |
| 36 | + DCHECK_EQ(escaping, options_.escaping); |
| 37 | + |
| 38 | + // The parsing state machine |
| 39 | + char c; |
| 40 | + |
| 41 | +FieldStart: |
| 42 | + // At the start of a field |
| 43 | + // Quoting is only recognized at start of field |
| 44 | + if (quoting && ARROW_PREDICT_TRUE(data != data_end) && *data == options_.quote_char) { |
| 45 | + data++; |
| 46 | + goto InQuotedField; |
| 47 | + } else { |
| 48 | + goto InField; |
| 49 | + } |
| 50 | + |
| 51 | +InField: |
| 52 | + // Inside a non-quoted part of a field |
| 53 | + if (ARROW_PREDICT_FALSE(data == data_end)) { |
| 54 | + goto AbortLine; |
| 55 | + } |
| 56 | + c = *data++; |
| 57 | + if (escaping && ARROW_PREDICT_FALSE(c == options_.escape_char)) { |
| 58 | + if (ARROW_PREDICT_FALSE(data == data_end)) { |
| 59 | + goto AbortLine; |
| 60 | + } |
| 61 | + data++; |
| 62 | + goto InField; |
| 63 | + } |
| 64 | + if (ARROW_PREDICT_FALSE(c == '\r')) { |
| 65 | + if (ARROW_PREDICT_TRUE(data != data_end) && *data == '\n') { |
| 66 | + data++; |
| 67 | + } |
| 68 | + goto LineEnd; |
| 69 | + } |
| 70 | + if (ARROW_PREDICT_FALSE(c == '\n')) { |
| 71 | + goto LineEnd; |
| 72 | + } |
| 73 | + if (ARROW_PREDICT_FALSE(c == options_.delimiter)) { |
| 74 | + goto FieldEnd; |
| 75 | + } |
| 76 | + goto InField; |
| 77 | + |
| 78 | +InQuotedField: |
| 79 | + // Inside a quoted part of a field |
| 80 | + if (ARROW_PREDICT_FALSE(data == data_end)) { |
| 81 | + goto AbortLine; |
| 82 | + } |
| 83 | + c = *data++; |
| 84 | + if (escaping && ARROW_PREDICT_FALSE(c == options_.escape_char)) { |
| 85 | + if (data == data_end) { |
| 86 | + goto AbortLine; |
| 87 | + } |
| 88 | + data++; |
| 89 | + goto InQuotedField; |
| 90 | + } |
| 91 | + if (ARROW_PREDICT_FALSE(c == options_.quote_char)) { |
| 92 | + if (options_.double_quote && data != data_end && *data == options_.quote_char) { |
| 93 | + // Double-quoting |
| 94 | + data++; |
| 95 | + } else { |
| 96 | + // End of single-quoting |
| 97 | + goto InField; |
| 98 | + } |
| 99 | + } |
| 100 | + goto InQuotedField; |
| 101 | + |
| 102 | +FieldEnd: |
| 103 | + // At the end of a field |
| 104 | + goto FieldStart; |
| 105 | + |
| 106 | +LineEnd: |
| 107 | + // At the end of line, possibly in the middle of the newline separator |
| 108 | + // if (ARROW_PREDICT_TRUE(data < data_end) && data[-1] == '\r' && *data == '\n') { |
| 109 | + // data++; |
| 110 | + // } |
| 111 | + return data; |
| 112 | + |
| 113 | +AbortLine: |
| 114 | + // Truncated line at end of block |
| 115 | + return nullptr; |
| 116 | +} |
| 117 | + |
| 118 | +template <bool quoting, bool escaping> |
| 119 | +Status Chunker::ProcessSpecialized(const char* start, uint32_t size, uint32_t* out_size) { |
| 120 | + DCHECK_EQ(quoting, options_.quoting); |
| 121 | + DCHECK_EQ(escaping, options_.escaping); |
| 122 | + |
| 123 | + num_rows_ = 0; |
| 124 | + const char* data = start; |
| 125 | + const char* data_end = start + size; |
| 126 | + |
| 127 | + while (data < data_end && num_rows_ < max_num_rows_) { |
| 128 | + const char* line_end = ReadLine<quoting, escaping>(data, data_end); |
| 129 | + if (line_end == nullptr) { |
| 130 | + // Cannot read any further |
| 131 | + break; |
| 132 | + } |
| 133 | + data = line_end; |
| 134 | + ++num_rows_; |
| 135 | + } |
| 136 | + *out_size = static_cast<uint32_t>(data - start); |
| 137 | + return Status::OK(); |
| 138 | +} |
| 139 | + |
| 140 | +Status Chunker::Process(const char* start, uint32_t size, uint32_t* out_size) { |
| 141 | + if (options_.quoting) { |
| 142 | + if (options_.escaping) { |
| 143 | + return ProcessSpecialized<true, true>(start, size, out_size); |
| 144 | + } else { |
| 145 | + return ProcessSpecialized<true, false>(start, size, out_size); |
| 146 | + } |
| 147 | + } else { |
| 148 | + if (options_.escaping) { |
| 149 | + return ProcessSpecialized<false, true>(start, size, out_size); |
| 150 | + } else { |
| 151 | + return ProcessSpecialized<false, false>(start, size, out_size); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace csv |
| 157 | +} // namespace arrow |
0 commit comments