Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joocer committed Jul 1, 2023
1 parent beeac93 commit 2643def
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
25 changes: 18 additions & 7 deletions opteryx/components/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@
from opteryx.exceptions import DatabaseError
from opteryx.exceptions import FunctionNotFoundError
from opteryx.exceptions import UnexpectedDatasetReferenceError
from opteryx.functions.v2 import FUNCTIONS

# from opteryx.functions.v2 import FUNCTIONS
from opteryx.functions import FUNCTIONS
from opteryx.managers.expression import NodeType
from opteryx.operators.aggregate_node import AGGREGATORS
from opteryx.samples import calculated

CAMEL_TO_SNAKE = re.compile(r"(?<!^)(?=[A-Z])")
logger = get_logger()
Expand All @@ -100,7 +104,7 @@ def inner_binder(node, relations):
return node

node_type = node.node_type
if node_type & NodeType.IDENTIFIER == NodeType.IDENTIFIER:
if node_type == NodeType.IDENTIFIER:
column = None
found_source_relation = relations.get(node.source)

Expand Down Expand Up @@ -146,15 +150,22 @@ def inner_binder(node, relations):

# add values to the node to indicate the source of this data
node.schema_column = column
if (node_type & NodeType.FUNCTION == NodeType.FUNCTION) or (
node_type & NodeType.AGGREGATOR == NodeType.AGGREGATOR
):
if node_type == NodeType.FUNCTION:
# we're just going to bind the function into the node
func = FUNCTIONS.get(node.value)
if not func:
suggest = FUNCTIONS.suggest(node.value)
# v1:
from opteryx.utils import fuzzy_search

suggest = fuzzy_search(node.value, FUNCTIONS.keys() + AGGREGATORS.keys())
# v2: suggest = FUNCTIONS.suggest(node.value)
raise FunctionNotFoundError(function=node.value, suggestion=suggest)
node.function = func
if node_type == NodeType.AGGREGATOR:
# We don't suggest because if it wasn't found as an aggregation, it's
# assumed to be a function
agg = AGGREGATORS.get(node.value)
node.function = agg

# Now recurse and do this again for all the sub parts of the evaluation plan
if node.left:
Expand Down Expand Up @@ -263,7 +274,7 @@ def merge_dicts(*dicts):
return merged_dict

if context is None:
context = {}
context = {"schemas": {"$calculated": calculated.schema}}

# Recursively visit children
children = graph.ingoing_edges(node)
Expand Down
5 changes: 1 addition & 4 deletions opteryx/components/logical_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ def inner_query_planner(ast_branch):
inner_plan.add_edge(previous_step_id, step_id)

# projection
_projection = [clause for clause in _projection if clause not in _aggregates]
if not _projection == [] and not (
len(_projection) == 1 and _projection[0].node_type == NodeType.WILDCARD
):
if not (len(_projection) == 1 and _projection[0].node_type == NodeType.WILDCARD):
project_step = LogicalPlanNode(node_type=LogicalPlanStepType.Project)
project_step.columns = _projection
previous_step_id, step_id = step_id, random_string()
Expand Down
3 changes: 2 additions & 1 deletion opteryx/connectors/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

WELL_KNOWN_DATASETS = {
"$astronauts": samples.astronauts,
"$calculated": None,
"$no_table": samples.no_table,
"$planets": samples.planets,
"$satellites": samples.satellites,
Expand All @@ -39,7 +40,7 @@ def suggest(dataset):
"""
from opteryx.utils import fuzzy_search

known_datasets = (k for k in WELL_KNOWN_DATASETS if k != "$no_table")
known_datasets = (k for k in WELL_KNOWN_DATASETS if k not in ("$no_table", "$calculated"))
suggestion = fuzzy_search(dataset, known_datasets)
if suggestion is not None:
return (
Expand Down
3 changes: 1 addition & 2 deletions opteryx/samples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime

import opteryx.samples.astronaut_data as astronauts
import opteryx.samples.calculated_data as calculated
import opteryx.samples.no_table_data as no_table
import opteryx.samples.planet_data as planets
import opteryx.samples.satellite_data as satellites
26 changes: 26 additions & 0 deletions opteryx/samples/calculated_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

"""
calculated
---------
This is used as the source relation for calculated values.
"""
from orso.schema import RelationSchema


def read(*args):
return None


schema = RelationSchema(name="$calculated", columns=[])

0 comments on commit 2643def

Please sign in to comment.