Skip to content

Commit

Permalink
fix: error around latest partition in BigQuery (apache#11274)
Browse files Browse the repository at this point in the history
* fix: error around latest partition in BigQuery

* lint

* Going with a backend-first approach

* fix test

* add an extra test
  • Loading branch information
mistercrunch authored Oct 21, 2020
1 parent c360413 commit 796a2a6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/components/TableElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class TableElement extends React.PureComponent {
/>
);
}
let latest = Object.entries(table.partitions.latest).map(
let latest = Object.entries(table.partitions?.latest || []).map(
([key, value]) => `${key}=${value}`,
);
latest = latest.join('/');
Expand Down
12 changes: 12 additions & 0 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ def get_datatype(cls, type_code: Any) -> Optional[str]:
return type_code.upper()
return None

@classmethod
def normalize_indexes(cls, indexes: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Normalizes indexes for more consistency across db engines
noop by default
:param indexes: Raw indexes as returned by SQLAlchemy
:return: cleaner, more aligned index definition
"""
return indexes

@classmethod
def extra_table_metadata(
cls, database: "Database", table_name: str, schema_name: str
Expand Down
19 changes: 19 additions & 0 deletions superset/db_engine_specs/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ def _truncate_label(cls, label: str) -> str:
"""
return "_" + hashlib.md5(label.encode("utf-8")).hexdigest()

@classmethod
def normalize_indexes(cls, indexes: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Normalizes indexes for more consistency across db engines
:param indexes: Raw indexes as returned by SQLAlchemy
:return: cleaner, more aligned index definition
"""
normalized_idxs = []
# Fixing a bug/behavior observed in pybigquery==0.4.15 where
# the index's `column_names` == [None]
# Here we're returning only non-None indexes
for ix in indexes:
column_names = ix.get("column_names") or []
ix["column_names"] = [col for col in column_names if col is not None]
if ix["column_names"]:
normalized_idxs.append(ix)
return normalized_idxs

@classmethod
def extra_table_metadata(
cls, database: "Database", table_name: str, schema_name: str
Expand Down
3 changes: 2 additions & 1 deletion superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ def get_columns(
def get_indexes(
self, table_name: str, schema: Optional[str] = None
) -> List[Dict[str, Any]]:
return self.inspector.get_indexes(table_name, schema)
indexes = self.inspector.get_indexes(table_name, schema)
return self.db_engine_spec.normalize_indexes(indexes)

def get_pk_constraint(
self, table_name: str, schema: Optional[str] = None
Expand Down
21 changes: 21 additions & 0 deletions tests/db_engine_specs/bigquery_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ def test_extra_table_metadata(self):
)
self.assertEqual(result, expected_result)

def test_normalize_indexes(self):
"""
DB Eng Specs (bigquery): Test extra table metadata
"""
indexes = [{"name": "partition", "column_names": [None], "unique": False}]
normalized_idx = BigQueryEngineSpec.normalize_indexes(indexes)
self.assertEqual(normalized_idx, [])

indexes = [{"name": "partition", "column_names": ["dttm"], "unique": False}]
normalized_idx = BigQueryEngineSpec.normalize_indexes(indexes)
self.assertEqual(normalized_idx, indexes)

indexes = [
{"name": "partition", "column_names": ["dttm", None], "unique": False}
]
normalized_idx = BigQueryEngineSpec.normalize_indexes(indexes)
self.assertEqual(
normalized_idx,
[{"name": "partition", "column_names": ["dttm"], "unique": False}],
)

def test_df_to_sql(self):
"""
DB Eng Specs (bigquery): Test DataFrame to SQL contract
Expand Down

0 comments on commit 796a2a6

Please sign in to comment.