Skip to content
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

Updates get_array_type_columns to handle object columns #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 23 additions & 12 deletions es/elastic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,29 @@ def get_array_type_columns(self, table_name: str) -> "Cursor":
raise exceptions.DataError(
f"Error inferring array type columns {self.url}: {e}"
)
for col_name, value in source.items():
# If it's a list (ES Array add to cursor)
if isinstance(value, list):
if len(value) > 0:
# If it's an array of objects add all keys
if isinstance(value[0], dict):
for in_col_name in value[0]:
array_columns.append((f"{col_name}.{in_col_name}",))
array_columns.append((f"{col_name}.{in_col_name}.keyword",))
continue
array_columns.append((col_name,))
array_columns.append((f"{col_name}.keyword",))
def _get_array_type_columns_recursive(source, prefix=""):
array_columns: List[Tuple[Any, ...]] = []
for col_name, value in source.items():
if prefix:
col_name = prefix + '.' + col_name
# If it's a list (ES Array add to cursor)
if isinstance(value, list):
if len(value) > 0:
# If it's an array of objects add all keys
if isinstance(value[0], dict):
for in_col_name in value[0]:
array_columns.append((f"{col_name}.{in_col_name}",))
array_columns.append((f"{col_name}.{in_col_name}.keyword",))
continue
array_columns.append((col_name,))
array_columns.append((f"{col_name}.keyword",))
# If it's an object, recurse over sub-columns
elif isinstance(value, dict):
array_columns += _get_array_type_columns_recursive(value, col_name)
return array_columns

array_columns = _get_array_type_columns_recursive(source)

if not array_columns:
array_columns = []
self.description = [
Expand Down