gemini-code-assist[bot] commented on code in PR #39052: URL: https://github.com/apache/beam/pull/39052#discussion_r3462306831
########## sdks/java/io/delta/src/test/java/org/apache/beam/sdk/io/delta/DeltaReadSchemaTransformProviderTest.java: ########## @@ -0,0 +1,127 @@ +/* + * 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. + */ +package org.apache.beam.sdk.io.delta; + +import static org.apache.beam.sdk.io.delta.DeltaReadSchemaTransformProvider.Configuration; +import static org.apache.beam.sdk.io.delta.DeltaReadSchemaTransformProvider.OUTPUT_TAG; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.apache.avro.generic.GenericRecord; +import org.apache.beam.sdk.extensions.avro.coders.AvroCoder; +import org.apache.beam.sdk.extensions.avro.schemas.utils.AvroUtils; +import org.apache.beam.sdk.io.Compression; +import org.apache.beam.sdk.io.FileIO; +import org.apache.beam.sdk.io.parquet.ParquetIO; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.testing.PAssert; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.transforms.windowing.PaneInfo; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.Row; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link DeltaReadSchemaTransformProvider}. */ +@RunWith(JUnit4.class) +public class DeltaReadSchemaTransformProviderTest { + + @Rule public TestPipeline writePipeline = TestPipeline.create(); + @Rule public TestPipeline readPipeline = TestPipeline.create(); + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testBuildTransformWithRow() { + java.util.Map<String, String> hadoopConfig = new java.util.HashMap<>(); + hadoopConfig.put("fs.gs.project.id", "test-project"); + + Row config = + Row.withSchema(new DeltaReadSchemaTransformProvider().configurationSchema()) + .withFieldValue("table", "/path/to/table") + .withFieldValue("version", 5L) + .withFieldValue("timestamp", "2026-06-04T12:00:00Z") + .withFieldValue("hadoop_config", hadoopConfig) + .build(); + + new DeltaReadSchemaTransformProvider().from(config); + } + + @Test + public void testSimpleScan() throws Exception { + File tableDir = tempFolder.newFolder("delta-table-simple"); + + // 1. Write a Parquet file using Beam + Schema schema = Schema.builder().addField("name", Schema.FieldType.STRING).build(); + Row row = Row.withSchema(schema).addValues("test-name").build(); + + org.apache.avro.Schema avroSchema = AvroUtils.toAvroSchema(schema); + GenericRecord record = AvroUtils.toGenericRecord(row, avroSchema); + + writePipeline + .apply("Create Input", Create.of(record).withCoder(AvroCoder.of(avroSchema))) + .apply( + "Write Parquet", + FileIO.<GenericRecord>write() + .via(ParquetIO.sink(avroSchema)) + .to(tableDir.getAbsolutePath() + "/") + .withNaming( + (BoundedWindow window, + PaneInfo paneInfo, + int numShards, + int shardIndex, + Compression compression) -> "part-00000.parquet")); + + writePipeline.run().waitUntilFinish(); + + File parquetFile = new File(tableDir, "part-00000.parquet"); + byte[] fileBytes = Files.readAllBytes(parquetFile.toPath()); + + // 2. Create the Delta log + File logDir = new File(tableDir, "_delta_log"); + logDir.mkdirs(); Review Comment:  Using `File.mkdirs()` ignores the return value, which can lead to silent failures if directory creation fails. It is safer and more idiomatic to use `Files.createDirectories(logDir.toPath())` which throws an `IOException` on failure. ```suggestion Files.createDirectories(logDir.toPath()); ``` ########## sdks/python/apache_beam/yaml/integration_tests.py: ########## @@ -68,6 +69,8 @@ def get_impl(self): None, lambda payload, components, context: BigEndianIntegerCoder()) import psycopg2 +import pyarrow as pa +import pyarrow.parquet as pq Review Comment:  Importing `pyarrow` at the module level can cause `ImportError` when running other tests in environments where `pyarrow` is not installed. It is safer to import them lazily inside the `temp_delta_table` function. ```suggestion # pyarrow is imported lazily inside temp_delta_table ``` ########## sdks/python/apache_beam/yaml/integration_tests.py: ########## @@ -618,6 +621,26 @@ def temp_pubsub_emulator(project_id="apache-beam-testing"): yield created_topic_object.name [email protected] +def temp_delta_table(): + with tempfile.TemporaryDirectory() as temp_dir: Review Comment:  Import `pyarrow` and `pyarrow.parquet` lazily inside the context manager to avoid module-level import errors when these optional dependencies are not installed. ```suggestion @contextlib.contextmanager def temp_delta_table(): import pyarrow as pa import pyarrow.parquet as pq with tempfile.TemporaryDirectory() as temp_dir: ``` ########## sdks/python/apache_beam/yaml/integration_tests.py: ########## @@ -618,6 +621,26 @@ def temp_pubsub_emulator(project_id="apache-beam-testing"): yield created_topic_object.name [email protected] +def temp_delta_table(): + with tempfile.TemporaryDirectory() as temp_dir: + log_dir = os.path.join(temp_dir, "_delta_log") + os.makedirs(log_dir, exist_ok=True) + table_data = pa.table({"name": ["a", "b", "c"]}) + parquet_path = os.path.join(temp_dir, "part-00000.parquet") + pq.write_table(table_data, parquet_path) + file_size = os.path.getsize(parquet_path) + commit_content = ( + '{"protocol":{"minReaderVersion":1,"minWriterVersion":2}}\n' + '{"metaData":{"id":"test-id","format":{"provider":"parquet","options":{}},"schemaString":"{\\"type\\":\\"struct\\",\\"fields\\":[{\\"name\\":\\"name\\",\\"type\\":\\"string\\",\\"nullable\\":true,\\"metadata\\":{}}]}","partitionColumns":[],"configuration":{},"createdAt":123456789}}\n' + f'{{"add":{{"path":"part-00000.parquet","partitionValues":{{}},"size":{file_size},"modificationTime":123456789,"dataChange":true}}}}\n' + ) + commit_file = os.path.join(log_dir, "00000000000000000000.json") + with open(commit_file, "w") as f: Review Comment:  Specify `encoding="utf-8"` when opening files for writing to ensure consistent behavior across different platforms (e.g., Windows, where the default encoding might not be UTF-8). ```suggestion with open(commit_file, "w", encoding="utf-8") as f: ``` -- 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]
