wgtmac commented on code in PR #1069: URL: https://github.com/apache/iceberg-cpp/pull/1069#discussion_r4696766498
########## src/iceberg/view_metadata.h: ########## @@ -0,0 +1,124 @@ +/* + * 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 + +/// \file iceberg/view_metadata.h +/// View metadata for Iceberg views. + +#include <cstdint> +#include <memory> +#include <string> +#include <unordered_map> +#include <vector> + +#include "iceberg/iceberg_export.h" +#include "iceberg/result.h" +#include "iceberg/type_fwd.h" +#include "iceberg/util/timepoint.h" +#include "iceberg/view_version.h" + +namespace iceberg { + +/// \brief Represents a view history entry, which records a change to the +/// current version of a view. +struct ICEBERG_EXPORT ViewHistoryEntry { + /// The timestamp in milliseconds of the change to the current version. + TimePointMs timestamp_ms; + /// ID of the view version that was made current. + int32_t version_id; + + /// \brief Compare two view history entries for equality. + friend bool operator==(const ViewHistoryEntry& lhs, const ViewHistoryEntry& rhs) { + return lhs.timestamp_ms == rhs.timestamp_ms && lhs.version_id == rhs.version_id; + } +}; + +/// \brief Represents the metadata for an Iceberg view. +/// +/// View metadata follows view format version 1. Table-format schema +/// restrictions are deliberately not applied to the view's schemas: view +/// schemas are projections of the referenced tables' columns and may use +/// types or nullability that table data would reject. +struct ICEBERG_EXPORT ViewMetadata { + static constexpr int8_t kDefaultViewFormatVersion = 1; + static constexpr int8_t kSupportedViewFormatVersion = 1; + + /// An integer version number for the view format; must be 1. + int8_t format_version; Review Comment: Please default-initialize the scalar members. A default-constructed `ViewMetadata` reads an indeterminate format version on its first call to `Validate()`. ########## src/iceberg/view_version.h: ########## @@ -0,0 +1,121 @@ +/* + * 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 + +/// \file iceberg/view_version.h +/// A version of an Iceberg view. + +#include <cstdint> +#include <optional> +#include <string> +#include <string_view> +#include <unordered_map> +#include <variant> +#include <vector> + +#include "iceberg/iceberg_export.h" +#include "iceberg/table_identifier.h" +#include "iceberg/util/timepoint.h" + +namespace iceberg { + +/// \brief The SQL representation of a view version's definition. +/// +/// Each SQL representation pairs a query with its engine dialect, such as +/// "spark". Dialect strings are preserved verbatim and compared with ASCII +/// case folding. +struct ICEBERG_EXPORT SqlViewRepresentation { + /// The SQL SELECT statement that defines the view. + std::string sql; + /// The dialect of the SELECT statement, such as "spark" or "trino". + std::string dialect; + + /// \brief Compare two SQL representations for equality. + friend bool operator==(const SqlViewRepresentation& lhs, + const SqlViewRepresentation& rhs) = default; +}; + +/// \brief An opaque representation type this library does not interpret. +/// +/// Unknown representations are preserved so that metadata round trips and +/// property edits do not lose them. Definition changes that involve them are +/// rejected instead of being partially rewritten. +struct ICEBERG_EXPORT UnknownViewRepresentation { + /// The representation type identifier from the metadata, such as "cypher". + std::string type; + + /// \brief Compare two unknown representations for equality. + friend bool operator==(const UnknownViewRepresentation& lhs, + const UnknownViewRepresentation& rhs) = default; +}; + +/// \brief A representation of a view version's definition: SQL or opaque. +using ViewRepresentation = std::variant<SqlViewRepresentation, UnknownViewRepresentation>; + +/// \brief The type identifier of the SQL representation. +inline constexpr std::string_view kSqlRepresentationType = "sql"; + +/// \brief Get the SQL representation. +/// +/// \return the SQL representation, or nullptr when the representation is not +/// a SQL representation. The pointer remains valid until the representation +/// is modified or destroyed. +ICEBERG_EXPORT const SqlViewRepresentation* GetSql(const ViewRepresentation& repr); Review Comment: Passing a named SQL alternative implicitly creates a temporary variant. The pointer returned by `GetSql` dangles at the end of the statement. Please reject temporary variants in both borrowing helpers. ########## src/iceberg/view_metadata.cc: ########## @@ -0,0 +1,166 @@ +/* + * 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 "iceberg/view_metadata.h" + +#include <algorithm> +#include <unordered_set> + +#include "iceberg/schema.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg { + +Status ViewMetadata::Validate() const { + if (format_version != kSupportedViewFormatVersion) { + return Invalid("Unsupported view format version: {}", + static_cast<int>(format_version)); + } + if (view_uuid.empty()) { + return Invalid("View UUID must not be empty"); + } + if (location.empty()) { + return Invalid("View location must not be empty"); + } + if (schemas.empty()) { + return Invalid("View metadata must list at least one schema"); + } + + std::unordered_set<int32_t> schema_ids; + for (const auto& schema : schemas) { + if (schema == nullptr) { + return Invalid("View schemas must not be null"); + } + if (!schema_ids.insert(schema->schema_id()).second) { + return Invalid("Duplicate view schema ID: {}", schema->schema_id()); + } + } + + if (versions.empty()) { + return Invalid("View metadata must list at least one version"); + } + std::unordered_set<int32_t> version_ids; + for (const auto& version : versions) { + if (version == nullptr) { + return Invalid("View versions must not be null"); + } + if (!version_ids.insert(version->version_id).second) { + return Invalid("Duplicate view version ID: {}", version->version_id); + } + } + if (!version_ids.contains(current_version_id)) { + return Invalid("Current version ID {} is not a listed view version", + current_version_id); + } + for (const auto& entry : version_log) { + if (!version_ids.contains(entry.version_id)) { + return Invalid("View history references unlisted version ID {}", entry.version_id); + } + } + + for (const auto& version : versions) { + if (!schema_ids.contains(version->schema_id)) { + return Invalid("View version {} references unlisted schema ID {}", + version->version_id, version->schema_id); + } + if (version->default_namespace.levels.empty()) { + return Invalid("View version {} must have a non-empty default namespace", + version->version_id); + } + + std::vector<std::string_view> dialects; + for (const auto& repr : version->representations) { + if (const auto* sql = GetSql(repr)) { + if (sql->sql.empty()) { + return Invalid("View version {} has an SQL representation without SQL", + version->version_id); + } + if (sql->dialect.empty()) { + return Invalid("View version {} has an SQL representation without a dialect", + version->version_id); + } + for (const auto& dialect : dialects) { + if (StringUtils::EqualsIgnoreCaseAscii(dialect, sql->dialect)) { Review Comment: Java treats `Éngine` and `éngine` as the same dialect. Please normalize each dialect before inserting it into a set and cover this pair in the duplicate-dialect test. ########## src/iceberg/view_metadata.cc: ########## @@ -0,0 +1,166 @@ +/* + * 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 "iceberg/view_metadata.h" + +#include <algorithm> +#include <unordered_set> + +#include "iceberg/schema.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg { + +Status ViewMetadata::Validate() const { + if (format_version != kSupportedViewFormatVersion) { + return Invalid("Unsupported view format version: {}", + static_cast<int>(format_version)); + } + if (view_uuid.empty()) { + return Invalid("View UUID must not be empty"); + } + if (location.empty()) { + return Invalid("View location must not be empty"); + } + if (schemas.empty()) { + return Invalid("View metadata must list at least one schema"); + } + + std::unordered_set<int32_t> schema_ids; + for (const auto& schema : schemas) { + if (schema == nullptr) { + return Invalid("View schemas must not be null"); + } + if (!schema_ids.insert(schema->schema_id()).second) { + return Invalid("Duplicate view schema ID: {}", schema->schema_id()); + } + } + + if (versions.empty()) { + return Invalid("View metadata must list at least one version"); + } + std::unordered_set<int32_t> version_ids; + for (const auto& version : versions) { + if (version == nullptr) { + return Invalid("View versions must not be null"); + } + if (!version_ids.insert(version->version_id).second) { + return Invalid("Duplicate view version ID: {}", version->version_id); + } + } + if (!version_ids.contains(current_version_id)) { + return Invalid("Current version ID {} is not a listed view version", + current_version_id); + } + for (const auto& entry : version_log) { + if (!version_ids.contains(entry.version_id)) { + return Invalid("View history references unlisted version ID {}", entry.version_id); + } + } + + for (const auto& version : versions) { + if (!schema_ids.contains(version->schema_id)) { + return Invalid("View version {} references unlisted schema ID {}", + version->version_id, version->schema_id); + } + if (version->default_namespace.levels.empty()) { Review Comment: Java can write an empty default namespace as `[]`. Please allow that value and change the existing test to cover it. ########## src/iceberg/view_metadata.cc: ########## @@ -0,0 +1,166 @@ +/* + * 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 "iceberg/view_metadata.h" + +#include <algorithm> +#include <unordered_set> + +#include "iceberg/schema.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg { + +Status ViewMetadata::Validate() const { + if (format_version != kSupportedViewFormatVersion) { + return Invalid("Unsupported view format version: {}", + static_cast<int>(format_version)); + } + if (view_uuid.empty()) { + return Invalid("View UUID must not be empty"); + } + if (location.empty()) { + return Invalid("View location must not be empty"); + } + if (schemas.empty()) { + return Invalid("View metadata must list at least one schema"); + } + + std::unordered_set<int32_t> schema_ids; + for (const auto& schema : schemas) { + if (schema == nullptr) { + return Invalid("View schemas must not be null"); + } + if (!schema_ids.insert(schema->schema_id()).second) { Review Comment: A view schema with duplicate field IDs passes this check and then fails on field lookup. Please validate field ID uniqueness independently of table format-version restrictions. ########## src/iceberg/view_metadata.cc: ########## @@ -0,0 +1,166 @@ +/* + * 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 "iceberg/view_metadata.h" + +#include <algorithm> +#include <unordered_set> + +#include "iceberg/schema.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg { + +Status ViewMetadata::Validate() const { + if (format_version != kSupportedViewFormatVersion) { + return Invalid("Unsupported view format version: {}", + static_cast<int>(format_version)); + } + if (view_uuid.empty()) { + return Invalid("View UUID must not be empty"); + } + if (location.empty()) { + return Invalid("View location must not be empty"); + } + if (schemas.empty()) { + return Invalid("View metadata must list at least one schema"); + } + + std::unordered_set<int32_t> schema_ids; + for (const auto& schema : schemas) { + if (schema == nullptr) { + return Invalid("View schemas must not be null"); + } + if (!schema_ids.insert(schema->schema_id()).second) { + return Invalid("Duplicate view schema ID: {}", schema->schema_id()); + } + } + + if (versions.empty()) { + return Invalid("View metadata must list at least one version"); + } + std::unordered_set<int32_t> version_ids; + for (const auto& version : versions) { + if (version == nullptr) { + return Invalid("View versions must not be null"); + } + if (!version_ids.insert(version->version_id).second) { + return Invalid("Duplicate view version ID: {}", version->version_id); + } + } + if (!version_ids.contains(current_version_id)) { + return Invalid("Current version ID {} is not a listed view version", + current_version_id); + } + for (const auto& entry : version_log) { + if (!version_ids.contains(entry.version_id)) { + return Invalid("View history references unlisted version ID {}", entry.version_id); + } + } + + for (const auto& version : versions) { + if (!schema_ids.contains(version->schema_id)) { + return Invalid("View version {} references unlisted schema ID {}", + version->version_id, version->schema_id); + } + if (version->default_namespace.levels.empty()) { + return Invalid("View version {} must have a non-empty default namespace", + version->version_id); + } + + std::vector<std::string_view> dialects; + for (const auto& repr : version->representations) { + if (const auto* sql = GetSql(repr)) { + if (sql->sql.empty()) { + return Invalid("View version {} has an SQL representation without SQL", + version->version_id); + } + if (sql->dialect.empty()) { + return Invalid("View version {} has an SQL representation without a dialect", + version->version_id); + } + for (const auto& dialect : dialects) { + if (StringUtils::EqualsIgnoreCaseAscii(dialect, sql->dialect)) { + return Invalid( + "View version {} has multiple SQL representations for dialect '{}'", + version->version_id, sql->dialect); + } + } + dialects.push_back(sql->dialect); + } else { + const auto& unknown = std::get<UnknownViewRepresentation>(repr); + if (unknown.type.empty()) { Review Comment: An unknown representation tagged `sql` passes validation without a query or dialect. Please reserve the SQL discriminator for the SQL alternative, including the case variants recognized by Java. ########## src/iceberg/view_metadata.cc: ########## @@ -0,0 +1,166 @@ +/* + * 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 "iceberg/view_metadata.h" + +#include <algorithm> +#include <unordered_set> + +#include "iceberg/schema.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg { + +Status ViewMetadata::Validate() const { + if (format_version != kSupportedViewFormatVersion) { + return Invalid("Unsupported view format version: {}", + static_cast<int>(format_version)); + } + if (view_uuid.empty()) { Review Comment: A non-empty string such as `x` passes as a view UUID. Please validate it with the existing UUID parser and return `kInvalid` for malformed values. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
