-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add readFromDeltaLake transform #4027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
derrickaw
merged 4 commits into
GoogleCloudPlatform:main
from
derrickaw:20260714_deltaLakeAdd
Jul 17, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
python/src/main/python/job-builder-util-transforms/read_from_delta_lake.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Module containing transforms to read data from Delta Lake tables.""" | ||
|
|
||
| from typing import Mapping, Optional | ||
| from apache_beam.transforms import PTransform | ||
| from apache_beam.transforms import managed | ||
| from apache_beam.transforms.external import BeamJarExpansionService | ||
| from apache_beam.transforms.external import SchemaAwareExternalTransform | ||
|
|
||
| DELTA_LAKE_READ_URN = "beam:schematransform:org.apache.beam:delta_lake_read:v1" | ||
|
|
||
|
|
||
| class ReadFromDeltaLake(PTransform): | ||
| """A PTransform that reads data from a Delta Lake table. | ||
|
|
||
| Args: | ||
| table: Identifier or path of the Delta Lake table. | ||
| version: Version of the Delta Lake table to read. | ||
| timestamp: Timestamp of the Delta Lake table to read. | ||
| hadoop_config: Properties passed to Hadoop Configuration. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| table: str, | ||
| version: Optional[int] = None, | ||
| timestamp: Optional[str] = None, | ||
| hadoop_config: Optional[Mapping[str, str]] = None, | ||
| ): | ||
| super().__init__() | ||
| self.table = table | ||
| self.version = version | ||
| self.timestamp = timestamp | ||
| self.hadoop_config = hadoop_config | ||
|
|
||
|
|
||
| def expand(self, pbegin): | ||
| """Expands the ReadFromDeltaLake transform.""" | ||
| config = { | ||
| 'table': self.table, | ||
| } | ||
| if self.version is not None: | ||
| config['version'] = self.version | ||
| if self.timestamp is not None: | ||
| config['timestamp'] = self.timestamp | ||
| if self.hadoop_config is not None: | ||
| config['hadoop_config'] = dict(self.hadoop_config) | ||
|
|
||
| delta_source = getattr(managed, 'DELTA', 'delta') | ||
| if hasattr(managed, 'Read') and delta_source in getattr( | ||
| managed.Read, '_READ_TRANSFORMS', {} | ||
| ): | ||
| return pbegin | managed.Read(delta_source, config=config) | ||
| else: | ||
| return pbegin | SchemaAwareExternalTransform( | ||
| identifier=DELTA_LAKE_READ_URN, | ||
| expansion_service=BeamJarExpansionService( | ||
| 'sdks:java:io:expansion-service:shadowJar' | ||
| ), | ||
| **config, | ||
| ) | ||
|
|
||
|
|
||
120 changes: 120 additions & 0 deletions
120
python/src/test/python/job-builder-util-transforms/read_from_delta_lake_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from unittest.mock import ANY, MagicMock, patch | ||
| import apache_beam as beam | ||
| from apache_beam.testing.test_pipeline import TestPipeline | ||
| from apache_beam.testing.util import assert_that, equal_to | ||
| from apache_beam.transforms import managed | ||
| import pyarrow as pa | ||
| import pyarrow.parquet as pq | ||
| from read_from_delta_lake import DELTA_LAKE_READ_URN, ReadFromDeltaLake | ||
|
|
||
|
|
||
| class ReadFromDeltaLakeTest(unittest.TestCase): | ||
|
derrickaw marked this conversation as resolved.
|
||
|
|
||
| @patch('read_from_delta_lake.SchemaAwareExternalTransform') | ||
| @patch.object(managed, 'DELTA', 'delta', create=True) | ||
| @patch('read_from_delta_lake.managed.Read') | ||
| def test_read_from_delta_lake_managed_read(self, mock_managed_read, mock_saet): | ||
| mock_managed_read._READ_TRANSFORMS = {'delta': DELTA_LAKE_READ_URN} | ||
| mock_transform = MagicMock() | ||
| mock_managed_read.return_value = mock_transform | ||
|
|
||
|
|
||
| table = 'gs://bucket/delta_table' | ||
| version = 2 | ||
| timestamp = '2026-05-01T12:00:00Z' | ||
| hadoop_config = {'fs.gs.project.id': 'test-project'} | ||
|
|
||
| transform = ReadFromDeltaLake( | ||
| table=table, | ||
| version=version, | ||
| timestamp=timestamp, | ||
| hadoop_config=hadoop_config, | ||
| ) | ||
|
|
||
| pbegin = MagicMock() | ||
| transform.expand(pbegin) | ||
|
|
||
| mock_managed_read.assert_called_once_with( | ||
| 'delta', | ||
| config={ | ||
| 'table': table, | ||
| 'version': version, | ||
| 'timestamp': timestamp, | ||
| 'hadoop_config': hadoop_config, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| @patch('read_from_delta_lake.SchemaAwareExternalTransform') | ||
| @patch('read_from_delta_lake.managed.Read') | ||
| def test_read_from_delta_lake_fallback(self, mock_managed_read, mock_saet): | ||
| mock_managed_read._READ_TRANSFORMS = {} | ||
| mock_transform = MagicMock() | ||
| mock_saet.return_value = mock_transform | ||
|
|
||
| table = '/path/to/table' | ||
| hadoop_config = {'fs.gs.project.id': 'test-project'} | ||
|
|
||
| transform = ReadFromDeltaLake( | ||
| table=table, | ||
| hadoop_config=hadoop_config, | ||
| ) | ||
|
|
||
| pbegin = MagicMock() | ||
| transform.expand(pbegin) | ||
|
|
||
| mock_saet.assert_called_once_with( | ||
| identifier=DELTA_LAKE_READ_URN, | ||
| expansion_service=ANY, | ||
| table=table, | ||
| hadoop_config=hadoop_config, | ||
| ) | ||
|
|
||
| def test_read_from_delta_lake_local_integration(self): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| table_dir = os.path.join(temp_dir, 'delta-table') | ||
| os.makedirs(table_dir) | ||
|
|
||
| # create parquet file | ||
| parquet_file_path = os.path.join(table_dir, 'part-00000.parquet') | ||
| table = pa.table({'name': ['test-name-1', 'test-name-2']}) | ||
| pq.write_table(table, parquet_file_path) | ||
|
|
||
| file_size = os.path.getsize(parquet_file_path) | ||
|
|
||
| log_dir = os.path.join(table_dir, '_delta_log') | ||
| os.makedirs(log_dir) | ||
|
|
||
| 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' | ||
| ) | ||
|
|
||
| # create delta log | ||
| with open(os.path.join(log_dir, '00000000000000000000.json'), 'w') as f: | ||
| f.write(commit_content) | ||
|
|
||
| with TestPipeline() as p: | ||
| output = ( | ||
| p | ||
| | ReadFromDeltaLake(table=table_dir) | ||
| | beam.Map(lambda row: row._asdict()) | ||
| ) | ||
|
|
||
| expected = [{'name': 'test-name-1'}, {'name': 'test-name-2'}] | ||
| assert_that(output, equal_to(expected)) | ||
|
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.