Skip to content
Open
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
1 change: 1 addition & 0 deletions cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ set(ARROW_SRCS
extension_type.cc
extension/bool8.cc
extension/json.cc
extension/parquet_file.cc
extension/parquet_variant.cc
extension/uuid.cc
pretty_print.cc
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/extension/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ add_arrow_test(test
PREFIX
"arrow-canonical-extensions")

add_arrow_test(test
SOURCES
parquet_file_test.cc
PREFIX
"arrow-parquet-file-extension")

arrow_install_all_headers("arrow/extension")
8 changes: 8 additions & 0 deletions cpp/src/arrow/extension/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ exc = executable(
)
test('arrow-canonical-extensions-test', exc)

file_extension_test = executable(
'arrow-parquet-file-extension-test',
sources: ['parquet_file_test.cc'],
dependencies: [arrow_test_dep],
)
test('arrow-parquet-file-extension-test', file_extension_test)

install_headers(
[
'bool8.h',
'fixed_shape_tensor.h',
'json.h',
'opaque.h',
'parquet_file.h',
'parquet_variant.h',
'uuid.h',
'variable_shape_tensor.h',
Expand Down
128 changes: 128 additions & 0 deletions cpp/src/arrow/extension/parquet_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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/parquet_file.h"

#include "arrow/extension_type.h"
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/logging_internal.h"

namespace arrow::extension {

namespace {

bool IsSupportedField(const std::shared_ptr<Field>& field) {
if (!field->nullable()) {
return false;
}
if (field->name() == "uri" || field->name() == "content_type" ||
field->name() == "checksum") {
return ::arrow::is_string_or_string_view(field->type()->id());
}
if (field->name() == "offset" || field->name() == "size") {
return field->type()->id() == Type::INT64;
}
if (field->name() == "inline") {
return ::arrow::is_binary_or_binary_view(field->type()->id());
}
return false;
}

} // namespace

FileExtensionType::FileExtensionType(const std::shared_ptr<DataType>& storage_type)
: ExtensionType(storage_type) {
for (const auto& field : storage_type->fields()) {
if (field->name() == "uri") {
uri_ = field;
} else if (field->name() == "offset") {
offset_ = field;
} else if (field->name() == "size") {
size_ = field;
} else if (field->name() == "content_type") {
content_type_ = field;
} else if (field->name() == "checksum") {
checksum_ = field;
} else if (field->name() == "inline") {
inline_bytes_ = field;
}
}
}

bool FileExtensionType::ExtensionEquals(const ExtensionType& other) const {
return other.extension_name() == extension_name() &&
other.storage_type()->Equals(*storage_type());
}

Result<std::shared_ptr<DataType>> FileExtensionType::Deserialize(
std::shared_ptr<DataType> storage_type, const std::string& serialized) const {
if (!serialized.empty()) {
return Status::Invalid("Unexpected serialized metadata: '", serialized, "'");
}
return FileExtensionType::Make(std::move(storage_type));
}

std::string FileExtensionType::Serialize() const { return ""; }

std::shared_ptr<Array> FileExtensionType::MakeArray(
std::shared_ptr<ArrayData> data) const {
DCHECK_EQ(data->type->id(), Type::EXTENSION);
DCHECK_EQ(kFileExtensionName,
internal::checked_cast<const ExtensionType&>(*data->type).extension_name());
return std::make_shared<FileArray>(std::move(data));
}

bool FileExtensionType::IsSupportedStorageType(
const std::shared_ptr<DataType>& storage_type) {
if (!storage_type || storage_type->id() != Type::STRUCT ||
storage_type->fields().empty()) {
return false;
}

for (const auto& field : storage_type->fields()) {
if (!IsSupportedField(field)) {
return false;
}
}

for (int i = 0; i < storage_type->num_fields(); ++i) {
for (int j = i + 1; j < storage_type->num_fields(); ++j) {
if (storage_type->field(i)->name() == storage_type->field(j)->name()) {
return false;
}
}
}
return true;
}

Result<std::shared_ptr<DataType>> FileExtensionType::Make(
std::shared_ptr<DataType> storage_type) {
if (!IsSupportedStorageType(storage_type)) {
return Status::Invalid("Invalid storage type for FileExtensionType: ",
storage_type ? storage_type->ToString() : "null");
}
return std::make_shared<FileExtensionType>(std::move(storage_type));
}

std::shared_ptr<DataType> file(std::shared_ptr<DataType> storage_type) {
return FileExtensionType::Make(std::move(storage_type)).ValueOrDie();
}

} // namespace arrow::extension
76 changes: 76 additions & 0 deletions cpp/src/arrow/extension/parquet_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 <memory>
#include <string>
#include <string_view>

#include "arrow/extension_type.h"
#include "arrow/util/visibility.h"

namespace arrow::extension {

/// \brief The extension name for the File extension type.
inline constexpr std::string_view kFileExtensionName = "parquet.file.experimental.v1";

class ARROW_EXPORT FileArray : public ExtensionArray {
public:
using ExtensionArray::ExtensionArray;
};

class ARROW_EXPORT FileExtensionType : public ExtensionType {
public:
explicit FileExtensionType(const std::shared_ptr<DataType>& storage_type);

std::string extension_name() const override { return std::string(kFileExtensionName); }

bool ExtensionEquals(const ExtensionType& other) const override;

Result<std::shared_ptr<DataType>> Deserialize(
std::shared_ptr<DataType> storage_type,
const std::string& serialized_data) const override;

std::string Serialize() const override;

std::shared_ptr<Array> MakeArray(std::shared_ptr<ArrayData> data) const override;

static Result<std::shared_ptr<DataType>> Make(std::shared_ptr<DataType> storage_type);

static bool IsSupportedStorageType(const std::shared_ptr<DataType>& storage_type);

std::shared_ptr<Field> uri() const { return uri_; }
std::shared_ptr<Field> offset() const { return offset_; }
std::shared_ptr<Field> size() const { return size_; }
std::shared_ptr<Field> content_type() const { return content_type_; }
std::shared_ptr<Field> checksum() const { return checksum_; }
std::shared_ptr<Field> inline_bytes() const { return inline_bytes_; }

private:
std::shared_ptr<Field> uri_;
std::shared_ptr<Field> offset_;
std::shared_ptr<Field> size_;
std::shared_ptr<Field> content_type_;
std::shared_ptr<Field> checksum_;
std::shared_ptr<Field> inline_bytes_;
};

/// \brief Return a FileExtensionType instance.
ARROW_EXPORT std::shared_ptr<DataType> file(std::shared_ptr<DataType> storage_type);

} // namespace arrow::extension
47 changes: 47 additions & 0 deletions cpp/src/arrow/extension/parquet_file_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 <vector>

#include <gtest/gtest.h>

#include "arrow/extension/parquet_file.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/type.h"

namespace arrow::extension {

TEST(FileType, InvalidStorage) {
// FILE storage fields must use one of the six recognized names.
ASSERT_NOT_OK(FileExtensionType::Make(struct_({field("unknown", binary())})));

// Every FILE storage field must be nullable.
ASSERT_NOT_OK(
FileExtensionType::Make(struct_({field("uri", utf8(), /*nullable=*/false)})));

// The offset and size fields must use INT64 storage.
ASSERT_NOT_OK(FileExtensionType::Make(struct_({field("offset", int32())})));

// The inline field must use a binary storage family.
ASSERT_NOT_OK(FileExtensionType::Make(struct_({field("inline", utf8())})));

// FILE storage field names must be unique.
ASSERT_NOT_OK(
FileExtensionType::Make(struct_({field("uri", utf8()), field("uri", utf8())})));
}

} // namespace arrow::extension
1 change: 1 addition & 0 deletions cpp/src/arrow/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ arrow_components = {
'extension_type.cc',
'extension/bool8.cc',
'extension/json.cc',
'extension/parquet_file.cc',
'extension/parquet_variant.cc',
'extension/uuid.cc',
'pretty_print.cc',
Expand Down
Loading
Loading