21
21
#include " vec/data_types/data_type_array.h"
22
22
23
23
#include " gen_cpp/data.pb.h"
24
+ #include " util/stack_util.h"
24
25
#include " vec/columns/column_array.h"
25
26
#include " vec/columns/column_nullable.h"
26
27
#include " vec/data_types/data_type_nullable.h"
@@ -175,15 +176,21 @@ std::string DataTypeArray::to_string(const IColumn& column, size_t row_num) cons
175
176
}
176
177
177
178
Status DataTypeArray::from_string (ReadBuffer& rb, IColumn* column) const {
179
+ DCHECK (!rb.eof ());
178
180
// only support one level now
179
181
auto * array_column = assert_cast<ColumnArray*>(column);
180
182
auto & offsets = array_column->get_offsets ();
181
183
182
184
IColumn& nested_column = array_column->get_data ();
185
+ DCHECK (nested_column.is_nullable ());
183
186
if (*rb.position () != ' [' ) {
184
187
return Status::InvalidArgument (" Array does not start with '[' character, found '{}'" ,
185
188
*rb.position ());
186
189
}
190
+ if (*(rb.end () - 1 ) != ' ]' ) {
191
+ return Status::InvalidArgument (" Array does not end with ']' character, found '{}'" ,
192
+ *(rb.end () - 1 ));
193
+ }
187
194
++rb.position ();
188
195
bool first = true ;
189
196
size_t size = 0 ;
@@ -210,13 +217,9 @@ Status DataTypeArray::from_string(ReadBuffer& rb, IColumn* column) const {
210
217
211
218
// dispose the case of [123,,,]
212
219
if (nested_str_len == 0 ) {
213
- if (nested_column.is_nullable ()) {
214
- auto & nested_null_col = reinterpret_cast <ColumnNullable&>(nested_column);
215
- nested_null_col.get_nested_column ().insert_default ();
216
- nested_null_col.get_null_map_data ().push_back (0 );
217
- } else {
218
- nested_column.insert_default ();
219
- }
220
+ auto & nested_null_col = reinterpret_cast <ColumnNullable&>(nested_column);
221
+ nested_null_col.get_nested_column ().insert_default ();
222
+ nested_null_col.get_null_map_data ().push_back (0 );
220
223
++size;
221
224
continue ;
222
225
}
@@ -236,19 +239,31 @@ Status DataTypeArray::from_string(ReadBuffer& rb, IColumn* column) const {
236
239
}
237
240
238
241
// dispose the case of ["123"] or ['123']
242
+ bool has_quota = false ;
243
+ size_t tmp_len = nested_str_len;
239
244
ReadBuffer read_buffer (rb.position (), nested_str_len);
240
245
auto begin_char = *(rb.position () + begin_pos);
241
246
auto end_char = *(rb.position () + end_pos);
242
247
if (begin_char == end_char && (begin_char == ' "' || begin_char == ' \' ' )) {
243
248
int64_t length = end_pos - begin_pos - 1 ;
244
249
read_buffer = ReadBuffer (rb.position () + begin_pos + 1 , (length > 0 ? length : 0 ));
250
+ tmp_len = (length > 0 ? length : 0 );
251
+ has_quota = true ;
245
252
}
246
253
247
- auto st = nested->from_string (read_buffer, &nested_column);
248
- if (!st.ok ()) {
249
- // we should do revert if error
250
- array_column->pop_back (size);
251
- return st;
254
+ // handle null, need to distinguish null and "null"
255
+ if (!has_quota && tmp_len == 4 && strncmp (read_buffer.position (), " null" , 4 ) == 0 ) {
256
+ // insert null
257
+ auto & nested_null_col = reinterpret_cast <ColumnNullable&>(nested_column);
258
+ nested_null_col.get_nested_column ().insert_default ();
259
+ nested_null_col.get_null_map_data ().push_back (1 );
260
+ } else {
261
+ auto st = nested->from_string (read_buffer, &nested_column);
262
+ if (!st.ok ()) {
263
+ // we should do revert if error
264
+ array_column->pop_back (size);
265
+ return st;
266
+ }
252
267
}
253
268
rb.position () += nested_str_len;
254
269
DCHECK_LE (rb.position (), rb.end ());
0 commit comments