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

Use statement #936

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions evadb/catalog/catalog_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@
)
from evadb.catalog.sql_config import IDENTIFIER_COLUMN
from evadb.configuration.configuration_manager import ConfigurationManager
from evadb.executor.executor_utils import ExecutorError
from evadb.expression.function_expression import FunctionExpression
from evadb.expression.tuple_value_expression import TupleValueExpression
from evadb.parser.create_statement import ColConstraintInfo, ColumnDefinition
from evadb.utils.generic_utils import get_str_hash, remove_directory_contents


def generate_sqlalchemy_conn_str(engine: str, params: Dict[str, str]):
if engine == "postgres":
conn_str = f"""postgresql://{params["user"]}:{params["password"]}@{params["host"]}:{params["port"]}/{params["database"]}"""
else:
raise ExecutorError(f"Native engine: {engine} is not currently supported")
return conn_str


def is_video_table(table: TableCatalogEntry):
return table.table_type == TableType.VIDEO_DATA

Expand Down
4 changes: 4 additions & 0 deletions evadb/executor/plan_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
from evadb.executor.show_info_executor import ShowInfoExecutor
from evadb.executor.storage_executor import StorageExecutor
from evadb.executor.union_executor import UnionExecutor
from evadb.executor.use_executor import UseExecutor
from evadb.executor.vector_index_scan_executor import VectorIndexScanExecutor
from evadb.models.storage.batch import Batch
from evadb.parser.create_statement import CreateDatabaseStatement
from evadb.parser.statement import AbstractStatement
from evadb.parser.use_statement import UseStatement
from evadb.plan_nodes.abstract_plan import AbstractPlan
from evadb.plan_nodes.types import PlanOprType
from evadb.utils.logging_manager import logger
Expand Down Expand Up @@ -86,6 +88,8 @@ def _build_execution_tree(
# First handle cases when the plan is actually a parser statement
if isinstance(plan, CreateDatabaseStatement):
return CreateDatabaseExecutor(db=self._db, node=plan)
elif isinstance(plan, UseStatement):
return UseExecutor(db=self._db, node=plan)

# Get plan node type
plan_opr_type = plan.opr_type
Expand Down
50 changes: 50 additions & 0 deletions evadb/executor/use_executor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding=utf-8
# Copyright 2018-2023 EvaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Iterator

import pandas as pd
from sqlalchemy import create_engine

from evadb.catalog.catalog_utils import generate_sqlalchemy_conn_str
from evadb.database import EvaDBDatabase
from evadb.executor.abstract_executor import AbstractExecutor
from evadb.models.storage.batch import Batch
from evadb.plan_nodes.native_plan import SQLAlchemyPlan


class UseExecutor(AbstractExecutor):
def __init__(self, db: EvaDBDatabase, node: SQLAlchemyPlan):
super().__init__(db, node)
self._database_name = node.database_name
self._query_string = node.query_string

def exec(self, *args, **kwargs) -> Iterator[Batch]:
db_catalog_entry = self.db.catalog().get_database_catalog_entry(
self._database_name
)

conn_str = generate_sqlalchemy_conn_str(
db_catalog_entry.engine,
db_catalog_entry.params,
)

engine = create_engine(conn_str)

with engine.connect() as con:
if "SELECT" in self._query_string or "select" in self._query_string:
yield Batch(pd.read_sql(self._query_string, engine))
else:
con.execute(self._query_string)
yield Batch(pd.DataFrame({"status": ["Ok"]}))
1 change: 1 addition & 0 deletions evadb/optimizer/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class OperatorType(IntEnum):
LOGICAL_APPLY_AND_MERGE = auto()
LOGICAL_EXTRACT_OBJECT = auto()
LOGICAL_VECTOR_INDEX_SCAN = auto()
LOGICAL_USE = auto()
LOGICALDELIMITER = auto()


Expand Down
14 changes: 13 additions & 1 deletion evadb/parser/evadb.lark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

start: (sql_statement? ";")+

sql_statement: ddl_statement | dml_statement | utility_statement
sql_statement: ddl_statement | dml_statement | utility_statement | context_statement

ddl_statement: create_database | create_table | create_index | create_udf
| drop_database | drop_table | drop_udf | drop_index | rename_table
Expand All @@ -12,6 +12,8 @@ dml_statement: select_statement | insert_statement | update_statement

utility_statement: describe_statement | show_statement | help_statement | explain_statement

context_statement: use_statement

// Data Definition Language

// Create statements
Expand Down Expand Up @@ -172,11 +174,19 @@ explain_statement: EXPLAIN explainable_statement

explainable_statement : select_statement | insert_statement | update_statement | delete_statement | create_table

// Context Statements

use_statement: USE database_name "{" query_string "}" // One shortcoming that query string cannot have parenthesis
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the limitation of the lark or ? Why we can not have parenthesis?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove this comment. The native query can have parenthesis after I change to use curly bracket if that makes sense

// now
USE postgres {
   // some query
}

// before
USE postgres (
    // some query
)

If I use the parenthesis and the native query has arbitrary parentheses as well, I have some trouble of coming up with a grammar that works for all cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me take care of it


// Common Clauses

// DB Objects

query_string: QUERY_STRING

full_id: uid dotted_id?

database_name: full_id

table_name: full_id

Expand Down Expand Up @@ -360,6 +370,7 @@ PARAMETERS: "PARAMETERS"i
PRIMARY: "PRIMARY"i
REFERENCES: "REFERENCES"i
RENAME: "RENAME"i
USE: "USE"i
SAMPLE: "SAMPLE"i
IFRAMES: "IFRAMES"i
AUDIORATE: "AUDIORATE"i
Expand Down Expand Up @@ -556,6 +567,7 @@ ID_LITERAL: /[A-Za-z_$0-9]*?[A-Za-z_$]+?[A-Za-z_$0-9]*/
DQUOTA_STRING: /"[^";]*"/
SQUOTA_STRING: /'[^';]*'/
BQUOTA_STRING: /`[^'`]*`/
QUERY_STRING: /[^{};]+/
DEC_DIGIT: /[0-9]/

// LARK
Expand Down
2 changes: 2 additions & 0 deletions evadb/parser/lark_visitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from evadb.parser.lark_visitor._select_statement import Select
from evadb.parser.lark_visitor._show_statements import Show
from evadb.parser.lark_visitor._table_sources import TableSources
from evadb.parser.lark_visitor._use_statement import Use

# To add new functionality to the parser, create a new file under
# the lark_visitor directory, and implement a new class which
Expand Down Expand Up @@ -70,6 +71,7 @@ class LarkInterpreter(
Show,
Explain,
Delete,
Use,
):
def __init__(self, query):
super().__init__()
Expand Down
28 changes: 28 additions & 0 deletions evadb/parser/lark_visitor/_use_statement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding=utf-8
# Copyright 2018-2023 EvaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from lark import Tree

from evadb.parser.use_statement import UseStatement


class Use:
def use_statement(self, tree):
for child in tree.children:
if isinstance(child, Tree):
if child.data == "database_name":
database_name = self.visit(child)
if child.data == "query_string":
query_string = self.visit(child)
return UseStatement(database_name, query_string)
1 change: 1 addition & 0 deletions evadb/parser/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class StatementType(EvaDBEnum):
EXPLAIN # noqa: F821
CREATE_INDEX # noqa: F821
CREATE_DATABASE # noqa: F821
USE # noqa: F821
# add other types


Expand Down
53 changes: 53 additions & 0 deletions evadb/parser/use_statement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# coding=utf-8
# Copyright 2018-2023 EvaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from evadb.parser.statement import AbstractStatement
from evadb.parser.types import StatementType


class UseStatement(AbstractStatement):
def __init__(self, database_name: str, query_string: str):
super().__init__(StatementType.USE)
self._database_name = database_name
self._query_string = query_string

@property
def database_name(self):
return self._database_name

@property
def query_string(self):
return self._query_string

def __str__(self):
return f"USE {self.database_name} ({self.query_string})"

def __eq__(self, other: object) -> bool:
if not isinstance(other, UseStatement):
return False
return (
self.database_name == other.database_name
and self.query_string == other.query_string
)

def __hash__(self) -> int:
return hash(
(
super().__hash__(),
self.database_name,
self.query_string,
)
)
3 changes: 2 additions & 1 deletion evadb/parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
from evadb.parser.select_statement import SelectStatement
from evadb.parser.show_statement import ShowStatement
from evadb.parser.types import ObjectType
from evadb.parser.use_statement import UseStatement

# List of statements for which we omit binder and optimizer and pass the statement
# directly to the executor.
SKIP_BINDER_AND_OPTIMIZER_STATEMENTS = (CreateDatabaseStatement,)
SKIP_BINDER_AND_OPTIMIZER_STATEMENTS = (CreateDatabaseStatement, UseStatement)


def parse_expression(expr: str):
Expand Down
50 changes: 50 additions & 0 deletions evadb/plan_nodes/native_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding=utf-8
# Copyright 2018-2023 EvaDB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from evadb.plan_nodes.abstract_plan import AbstractPlan
from evadb.plan_nodes.types import PlanOprType


class NativePlan(AbstractPlan):
"""
This plan is used for pushing down query string directly to
backend database engine.
"""

def __init__(self, plan_type: PlanOprType, database_name: str, query_string: str):
self._database_name = database_name
self._query_string = query_string
super().__init__(plan_type)

@property
def database_name(self):
return self._database_name

@property
def query_string(self):
return self._query_string

def __str__(self):
return "NativePlan(database_name={}, query_string={})".format(
self._database_name,
self._query_string,
)

def __hash__(self) -> int:
return hash((super().__hash__(), self._database_name, self._query_string))


class SQLAlchemyPlan(NativePlan):
def __init__(self, database_name: str, query_string: str):
super().__init__(PlanOprType.SQLALCHEMY, database_name, query_string)
2 changes: 2 additions & 0 deletions evadb/plan_nodes/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ class PlanOprType(Enum):
CREATE_INDEX = auto()
APPLY_AND_MERGE = auto()
VECTOR_INDEX_SCAN = auto()
NATIVE = auto()
SQLALCHEMY = auto()
# add other types
Loading