pitrou commented on code in PR #13901: URL: https://github.com/apache/arrow/pull/13901#discussion_r1740924412
########## cpp/src/arrow/extension/json_test.cc: ########## @@ -0,0 +1,91 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/extension/json.h" + +#include "arrow/array/validate.h" +#include "arrow/ipc/test_common.h" +#include "arrow/record_batch.h" +#include "arrow/testing/gtest_util.h" +#include "parquet/exception.h" + +namespace arrow { + +using arrow::ipc::test::RoundtripBatch; +using extension::json; + +class TestJsonExtensionType : public ::testing::Test {}; + +std::shared_ptr<Array> ExampleJson(const std::shared_ptr<DataType>& storage_type) { + std::shared_ptr<Array> arr = ArrayFromJSON(storage_type, R"([ + "null", + "1234", + "3.14159", + "true", + "false", + "\"a json string\"", + "[\"a\", \"json\", \"array\"]", + "{\"obj\": \"a simple json object\"}" + ])"); + return ExtensionType::WrapArray(arrow::extension::json(storage_type), arr); +} + +static std::shared_ptr<Array> ExampleJsonInvalidUTF8( + const std::shared_ptr<DataType>& storage_type) { + return ArrayFromJSON(storage_type, + "[" + R"( Review Comment: Can you put the `R(` around the entire string? It would be slightly easier to read (as above). ########## cpp/src/arrow/extension/json_test.cc: ########## @@ -0,0 +1,91 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/extension/json.h" + +#include "arrow/array/validate.h" +#include "arrow/ipc/test_common.h" +#include "arrow/record_batch.h" +#include "arrow/testing/gtest_util.h" +#include "parquet/exception.h" + +namespace arrow { + +using arrow::ipc::test::RoundtripBatch; +using extension::json; + +class TestJsonExtensionType : public ::testing::Test {}; + +std::shared_ptr<Array> ExampleJson(const std::shared_ptr<DataType>& storage_type) { + std::shared_ptr<Array> arr = ArrayFromJSON(storage_type, R"([ + "null", + "1234", + "3.14159", + "true", + "false", + "\"a json string\"", + "[\"a\", \"json\", \"array\"]", + "{\"obj\": \"a simple json object\"}" + ])"); + return ExtensionType::WrapArray(arrow::extension::json(storage_type), arr); +} + +static std::shared_ptr<Array> ExampleJsonInvalidUTF8( + const std::shared_ptr<DataType>& storage_type) { + return ArrayFromJSON(storage_type, + "[" + R"( + "Hi", + "olá mundo", + "你好世界", + "", + )" + "\"\xa0\xa1\"" + "]"); +} + +TEST_F(TestJsonExtensionType, JsonRoundtrip) { + for (const auto& storage_type : {utf8(), large_utf8(), utf8_view()}) { + std::shared_ptr<Array> ext_arr = ExampleJson(storage_type); + auto batch = + RecordBatch::Make(schema({field("f0", json(storage_type))}), 8, {ext_arr}); + + std::shared_ptr<RecordBatch> read_batch; + ASSERT_OK(RoundtripBatch(batch, &read_batch)); + ASSERT_OK(read_batch->ValidateFull()); + CompareBatch(*batch, *read_batch, /*compare_metadata*/ true); + + auto read_ext_arr = read_batch->column(0); + ASSERT_OK(internal::ValidateUTF8(*read_ext_arr)); + ASSERT_OK(read_ext_arr->ValidateFull()); + } +} + +TEST_F(TestJsonExtensionType, InvalidUTF8) { + for (const auto& storage_type : {utf8(), large_utf8(), utf8_view()}) { + std::shared_ptr<Array> ext_arr = ExampleJsonInvalidUTF8(storage_type); + auto batch = + RecordBatch::Make(schema({field("f0", json(storage_type))}), 8, {ext_arr}); + + std::shared_ptr<RecordBatch> read_batch; + ASSERT_RAISES_WITH_MESSAGE(IOError, + "IOError: Array length did not match record batch length", + RoundtripBatch(batch, &read_batch)); Review Comment: I don't understand this test. This should ideally check that `ext_arr->ValidateFull()` returns an error, and ditto for `ValidateUTF8`. ########## cpp/src/parquet/arrow/schema.cc: ########## @@ -984,21 +992,51 @@ Result<bool> ApplyOriginalMetadata(const Field& origin_field, SchemaField* infer bool modified = false; auto& origin_type = origin_field.type(); + auto inferred_type = inferred->field->type(); if (origin_type->id() == ::arrow::Type::EXTENSION) { const auto& ex_type = checked_cast<const ::arrow::ExtensionType&>(*origin_type); - auto origin_storage_field = origin_field.WithType(ex_type.storage_type()); + if (inferred_type->id() != ::arrow::Type::EXTENSION && + ex_type.extension_name() == ::arrow::extension::JsonExtensionType::type_name()) { + // Schema mismatch. + // + // Arrow extensions are DISABLED in Parquet. + // origin_type is ::arrow::extension::json() + // inferred_type is ::arrow::binary() + // + // Origin type is restored as Arrow should be considered the source of truth. + DCHECK_EQ(inferred_type->id(), ::arrow::Type::BINARY); + inferred->field = inferred->field->WithType(origin_type); + RETURN_NOT_OK(ApplyOriginalStorageMetadata(origin_field, inferred)); + } else { + auto origin_storage_field = origin_field.WithType(ex_type.storage_type()); - // Apply metadata recursively to storage type - RETURN_NOT_OK(ApplyOriginalStorageMetadata(*origin_storage_field, inferred)); + // Apply metadata recursively to storage type + RETURN_NOT_OK(ApplyOriginalStorageMetadata(*origin_storage_field, inferred)); - // Restore extension type, if the storage type is the same as inferred - // from the Parquet type - if (ex_type.storage_type()->Equals(*inferred->field->type())) { - inferred->field = inferred->field->WithType(origin_type); + // Restore extension type, if the storage type is the same as inferred + // from the Parquet type + if (ex_type.storage_type()->Equals(*inferred->field->type())) { + inferred->field = inferred->field->WithType(origin_type); + } } modified = true; } else { + if (inferred_type->id() == ::arrow::Type::EXTENSION) { + const auto& ex_type = checked_cast<const ::arrow::ExtensionType&>(*inferred_type); + if (ex_type.extension_name() == + ::arrow::extension::JsonExtensionType::type_name()) { + // Schema mismatch. + // + // Arrow extensions are ENABLED in Parquet. + // origin_type is ::arrow::binary() + // inferred_type is ::arrow::extension::json() + // + // Origin type is restored as Arrow should be considered the source of truth. Review Comment: This would only happen if a Parquet writer wrote a Arrow utf8 array (_not_ JSON) as a JSON Parquet logical type. Why would it do that? ########## cpp/src/arrow/util/utf8_util_test.cc: ########## @@ -562,5 +564,30 @@ TEST(UTF8Length, Basics) { ASSERT_EQ(length("\xf0\x9f\x99\x8c"), 1); } +class UTF8ExtensionArrayTest : public ::testing::Test { + public: + static std::shared_ptr<Array> ExampleJson( + const std::shared_ptr<DataType>& storage_type) { + std::shared_ptr<Array> arr = ArrayFromJSON(storage_type, R"([ + "null", + "1234", + "3.14159", + "true", + "false", + "\"a json string\"", + "[\"a\", \"json\", \"array\"]", + "{\"obj\": \"a simple json object\"}" + ])"); + return ExtensionType::WrapArray(arrow::extension::json(storage_type), arr); + } +}; + +TEST_F(UTF8ExtensionArrayTest, JSONExtensionType) { Review Comment: IMHO, this should simply go in `json_test.cc`. ########## cpp/src/arrow/extension/json.h: ########## @@ -0,0 +1,60 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <stdexcept> +#include <string> + +#include "arrow/extension_type.h" +#include "arrow/result.h" +#include "arrow/type_fwd.h" +#include "arrow/util/visibility.h" + +namespace arrow::extension { + +/// \brief Concrete type class for variable-size JSON data, utf8-encoded. +class ARROW_EXPORT JsonExtensionType : public ExtensionType { + public: + explicit JsonExtensionType(const std::shared_ptr<DataType>& storage_type) + : ExtensionType(storage_type), storage_type_(storage_type) {} + + std::string extension_name() const override { return "arrow.json"; } + + std::string ToString(bool show_metadata = false) const override { Review Comment: I don't think you need to override this at all, `ExtensionType::ToString` already does the required thing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
