-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
297 additions
and
218 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from enum import Enum | ||
|
||
import numpy | ||
import pyarrow | ||
from orso.types import OrsoTypes | ||
from pyarrow import Table | ||
|
||
from opteryx.functions import FUNCTIONS | ||
from opteryx.functions.binary_operators import binary_operations | ||
from opteryx.functions.unary_operations import UNARY_OPERATIONS | ||
from opteryx.models.node import Node | ||
from opteryx.third_party.pyarrow_ops.ops import filter_operations | ||
|
||
|
||
def format_expression(root): | ||
# circular imports | ||
from . import NodeType, INTERNAL_TYPE, LOGICAL_TYPE | ||
|
||
if root is None: | ||
return "null" | ||
|
||
if isinstance(root, list): | ||
return [format_expression(item) for item in root] | ||
|
||
node_type = root.node_type | ||
_map: dict = {} | ||
|
||
# LITERAL TYPES | ||
if node_type == NodeType.LITERAL: | ||
literal_type = root.type | ||
if literal_type == OrsoTypes.VARCHAR: | ||
return "'" + root.value.replace("'", "'") + "'" | ||
if literal_type == OrsoTypes.TIMESTAMP: | ||
return "'" + str(root) + "'" | ||
if literal_type == OrsoTypes.INTERVAL: | ||
return "<INTERVAL>" | ||
return str(root.value) | ||
# INTERAL IDENTIFIERS | ||
if node_type & INTERNAL_TYPE == INTERNAL_TYPE: | ||
if node_type in (NodeType.FUNCTION, NodeType.AGGREGATOR): | ||
if root.value == "CASE": | ||
con = [format_expression(a) for a in root.parameters[0].value] | ||
vals = [format_expression(a) for a in root.parameters[1].value] | ||
return "CASE " + "".join([f"WHEN {c} THEN {v} " for c, v in zip(con, vals)]) + "END" | ||
distinct = "DISTINCT " if root.distinct else "" | ||
order = "" | ||
if root.order: | ||
order = f" ORDER BY {', '.join(item[0].value + (' DESC' if not item[1] else '') for item in (root.order or []))}" | ||
if root.value == "ARRAY_AGG": | ||
limit = f" LIMIT {root.limit}" if root.limit else "" | ||
return f"{root.value.upper()}({distinct}{format_expression(root.expression)}{order}{limit})" | ||
return f"{root.value.upper()}({distinct}{','.join([format_expression(e) for e in root.parameters])}{order})" | ||
if node_type == NodeType.WILDCARD: | ||
return "*" | ||
if node_type == NodeType.BINARY_OPERATOR: | ||
_map = { | ||
"StringConcat": "||", | ||
"Plus": "+", | ||
"Minus": "-", | ||
"Multiply": "*", | ||
"Divide": "/", | ||
"MyIntegerDivide": "div", | ||
} | ||
return f"{format_expression(root.left)} {_map.get(root.value, root.value).upper()} {format_expression(root.right)}" | ||
if node_type == NodeType.COMPARISON_OPERATOR: | ||
_map = {"Eq": "=", "Lt": "<", "Gt": ">", "NotEq": "!=", "BitwiseOr": "|"} | ||
return f"{format_expression(root.left)} {_map.get(root.value, root.value).upper()} {format_expression(root.right)}" | ||
if node_type == NodeType.UNARY_OPERATOR: | ||
_map = {"IsNull": "%s IS NULL", "IsNotNull": "%s IS NOT NULL"} | ||
return _map.get(root.value, root.value + "(%s)").replace( | ||
"%s", format_expression(root.centre) | ||
) | ||
if node_type == NodeType.NOT: | ||
return f"NOT {format_expression(root.centre)}" | ||
if node_type in (NodeType.AND, NodeType.OR, NodeType.XOR): | ||
_map = { | ||
NodeType.AND: "AND", | ||
NodeType.OR: "OR", | ||
NodeType.XOR: "XOR", | ||
} # type:ignore | ||
return f"{format_expression(root.left)} {_map[node_type]} {format_expression(root.right)}" | ||
|
||
return str(root.value) |
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
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
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,24 @@ | ||
import os | ||
import sys | ||
|
||
sys.path.insert(1, os.path.join(sys.path[0], "../..")) | ||
|
||
from orso.types import OrsoTypes | ||
|
||
from opteryx.managers.expression import format_expression | ||
from opteryx.managers.expression import NodeType | ||
from opteryx.models.node import Node | ||
|
||
|
||
def test_format_nodes(): | ||
left_node = Node(NodeType.LITERAL, type=OrsoTypes.INTEGER, value=1) | ||
right_node = Node(NodeType.LITERAL, type=OrsoTypes.DOUBLE, value=1.1) | ||
|
||
print(format_expression(left_node)) | ||
print(format_expression(right_node)) | ||
|
||
|
||
if __name__ == "__main__": # pragma: no cover | ||
from tests.tools import run_tests | ||
|
||
run_tests() |
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
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
Oops, something went wrong.