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

Planner: create empty table #363

Merged
merged 3 commits into from
Mar 19, 2024
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
11 changes: 11 additions & 0 deletions mindsdb_sql/parser/ast/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def __init__(self, name, type='integer', length=None):
self.default = None
self.length = length

def __eq__(self, other):
if type(self) != type(other):
return False

for k in ['name', 'is_primary_key', 'type', 'default', 'length']:

if getattr(self, k) != getattr(other, k):
return False

return True


class CreateTable(ASTNode):
def __init__(self,
Expand Down
12 changes: 10 additions & 2 deletions mindsdb_sql/planner/query_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from mindsdb_sql.planner.steps import (FetchDataframeStep, ProjectStep, ApplyPredictorStep,
ApplyPredictorRowStep, UnionStep, GetPredictorColumns, SaveToTable,
InsertToTable, UpdateToTable, SubSelectStep,
DeleteStep, DataStep)
DeleteStep, DataStep, CreateTableStep)
from mindsdb_sql.planner.utils import (disambiguate_predictor_column_identifier,
get_deepest_select,
recursively_extract_column_values,
Expand Down Expand Up @@ -532,8 +532,16 @@ def plan_project(self, query, dataframe, ignore_doubles=False):
out_identifiers.append(new_identifier)
return self.plan.add_step(ProjectStep(dataframe=dataframe, columns=out_identifiers, ignore_doubles=ignore_doubles))

def plan_create_table(self, query):
def plan_create_table(self, query: CreateTable):
if query.from_select is None:
if query.columns is not None:
self.plan.add_step(CreateTableStep(
table=query.name,
columns=query.columns,
is_replace=query.is_replace,
))
return

raise PlanningException(f'Not implemented "create table": {query.to_string()}')

integration_name = query.name.parts[0]
Expand Down
9 changes: 9 additions & 0 deletions mindsdb_sql/planner/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ def __init__(self, table, dataframe=None, query=None, *args, **kwargs):
self.query = query


class CreateTableStep(PlanStep):
def __init__(self, table, columns=None, is_replace=False, *args, **kwargs):
"""Fills table with content of dataframe"""
super().__init__(*args, **kwargs)
self.table = table
self.columns = columns
self.is_replace = is_replace


class UpdateToTable(PlanStep):
def __init__(self, table, dataframe, update_command, *args, **kwargs):
"""Fills table with content of dataframe"""
Expand Down
5 changes: 4 additions & 1 deletion mindsdb_sql/render/sqlalchemy_render.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import datetime as dt

import sqlalchemy as sa
Expand Down Expand Up @@ -285,8 +286,10 @@ def get_type(self, typename):
return typename

typename = typename.upper()
if typename == 'INT64':
if re.match('^INT[\d]*$', typename):
typename = 'BIGINT'
if re.match('^FLOAT[\d]*$', typename):
typename = 'FLOAT'
type = self.types_map[typename]
return type

Expand Down
33 changes: 31 additions & 2 deletions tests/test_planner/test_integration_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from mindsdb_sql.planner import plan_query
from mindsdb_sql.planner.query_plan import QueryPlan
from mindsdb_sql.planner.step_result import Result
from mindsdb_sql.planner.steps import (FetchDataframeStep, ProjectStep, FilterStep, JoinStep, ApplyPredictorStep,
ApplyPredictorRowStep, GroupByStep, SubSelectStep, UpdateToTable,
from mindsdb_sql.planner.steps import (FetchDataframeStep, CreateTableStep, SubSelectStep, UpdateToTable,
DeleteStep)


Expand Down Expand Up @@ -689,4 +688,34 @@ def test_delete_from_table_subselect_sql_different_integration(self):
predictor_metadata=[{'name': 'pred', 'integration_name': 'mindsdb'}]
)

assert plan.steps == expected_plan.steps

def test_create_table(self):
query = parse_sql('''
CREATE or replace table int2.tab1 (
id int8,
data varchar
)
''', dialect='mindsdb')

expected_plan = QueryPlan(
predictor_namespace='mindsdb',
steps=[
CreateTableStep(
table=Identifier('int2.tab1'),
columns=[
TableColumn(name='id', type='int8'),
TableColumn(name='data', type='varchar'),
],
is_replace=True
),
],
)

plan = plan_query(
query,
integrations=[{'name': 'int1', 'class_type': 'api', 'type': 'data'}, 'int2'],
predictor_metadata=[{'name': 'pred', 'integration_name': 'mindsdb'}]
)

assert plan.steps == expected_plan.steps
Loading