-
Notifications
You must be signed in to change notification settings - Fork 265
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
Use statement #936
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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