Skip to content

Commit

Permalink
bug: fixed if_not_exists for native table
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav274 committed Sep 26, 2023
1 parent 7dc410a commit 6b3f267
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 103 deletions.
36 changes: 10 additions & 26 deletions evadb/binder/binder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,19 @@ def check_data_source_and_table_are_valid(
Validate the database is valid and the requested table in database is
also valid.
"""
db_catalog_entry = catalog.get_database_catalog_entry(database_name)

if db_catalog_entry is not None:
with get_database_handler(
db_catalog_entry.engine, **db_catalog_entry.params
) as handler:
# Get table definition.
resp = handler.get_tables()

if resp.error is not None:
error = "There is no table in data source {}. Create the table using native query.".format(
database_name,
)
logger.error(error)
raise BinderError(error)

# Check table existence.
table_df = resp.data
if table_name not in table_df["table_name"].values:
error = "Table {} does not exist in data source {}. Create the table using native query.".format(
table_name,
database_name,
)
logger.error(error)
raise BinderError(error)
else:
error = None
if catalog.get_database_catalog_entry(database_name) is None:
error = "{} data source does not exist. Create the new database source using CREATE DATABASE.".format(
database_name,
)

if not catalog.check_table_exists(table_name, database_name):
error = "Table {} does not exist in data source {}. Create the table using native query.".format(
table_name,
database_name,
)

if error:
logger.error(error)
raise BinderError(error)

Expand Down
42 changes: 36 additions & 6 deletions evadb/catalog/catalog_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from evadb.parser.create_statement import ColumnDefinition
from evadb.parser.table_ref import TableInfo
from evadb.parser.types import FileFormatType
from evadb.third_party.databases.interface import get_database_handler
from evadb.utils.generic_utils import generate_file_path, get_file_checksum
from evadb.utils.logging_manager import logger

Expand Down Expand Up @@ -174,6 +175,33 @@ def drop_database_catalog_entry(self, database_entry: DatabaseCatalogEntry) -> b
# taken care by the underlying db
return self._db_catalog_service.delete_entry(database_entry)

def check_native_table_exists(self, table_name: str, database_name: str):
"""
Validate the database is valid and the requested table in database is
also valid.
"""
db_catalog_entry = self.get_database_catalog_entry(database_name)

if db_catalog_entry is None:
return False

with get_database_handler(
db_catalog_entry.engine, **db_catalog_entry.params
) as handler:
# Get table definition.
resp = handler.get_tables()

if resp.error is not None:
return False

# Check table existence.
table_df = resp.data
print(table_df)
if table_name not in table_df["table_name"].values:
return False

return True

"Table catalog services"

def insert_table_catalog_entry(
Expand Down Expand Up @@ -249,13 +277,15 @@ def rename_table_catalog_entry(
return self._table_catalog_service.rename_entry(curr_table, new_name.table_name)

def check_table_exists(self, table_name: str, database_name: str = None):
table_entry = self._table_catalog_service.get_entry_by_name(
database_name, table_name
)
if table_entry is None:
return False
is_native_table = database_name is not None

if is_native_table:
return self.check_native_table_exists(table_name, database_name)
else:
return True
table_entry = self._table_catalog_service.get_entry_by_name(
database_name, table_name
)
return table_entry is not None

def get_all_table_catalog_entries(self):
return self._table_catalog_service.get_all_entries()
Expand Down
10 changes: 4 additions & 6 deletions evadb/executor/create_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ def __init__(self, db: EvaDBDatabase, node: CreatePlan):
def exec(self, *args, **kwargs):
# create a table in the ative database if set
is_native_table = self.node.table_info.database_name is not None
check_if_exists = False
# if exists only supported for evadb tables
if not is_native_table:
check_if_exists = handle_if_not_exists(
self.catalog(), self.node.table_info, self.node.if_not_exists
)

check_if_exists = handle_if_not_exists(
self.catalog(), self.node.table_info, self.node.if_not_exists
)

if not check_if_exists:
create_table_done = False
Expand Down
3 changes: 0 additions & 3 deletions evadb/storage/native_storage_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ def write(self, table: TableCatalogEntry, rows: Batch):
logger.exception(err_msg)
raise Exception(err_msg)

def create(self, table: TableCatalogEntry):
pass

def read(self, table: TableCatalogEntry) -> Iterator[Batch]:
try:
db_catalog_entry = self._get_database_catalog_entry(table.database_name)
Expand Down
62 changes: 0 additions & 62 deletions test/third_party_tests/test_advanced_queries_on_native_db.py

This file was deleted.

0 comments on commit 6b3f267

Please sign in to comment.