Skip to content

Commit

Permalink
Support both old and new catalog (#94)
Browse files Browse the repository at this point in the history
In Spark 3.1 or earlier, the info_name field was named database_description_item and the info_value field was named database_description_value for the builtin catalog.
  • Loading branch information
drudim committed Dec 19, 2022
1 parent 7267a91 commit 2c5d50e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 13 additions & 3 deletions sparkly/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,27 @@ def get_database_properties(self, db_name):
Returns:
dict[str,str]: Key/value for properties.
"""
describe = self._spark.sql(f'DESCRIBE DATABASE EXTENDED {db_name}')

if 'database_description_item' in describe.columns:
key_col = 'database_description_item'
val_col = 'database_description_value'
else:
key_col = 'info_name'
val_col = 'info_value'

properties = (
self._spark.sql('DESCRIBE DATABASE EXTENDED {}'.format(db_name))
.where(F.col('info_name') == 'Properties')
.select('info_value')
.where(F.col(key_col) == 'Properties')
.select(val_col)
.first()
)

parsed_properties = {}

if properties:
for name, value in read_db_properties_format(properties.info_value):
info_value = getattr(properties, val_col)
for name, value in read_db_properties_format(info_value):
parsed_properties[name] = value

return parsed_properties
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ class SparklyTestSession(SparklySession):
# will be overwritten by additional_options passed in setup_session
'my.custom.option.3': '319',
}


class SparklyTestSessionWithOldCatalog(SparklyTestSession):
options = {
'spark.sql.legacy.keepCommandOutputSchema': 'true',
}
6 changes: 5 additions & 1 deletion tests/integration/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

from sparkly.testing import SparklyGlobalSessionTest
from tests.integration.base import SparklyTestSession
from tests.integration.base import SparklyTestSession, SparklyTestSessionWithOldCatalog
from sparkly.catalog import read_db_properties_format


Expand Down Expand Up @@ -197,3 +197,7 @@ def test_read_db_properties_format_for_broken_input(self):

with self.assertRaises(ValueError):
read_db_properties_format(')')


class TestSparklyWithOldCatalog(TestSparklyCatalog):
session = SparklyTestSessionWithOldCatalog

0 comments on commit 2c5d50e

Please sign in to comment.