Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
- uses: actions/checkout@v4
with:
lfs: true
- name: Set up Java 21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "21"
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions src/jsoniq/jars/rumbledb-3.1.0.jar
Git LFS file not shown
40 changes: 40 additions & 0 deletions src/jsoniq/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self):
self._appendable_keys = {
"spark.jars.packages",
"spark.sql.extensions",
"spark.kryo.registrator",
}

def rumbleConfig(self, path, value):
Expand Down Expand Up @@ -228,6 +229,45 @@ def withIceberg(self, catalog_names=None):
.config("spark.sql.iceberg.check-ordering", "false")
return self;

def withHudi(self):
"""
Configure Apache Hudi on the session catalog (spark_catalog).

- Hudi works like Delta. Its HoodieCatalog wraps the session
catalog, so named catalogs are not supported. Tables are named <table> or
<database>.<table> (the default database is "default").
- Because it takes over spark_catalog, it cannot be combined with withDelta() or
with withIceberg() without catalog names. withIceberg([...]) with named catalogs
can be combined with it.
- Managed tables are stored under ./hudi-warehouse/<database>.db/<table>.
- Hudi needs the Kryo serializer, both Rumble's and Hudi's registrators are set.
- These are the default settings, which can be overridden with config() if needed.
"""
hudi_catalog = "org.apache.spark.sql.hudi.catalog.HoodieCatalog"
current_catalog = self._sparkbuilder._options.get("spark.sql.catalog.spark_catalog")
if current_catalog and current_catalog != hudi_catalog:
raise ValueError(
f"spark_catalog is already configured as {current_catalog}. Hudi also needs spark_catalog, "
"so it cannot be combined with withDelta() or with withIceberg() without catalog names. "
"Iceberg can be combined with Hudi by giving withIceberg() catalog names."
)
self._sparkbuilder = self._sparkbuilder \
.config("spark.sql.catalog.spark_catalog", hudi_catalog)
self._append_config("spark.jars.packages", "org.apache.hudi:hudi-spark4.0-bundle_2.13:1.2.1")
self._append_config("spark.sql.extensions", "org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
# Rumble only installs its own Kryo registrator when spark.serializer is unset,
# so both registrators are listed here.
self._sparkbuilder = self._sparkbuilder \
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
self._append_config("spark.kryo.registrator", "org.rumbledb.spark.RumbleKryoRegistrator")
self._append_config("spark.kryo.registrator", "org.apache.spark.HoodieSparkKryoRegistrar")
self._sparkbuilder = self._sparkbuilder \
.config("spark.sql.warehouse.dir", "./hudi-warehouse") \
.config("hoodie.schema.on.read.enable", "true") \
.config("hoodie.datasource.write.schema.allow.auto.evolution.column.drop", "true") \
.config("spark.hadoop.hive.metastore.disallow.incompatible.col.type.changes", "false")
return self;

def withMongo(self):
self._append_config("spark.jars.packages", "org.mongodb.spark:mongo-spark-connector_2.13:10.5.0")
return self;
Expand Down
70 changes: 70 additions & 0 deletions tests/test_hudi_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from jsoniq import RumbleSession
from unittest import TestCase
import uuid


class TestHudiBuilderConfig(TestCase):
"""
Hudi wraps the session catalog (spark_catalog), like Delta, so it cannot be
combined with another format that also claims it. These tests only inspect the
builder and do not start a Spark session.
"""
def test_hudi_conflicts_with_delta(self):
with self.assertRaises(ValueError):
RumbleSession.Builder().withDelta().withHudi()

def test_hudi_conflicts_with_iceberg_session_catalog(self):
with self.assertRaises(ValueError):
RumbleSession.Builder().withIceberg().withHudi()

def test_hudi_with_named_iceberg_catalogs(self):
options = RumbleSession.Builder().withIceberg(["ice"]).withHudi()._sparkbuilder._options
self.assertEqual(
options["spark.sql.catalog.spark_catalog"], "org.apache.spark.sql.hudi.catalog.HoodieCatalog"
)
self.assertEqual(options["spark.sql.catalog.ice"], "org.apache.iceberg.spark.SparkCatalog")


class TestHudiUpdate(TestCase):
"""
Hudi tables live in the session catalog: <table> or <database>.<table>.
"""
@classmethod
def setUpClass(cls):
RumbleSession._rumbleSession = None
RumbleSession._builder = RumbleSession.Builder()
cls.rumble = (
RumbleSession.builder
.withHudi()
.getOrCreate()
)

def _create_insert_count(self, table_name):
self.rumble.jsoniq(f'create collection hudi-table("{table_name}") with {{"k": 1}}').applyPUL()
self.rumble.jsoniq(f'insert {{"k": 2}} last into collection hudi-table("{table_name}")').applyPUL()
count_value = self.rumble.jsoniq(f'count(hudi-table("{table_name}"))').json()
self.assertEqual(count_value, (2,))

def test_unqualified_table(self):
self._create_insert_count(f"hudi_plain_{uuid.uuid4().hex}")

def test_database_qualified_table(self):
self._create_insert_count(f"default.hudi_db_{uuid.uuid4().hex}")

@staticmethod
def _cleanup_warehouses():
import os
import shutil

for dirname in ("hudi-warehouse", "spark-warehouse"):
path = os.path.join(os.getcwd(), dirname)
shutil.rmtree(path, ignore_errors=True)

@classmethod
def tearDownClass(cls):
try:
if cls.rumble is not None:
cls.rumble._sparksession.stop()
finally:
RumbleSession._rumbleSession = None
cls._cleanup_warehouses()
Loading