Skip to content

Commit

Permalink
GH-37643: [C++] Enhance arrow::Datum::ToString (#37646)
Browse files Browse the repository at this point in the history
### Rationale for this change

Add print child fields for `arrow::Datum::ToString`. Because previously it just print a type.

### What changes are included in this PR?

Add detail in `arrow::Datum::ToString`

### Are these changes tested?

no

### Are there any user-facing changes?

Yes

* Closes: #37643

Authored-by: mwish <[email protected]>
Signed-off-by: Benjamin Kietzman <[email protected]>
  • Loading branch information
mapleFU authored Sep 15, 2023
1 parent ac7e9a4 commit d9ee3a7
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
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() + ")";
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

0 comments on commit d9ee3a7

Please sign in to comment.