| 1598 | } |
| 1599 | |
| 1600 | struct FromRecordBatch { |
| 1601 | const RecordBatch& batch_; |
| 1602 | int index_; |
| 1603 | |
| 1604 | const KeyValueMetadata& metadata() { return *batch_.schema()->metadata(); } |
| 1605 | |
| 1606 | bool ParseInteger(const std::string& s, int32_t* value) { |
| 1607 | return ::arrow::internal::ParseValue<Int32Type>(s.data(), s.length(), value); |
| 1608 | } |
| 1609 | |
| 1610 | Result<std::shared_ptr<Scalar>> GetScalar(const std::string& i) { |
| 1611 | int32_t column_index; |
| 1612 | if (!ParseInteger(i, &column_index)) { |
| 1613 | return Status::Invalid("Couldn't parse column_index"); |
| 1614 | } |
| 1615 | if (column_index >= batch_.num_columns()) { |
| 1616 | return Status::Invalid("column_index out of bounds"); |
| 1617 | } |
| 1618 | return batch_.column(column_index)->GetScalar(0); |
| 1619 | } |
| 1620 | |
| 1621 | Result<Expression> GetOne() { |
| 1622 | if (index_ >= metadata().size()) { |
| 1623 | return Status::Invalid("unterminated serialized Expression"); |
| 1624 | } |
| 1625 | |
| 1626 | const std::string& key = metadata().key(index_); |
| 1627 | const std::string& value = metadata().value(index_); |
| 1628 | ++index_; |
| 1629 | |
| 1630 | if (key == "literal") { |
| 1631 | ARROW_ASSIGN_OR_RAISE(auto scalar, GetScalar(value)); |
| 1632 | return literal(std::move(scalar)); |
| 1633 | } |
| 1634 | |
| 1635 | if (key == "nested_field_ref") { |
| 1636 | int32_t size; |
| 1637 | if (!ParseInteger(value, &size)) { |
| 1638 | return Status::Invalid("Couldn't parse nested field ref length"); |
| 1639 | } |
| 1640 | if (size <= 0) { |
| 1641 | return Status::Invalid("nested field ref length must be > 0"); |
| 1642 | } |
| 1643 | std::vector<FieldRef> nested; |
| 1644 | nested.reserve(size); |
| 1645 | while (size-- > 0) { |
| 1646 | ARROW_ASSIGN_OR_RAISE(auto ref, GetOne()); |
| 1647 | if (!ref.field_ref()) { |
| 1648 | return Status::Invalid("invalid nested field ref"); |
| 1649 | } |
| 1650 | nested.push_back(*ref.field_ref()); |
| 1651 | } |
| 1652 | return field_ref(FieldRef(std::move(nested))); |
| 1653 | } |
| 1654 | |
| 1655 | if (key == "field_ref") { |
| 1656 | return field_ref(value); |
| 1657 | } |