|
| 1 | +#include "pandas/pandas_analyzer.h" |
| 2 | + |
| 3 | +#include "function/built_in_function.h" |
| 4 | +#include "py_conversion.h" |
| 5 | + |
| 6 | +namespace kuzu { |
| 7 | + |
| 8 | +static bool upgradeType(common::LogicalType& left, const common::LogicalType& right) { |
| 9 | + if (right.getLogicalTypeID() == common::LogicalTypeID::ANY) { |
| 10 | + return true; |
| 11 | + } |
| 12 | + if (left.getLogicalTypeID() == common::LogicalTypeID::ANY || |
| 13 | + ((left.getLogicalTypeID() == common::LogicalTypeID::VAR_LIST) && |
| 14 | + (common::VarListType::getChildType(&left)->getLogicalTypeID() == |
| 15 | + common::LogicalTypeID::ANY))) { |
| 16 | + left = right; |
| 17 | + return true; |
| 18 | + } |
| 19 | + if (((right.getLogicalTypeID() == common::LogicalTypeID::VAR_LIST) && |
| 20 | + (common::VarListType::getChildType(&right)->getLogicalTypeID() == |
| 21 | + common::LogicalTypeID::ANY))) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + auto leftToRightCost = |
| 25 | + function::BuiltInFunctions::getCastCost(left.getLogicalTypeID(), right.getLogicalTypeID()); |
| 26 | + if (leftToRightCost != common::UNDEFINED_CAST_COST) { |
| 27 | + left = right; |
| 28 | + } else { |
| 29 | + return false; |
| 30 | + } |
| 31 | + return true; |
| 32 | +} |
| 33 | + |
| 34 | +common::LogicalType PandasAnalyzer::getListType(py::object& ele, bool& canConvert) { |
| 35 | + uint64_t i = 0; |
| 36 | + common::LogicalType listType; |
| 37 | + for (auto pyVal : ele) { |
| 38 | + auto object = py::reinterpret_borrow<py::object>(pyVal); |
| 39 | + auto itemType = getItemType(object, canConvert); |
| 40 | + if (i != 0) { |
| 41 | + listType = itemType; |
| 42 | + } else { |
| 43 | + if (!upgradeType(listType, itemType)) { |
| 44 | + canConvert = false; |
| 45 | + } |
| 46 | + } |
| 47 | + if (!canConvert) { |
| 48 | + break; |
| 49 | + } |
| 50 | + i++; |
| 51 | + } |
| 52 | + return listType; |
| 53 | +} |
| 54 | + |
| 55 | +common::LogicalType PandasAnalyzer::getItemType(py::object ele, bool& canConvert) { |
| 56 | + auto objectType = getPythonObjectType(ele); |
| 57 | + switch (objectType) { |
| 58 | + case PythonObjectType::None: |
| 59 | + return *common::LogicalType::ANY(); |
| 60 | + case PythonObjectType::Bool: |
| 61 | + return *common::LogicalType::BOOL(); |
| 62 | + case PythonObjectType::Integer: |
| 63 | + return *common::LogicalType::INT64(); |
| 64 | + case PythonObjectType::Float: |
| 65 | + return *common::LogicalType::DOUBLE(); |
| 66 | + case PythonObjectType::Datetime: |
| 67 | + return *common::LogicalType::TIMESTAMP(); |
| 68 | + case PythonObjectType::Date: |
| 69 | + return *common::LogicalType::DATE(); |
| 70 | + case PythonObjectType::String: |
| 71 | + return *common::LogicalType::STRING(); |
| 72 | + case PythonObjectType::List: |
| 73 | + return *common::LogicalType::VAR_LIST(getListType(ele, canConvert)); |
| 74 | + default: |
| 75 | + KU_UNREACHABLE; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +static py::object findFirstNonNull(const py::handle& row, uint64_t numRows) { |
| 80 | + for (auto i = 0u; i < numRows; i++) { |
| 81 | + auto obj = row(i); |
| 82 | + if (!obj.is_none()) { |
| 83 | + return obj; |
| 84 | + } |
| 85 | + } |
| 86 | + return py::none(); |
| 87 | +} |
| 88 | + |
| 89 | +common::LogicalType PandasAnalyzer::innerAnalyze(py::object column, bool& canConvert) { |
| 90 | + auto numRows = py::len(column); |
| 91 | + auto pandasModule = py::module::import("pandas"); |
| 92 | + auto pandasSeries = pandasModule.attr("core").attr("series").attr("Series"); |
| 93 | + |
| 94 | + if (py::isinstance(column, pandasSeries)) { |
| 95 | + column = column.attr("__array__")(); |
| 96 | + } |
| 97 | + auto row = column.attr("__getitem__"); |
| 98 | + common::LogicalType itemType = getItemType(findFirstNonNull(row, numRows), canConvert); |
| 99 | + for (auto i = 1u; i < numRows; i += 1) { |
| 100 | + auto obj = row(i); |
| 101 | + auto curItemType = getItemType(obj, canConvert); |
| 102 | + if (!canConvert || !upgradeType(itemType, curItemType)) { |
| 103 | + canConvert = false; |
| 104 | + return curItemType; |
| 105 | + } |
| 106 | + } |
| 107 | + if (itemType.getPhysicalType() == common::PhysicalTypeID::ANY) { |
| 108 | + canConvert = false; |
| 109 | + } |
| 110 | + return itemType; |
| 111 | +} |
| 112 | + |
| 113 | +bool PandasAnalyzer::analyze(py::object column) { |
| 114 | + bool canConvert = true; |
| 115 | + auto type = innerAnalyze(std::move(column), canConvert); |
| 116 | + if (canConvert) { |
| 117 | + analyzedType = type; |
| 118 | + } |
| 119 | + return canConvert; |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace kuzu |
0 commit comments