-
Notifications
You must be signed in to change notification settings - Fork 6
ordeq-iceberg: Add Ordeq Iceberg package
#416
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
Open
Josersanvil
wants to merge
28
commits into
ing-bank:main
Choose a base branch
from
Josersanvil:feat/package/ordeq-iceberg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d619f96
Added new Ordeq-Iceberg package
Josersanvil 9080f73
Added resources tests
Josersanvil 27fb09a
Remove unused import from test_table.py
Josersanvil 5e743fd
Update IcebergCatalog and IcebergTableCreate examples; fix return typ…
Josersanvil 1e9acc6
Enhance Ordeq-Iceberg package: update dependencies, improve code form…
Josersanvil 9477954
Refactor imports and update dependencies in Ordeq-Iceberg package
Josersanvil efabeeb
Refactor IcebergTable and IcebergTableCreate to use Input for catalog…
Josersanvil 70945a8
Add ordeq-iceberg to workspace dependencies in pyproject.toml
Josersanvil 8600081
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil ff2d267
fix: standardize log message quotes in create_and_load snapshot
Josersanvil 9ad5c19
refactor: change IfTableExistsSaveOptions from StrEnum to Enum and up…
Josersanvil cd024f5
fix: add type ignore for return value in catalog load methods
Josersanvil 23b4b0e
refactor: consolidate IcebergTable creation logic in a single IO and …
Josersanvil c239ef4
Don't persist data for IcebergTable
Josersanvil 4acdbe8
fix: update create_save_table to use create_namespace_if_not_exists
Josersanvil 50729c5
fix: update create_save_table to use create_namespace_if_not_exists
Josersanvil 97308d1
refactor: remove unused table_resource variable from create_and_load.py
Josersanvil ff54057
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil 7e60e69
fix: enhance logging for IcebergCatalog and table operations
Josersanvil 4fc6af2
refactor: rename private properties for clarity and add validation fo…
Josersanvil 8786cbe
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil 1314347
Update uv.lock after pulling from main
Josersanvil cd510f1
refactor: remove unused IcebergIOError import from table.py
Josersanvil 048e8e6
Remove save method on IcebergTable class
Josersanvil 586e2f2
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil 04808b7
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil e2a53c4
refactor: add checks to create_table node and enhance load_table func…
Josersanvil d8681d4
Merge branch 'main' into feat/package/ordeq-iceberg
Josersanvil 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=75.0", "setuptools_scm>=8.1.0"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "ordeq_iceberg" | ||
| dynamic = ["version"] | ||
| readme = "README.md" | ||
| license = "MIT" | ||
| license-files = ["LICENSE"] | ||
| authors = [{ name = "Jose R. Sanchez Viloria <@josersanvil>" }] | ||
| maintainers = [{ name = "Jose R. Sanchez Viloria <@josersanvil>" }] | ||
| requires-python = ">=3.10" | ||
| dependencies = ["ordeq>=1.3.3", "pyiceberg[sql-sqlite]>=0.10.0"] | ||
|
|
||
| [dependency-groups] | ||
| dev = ["mypy-boto3-glue>=1.40.39"] | ||
| test = [ | ||
| "ordeq-common", | ||
| "ordeq-test-utils", | ||
| "ordeq-test-examples", | ||
| "ordeq-dev-tools", | ||
| "pytest", | ||
| ] | ||
|
|
||
| [tool.setuptools_scm] | ||
| root = "../../" | ||
| version_file = "src/ordeq_iceberg/_version.py" | ||
| tag_regex = "^ordeq-iceberg/(?:[\\w-]+-)?(?P<version>[vV]?\\d+(?:\\.\\d+){0,2}[^\\+]*)(?:\\+.*)?$" | ||
| git_describe_command = "git describe --dirty --tags --long --match ordeq-iceberg/*" | ||
|
|
||
| [tool.setuptools.packages.find] | ||
| namespaces = true | ||
| where = ["src"] | ||
|
|
||
| [tool.pytest.ini_options] | ||
| pythonpath = ["src", "tests"] | ||
| addopts = "--doctest-modules" | ||
|
|
||
| [tool.coverage.run] | ||
| omit = ["src/ordeq_iceberg/_version.py"] | ||
|
|
||
| [tool.ruff] | ||
Josersanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| line-length = 88 | ||
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,13 @@ | ||
| from ordeq_iceberg.catalog import IcebergCatalog | ||
| from ordeq_iceberg.errors import IcebergIOError, IcebergTableAlreadyExistsError | ||
| from ordeq_iceberg.table import IcebergTable | ||
| from ordeq_iceberg.table_create import IcebergTableCreate, IfTableExistsSaveOptions | ||
|
|
||
| __all__ = ( | ||
| "IcebergCatalog", | ||
| "IcebergTable", | ||
| "IcebergTableCreate", | ||
| "IfTableExistsSaveOptions", | ||
| "IcebergIOError", | ||
| "IcebergTableAlreadyExistsError", | ||
| ) |
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,31 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from ordeq import Input | ||
| from pyiceberg.catalog import Catalog, CatalogType, load_catalog | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IcebergCatalog(Input[Catalog]): | ||
| """IO for loading an Iceberg Catalog. | ||
|
|
||
| Example: | ||
|
|
||
| ```pycon | ||
Josersanvil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| >>> from pyiceberg.catalog import CatalogType | ||
| >>> iceberg_catalog = IcebergCatalog( | ||
| ... name="my_catalog", catalog_type=CatalogType.IN_MEMORY | ||
| ... ) | ||
| >>> catalog = iceberg_catalog.load() | ||
|
|
||
| ``` | ||
|
|
||
| """ | ||
|
|
||
| name: str | ||
| catalog_type: CatalogType | str | ||
|
|
||
| def load(self, **load_options) -> Catalog: | ||
| catalog_type_value = self.catalog_type | ||
| if isinstance(self.catalog_type, CatalogType): | ||
| catalog_type_value = self.catalog_type.value | ||
| return load_catalog(self.name, type=catalog_type_value, **load_options) | ||
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,11 @@ | ||
| """Module defining custom exceptions for Iceberg-related operations.""" | ||
|
|
||
| from ordeq import IOException | ||
|
|
||
|
|
||
| class IcebergIOError(IOException): | ||
| """Base class for Iceberg-related errors.""" | ||
|
|
||
|
|
||
| class IcebergTableAlreadyExistsError(IcebergIOError): | ||
| """Error raised when trying to create a table that already exists.""" |
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 @@ | ||
|
|
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,50 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from ordeq import Input | ||
| from pyiceberg.catalog import Catalog | ||
| from pyiceberg.table import Table | ||
|
|
||
| from ordeq_iceberg.catalog import IcebergCatalog | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IcebergTable(Input[Table]): | ||
| """IO for loading an Iceberg table. | ||
|
|
||
| Example: | ||
|
|
||
| ```pycon | ||
Josersanvil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| >>> from ordeq_iceberg import IcebergTable, IcebergCatalog | ||
| >>> catalog = IcebergCatalog( | ||
| ... name="my_catalog", | ||
| ... catalog_type="IN_MEMORY" | ||
| ... ) | ||
| >>> table = IcebergTable( | ||
| ... catalog=catalog, | ||
| ... table_name="my_table", | ||
| ... namespace="my_namespace" | ||
| ... ) | ||
|
|
||
| ``` | ||
|
|
||
| """ | ||
|
|
||
| catalog: IcebergCatalog | Catalog | ||
| table_name: str | ||
| namespace: str | ||
|
|
||
| @property | ||
| def table_identifier(self) -> str: | ||
| return f"{self.namespace}.{self.table_name}" | ||
|
|
||
| def load(self, **load_options) -> Table: | ||
| """Load the table instance from the catalog | ||
|
|
||
| Returns: | ||
| The loaded Iceberg table instance | ||
| """ | ||
| if isinstance(self.catalog, IcebergCatalog): | ||
| catalog = self.catalog.load() | ||
| else: | ||
| catalog = self.catalog | ||
| return catalog.load_table(self.table_identifier, **load_options) | ||
135 changes: 135 additions & 0 deletions
135
packages/ordeq-iceberg/src/ordeq_iceberg/table_create.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,135 @@ | ||
| from dataclasses import dataclass | ||
| from enum import StrEnum | ||
|
|
||
| from ordeq import Output | ||
Josersanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from pyiceberg.catalog import Catalog | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.types import StructType | ||
|
|
||
| from ordeq_iceberg.catalog import IcebergCatalog | ||
| from ordeq_iceberg.errors import IcebergIOError, IcebergTableAlreadyExistsError | ||
|
|
||
|
|
||
| class IfTableExistsSaveOptions(StrEnum): | ||
| """Options for handling existing tables when saving.""" | ||
|
|
||
| DROP = "drop" | ||
| """Drop the existing table before creating a new one.""" | ||
| IGNORE = "ignore" | ||
| """Do nothing if the table already exists.""" | ||
| RAISE = "raise" | ||
| """Raise an error if the table already exists.""" | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class IcebergTableCreate(Output[None]): | ||
| """Ordeq output for creating an Iceberg table. | ||
|
|
||
| Example: | ||
|
|
||
| ```pycon | ||
| >>> from ordeq_iceberg import IcebergTableCreate, IcebergCatalog | ||
| >>> from pyiceberg.schema import Schema, NestedField | ||
| >>> from pyiceberg.catalog import CatalogType | ||
| >>> from pyiceberg.types import StringType, IntegerType | ||
| >>> catalog = IcebergCatalog( | ||
| ... name="my_catalog", catalog_type=CatalogType.IN_MEMORY | ||
| ... ) | ||
| >>> table_create = IcebergTableCreate( | ||
| ... catalog=catalog, | ||
| ... table_name="my_table", | ||
| ... namespace="my_namespace", | ||
| ... schema=Schema( | ||
| ... NestedField(field_id=1, required=True, name="id", field_type=IntegerType()), | ||
| ... NestedField(field_id=2, required=True, name="name", field_type=StringType()), | ||
| ... ), | ||
| ... if_exists=IfTableExistsSaveOptions.DROP, | ||
| ... ) | ||
|
|
||
| ``` | ||
|
|
||
| If using for an Ordeq pipeline where you need to control both | ||
| the creation and loading of the table, use this in | ||
| combination with `IcebergTable` with a shared resource name. | ||
|
|
||
| eg: | ||
|
|
||
| ```pycon | ||
| >>> from ordeq_iceberg import IcebergTable, IcebergTableCreate, IcebergCatalog | ||
| >>> from pyiceberg.schema import Schema, NestedField | ||
| >>> from pyiceberg.catalog import CatalogType | ||
| >>> from pyiceberg.types import StringType, IntegerType | ||
| >>> catalog = IcebergCatalog( | ||
| ... name="my_catalog", catalog_type=CatalogType.IN_MEMORY | ||
| ... ) | ||
| >>> my_table_resource_name = "my_table.my_namespace" | ||
| >>> table_create = IcebergTableCreate( | ||
| ... catalog=catalog, | ||
| ... table_name="my_table", | ||
| ... namespace="my_namespace", | ||
| ... schema=Schema( | ||
| ... NestedField(field_id=1, required=True, name="id", field_type=IntegerType()), | ||
| ... NestedField(field_id=2, required=True, name="name", field_type=StringType()), | ||
| ... ), | ||
| ... if_exists=IfTableExistsSaveOptions.DROP, | ||
| ... ) @ my_table_resource_name | ||
| >>> table = IcebergTable( | ||
| ... catalog=catalog, | ||
| ... table_name="my_table", | ||
| ... namespace="my_namespace", | ||
| ... ) @ my_table_resource_name | ||
| """ | ||
|
|
||
| catalog: IcebergCatalog | Catalog | ||
| table_name: str | ||
| namespace: str | ||
| schema: Schema | StructType | ||
| """Schema to use when creating the table""" | ||
| if_exists: IfTableExistsSaveOptions | None = None | ||
| """What to do if the table already exists. | ||
| None (default) lets the underlying catalog handle | ||
| the situation (usually raises an error). | ||
| """ | ||
|
|
||
| @property | ||
| def table_identifier(self) -> str: | ||
| return f"{self.namespace}.{self.table_name}" | ||
|
|
||
| @property | ||
| def _catalog_value(self) -> Catalog: | ||
| if isinstance(self.catalog, IcebergCatalog): | ||
| return self.catalog.load() | ||
| return self.catalog | ||
|
|
||
| def table_exists(self) -> bool: | ||
| catalog = self._catalog_value | ||
| return catalog.table_exists(self.table_identifier) | ||
|
|
||
| def save(self, _: None = None, **save_options) -> None: | ||
| """Create the table in the catalog with the provided schema. | ||
|
|
||
| Raises: | ||
| IcebergIOError: If the schema is not provided when creating a new table | ||
| IcebergTableAlreadyExistsError: If the table already exists and | ||
| `if_exists` is set to RAISE | ||
| """ | ||
| catalog = self._catalog_value | ||
| schema = self.schema or save_options.pop("schema", None) | ||
| if schema is None: | ||
| raise IcebergIOError("Schema must be provided to create a new table.") | ||
| if isinstance(schema, StructType): | ||
| schema = Schema(*schema.fields) | ||
| table_exists = self.table_exists() | ||
| if table_exists: | ||
| match self.if_exists: | ||
| case IfTableExistsSaveOptions.IGNORE: | ||
| return | ||
| case IfTableExistsSaveOptions.DROP: | ||
| catalog.drop_table(self.table_identifier) | ||
| case IfTableExistsSaveOptions.RAISE: | ||
| raise IcebergTableAlreadyExistsError( | ||
| f"Table '{self.table_identifier}' already exists." | ||
| ) | ||
| catalog.create_table( | ||
| identifier=self.table_identifier, schema=schema, **save_options | ||
| ) | ||
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,68 @@ | ||
| from collections.abc import Generator | ||
| from pathlib import Path | ||
| from tempfile import gettempdir | ||
| from typing import cast | ||
| from shutil import rmtree | ||
|
|
||
| import pytest | ||
| from pyiceberg.catalog import CatalogType | ||
| from pyiceberg.catalog.sql import SqlCatalog | ||
|
|
||
| from ordeq_iceberg import IcebergCatalog | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def resources_dir(request: pytest.FixtureRequest) -> Path: | ||
| """Loads a `Path` referring to the resources directory specific to the | ||
| requesting test module. | ||
|
|
||
| For example, in tests/test_sth.py, this test should pass: | ||
|
|
||
| >>> def test_method(resources_dir): | ||
| ... assert resources_dir == 'tests/test_sth' | ||
|
|
||
| Can be used to load and save data during tests in its own dedicated | ||
| folder, for instance: | ||
|
|
||
| >>> def test_method(resources_dir): | ||
| ... with open(resources_dir / "test_method.csv", 'r') as file: | ||
| ... assert len(file.readlines()) == 1 | ||
Josersanvil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Args: | ||
| request: the `FixtureRequest` of the requesting test module | ||
|
|
||
| Returns: | ||
| the path to the resources directory | ||
| """ | ||
|
|
||
| (test_file_dot_py, _, _) = request.node.location | ||
| test_file = Path(*Path(test_file_dot_py).with_suffix("").parts[3:]) | ||
| return Path(__file__).parent / "tests-resources" / test_file | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def sql_catalog_io() -> Generator[IcebergCatalog]: | ||
| temp_path = Path(gettempdir()) | ||
| catalog_path = temp_path / "test_sql_catalog" | ||
| catalog_path.mkdir(parents=True, exist_ok=True) | ||
| catalog_io = IcebergCatalog( | ||
| name="test_sql_catalog", | ||
| catalog_type=CatalogType.SQL, | ||
| ).with_load_options( | ||
| uri=f"sqlite:///{catalog_path / 'iceberg.db'}", | ||
| warehouse=str(catalog_path / "warehouse"), | ||
| ) | ||
| catalog = cast(SqlCatalog, catalog_io.load()) | ||
| catalog.create_namespace("test_namespace") | ||
| yield catalog_io | ||
| catalog.close() | ||
| (catalog_path / "iceberg.db").unlink(missing_ok=True) | ||
| rmtree(catalog_path / "warehouse", ignore_errors=True) | ||
| catalog_path.rmdir() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def sql_catalog(sql_catalog_io: IcebergCatalog) -> Generator[SqlCatalog]: | ||
| catalog = cast(SqlCatalog, sql_catalog_io.load()) | ||
| yield catalog | ||
| catalog.close() | ||
Empty file.
Oops, something went wrong.
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.