Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-37643: [C++] Enhance arrow::Datum::ToString #37646

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion c_glib/test/test-array-datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_false
end

def test_to_string
assert_equal("Array", @datum.to_s)
assert_equal("Array([\n" + " true,\n" + " false\n" + "])", @datum.to_s)
end

def test_value
Expand Down
2 changes: 1 addition & 1 deletion c_glib/test/test-chunked-array-datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_false
end

def test_to_string
assert_equal("ChunkedArray", @datum.to_s)
assert_equal("ChunkedArray([\n" + " [\n" + " true,\n" + " false\n" + " ]\n" + "])", @datum.to_s)
end

def test_value
Expand Down
2 changes: 1 addition & 1 deletion c_glib/test/test-record-batch-datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_false
end

def test_to_string
assert_equal("RecordBatch", @datum.to_s)
assert_equal("RecordBatch(visible: [\n" + " true,\n" + " false\n" + " ]\n" + ")", @datum.to_s)
end

def test_value
Expand Down
2 changes: 1 addition & 1 deletion c_glib/test/test-scalar-datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_false
end

def test_to_string
assert_equal("Scalar", @datum.to_s)
assert_equal("Scalar(true)", @datum.to_s)
end

def test_value
Expand Down
11 changes: 10 additions & 1 deletion c_glib/test/test-table-datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ def test_false
end

def test_to_string
assert_equal("Table", @datum.to_s)
assert_equal("Table(visible: bool\n" +
"----\n" +
"visible:\n" +
" [\n" +
" [\n" +
" true,\n" +
" false\n" +
" ]\n" +
" ]\n" +
")", @datum.to_s)
end

def test_value
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/arrow/datum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ std::string Datum::ToString() const {
case Datum::NONE:
return "nullptr";
case Datum::SCALAR:
return "Scalar";
return "Scalar(" + scalar()->ToString() + ")";
case Datum::ARRAY:
return "Array";
return "Array(" + make_array()->ToString() + ")";
Copy link
Member Author

Choose a reason for hiding this comment

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

make_array will generate a temp ::arrow::Array object, don't know if we have a better way for this

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately I don't think so. It won't be an O(N) copy or anything though, so not the end of the world.

case Datum::CHUNKED_ARRAY:
return "ChunkedArray";
return "ChunkedArray(" + chunked_array()->ToString() + ")";
case Datum::RECORD_BATCH:
return "RecordBatch";
return "RecordBatch(" + record_batch()->ToString() + ")";
case Datum::TABLE:
return "Table";
return "Table(" + table()->ToString() + ")";
default:
DCHECK(false);
return "";
Expand Down
1 change: 0 additions & 1 deletion cpp/src/arrow/datum.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ struct ARROW_EXPORT Datum {
bool operator==(const Datum& other) const { return Equals(other); }
bool operator!=(const Datum& other) const { return !Equals(other); }

/// \brief Return a string representation of the kind of datum stored.
std::string ToString() const;
};

Expand Down
4 changes: 2 additions & 2 deletions cpp/src/arrow/datum_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ TEST(Datum, ToString) {
Datum v1(arr);
Datum v2(std::make_shared<Int8Scalar>(1));

ASSERT_EQ("Array", v1.ToString());
ASSERT_EQ("Scalar", v2.ToString());
ASSERT_EQ("Array([\n 1,\n 2,\n 3,\n 4\n])", v1.ToString());
ASSERT_EQ("Scalar(1)", v2.ToString());
}

TEST(Datum, TotalBufferSize) {
Expand Down