You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "EnricoMi (via GitHub)" <gi...@apache.org> on 2025/02/07 11:12:39 UTC

[PR] GH-41246: [C++][Python] Simplify nested field encryption [arrow]

EnricoMi opened a new pull request, #45462:
URL: https://github.com/apache/arrow/pull/45462

   ### Rationale for this change
   
   Columns can b encrypted with individual keys. For this, the column name have to be set in `EncryptionConfiguration::column_keys`. This poses the following challenges for columns with nested fields like `MapType`, `ListType`, and `StructType`:
   
   - Encrypting a column of such type requires providing an encryption key for all nested (leaf) fields. Ideally, the column name should be sufficient (as it is for any other data type) to encrypt all nested fields.
   - The actual name of nested fields is not obvious and intuitive from the Arrow schema of the table. An intuitive naming should be possible.
   
   ### What changes are included in this PR?
   
   This adds a user-friendly notation for nested fields:
   - Columns `col.key` and `col.value` can be used to reference they key and value nested field of a `MapType` column. Currently, `col.key_value.key` and `col.key_value.value` are required, respectively.
   - Columns `col.element` can be used to reference they individual list elements of a `ListType` column. Currently, `col.list.element` is required.
   - The actual column name can be used to encrypt all nested fields with the same encryption key.
   - The current column naming scheme can still be used for backward compatibility.
   
   ### Are these changes tested?
   Tested in C++ and Python.
   
   ### Are there any user-facing changes?
   Column encryption can be configured through simpler and intuitive naming.
   
   Documentation will be extended once #45411 is merged.
   
   Fixes #41246.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2131742482


##########
cpp/src/parquet/encryption/encryption.h:
##########
@@ -429,6 +429,16 @@ class PARQUET_EXPORT FileEncryptionProperties {
     return encrypted_columns_;
   }
 
+  /// All columns in encrypted_columns must refer to columns in the given schema.

Review Comment:
   Rephrased entirely
   
   ```
     /// \brief Check and replace encrypted columns according to schema
     ///
     /// Checks all columns in encrypted_columns reference columns in the given schema.
     /// Replaces parent fields in encrypted_columns with all their nested fields,
     /// all using the same encrypion key.
     /// Fields referenced by schema paths are replaced with the respective parquet paths.
     /// These are usually identical, except for nested fields of lists and maps.
   ```



##########
cpp/src/parquet/encryption/encryption.h:
##########
@@ -429,6 +429,16 @@ class PARQUET_EXPORT FileEncryptionProperties {
     return encrypted_columns_;
   }
 
+  /// All columns in encrypted_columns must refer to columns in the given schema.
+  /// They can also refer to parent fields if schema contains nested fields. Then
+  /// all those nested fields of a matching parent field are encrypted by the same key.
+  /// This method modifies encrypted_columns to reflect this.
+  ///
+  /// Columns in encrypted_columns can refer to the parquet column paths as well as the
+  /// schema paths of columns. Those are usually identical, except for nested fields of
+  /// lists and maps.
+  void encrypt_schema(const SchemaDescriptor& schema);

Review Comment:
   done



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3252248743

   @pitrou what are your thoughts on the latest logic / semantics?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2131632441


##########
cpp/src/parquet/encryption/encryption.cc:
##########
@@ -163,6 +163,76 @@ FileEncryptionProperties::Builder* FileEncryptionProperties::Builder::encrypted_
   return this;
 }
 
+void FileEncryptionProperties::encrypt_schema(const SchemaDescriptor& schema) {
+  // Check that all columns in columnEncryptionProperties exist in the schema.
+  // Copy the encrypted_columns map as we are going to modify it while iterating it
+  auto encrypted_columns = ColumnPathToEncryptionPropertiesMap(encrypted_columns_);
+  // if columnEncryptionProperties is empty, every column in file schema will be
+  // encrypted with footer key.
+  if (!encrypted_columns.empty()) {
+    std::vector<std::pair<std::string, std::string>> column_path_vec;
+    // First, memorize all column or schema paths of the schema as dot-strings.
+    for (int i = 0; i < schema.num_columns(); i++) {
+      auto column = schema.Column(i);
+      auto column_path = column->path()->ToDotString();
+      auto schema_path = column->schema_path()->ToDotString();
+      column_path_vec.emplace_back(column_path, column_path);
+      if (schema_path != column_path) {
+        column_path_vec.emplace_back(schema_path, column_path);
+      }
+    }
+    // Sort them alphabetically, so that we can use binary-search and look up parent
+    // columns.
+    std::sort(column_path_vec.begin(), column_path_vec.end());
+
+    // Check if encrypted column exists in schema, or if it is a parent field of a column.
+    for (const auto& elem : encrypted_columns) {
+      auto& encrypted_column = elem.first;
+      auto encrypted_column_prefix = encrypted_column + ".";
+      auto encrypted_column_prefix_len = encrypted_column_prefix.size();
+
+      // first we look up encrypted_columns as
+      // find first column that equals encrypted_column or starts with encrypted_column
+      auto it = std::lower_bound(
+          column_path_vec.begin(), column_path_vec.end(), encrypted_column,
+          [&](const std::pair<std::string, std::string>& item, const std::string& term) {
+            return item.first < term;
+          });
+      bool matches = false;
+
+      // encrypted_column encrypts column 'it' when 'it' is either equal to
+      // encrypted_column, or 'it' starts with encrypted_column_prefix,
+      // i.e. encrypted_column followed by a '.'
+      while (
+          it != column_path_vec.end() &&
+          (it->first == encrypted_column ||
+           // C++20: can be replaced with it->first.starts_with(encrypted_column_prefix)

Review Comment:
   thanks for the pointer, applied



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2534534790


##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -398,9 +411,191 @@ TEST_P(DatasetEncryptionTest, ReadSingleFile) {
 
 INSTANTIATE_TEST_SUITE_P(DatasetEncryptionTest, DatasetEncryptionTest, kAllParamValues);
 
+struct NestedFieldsEncryptionTestParam : EncryptionTestParam {

Review Comment:
   Should this use `public` inheritance?



##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -79,14 +83,20 @@ const auto kAllParamValues = ::testing::Values(
     EncryptionTestParam{false, true, true}, EncryptionTestParam{true, true, true});
 
 // Base class to test writing and reading encrypted dataset.
-class DatasetEncryptionTestBase : public testing::TestWithParam<EncryptionTestParam> {
+template <typename T, typename = typename std::enable_if<
+                          std::is_base_of<EncryptionTestParam, T>::value>::type>
+class DatasetEncryptionTestBase : public testing::TestWithParam<T> {
  public:
 #ifdef ARROW_VALGRIND
   static constexpr int kConcurrentIterations = 4;
 #else
   static constexpr int kConcurrentIterations = 20;
 #endif
 
+  static const EncryptionTestParam& GetParam() {

Review Comment:
   I'm honestly curious why this is required.



##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -79,14 +83,20 @@ const auto kAllParamValues = ::testing::Values(
     EncryptionTestParam{false, true, true}, EncryptionTestParam{true, true, true});
 
 // Base class to test writing and reading encrypted dataset.
-class DatasetEncryptionTestBase : public testing::TestWithParam<EncryptionTestParam> {
+template <typename T, typename = typename std::enable_if<
+                          std::is_base_of<EncryptionTestParam, T>::value>::type>

Review Comment:
   No need to change this, but as a nit it can probably be simplified to
   ```suggestion
   template <typename T, typename = typename std::enable_if_t<
                             std::is_base_of_v<EncryptionTestParam, T>>
   ```



##########
python/pyarrow/tests/parquet/encryption.py:
##########
@@ -41,6 +41,8 @@ def wrap_key(self, key_bytes, master_key_identifier):
     def unwrap_key(self, wrapped_key, master_key_identifier):
         """Not a secure cipher - just extract the key from
         the wrapped key"""
+        if master_key_identifier not in self.master_keys_map:
+            raise ValueError("Unknown master key", master_key_identifier)

Review Comment:
   Why not keep the `KeyError` that the line below would raise?



##########
python/pyarrow/tests/test_dataset_encryption.py:
##########
@@ -82,11 +88,11 @@ def create_decryption_config():
     return pe.DecryptionConfiguration(cache_lifetime=300)
 
 
-def create_kms_connection_config():
+def create_kms_connection_config(keys):
     return pe.KmsConnectionConfig(
         custom_kms_conf={
-            FOOTER_KEY_NAME: FOOTER_KEY.decode("UTF-8"),
-            COL_KEY_NAME: COL_KEY.decode("UTF-8"),
+            key_name: key.decode("UTF-8") if isinstance(key, bytes) else key

Review Comment:
   I'm not sure we want to allow both `bytes` and `str` key names here?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2622067506


##########
python/pyarrow/tests/test_dataset_encryption.py:
##########
@@ -82,11 +88,11 @@ def create_decryption_config():
     return pe.DecryptionConfiguration(cache_lifetime=300)
 
 
-def create_kms_connection_config():
+def create_kms_connection_config(keys):
     return pe.KmsConnectionConfig(
         custom_kms_conf={
-            FOOTER_KEY_NAME: FOOTER_KEY.decode("UTF-8"),
-            COL_KEY_NAME: COL_KEY.decode("UTF-8"),
+            key_name: key.decode("UTF-8") if isinstance(key, bytes) else key

Review Comment:
   right



##########
python/pyarrow/tests/parquet/encryption.py:
##########
@@ -41,6 +41,8 @@ def wrap_key(self, key_bytes, master_key_identifier):
     def unwrap_key(self, wrapped_key, master_key_identifier):
         """Not a secure cipher - just extract the key from
         the wrapped key"""
+        if master_key_identifier not in self.master_keys_map:
+            raise ValueError("Unknown master key", master_key_identifier)

Review Comment:
   I found
   
       ValueError: ('Unknown master key', 'XYZ')
   
   more expressive than
   
       KeyError: 'XYZ'



##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -79,14 +83,20 @@ const auto kAllParamValues = ::testing::Values(
     EncryptionTestParam{false, true, true}, EncryptionTestParam{true, true, true});
 
 // Base class to test writing and reading encrypted dataset.
-class DatasetEncryptionTestBase : public testing::TestWithParam<EncryptionTestParam> {
+template <typename T, typename = typename std::enable_if<
+                          std::is_base_of<EncryptionTestParam, T>::value>::type>
+class DatasetEncryptionTestBase : public testing::TestWithParam<T> {
  public:
 #ifdef ARROW_VALGRIND
   static constexpr int kConcurrentIterations = 4;
 #else
   static constexpr int kConcurrentIterations = 20;
 #endif
 
+  static const EncryptionTestParam& GetParam() {

Review Comment:
   Accessing `GetParam` could not derive return type of `testing::WithParamInterface<T>::GetParam`:
   
       there are no arguments to ‘GetParam’ that depend on a template parameter, so a declaration of ‘GetParam’ must be available
   
   Fixed accessing `GetParam` in 473839e597.



##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -398,9 +411,191 @@ TEST_P(DatasetEncryptionTest, ReadSingleFile) {
 
 INSTANTIATE_TEST_SUITE_P(DatasetEncryptionTest, DatasetEncryptionTest, kAllParamValues);
 
+struct NestedFieldsEncryptionTestParam : EncryptionTestParam {

Review Comment:
   As in
   
       struct NestedFieldsEncryptionTestParam : public EncryptionTestParam {
   
   IDE says `public` is redundant here.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2622165846


##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -398,9 +411,191 @@ TEST_P(DatasetEncryptionTest, ReadSingleFile) {
 
 INSTANTIATE_TEST_SUITE_P(DatasetEncryptionTest, DatasetEncryptionTest, kAllParamValues);
 
+struct NestedFieldsEncryptionTestParam : EncryptionTestParam {

Review Comment:
   Ha, another C++ subtletly that I ignored/forgot :-)



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3213883384

   I have reworked the logic. It does not support the simplified column paths like `map.key`. Users that want to use different encryption keys for different nested fields (which is still supported) have to use the non-intuitive column paths like `map.key_value.key`.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3213884295

   TODO:
   - update PR description
   - update docs


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou merged PR #45462:
URL: https://github.com/apache/arrow/pull/45462


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-2916672939

   > Note that I have removed encrypting the individual list elements via `col.element` as I cannot see a difference in semantics and encryption output between `col.element` and `col`. Both notations are equivalent.
   > 
   > Encrypting a single field of a list of structs column can be denoted as `col.f1` rather than `col.element.f1`.
   
   Sorry for the delay. That seems fine to me, and the documentation will be easier to read like that.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3660231277

   I'm sorry that it took so long @EnricoMi . Looking forward to more contributions from you!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3659240411

   Ha, sorry, CI fails now that https://github.com/apache/arrow/pull/48338 was merged, you need to stop passing the column path to `ColumnEncryptionProperties::Builder`.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-2754349279

   cc @pitrou, this is connected to https://github.com/apache/arrow/pull/45411.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3542636920

   Revision: b0ef03f5320c3397a4db4036ef60ead955154509
   
   Submitted crossbow builds: [ursacomputing/crossbow @ actions-151fb9c2fa](https://github.com/ursacomputing/crossbow/branches/all?query=actions-151fb9c2fa)
   
   |Task|Status|
   |----|------|
   |preview-docs|[![GitHub Actions](https://github.com/ursacomputing/crossbow/actions/workflows/crossbow.yml/badge.svg?branch=actions-151fb9c2fa-github-preview-docs)](https://github.com/ursacomputing/crossbow/actions/runs/19435894842/job/55606484509)|


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3661976389

   After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 052e0aac1983d9803cd2919f7de2a4227e5aa40f.
   
   There were no benchmark performance regressions. 🎉
   
   The [full Conbench report](https://github.com/apache/arrow/runs/58237441523) has more details. It also includes information about 83 possible false positives for unstable benchmarks that are known to sometimes produce them.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2112222174


##########
cpp/src/parquet/encryption/encryption.cc:
##########
@@ -163,6 +163,76 @@ FileEncryptionProperties::Builder* FileEncryptionProperties::Builder::encrypted_
   return this;
 }
 
+void FileEncryptionProperties::encrypt_schema(const SchemaDescriptor& schema) {
+  // Check that all columns in columnEncryptionProperties exist in the schema.
+  // Copy the encrypted_columns map as we are going to modify it while iterating it
+  auto encrypted_columns = ColumnPathToEncryptionPropertiesMap(encrypted_columns_);
+  // if columnEncryptionProperties is empty, every column in file schema will be
+  // encrypted with footer key.
+  if (!encrypted_columns.empty()) {
+    std::vector<std::pair<std::string, std::string>> column_path_vec;
+    // First, memorize all column or schema paths of the schema as dot-strings.
+    for (int i = 0; i < schema.num_columns(); i++) {
+      auto column = schema.Column(i);
+      auto column_path = column->path()->ToDotString();
+      auto schema_path = column->schema_path()->ToDotString();
+      column_path_vec.emplace_back(column_path, column_path);
+      if (schema_path != column_path) {
+        column_path_vec.emplace_back(schema_path, column_path);
+      }
+    }
+    // Sort them alphabetically, so that we can use binary-search and look up parent
+    // columns.
+    std::sort(column_path_vec.begin(), column_path_vec.end());
+
+    // Check if encrypted column exists in schema, or if it is a parent field of a column.
+    for (const auto& elem : encrypted_columns) {
+      auto& encrypted_column = elem.first;
+      auto encrypted_column_prefix = encrypted_column + ".";
+      auto encrypted_column_prefix_len = encrypted_column_prefix.size();
+
+      // first we look up encrypted_columns as
+      // find first column that equals encrypted_column or starts with encrypted_column
+      auto it = std::lower_bound(
+          column_path_vec.begin(), column_path_vec.end(), encrypted_column,
+          [&](const std::pair<std::string, std::string>& item, const std::string& term) {
+            return item.first < term;
+          });
+      bool matches = false;
+
+      // encrypted_column encrypts column 'it' when 'it' is either equal to
+      // encrypted_column, or 'it' starts with encrypted_column_prefix,
+      // i.e. encrypted_column followed by a '.'
+      while (
+          it != column_path_vec.end() &&
+          (it->first == encrypted_column ||
+           // C++20: can be replaced with it->first.starts_with(encrypted_column_prefix)

Review Comment:
   We have a `StartsWith` utility in `arrow/util/string.h`, FTR



##########
cpp/src/parquet/schema.h:
##########
@@ -132,6 +134,8 @@ class PARQUET_EXPORT Node {
 
   const std::shared_ptr<ColumnPath> path() const;
 
+  const std::shared_ptr<ColumnPath> schema_path() const;

Review Comment:
   If we decide these APIs fit here, we should add docstrings to `path` and `schema_path` so that the distinction is clear (but I'm not sure we should really put them here).



##########
cpp/src/parquet/schema.cc:
##########
@@ -68,11 +68,25 @@ std::shared_ptr<ColumnPath> ColumnPath::FromDotString(const std::string& dotstri
 }
 
 std::shared_ptr<ColumnPath> ColumnPath::FromNode(const Node& node) {
+  return FromNode(node, false);
+}
+
+std::shared_ptr<ColumnPath> ColumnPath::FromNode(const Node& node, bool schema_path) {
   // Build the path in reverse order as we traverse the nodes to the top
   std::vector<std::string> rpath_;
   const Node* cursor = &node;
   // The schema node is not part of the ColumnPath
   while (cursor->parent()) {
+    if (schema_path &&
+        (
+            // nested fields in arrow schema do not know these intermediate nodes
+            cursor->parent()->converted_type() == ConvertedType::MAP ||
+            cursor->parent()->converted_type() == ConvertedType::LIST ||
+            (cursor->parent()->parent() &&
+             cursor->parent()->parent()->converted_type() == ConvertedType::LIST))) {
+      cursor = cursor->parent();
+      continue;
+    }

Review Comment:
   I'm not sure this logic deserves to be here, as it's quite Arrow-specific.
   Furthermore, the actual conversion logic from Parquet schema to Arrow schema is more complicated, example here:
   https://github.com/apache/arrow/blob/d7015bd6e610b6cd6752f6cd543509bd5f8853ff/cpp/src/parquet/arrow/arrow_schema_test.cc#L533-L556
   



##########
cpp/src/parquet/schema.h:
##########
@@ -386,6 +390,8 @@ class PARQUET_EXPORT ColumnDescriptor {
 
   const std::shared_ptr<schema::ColumnPath> path() const;
 
+  const std::shared_ptr<schema::ColumnPath> schema_path() const;

Review Comment:
   Same here.



##########
cpp/src/parquet/encryption/encryption.h:
##########
@@ -429,6 +429,16 @@ class PARQUET_EXPORT FileEncryptionProperties {
     return encrypted_columns_;
   }
 
+  /// All columns in encrypted_columns must refer to columns in the given schema.
+  /// They can also refer to parent fields if schema contains nested fields. Then
+  /// all those nested fields of a matching parent field are encrypted by the same key.
+  /// This method modifies encrypted_columns to reflect this.
+  ///
+  /// Columns in encrypted_columns can refer to the parquet column paths as well as the
+  /// schema paths of columns. Those are usually identical, except for nested fields of
+  /// lists and maps.
+  void encrypt_schema(const SchemaDescriptor& schema);

Review Comment:
   Since this is not a trivial accessor, let's make it camel-case: `EncryptSchema`



##########
cpp/src/parquet/encryption/encryption.h:
##########
@@ -429,6 +429,16 @@ class PARQUET_EXPORT FileEncryptionProperties {
     return encrypted_columns_;
   }
 
+  /// All columns in encrypted_columns must refer to columns in the given schema.

Review Comment:
   Add a brief summary to this? For example:
   ```suggestion
     /// \brief Modify encrypted columns to conform to the schema
     ///
     /// All columns in encrypted_columns must refer to columns in the given schema.
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-2757322484

   > This adds a user-friendly notation for nested fields:
   > * Columns `col.key` and `col.value` can be used to reference they key and value nested field of a `MapType` column. Currently, `col.key_value.key` and `col.key_value.value` are required, respectively.
   
   Is this useful? Is there a reasonable use case for using different encryption settings for the map key and value columns, respectively? Otherwise I think we can only keep the following items, which will also simplify the semantics and the implementation:
   
   > * Columns `col.element` can be used to reference they individual list elements of a `ListType` column. Currently, `col.list.element` is required.
   > * The actual column name can be used to encrypt all nested fields with the same encryption key.
   > * The current column naming scheme can still be used for backward compatibility.
   
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-2753260759

   The C++ failure looks related.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3464184057

   @pitrou hope you have a moment for this


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3469571700

   I'll be out on vacation, so not before ~10 days I think.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3227471103

   I have further reduced the logic. You can either configure an encryption key for a full column path match, or a root field name match. This simplifies match logic and computational effort when looking up encryption properties for column paths.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2622167155


##########
python/pyarrow/tests/parquet/encryption.py:
##########
@@ -41,6 +41,8 @@ def wrap_key(self, key_bytes, master_key_identifier):
     def unwrap_key(self, wrapped_key, master_key_identifier):
         """Not a secure cipher - just extract the key from
         the wrapped key"""
+        if master_key_identifier not in self.master_keys_map:
+            raise ValueError("Unknown master key", master_key_identifier)

Review Comment:
   Ok, thanks. 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2131759493


##########
cpp/src/parquet/schema.h:
##########
@@ -132,6 +134,8 @@ class PARQUET_EXPORT Node {
 
   const std::shared_ptr<ColumnPath> path() const;
 
+  const std::shared_ptr<ColumnPath> schema_path() const;

Review Comment:
   added for now



##########
cpp/src/parquet/schema.h:
##########
@@ -386,6 +390,8 @@ class PARQUET_EXPORT ColumnDescriptor {
 
   const std::shared_ptr<schema::ColumnPath> path() const;
 
+  const std::shared_ptr<schema::ColumnPath> schema_path() const;

Review Comment:
   same here



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-2747677367

   Note, CI Win failures do not look related but there is a linter fix needed:
   
   ```
   @@ -257,11 +257,12 @@ void FileEncryptionProperties::encrypt_schema(const SchemaDescriptor& schema) {
          // encrypted_column encrypts column 'it' when 'it' is either equal to
          // encrypted_column, or 'it' starts with encrypted_column_prefix,
          // i.e. encrypted_column followed by a '.'
   -      while (it != column_path_vec.end() &&
   -             (it->first == encrypted_column ||
   -               // C++20: can be replaced with it->first.starts_with(encrypted_column_prefix)
   -              it->first.compare(0, encrypted_column_prefix_len,
   -                                encrypted_column_prefix) == 0)) {
   +      while (
   +          it != column_path_vec.end() &&
   +          (it->first == encrypted_column ||
   +           // C++20: can be replaced with it->first.starts_with(encrypted_column_prefix)
   +           it->first.compare(0, encrypted_column_prefix_len, encrypted_column_prefix) ==
   +               0)) {
   ```


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2404401595


##########
cpp/src/parquet/schema.cc:
##########
@@ -68,11 +68,25 @@ std::shared_ptr<ColumnPath> ColumnPath::FromDotString(const std::string& dotstri
 }
 
 std::shared_ptr<ColumnPath> ColumnPath::FromNode(const Node& node) {
+  return FromNode(node, false);
+}
+
+std::shared_ptr<ColumnPath> ColumnPath::FromNode(const Node& node, bool schema_path) {
   // Build the path in reverse order as we traverse the nodes to the top
   std::vector<std::string> rpath_;
   const Node* cursor = &node;
   // The schema node is not part of the ColumnPath
   while (cursor->parent()) {
+    if (schema_path &&
+        (
+            // nested fields in arrow schema do not know these intermediate nodes
+            cursor->parent()->converted_type() == ConvertedType::MAP ||
+            cursor->parent()->converted_type() == ConvertedType::LIST ||
+            (cursor->parent()->parent() &&
+             cursor->parent()->parent()->converted_type() == ConvertedType::LIST))) {
+      cursor = cursor->parent();
+      continue;
+    }

Review Comment:
   This specific logic has been removed. Lookup logic has been simplified to looking up column path or root column among the keys. No type-specific logic left.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3469613563

   No worries, enjoy!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #45462:
URL: https://github.com/apache/arrow/pull/45462#discussion_r2622068399


##########
cpp/src/arrow/dataset/file_parquet_encryption_test.cc:
##########
@@ -79,14 +83,20 @@ const auto kAllParamValues = ::testing::Values(
     EncryptionTestParam{false, true, true}, EncryptionTestParam{true, true, true});
 
 // Base class to test writing and reading encrypted dataset.
-class DatasetEncryptionTestBase : public testing::TestWithParam<EncryptionTestParam> {
+template <typename T, typename = typename std::enable_if<
+                          std::is_base_of<EncryptionTestParam, T>::value>::type>

Review Comment:
   applied



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-41246: [C++][Python] Simplify nested field encryption configuration [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #45462:
URL: https://github.com/apache/arrow/pull/45462#issuecomment-3542621206

   @github-actions crossbow submit preview-docs
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org