Skip to content

ARROW-969: [C++] Add add/remove field functions for RecordBatch#1574

Closed
xuepanchen wants to merge 4 commits intoapache:masterfrom
xuepanchen:ARROW-969
Closed

ARROW-969: [C++] Add add/remove field functions for RecordBatch#1574
xuepanchen wants to merge 4 commits intoapache:masterfrom
xuepanchen:ARROW-969

Conversation

@xuepanchen
Copy link
Copy Markdown
Contributor

Add AddColumn and RemoveColumn methods for RecordBatch, as well as test cases

Copy link
Copy Markdown
Member

@xhochy xhochy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM

@wesm
Copy link
Copy Markdown
Member

wesm commented Feb 7, 2018

Will review when I can, thanks!

Copy link
Copy Markdown
Member

@wesm wesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spotted some possible issues / bugs

Status AddColumn(int i, const std::shared_ptr<Field>& field,
const std::shared_ptr<ArrayData>& column,
std::shared_ptr<RecordBatch>* out) const override {
if (i < 0 || i > num_columns() + 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be i > num_columns(). This is also a bug in SimpleTable::AddColumn. Can you add a test where i == num_columns()?

std::stringstream ss;
ss << "Column " << i << " was null";
return Status::Invalid(ss.str());
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should both be DCHECK, since null would indicate a problem with application logic, so should be a "can't fail"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at SimpleTable::AddColumn; there col is being null-checked -- I think that should also be a DCHECK

std::shared_ptr<ArrayData> column_data(int i) const override { return columns_[i]; }

Status AddColumn(int i, const std::shared_ptr<Field>& field,
const std::shared_ptr<ArrayData>& column,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass Array here instead, since that's more likely to be what the user has

}

std::shared_ptr<Schema> new_schema;
RETURN_NOT_OK(schema_->AddField(i, field, &new_schema));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could leave the boundschecking above to Schema::AddField -- could you also check whether that function has the issues described above?

std::shared_ptr<RecordBatch>* out) const = 0;

/// \brief Remove column from the record batch, producing a new RecordBatch
virtual Status RemoveColumn(int i, std::shared_ptr<RecordBatch>* out) const = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document the parameters for these functions?

std::shared_ptr<RecordBatch> result;

// Negative tests with invalid index
Status status = batch.AddColumn(5, field1, array1->data(), &result);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for batch.AddColumn(2, ...) to address the edge case in the implementation. We probably need a corresponding test for Table (and maybe also Schema).

ASSERT_TRUE(result->Equals(*batch3));

ASSERT_OK(batch.RemoveColumn(2, &result));
ASSERT_TRUE(result->Equals(*batch4));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for removing an out of bounds index

Copy link
Copy Markdown
Member

@wesm wesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I am going to quickly add a convenience method AddColumn that accepts a string name instead of a Field

… of a Field

Change-Id: I628c4d3a66f4f7b5f3946bf0047b551efceb077f
@wesm
Copy link
Copy Markdown
Member

wesm commented Feb 13, 2018

Merging, thanks @xuepanchen! @trxcllnt @TheNeuralBit odd unrelated JS failure here, in case you want to have a look

@wesm wesm closed this in 4a8a102 Feb 13, 2018
@trxcllnt
Copy link
Copy Markdown
Contributor

@wesm looks like noise, but ping me if you see it again.

@xuepanchen xuepanchen deleted the ARROW-969 branch February 13, 2018 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants