Skip to content

Commit

Permalink
Merge pull request #368 from mindsdb/staging
Browse files Browse the repository at this point in the history
Release 0.12.0
  • Loading branch information
ea-rus authored Mar 20, 2024
2 parents d4ae877 + 6ab3008 commit 0e2fb2a
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 185 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.11.0'
__version__ = '0.12.0'
__description__ = "Pure python SQL parser"
__email__ = "[email protected]"
__author__ = 'MindsDB Inc'
Expand Down
12 changes: 7 additions & 5 deletions mindsdb_sql/planner/plan_join_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ def plan_timeseries_predictor(self, query, table, predictor_namespace, predictor
allowed_columns = [predictor_time_column_name.lower()]
if len(predictor_group_by_names) > 0:
allowed_columns += [i.lower() for i in predictor_group_by_names]
validate_ts_where_condition(query.where, allowed_columns=allowed_columns)

time_filter = find_time_filter(query.where, time_column_name=predictor_time_column_name)
no_time_filter_query = copy.deepcopy(query)

order_by = [OrderBy(Identifier(parts=[predictor_time_column_name]), direction='DESC')]
preparation_where = no_time_filter_query.where

validate_ts_where_condition(preparation_where, allowed_columns=allowed_columns)

preparation_where = copy.deepcopy(query.where)
time_filter = find_time_filter(preparation_where, time_column_name=predictor_time_column_name)

order_by = [OrderBy(Identifier(parts=[predictor_time_column_name]), direction='DESC')]

query_modifiers = query.modifiers

Expand Down Expand Up @@ -342,7 +345,6 @@ def add_order_not_null(condition):
steps=[self.planner.get_integration_select_step(s) for s in integration_selects], reduce='union')

# get groping values
no_time_filter_query = copy.deepcopy(query)
no_time_filter_query.where = find_and_remove_time_filter(no_time_filter_query.where, time_filter)
select_partitions_step = self.plan_fetch_timeseries_partitions(no_time_filter_query, table, predictor_group_by_names)

Expand Down
2 changes: 0 additions & 2 deletions mindsdb_sql/planner/query_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ def _prepare_integration_select(node, is_table, is_target, parent_query, **kwarg
if node.parts[:len(prefix)] != prefix:
raise PlanningException(f'Tried to query column {node.to_string()} from table'
f' {table.to_string()}, but a different table name has been specified.')
else:
node.parts = prefix + node.parts

# keep column name for target
if is_target:
Expand Down
2 changes: 2 additions & 0 deletions mindsdb_sql/planner/ts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def validate_ts_where_condition(op, allowed_columns, allow_and=True):
if arg.parts[-1].lower() not in allowed_columns:
raise PlanningException(
f'For time series predictor only the following columns are allowed in WHERE: {str(allowed_columns)}, found instead: {str(arg)}.')
# remove alias
arg.parts = [arg.parts[-1]]

if isinstance(op.args[0], Operation):
validate_ts_where_condition(op.args[0], allowed_columns, allow_and=True)
Expand Down
16 changes: 15 additions & 1 deletion mindsdb_sql/render/sqlalchemy_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy.dialects import mysql, postgresql, sqlite, mssql, oracle
from sqlalchemy.schema import CreateTable, DropTable
from sqlalchemy.sql import ColumnElement
from sqlalchemy.sql import functions as sa_fnc

from mindsdb_sql.parser import ast

Expand Down Expand Up @@ -114,7 +115,20 @@ def to_expression(self, t):
alias = str(t.value)
col = col.label(alias)
elif isinstance(t, ast.Identifier):
col = self.to_column(t.parts)
# sql functions
col = None
if len(t.parts) == 1:
name = t.parts[0].upper()
if name == 'CURRENT_DATE':
col = sa_fnc.current_date()
elif name == 'CURRENT_TIME':
col = sa_fnc.current_time()
elif name == 'CURRENT_TIMESTAMP':
col = sa_fnc.current_timestamp()
elif name == 'CURRENT_USER':
col = sa_fnc.current_user()
if col is None:
col = self.to_column(t.parts)
if t.alias:
col = col.label(self.get_alias(t.alias))
elif isinstance(t, ast.Select):
Expand Down
84 changes: 42 additions & 42 deletions tests/test_planner/test_integration_select.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tests/test_planner/test_join_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_subselect(self):
default_namespace='mindsdb',
steps=[
FetchDataframeStep(integration='int',
query=parse_sql('select covid.col as col from covid limit 10')),
query=parse_sql('select col as col from covid limit 10')),
SubSelectStep(query=Select(targets=[Star()]), dataframe=Result(0), table_name='t'),
ApplyPredictorStep(namespace='mindsdb', dataframe=Result(1), predictor=Identifier('pred')),
JoinStep(left=Result(1), right=Result(2),
Expand Down Expand Up @@ -700,11 +700,11 @@ def test_complex_subselect(self):
steps=[
# nested queries
FetchDataframeStep(integration='int',
query=parse_sql('select tab0.a as a from tab0 where tab0.x=0')),
query=parse_sql('select a as a from tab0 where x=0')),
FetchDataframeStep(integration='int',
query=parse_sql('select tab3.a as a from tab3 where tab3.x=3')),
query=parse_sql('select a as a from tab3 where x=3')),
FetchDataframeStep(integration='int',
query=parse_sql('select tab4.a as a from tab4 where tab4.x=4')),
query=parse_sql('select a as a from tab4 where x=4')),
# tables
FetchDataframeStep(integration='int',
query=parse_sql('select * from tab1 as t1 where b=1')),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_planner/test_mindsdb_predictors_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ def test_predictors_select_plan(self):
expected_plan = QueryPlan(integrations=['mindsdb'],
steps=[
FetchDataframeStep(integration='mindsdb',
query=Select(targets=[Identifier('predictors.column1', alias=Identifier('column1')),
query=Select(targets=[Identifier('column1', alias=Identifier('column1')),
Constant(1),
NullConstant(),
Function('database', args=[]),
],
from_table=Identifier('predictors'),
where=BinaryOperation('and', args=[
BinaryOperation('=',
args=[Identifier('predictors.column1'),
Identifier('predictors.column2')]),
args=[Identifier('column1'),
Identifier('column2')]),
BinaryOperation('>',
args=[Identifier('predictors.column3'),
args=[Identifier('column3'),
Constant(0)]),
])
),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_planner/test_plan_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def test_plan_union_queries(self):
steps=[
# Query 1
FetchDataframeStep(integration='int',
query=Select(targets=[Identifier('tab.column1', alias=Identifier('column1')),
query=Select(targets=[Identifier('column1', alias=Identifier('column1')),
Constant(None, alias=Identifier('predicted'))],
from_table=Identifier('tab'),
where=BinaryOperation('and', args=[
BinaryOperation('=',
args=[Identifier('tab.column1'),
Identifier('tab.column2')]),
args=[Identifier('column1'),
Identifier('column2')]),
BinaryOperation('>',
args=[Identifier('tab.column3'),
args=[Identifier('column3'),
Constant(0)]),
])
)),
Expand Down
1 change: 1 addition & 0 deletions tests/test_planner/test_prepared_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def execute(self, step):
{'name': 'column1', 'type': 'str'},
{'name': 'column2', 'type': 'str'},
{'name': 'column3', 'type': 'str'},
{'name': 'col1', 'type': 'str'},
{'name': 'asset', 'type': 'float'},
{'name': 'time', 'type': 'datetime'},
{'name': 'predicted', 'type': 'float'},
Expand Down
14 changes: 7 additions & 7 deletions tests/test_planner/test_select_from_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_select_from_predictor_subselect(self):
steps=[
FetchDataframeStep(
integration='int1',
query=parse_sql('select t1.id as id from t1'),
query=parse_sql('select id as id from t1'),
),
ApplyPredictorRowStep(
namespace='mindsdb',
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_select_from_view_subselect(self):
steps=[
FetchDataframeStep(
integration='int1',
query=parse_sql('select tab1.id as id from tab1'),
query=parse_sql('select id as id from tab1'),
),
FetchDataframeStep(
integration='mindsdb',
Expand All @@ -286,7 +286,7 @@ def test_select_from_view_subselect(self):
where=BinaryOperation(
op='in',
args=[
Identifier(parts=['v1', 'x1']),
Identifier(parts=['x1']),
Parameter(Result(0))
]
)
Expand All @@ -308,7 +308,7 @@ def test_select_from_view_subselect(self):
def test_select_from_view_subselect_view(self):
query = parse_sql('''
select * from v1
where x1 in (select id from v2)
where x1 in (select v2.id from v2)
''', dialect='mindsdb')

expected_plan = QueryPlan(
Expand All @@ -326,7 +326,7 @@ def test_select_from_view_subselect_view(self):
where=BinaryOperation(
op='in',
args=[
Identifier(parts=['v1', 'x1']),
Identifier(parts=['x1']),
Parameter(Result(0))
]
)
Expand Down Expand Up @@ -408,7 +408,7 @@ def test_using_predictor_in_subselect(self):
where=BinaryOperation(
op='=',
args=[
Identifier(parts=['test_tabl', 'search_vector']),
Identifier(parts=['search_vector']),
Parameter(Result(1))
]
)
Expand Down Expand Up @@ -446,7 +446,7 @@ def test_using_integration_in_subselect(self):
FetchDataframeStep(
step_num=0,
integration='chromadb',
query=parse_sql('SELECT test_tabl.content AS content FROM test_tabl LIMIT 1')
query=parse_sql('SELECT content AS content FROM test_tabl LIMIT 1')
),
ApplyPredictorRowStep(
step_num=1,
Expand Down
Loading

0 comments on commit 0e2fb2a

Please sign in to comment.