Skip to content

Commit

Permalink
Merge pull request #1215 from mabel-dev/#1211
Browse files Browse the repository at this point in the history
  • Loading branch information
joocer authored Oct 26, 2023
2 parents 7e16cda + 2935b8d commit dd7a721
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
9 changes: 7 additions & 2 deletions opteryx/components/binder/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
COMBINED_FUNCTIONS = {**FUNCTIONS, **AGGREGATORS}


def merge_schemas(*dicts: Dict[str, RelationSchema]) -> Dict[str, RelationSchema]:
def merge_schemas(*schemas: Dict[str, RelationSchema]) -> Dict[str, RelationSchema]:
"""
Handles the merging of relations, requiring a custom merge function.
Expand All @@ -47,7 +47,7 @@ def merge_schemas(*dicts: Dict[str, RelationSchema]) -> Dict[str, RelationSchema
A merged dictionary containing RelationSchemas.
"""
merged_dict: Dict[str, RelationSchema] = {}
for dic in dicts:
for dic in schemas:
if not isinstance(dic, dict):
raise InvalidInternalStateError("Internal Error - merge_schemas expected dicts")
for key, value in dic.items():
Expand Down Expand Up @@ -198,6 +198,11 @@ def inner_binder(node: Node, context: Dict[str, Any], step: str) -> Tuple[Node,
if node_type in (NodeType.IDENTIFIER, NodeType.EVALUATED):
return locate_identifier(node, context)

if node_type == NodeType.EXPRESSION_LIST:
node.value, new_contexts = zip(*(inner_binder(parm, context, step) for parm in node.value))
merged_schemas = merge_schemas(*[ctx.schemas for ctx in new_contexts])
context.schemas = merged_schemas

# Early exit for nodes representing calculated columns.
# If the node represents a calculated column, if we're seeing it again it's because it
# has appeared earlier in the plan and in that case we don't need to recalcuate, we just
Expand Down
25 changes: 24 additions & 1 deletion opteryx/functions/date_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,40 @@ def date_part(part, arr):
"""

extractors = {
"nanosecond": compute.nanosecond,
"nanoseconds": compute.nanosecond,
"microsecond": compute.microsecond,
"microseconds": compute.microsecond,
"millisecond": compute.millisecond,
"milliseconds": compute.millisecond,
"second": compute.second,
"minute": compute.minute,
"hour": compute.hour,
"day": compute.day,
"dow": compute.day_of_week,
"week": compute.iso_week,
"week": compute.week,
"month": compute.month,
"quarter": compute.quarter,
"doy": compute.day_of_year,
"year": compute.year,
"isoyear": compute.iso_year,
# ** supported by parser but not by pyarrow
# century
# date
# decade
# epoch
# isodow
# julian
# millenium
# millennium
# timezone
# ** future support in parser
# dayofweek
# dayofyear
# isoweek
# time
# timezonehour
# timezoneminute
}

# if we get a date literal
Expand Down
12 changes: 12 additions & 0 deletions opteryx/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ def _inner_copy(obj: Any) -> Any:
new_value = _inner_copy(value)
setattr(new_node, key, new_value)
return new_node

def __deepcopy__(self, memo):
# Check if this object is already in `memo`
if id(self) in memo:
return memo[id(self)]

# Copying Nodes is hard, so we already had a helper
new_obj = self.copy()

# Store the new object in `memo` and return it
memo[id(self)] = new_obj
return new_obj
3 changes: 2 additions & 1 deletion opteryx/shared/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ class VariableOwner(int, Enum):
"default_tmp_storage_engine": (OrsoTypes.VARCHAR, "opteryx", VariableOwner.SERVER),

# these are Opteryx specific variables
"cursor_read_size": (OrsoTypes.INTEGER, 100, VariableOwner.USER), # number of records returned from FETCH
"max_cache_evictions": (OrsoTypes.INTEGER, 32, VariableOwner.USER),
"max_size_single_cache_item": (OrsoTypes.INTEGER, 1024 * 1024, VariableOwner.SERVER),
"max_size_single_cache_item": (OrsoTypes.INTEGER, 2 * 1024 * 1024, VariableOwner.SERVER),
"local_buffer_pool_size": (OrsoTypes.INTEGER, 256, VariableOwner.SERVER),
"disable_high_priority": (OrsoTypes.BOOLEAN, False, VariableOwner.SERVER),
"morsel_size": (OrsoTypes.INTEGER, 64 * 1024 * 1024, VariableOwner.USER),
Expand Down
15 changes: 12 additions & 3 deletions tests/sql_battery/test_shapes_and_errors_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,15 @@
("SELECT EXTRACT(dow FROM birth_date) FROM $astronauts", 357, 1, None),
("SELECT EXTRACT(DOW FROM birth_date) FROM $astronauts", 357, 1, None),
("SELECT EXTRACT(YEAR FROM '2022-02-02')", 1, 1, None),
("SELECT EXTRACT(ISOYEAR FROM '2022-02-02')", 1, 1, None),
("SELECT EXTRACT(millisecond FROM NOW())", 1, 1, None),
("SELECT EXTRACT(milliseconds FROM NOW())", 1, 1, None),
("SELECT EXTRACT(nanosecond FROM NOW())", 1, 1, None),
("SELECT EXTRACT(millisecond FROM NOW())", 1, 1, None),
("SELECT EXTRACT(nanoseconds FROM NOW())", 1, 1, None),
("SELECT EXTRACT(millisecond FROM NOW())", 1, 1, None),
("SELECT EXTRACT(dayofweek FROM NOW())", 1, 1, SqlError),

("SELECT DATE_FORMAT(birth_date, '%m-%y') FROM $astronauts", 357, 1, None),
("SELECT DATEDIFF('year', '2017-08-25', '2011-08-25') AS DateDiff;", 1, 1, None),
("SELECT DATEDIFF('days', '2022-07-07', birth_date) FROM $astronauts", 357, 1, None),
Expand Down Expand Up @@ -995,12 +1004,12 @@
("SELECT name FROM $satellites WHERE '192.168.0.1' | '192.168.0.0/24'", 177, 1, None),
("SELECT name FROM $satellites WHERE '192.168.0.1' | '192.167.0.0/24'", 0, 1, None),
("SELECT name FROM $satellites WHERE 12 | 22", 177, 1, None),
]
A = [

("SELECT COUNT(*), place FROM (SELECT CASE id WHEN 3 THEN 'Earth' WHEN 1 THEN 'Mercury' ELSE 'Elsewhere' END as place FROM $planets) AS SQ GROUP BY place;", 3, 2, None),
("SELECT COUNT(*), place FROM (SELECT CASE id WHEN 3 THEN 'Earth' WHEN 1 THEN 'Mercury' END as place FROM $planets) AS SQ GROUP BY place HAVING place IS NULL;", 1, 2, None),
("SELECT COUNT(*), place FROM (SELECT CASE id WHEN 3 THEN 'Earth' WHEN 1 THEN 'Mercury' ELSE 'Elsewhere' END as place FROM $planets) AS SQ GROUP BY place HAVING place IS NULL;", 0, 2, None),

]
A = [
("SELECT TRIM(LEADING 'E' FROM name) FROM $planets;", 9, 1, None),
("SELECT * FROM $planets WHERE TRIM(TRAILING 'arth' FROM name) = 'E'", 1, 20, None),
("SELECT * FROM $planets WHERE TRIM(TRAILING 'ahrt' FROM name) = 'E'", 1, 20, None),
Expand Down

0 comments on commit dd7a721

Please sign in to comment.