Skip to content

Commit

Permalink
Merge pull request #370 from mindsdb/staging
Browse files Browse the repository at this point in the history
Release ...
  • Loading branch information
ea-rus authored Apr 15, 2024
2 parents 0e2fb2a + 2ecdae0 commit acda26f
Show file tree
Hide file tree
Showing 6 changed files with 402 additions and 355 deletions.
2 changes: 1 addition & 1 deletion mindsdb_sql/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = 'mindsdb_sql'
__package_name__ = 'mindsdb_sql'
__version__ = '0.12.0'
__version__ = '0.13.0'
__description__ = "Pure python SQL parser"
__email__ = "[email protected]"
__author__ = 'MindsDB Inc'
Expand Down
26 changes: 17 additions & 9 deletions mindsdb_sql/parser/dialects/mindsdb/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,28 @@ def to_tree(self, *args, level=0, **kwargs):
return out_str

def get_string(self, *args, **kwargs):
params = self.params.copy()
using_ar = [f"{k}={repr(v)}" for k, v in params.items()]
using_str = ", ".join(using_ar)
from_query_str = (
f"FROM ({self.from_query.get_string()})" if self.from_query else ""
)
storage_str = f" STORAGE = {self.storage.to_string()}" if self.storage else ""
model_str = f" MODEL = {self.model.to_string()},\n" if self.model else ""

using_ar = []
if self.storage:
using_ar.append(f" STORAGE={self.storage.to_string()}")
if self.model:
using_ar.append(f" MODEL={self.model.to_string()}")

params = self.params.copy()
if params:
using_ar += [f"{k}={repr(v)}" for k, v in params.items()]
if using_ar:
using_str = "USING " + ", ".join(using_ar)
else:
using_str = ""

out_str = (
f"CREATE KNOWLEDGE_BASE {'IF NOT EXISTS' if self.if_not_exists else ''}{self.name.to_string()} "
f"CREATE KNOWLEDGE_BASE {'IF NOT EXISTS ' if self.if_not_exists else ''}{self.name.to_string()} "
f"{from_query_str} "
f"USING {using_str},"
f"{model_str}{storage_str}"
f"{using_str}"
)

return out_str
Expand Down Expand Up @@ -95,5 +103,5 @@ def to_tree(self, *args, level=0, **kwargs):
return out_str

def get_string(self, *args, **kwargs):
out_str = f'DROP KNOWLEDGE_BASE {"IF EXISTS" if self.if_exists else ""}{self.name.to_string()}'
out_str = f'DROP KNOWLEDGE_BASE {"IF EXISTS " if self.if_exists else ""}{self.name.to_string()}'
return out_str
19 changes: 16 additions & 3 deletions mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,23 @@ def expr_list_set(self, p):
def describe(self, p):
return Describe(value=p.identifier)

@_('DESCRIBE PREDICTOR identifier',
'DESCRIBE MODEL identifier')
@_('DESCRIBE JOB identifier',
'DESCRIBE SKILL identifier',
'DESCRIBE CHATBOT identifier',
'DESCRIBE TRIGGER identifier',
'DESCRIBE KNOWLEDGE_BASE identifier',
'DESCRIBE PROJECT identifier',
'DESCRIBE ML_ENGINE identifier',
'DESCRIBE identifier identifier',
)
def describe(self, p):
return Describe(value=p.identifier, type='predictor')
if isinstance(p[1], Identifier):
type = p[1].parts[-1]
else:
type = p[1]
type = type.replace(' ', '_')
return Describe(value=p[2], type=type)


# USE

Expand Down
16 changes: 16 additions & 0 deletions mindsdb_sql/render/sqlalchemy_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,29 @@ def prepare_update(self, ast_query):

return stmt

def prepare_delete(self, ast_query: ast.Delete):
schema, table_name = self.get_table_name(ast_query.table)

columns = []

table = sa.table(table_name, schema=schema, *columns)

stmt = table.delete()

if ast_query.where is not None:
stmt = stmt.where(self.to_expression(ast_query.where))

return stmt

def get_query(self, ast_query):
if isinstance(ast_query, ast.Select):
stmt = self.prepare_select(ast_query)
elif isinstance(ast_query, ast.Insert):
stmt = self.prepare_insert(ast_query)
elif isinstance(ast_query, ast.Update):
stmt = self.prepare_update(ast_query)
elif isinstance(ast_query, ast.Delete):
stmt = self.prepare_delete(ast_query)
elif isinstance(ast_query, ast.CreateTable):
stmt = self.prepare_create_table(ast_query)
elif isinstance(ast_query, ast.DropTables):
Expand Down
21 changes: 17 additions & 4 deletions tests/test_parser/test_base_sql/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,52 @@ class TestDescribeMindsdb:
def test_describe_predictor(self):
sql = "DESCRIBE PREDICTOR my_identifier"
ast = parse_sql(sql, dialect='mindsdb')
expected_ast = Describe(type='predictor', value=Identifier('my_identifier'))
expected_ast = Describe(type='PREDICTOR', value=Identifier('my_identifier'))

assert str(ast) == str(expected_ast)
assert ast.to_tree() == expected_ast.to_tree()

sql = "DESCRIBE MODEL my_identifier"
ast = parse_sql(sql, dialect='mindsdb')

expected_ast = Describe(type='MODEL', value=Identifier('my_identifier'))
assert str(ast) == str(expected_ast)
assert ast.to_tree() == expected_ast.to_tree()

# describe attr
sql = "DESCRIBE MODEL pred.attr"
ast = parse_sql(sql, dialect='mindsdb')

expected_ast = Describe(type='predictor', value=Identifier(parts=['pred', 'attr']))
expected_ast = Describe(type='MODEL', value=Identifier(parts=['pred', 'attr']))

assert str(ast) == str(expected_ast)

# version
sql = "DESCRIBE MODEL pred.11"
ast = parse_sql(sql, dialect='mindsdb')

expected_ast = Describe(type='predictor', value=Identifier(parts=['pred', '11']))
expected_ast = Describe(type='MODEL', value=Identifier(parts=['pred', '11']))

assert str(ast) == str(expected_ast)

# version and attr
sql = "DESCRIBE MODEL pred.11.attr"
ast = parse_sql(sql, dialect='mindsdb')

expected_ast = Describe(type='predictor', value=Identifier(parts=['pred', '11', 'attr']))
expected_ast = Describe(type='MODEL', value=Identifier(parts=['pred', '11', 'attr']))

assert str(ast) == str(expected_ast)

# other objects
for obj in (
"AGENT", "JOB", "SKILL", "CHATBOT", "TRIGGER", "VIEW",
"KNOWLEDGE_BASE", "KNOWLEDGE BASE", "PREDICTOR", "MODEL",
'database', 'project', 'handler', 'ml_engine'):

sql = f"DESCRIBE {obj} aaa"
ast = parse_sql(sql, dialect='mindsdb')

obj = obj.replace(' ', '_')
expected_ast = Describe(type=obj, value=Identifier(parts=['aaa']))

assert str(ast) == str(expected_ast)
Loading

0 comments on commit acda26f

Please sign in to comment.