| 1584 | } |
| 1585 | |
| 1586 | Result<Expression> Deserialize(std::shared_ptr<Buffer> buffer) { |
| 1587 | #ifdef ARROW_IPC |
| 1588 | io::BufferReader stream(std::move(buffer)); |
| 1589 | ARROW_ASSIGN_OR_RAISE(auto reader, ipc::RecordBatchFileReader::Open(&stream)); |
| 1590 | ARROW_ASSIGN_OR_RAISE(auto batch, reader->ReadRecordBatch(0)); |
| 1591 | if (batch->schema()->metadata() == nullptr) { |
| 1592 | return Status::Invalid("serialized Expression's batch repr had null metadata"); |
| 1593 | } |
| 1594 | if (batch->num_rows() != 1) { |
| 1595 | return Status::Invalid( |
| 1596 | "serialized Expression's batch repr was not a single row - had ", |
| 1597 | batch->num_rows()); |
| 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; |