Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joocer committed Nov 11, 2023
1 parent 426fa27 commit 99a47c0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion opteryx/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _coalesce(*arrays):
"TODAY": _repeat_no_parameters(date_functions.get_today),
"TIME": _repeat_no_parameters(date_functions.get_time),
"YESTERDAY": _repeat_no_parameters(date_functions.get_yesterday),
"DATE": _iterate_single_parameter(date_functions.get_date),
"DATE": lambda x: compute.cast(x, "date32"), #_iterate_single_parameter(date_functions.get_date),
"YEAR": compute.year,
"MONTH": compute.month,
"DAY": compute.day,
Expand Down
8 changes: 4 additions & 4 deletions tests/misc/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ def test_python_client():
conn = opteryx.connect()
cursor = conn.cursor()
cursor.execute("SELECT * FROM $planets;")
rows_first = list(cursor.fetchall())
# if we convert to bytes we're comparing the values only
rows_first = b"".join([r.as_bytes for r in cursor.fetchall()])

import opteryx

cursor = opteryx.query("SELECT * FROM $planets;")
rows_second = list(cursor.fetchall())
# if we convert to bytes we're comparing the values only
rows_second = b"".join([r.as_bytes for r in cursor.fetchall()])

assert rows_first == rows_second

Expand Down Expand Up @@ -204,6 +206,4 @@ def get_user_permissions(user_roles):
if __name__ == "__main__": # pragma: no cover
from tests.tools import run_tests

test_readme_3()

run_tests()
39 changes: 26 additions & 13 deletions tests/sql_battery/test_null_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pytest
import opteryx
import numpy

# fmt:off
STATEMENTS = [
Expand Down Expand Up @@ -80,47 +81,47 @@
SELECT * FROM (VALUES (True), (False), (NULL)) AS tristatebooleans(bool) WHERE bool IS NOT FALSE;
""", {True, None}),(
"""
-- Query 1: Expected rows: 1 ("true")
-- Query 14: Expected rows: 1 ("true")
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE bool = 'true';
""", {'true'}),(
"""
-- Query 2: Expected rows: 1 (NULL)
-- Query 15: Expected rows: 1 (NULL)
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE bool IS NULL;
""", {None}),(
"""
-- Query 3: Expected rows: 2 ("true", "false")
-- Query 16: Expected rows: 2 ("true", "false")
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE bool IS NOT NULL;
""", {'true', 'false'}),(
"""
-- Query 4: Expected rows: 0
-- Query 17: Expected rows: 0
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE bool = NULL;
""", {}),(
"""
-- Query 5: Expected rows: 2 ("true", "false")
-- Query 18: Expected rows: 2 ("true", "false")
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE NOT bool IS NULL;
""", {'true', 'false'}),(
"""
-- Query 6: Expected rows: 1 ("false")
-- Query 19: Expected rows: 1 ("false")
SELECT * FROM (VALUES ('true'), ('false'), (NULL)) AS tristatebooleans(bool) WHERE NOT bool = "true";
""", {'false'}),(
"""
-- Query 1: Expected rows: 1 (1)
-- Query 20: Expected rows: 1 (1)
SELECT * FROM (VALUES (1), (-1), (NULL)) AS tristatebooleans(bool) WHERE bool = 1;
""", {1}),(
"""
-- Query 2: Expected rows: 1 (NULL)
-- Query 21: Expected rows: 1 (NULL)
SELECT * FROM (VALUES (1), (-1), (NULL)) AS tristatebooleans(bool) WHERE bool IS NULL;
""", {None}),(
""", {numpy.nan}),(
"""
-- Query 3: Expected rows: 2 (1, -1)
-- Query 22: Expected rows: 2 (1, -1)
SELECT * FROM (VALUES (1), (-1), (NULL)) AS tristatebooleans(bool) WHERE bool IS NOT NULL;
""", {1, -1}),(
"""
-- Query 4: Expected rows: 0
-- Query 23: Expected rows: 0
SELECT * FROM (VALUES (1), (-1), (NULL)) AS tristatebooleans(bool) WHERE bool = NULL;
""", {}),(
"""
-- Query 5: Expected rows: 3 (1, -1, NULL)
-- Query 24: Expected rows: 3 (1, -1, NULL)
SELECT * FROM (VALUES (1), (-1), (NULL)) AS tristatebooleans(bool) WHERE NOT bool IS NULL;
""", {1, -1})

Expand All @@ -130,10 +131,22 @@
# fmt:on


def process_set(set_with_nan):
has_nan = any(item != item for item in set_with_nan) # Check for NaN using NaN's property
set_without_nan = {
item for item in set_with_nan if item == item
} # Create a new set without NaNs
return has_nan, set_without_nan


def compare_sets(set1, set2):
if not set1 and not set2:
return True
return set1 == set2

s1_nan, s1_no_nan = process_set(set1)
s2_nan, s2_no_nan = process_set(set2)

return s1_nan == s2_nan and s1_no_nan == s2_no_nan


@pytest.mark.parametrize("statement, expected_result", STATEMENTS)
Expand Down
6 changes: 3 additions & 3 deletions tests/sql_battery/test_results_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def test_results_tests(test):
cursor.execute(sql)
result = cursor.arrow().to_pydict()

printable_result = orjson.dumps(result, default=str).decode()
printable_expected = orjson.dumps(test["result"]).decode()
printable_result = orjson.dumps(result, default=str, option=orjson.OPT_SORT_KEYS).decode()
printable_expected = orjson.dumps(test["result"], option=orjson.OPT_SORT_KEYS).decode()

assert (
result == test["result"]
printable_result == printable_expected
), f"Outcome:\n{printable_result}\nExpected:\n{printable_expected}"


Expand Down
4 changes: 2 additions & 2 deletions tests/sql_battery/tests/results/complex_003.results_tests
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"statement": "SELECT pl.name AS planet_name, pl.orbital_period, pl.diameter, dense_moons_stats.total_dense_moons, dense_moons_stats.avg_density, bright_moons_stats.avg_magnitude, bright_moons_stats.total_bright_moons FROM $planets pl LEFT JOIN (SELECT planetId, COUNT(*) AS total_dense_moons, AVG(density) AS avg_density FROM $satellites WHERE density > 2 GROUP BY planetId) dense_moons_stats ON pl.id = dense_moons_stats.planetId LEFT JOIN (SELECT planetId, AVG(magnitude) AS avg_magnitude, COUNT(*) AS total_bright_moons FROM $satellites WHERE magnitude < 5 GROUP BY planetId) bright_moons_stats ON pl.id = bright_moons_stats.planetId WHERE pl.distance_from_sun BETWEEN 100 AND 200 AND pl.orbital_eccentricity < 0.1 ORDER BY dense_moons_stats.total_dense_moons DESC, bright_moons_stats.avg_magnitude ASC LIMIT 10;",
"result": {
"bright_moons_stats.avg_magnitude": [-12.74, null],
"bright_moons_stats.total_bright_moons": [1, null],
"dense_moons_stats.total_dense_moons": [1, null],
"bright_moons_stats.total_bright_moons": [1.0, null],
"dense_moons_stats.total_dense_moons": [1.0, null],
"dense_moons_stats.avg_density": [3.344, null],
"planet_name": ["Earth", "Venus"],
"pl.diameter": [12756, 12104],
Expand Down

0 comments on commit 99a47c0

Please sign in to comment.