Skip to content

Commit

Permalink
fix: crash with empty indexes (#39)
Browse files Browse the repository at this point in the history
* fix: crash with empty indexes

* no cols no meta

* codecov check

* Update es/basesqlalchemy.py

Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com>

* black it

Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com>
  • Loading branch information
dpgaspar and villebro committed Jan 5, 2021
1 parent b7cdac4 commit 8c1bb37
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
11 changes: 9 additions & 2 deletions es/basesqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import unicode_literals

import logging
from typing import List

import es
from es import exceptions
Expand Down Expand Up @@ -145,10 +146,16 @@ def get_schema_names(self, connection, **kwargs):
def has_table(self, connection, table_name, schema=None):
return table_name in self.get_table_names(connection, schema)

def get_table_names(self, connection, schema=None, **kwargs):
def get_table_names(self, connection, schema=None, **kwargs) -> List[str]:
query = "SHOW TABLES"
result = connection.execute(query)
return [row.name for row in result if row.name[0] != "."]
# return a list of table names exclude hidden and empty indexes
return [
table.name
for table in result
if table.name[0] != "."
and len(self.get_columns(connection, table.name)) > 0
]

def get_view_names(self, connection, schema=None, **kwargs):
return []
Expand Down
27 changes: 15 additions & 12 deletions es/elastic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
from __future__ import unicode_literals

import re
from typing import Dict
from typing import Any, Dict, Optional

from elasticsearch import Elasticsearch, exceptions as es_exceptions
from es import exceptions
from es.baseapi import apply_parameters, BaseConnection, BaseCursor, check_closed, Type


def connect(
host="localhost",
port=9200,
path="",
scheme="http",
user=None,
password=None,
context=None,
**kwargs,
host: str = "localhost",
port: int = 9200,
path: str = "",
scheme: str = "http",
user: Optional[str] = None,
password: Optional[str] = None,
context: Optional[Dict] = None,
**kwargs: Any,
):
"""
Constructor for creating a connection to the database.
Expand Down Expand Up @@ -156,7 +156,7 @@ def get_array_type_columns(self, table_name: str) -> "Cursor":
"""
array_columns = []
try:
resp = self.es.search(index=table_name, size=1)
response = self.es.search(index=table_name, size=1)
except es_exceptions.ConnectionError as e:
raise exceptions.OperationalError(
f"Error connecting to {self.url}: {e.info}"
Expand All @@ -166,12 +166,15 @@ def get_array_type_columns(self, table_name: str) -> "Cursor":
f"Error ({e.error}): {e.info['error']['reason']}"
)
try:
_source = resp["hits"]["hits"][0]["_source"]
if response["hits"]["total"]["value"] == 0:
source = {}
else:
source = response["hits"]["hits"][0]["_source"]
except KeyError as e:
raise exceptions.DataError(
f"Error inferring array type columns {self.url}: {e}"
)
for col_name, value in _source.items():
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:
Expand Down
17 changes: 9 additions & 8 deletions es/opendistro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import csv
import re
from typing import Any, Dict, Optional # pragma: no cover

from elasticsearch import Elasticsearch
from es import exceptions
Expand All @@ -13,14 +14,14 @@


def connect(
host="localhost",
port=443,
path="",
scheme="https",
user=None,
password=None,
context=None,
**kwargs,
host: str = "localhost",
port: int = 443,
path: str = "",
scheme: str = "https",
user: Optional[str] = None,
password: Optional[str] = None,
context: Optional[Dict] = None,
**kwargs: Any,
): # pragma: no cover
"""
Constructor for creating a connection to the database.
Expand Down
4 changes: 4 additions & 0 deletions es/tests/fixtures/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ def import_flights(base_url):
def import_data1(base_url):
path = os.path.join(os.path.dirname(__file__), "data1.json")
import_file_to_es(base_url, path, "data1")


def import_empty_index(base_url):
set_index_replica_zero(base_url, "empty_index")
11 changes: 10 additions & 1 deletion es/tests/test_0_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os
import unittest

from .fixtures.fixtures import delete_index, import_data1, import_flights
from .fixtures.fixtures import (
delete_index,
import_data1,
import_empty_index,
import_flights,
)

BASE_URL = "http://localhost:9200"

Expand All @@ -17,3 +22,7 @@ def test_data_flights(self):
def test_data_data1(self):
delete_index(self.base_url, "data1")
import_data1(self.base_url)

def test_data_empty_index(self):
delete_index(self.base_url, "empty_index")
import_empty_index(self.base_url)

0 comments on commit 8c1bb37

Please sign in to comment.