From 99a47c007fe23da1ae4f49ccbb146bab56bea9f4 Mon Sep 17 00:00:00 2001 From: joocer Date: Sat, 11 Nov 2023 13:18:14 +0000 Subject: [PATCH] #1254 --- opteryx/functions/__init__.py | 2 +- tests/misc/test_documentation.py | 8 ++-- tests/sql_battery/test_null_semantics.py | 39 ++++++++++++------- tests/sql_battery/test_results_battery.py | 6 +-- .../tests/results/complex_003.results_tests | 4 +- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/opteryx/functions/__init__.py b/opteryx/functions/__init__.py index 9add1f72e..b749883d3 100644 --- a/opteryx/functions/__init__.py +++ b/opteryx/functions/__init__.py @@ -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, diff --git a/tests/misc/test_documentation.py b/tests/misc/test_documentation.py index 89619cc30..643cac82a 100644 --- a/tests/misc/test_documentation.py +++ b/tests/misc/test_documentation.py @@ -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 @@ -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() diff --git a/tests/sql_battery/test_null_semantics.py b/tests/sql_battery/test_null_semantics.py index 522a447fd..2a3119354 100644 --- a/tests/sql_battery/test_null_semantics.py +++ b/tests/sql_battery/test_null_semantics.py @@ -10,6 +10,7 @@ import pytest import opteryx +import numpy # fmt:off STATEMENTS = [ @@ -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}) @@ -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) diff --git a/tests/sql_battery/test_results_battery.py b/tests/sql_battery/test_results_battery.py index e5f20a668..4e54ab037 100644 --- a/tests/sql_battery/test_results_battery.py +++ b/tests/sql_battery/test_results_battery.py @@ -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}" diff --git a/tests/sql_battery/tests/results/complex_003.results_tests b/tests/sql_battery/tests/results/complex_003.results_tests index 841bb1a59..c3b14871c 100644 --- a/tests/sql_battery/tests/results/complex_003.results_tests +++ b/tests/sql_battery/tests/results/complex_003.results_tests @@ -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],