-- description: Test ordering of NaN and infinity values
-- FLOAT type
-- storing nan in a table
CREATE TABLE floats(f FLOAT, ts TIMESTAMP TIME INDEX);

Affected Rows: 0

INSERT INTO floats VALUES ('NAN'::FLOAT, 1), (1::FLOAT, 2), ('infinity'::FLOAT, 3), ('-infinity'::FLOAT, 4), (-1::FLOAT, 5), (NULL, 6);

Affected Rows: 6

-- standard ordering
SELECT f FROM floats ORDER BY f;

+------+
| f    |
+------+
| -inf |
| -1.0 |
| 1.0  |
| inf  |
| NaN  |
|      |
+------+

SELECT f FROM floats ORDER BY f DESC;

+------+
| f    |
+------+
|      |
| NaN  |
| inf  |
| 1.0  |
| -1.0 |
| -inf |
+------+

-- top-n
SELECT f FROM floats ORDER BY f DESC NULLS LAST LIMIT 2;

+-----+
| f   |
+-----+
| NaN |
| inf |
+-----+

SELECT f FROM floats ORDER BY f NULLS LAST LIMIT 2;

+------+
| f    |
+------+
| -inf |
| -1.0 |
+------+

SELECT f FROM floats ORDER BY f DESC NULLS LAST LIMIT 4;

+------+
| f    |
+------+
| NaN  |
| inf  |
| 1.0  |
| -1.0 |
+------+

SELECT f FROM floats ORDER BY f NULLS LAST LIMIT 4;

+------+
| f    |
+------+
| -inf |
| -1.0 |
| 1.0  |
| inf  |
+------+

-- count with filters
SELECT COUNT(*) FROM floats WHERE f > 0;

+----------+
| COUNT(*) |
+----------+
| 3        |
+----------+

SELECT COUNT(*) FROM floats WHERE f < 0;

+----------+
| COUNT(*) |
+----------+
| 2        |
+----------+

DROP TABLE floats;

Affected Rows: 0

-- DOUBLE type
-- storing nan in a table
CREATE TABLE doubles(d DOUBLE, ts TIMESTAMP TIME INDEX);

Affected Rows: 0

INSERT INTO doubles VALUES ('NAN'::DOUBLE, 1), (1::DOUBLE, 2), ('infinity'::DOUBLE, 3), ('-infinity'::DOUBLE, 4), (-1::DOUBLE, 5), (NULL, 6);

Affected Rows: 6

-- standard ordering
SELECT d FROM doubles ORDER BY d;

+------+
| d    |
+------+
| -inf |
| -1.0 |
| 1.0  |
| inf  |
| NaN  |
|      |
+------+

SELECT d FROM doubles ORDER BY d DESC;

+------+
| d    |
+------+
|      |
| NaN  |
| inf  |
| 1.0  |
| -1.0 |
| -inf |
+------+

-- top-n
SELECT d FROM doubles ORDER BY d DESC NULLS LAST LIMIT 2;

+-----+
| d   |
+-----+
| NaN |
| inf |
+-----+

SELECT d FROM doubles ORDER BY d NULLS LAST LIMIT 2;

+------+
| d    |
+------+
| -inf |
| -1.0 |
+------+

SELECT d FROM doubles ORDER BY d DESC NULLS LAST LIMIT 4;

+------+
| d    |
+------+
| NaN  |
| inf  |
| 1.0  |
| -1.0 |
+------+

SELECT d FROM doubles ORDER BY d NULLS LAST LIMIT 4;

+------+
| d    |
+------+
| -inf |
| -1.0 |
| 1.0  |
| inf  |
+------+

-- count with filters
SELECT COUNT(*) FROM doubles WHERE d > 0;

+----------+
| COUNT(*) |
+----------+
| 3        |
+----------+

SELECT COUNT(*) FROM doubles WHERE d < 0;

+----------+
| COUNT(*) |
+----------+
| 2        |
+----------+

DROP TABLE doubles;

Affected Rows: 0

