|
| 1 | +#include "ListScan.h" |
| 2 | +#include "PythonConversion.h" |
| 3 | + |
| 4 | +#include <Columns/ColumnObject.h> |
| 5 | +#include <DataTypes/IDataType.h> |
| 6 | +#include <DataTypes/Serializations/SerializationJSON.h> |
| 7 | +#include <IO/WriteHelpers.h> |
| 8 | + |
| 9 | +namespace DB |
| 10 | +{ |
| 11 | + |
| 12 | +namespace ErrorCodes |
| 13 | +{ |
| 14 | + extern const int BAD_ARGUMENTS; |
| 15 | + extern const int PY_EXCEPTION_OCCURED; |
| 16 | +} |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +using namespace DB; |
| 21 | + |
| 22 | +namespace CHDB { |
| 23 | + |
| 24 | +ColumnPtr ListScan::scanObject( |
| 25 | + const ColumnWrapper & col_wrap, |
| 26 | + const size_t cursor, |
| 27 | + const size_t count, |
| 28 | + const FormatSettings & format_settings) |
| 29 | +{ |
| 30 | + innerCheck(col_wrap); |
| 31 | + |
| 32 | + auto & data_type = col_wrap.dest_type; |
| 33 | + auto column = data_type->createColumn(); |
| 34 | + auto serialization = data_type->getDefaultSerialization(); |
| 35 | + |
| 36 | + innerScanObject(cursor, count, format_settings, serialization, col_wrap.data, column); |
| 37 | + |
| 38 | + return column; |
| 39 | +} |
| 40 | + |
| 41 | +void ListScan::scanObject( |
| 42 | + const size_t cursor, |
| 43 | + const size_t count, |
| 44 | + const FormatSettings & format_settings, |
| 45 | + const py::handle & obj, |
| 46 | + MutableColumnPtr & column) |
| 47 | +{ |
| 48 | + auto data_type = std::make_shared<DataTypeObject>(DataTypeObject::SchemaFormat::JSON); |
| 49 | + SerializationPtr serialization = data_type->getDefaultSerialization(); |
| 50 | + |
| 51 | + innerScanObject(cursor, count, format_settings, serialization, obj, column); |
| 52 | +} |
| 53 | + |
| 54 | +void ListScan::innerScanObject( |
| 55 | + const size_t cursor, |
| 56 | + const size_t count, |
| 57 | + const FormatSettings & format_settings, |
| 58 | + SerializationPtr & serialization, |
| 59 | + const py::handle & obj, |
| 60 | + MutableColumnPtr & column) |
| 61 | +{ |
| 62 | + py::gil_scoped_acquire acquire; |
| 63 | + |
| 64 | + auto list = obj.cast<py::list>(); |
| 65 | + |
| 66 | + for (size_t i = cursor; i < cursor + count; ++i) |
| 67 | + { |
| 68 | + auto item = list.attr("__getitem__")(i); |
| 69 | + if (!tryInsertJsonResult(item, format_settings, column, serialization)) |
| 70 | + column->insertDefault(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void ListScan::innerCheck(const ColumnWrapper & col_wrap) |
| 75 | +{ |
| 76 | + if (col_wrap.data.is_none()) |
| 77 | + throw Exception(ErrorCodes::PY_EXCEPTION_OCCURED, "Column data is None"); |
| 78 | + |
| 79 | + if (!col_wrap.buf) |
| 80 | + throw Exception(ErrorCodes::PY_EXCEPTION_OCCURED, "Column buffer is null"); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace CHDB |
0 commit comments