Skip to content

Commit

Permalink
support create table from select api
Browse files Browse the repository at this point in the history
  • Loading branch information
yulaicui committed Jul 8, 2023
1 parent d9a10de commit 0a9e475
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
5 changes: 2 additions & 3 deletions apps/youtube_qa/youtube_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ def generate_online_video_transcript(cursor: evadb.EvaDBCursor) -> str:

# extract speech texts from videos
cursor.drop_table("youtube_video_text", if_exists=True).execute()
cursor.query(
"CREATE TABLE IF NOT EXISTS youtube_video_text AS SELECT SpeechRecognizer(audio) FROM youtube_video;"
).execute()
speech_recognizer_rel = cursor.table("youtube_video").select("SpeechRecognizer(audio)")
cursor.create_table("youtube_video_text", query=speech_recognizer_rel).execute()
print("✅ Video analysis completed.")

raw_transcript_string = (
Expand Down
18 changes: 12 additions & 6 deletions evadb/interfaces/relational/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from evadb.parser.select_statement import SelectStatement
from evadb.parser.utils import (
parse_create_table,
parse_create_table_from_select,
parse_create_udf,
parse_create_vector_index,
parse_drop_index,
Expand Down Expand Up @@ -390,20 +391,21 @@ def create_function(
return EvaDBQuery(self._evadb, stmt)

def create_table(
self, table_name: str, if_not_exists: bool = True, columns: str = None, **kwargs
self, table_name: str, if_not_exists: bool = True, columns: str = None, query: EvaDBQuery = None, **kwargs
) -> "EvaDBQuery":
"""
Create a udf in the database.
Args:
udf_name (str): Name of the udf to be created.
table_name (str): Name of table to be created.
if_not_exists (bool): If True, do not raise an error if the UDF already exist. If False, raise an error.
impl_path (str): Path string to udf's implementation.
columns (str): Column metadata of the table.
query (EvaDBQuery): Query, only filled when creating table from select query.
type (str): Type of the udf (e.g. HuggingFace).
**kwargs: Additional keyword arguments for configuring the create udf operation.
**kwargs: Additional keyword arguments for configuring the create table operation.
Returns:
EvaDBQuery: The EvaDBQuery object representing the UDF created.
EvaDBQuery: The EvaDBQuery object representing the table created.
Examples:
>>> cursor.create_table("MyCSV", if_exists = True, columns=\"\"\"
Expand All @@ -418,7 +420,11 @@ def create_table(
0
0 Table Successfully created: MyCSV
"""
stmt = parse_create_table(table_name, if_not_exists, columns, **kwargs)
if query is None:
stmt = parse_create_table(table_name, if_not_exists, columns, **kwargs)
else:
select_query = query.sql_query()
stmt = parse_create_table_from_select(table_name, if_not_exists, select_query, **kwargs)
return EvaDBQuery(self._evadb, stmt)

def query(self, sql_query: str) -> EvaDBQuery:
Expand Down
10 changes: 10 additions & 0 deletions evadb/parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ def parse_create_table(table_name: str, if_not_exists: bool, columns: str, **kwa
assert isinstance(stmt, CreateTableStatement), "Expected a create table statement"
return stmt

def parse_create_table_from_select(table_name: str, if_not_exists: bool, query: str, **kwargs):
mock_query = (
f"CREATE TABLE IF NOT EXISTS {table_name} AS {query};"
if if_not_exists
else f"CREATE TABLE AS {query};"
)
stmt = Parser().parse(mock_query)[0]
assert isinstance(stmt, CreateTableStatement), "Expected a create table statement"
return stmt


def parse_show(show_type: str, **kwargs):
mock_query = f"SHOW {show_type};"
Expand Down
26 changes: 26 additions & 0 deletions test/interfaces/relational/test_relational_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,32 @@ def test_create_table_relational(self):
with self.assertRaises(ExecutorError):
rel.execute()

def test_create_table_from_select_relational(self):
video_file_path = create_sample_video(10)

cursor = self.conn.cursor()
# load video
rel = cursor.load(
video_file_path,
table_name="dummy_video",
format="video",
)
rel.execute()

# Create dummy udf
create_dummy_object_detector_udf = cursor.create_function(
"DummyObjectDetector", if_not_exists=True, impl_path="test/util.py"
)
create_dummy_object_detector_udf.execute()

# Check create table from select relation
select_query_sql_rel = cursor.table("dummy_video").select("id, DummyObjectDetector(data)")
cursor.drop_table("dummy_objects", if_exists=True)
cursor.create_table("dummy_objects", query=select_query_sql_rel).execute()

table_df = cursor.table("dummy_objects").select("id").df()
self.assertTrue(table_df is not None)


if __name__ == "__main__":
unittest.main()

0 comments on commit 0a9e475

Please sign in to comment.