Repository: mabel-dev/opteryx

Issue:
🪲 query execution fails with bytes input
### Thank you for taking the time to report a problem with Opteryx.
_To help us to respond to your request we ask that you try to provide the below detail about the bug._

**Describe the bug** _A clear and specific description of what the bug is. What the error, incorrect or unexpected behaviour was._


**Expected behaviour** _A clear and concise description of what you expected to happen._


**Sample Code/Statement** _If you can, please submit the SQL statement or Python code snippet, or a representative example using the sample datasets._

~~~sql

~~~

**Additional context** _Add any other context about the problem here, for example what you have done to try to diagnose or workaround the problem._



Discussion:

Resolving patch:
diff --git a/opteryx/components/logical_planner_builders.py b/opteryx/components/logical_planner_builders.py
--- a/opteryx/components/logical_planner_builders.py
+++ b/opteryx/components/logical_planner_builders.py
@@ -249,6 +249,8 @@ def cast(branch, alias=None, key=None):
         data_type = branch["data_type"]["Custom"][0][0]["value"].upper()
     if data_type == "Timestamp":
         data_type = "TIMESTAMP"
+    elif data_type == "Date":
+        data_type = "DATE"
     elif "Varchar" in data_type:
         data_type = "VARCHAR"
     elif "Decimal" in data_type:
@@ -291,6 +293,8 @@ def try_cast(branch, alias=None, key="TryCast"):
         data_type = branch["data_type"]["Custom"][0][0]["value"].upper()
     if data_type == "Timestamp":
         data_type = "TIMESTAMP"
+    elif data_type == "Date":
+        data_type = "DATE"
     elif "Varchar" in data_type:
         data_type = "VARCHAR"
     elif "Decimal" in data_type:
@@ -306,9 +310,6 @@ def try_cast(branch, alias=None, key="TryCast"):
     else:
         raise SqlError(f"Unsupported type for `{function_name}`  - '{data_type}'")
 
-    #    alias.append(f"{function_name}({args[0].value} AS {data_type})")
-    #    alias.append(f"{data_type.upper} {args[0].value}")
-
     return Node(
         NodeType.FUNCTION,
         value=f"TRY_{data_type.upper()}",
diff --git a/opteryx/cursor.py b/opteryx/cursor.py
--- a/opteryx/cursor.py
+++ b/opteryx/cursor.py
@@ -292,6 +292,8 @@ def execute_to_arrow(
         Returns:
             The query results in Arrow table format.
         """
+        if hasattr(operation, "decode"):
+            operation = operation.decode()
         results = self._execute_statements(operation, params)
         if results is not None:
             result_data, self._result_type = next(results, (ResultType._UNDEFINED, None))


Test patch:
diff --git a/tests/misc/test_connection.py b/tests/misc/test_connection.py
--- a/tests/misc/test_connection.py
+++ b/tests/misc/test_connection.py
@@ -44,6 +44,26 @@ def test_execute():
     assert isinstance(planets, pandas.DataFrame)
 
 
+def test_byte_strings():
+    import opteryx
+
+    cur = opteryx.query(b"SELECT * FROM $planets")
+    assert cur.rowcount == 9
+    assert cur.shape == (9, 20)
+
+    conn = opteryx.connect()
+    cur = conn.cursor()
+    cur.execute(b"SELECT * FROM $planets")
+    assert cur.rowcount == 9
+    assert cur.shape == (9, 20)
+
+    conn = opteryx.connect()
+    cur = conn.cursor()
+    arrow = cur.execute_to_arrow(b"SELECT * FROM $planets")
+    assert arrow.num_rows == 9, cur.rowcount
+    assert arrow.shape == (9, 20)
+
+
 if __name__ == "__main__":  # pragma: no cover
     from tests.tools import run_tests
 
diff --git a/tests/sql_battery/test_battery_sql92.py b/tests/sql_battery/test_battery_sql92.py
--- a/tests/sql_battery/test_battery_sql92.py
+++ b/tests/sql_battery/test_battery_sql92.py
@@ -161,7 +161,7 @@
     ("SELECT DATE '2016-03-26' <> DATE '2016-03-26'", "F051-04"),
     ("SELECT DATE '2016-03-26' = DATE '2016-03-26'", "F051-04"),
     ("SELECT DATE '2016-03-26' > DATE '2016-03-26'", "F051-04"),
-#    ("SELECT DATE '2016-03-26' >= DATE '2016-03-26'", "F051-04"),
+    ("SELECT DATE '2016-03-26' >= DATE '2016-03-26'", "F051-04"),
 #    ("SELECT TIME '01:02:03' < TIME '01:02:03'", "F051-04"),
 #    ("SELECT TIME '01:02:03' <= TIME '01:02:03'", "F051-04"),
 #    ("SELECT TIME '01:02:03' <> TIME '01:02:03'", "F051-04"),
@@ -174,10 +174,10 @@
     ("SELECT TIMESTAMP '2016-03-26 01:02:03' = TIMESTAMP '2016-03-26 01:02:03'", "F051-04"),
     ("SELECT TIMESTAMP '2016-03-26 01:02:03' > TIMESTAMP '2016-03-26 01:02:03'", "F051-04"),
     ("SELECT TIMESTAMP '2016-03-26 01:02:03' >= TIMESTAMP '2016-03-26 01:02:03'", "F051-04"),
-#    ("SELECT CAST ( '2016-03-26' AS DATE )", "F051-05"),
+    ("SELECT CAST ( '2016-03-26' AS DATE )", "F051-05"),
 #    ("SELECT CAST ( '01:02:03' AS TIME )", "F051-05"),
     ("SELECT CAST ( '2016-03-26 01:02:03' AS TIMESTAMP WITHOUT TIME ZONE )", "F051-05"),
-#    ("SELECT CAST ( CAST ( '2016-03-26' AS DATE ) AS VARCHAR )", "F051-05"),
+    ("SELECT CAST ( CAST ( '2016-03-26' AS DATE ) AS VARCHAR )", "F051-05"),
 #    ("SELECT CAST ( CAST ( '01:02:03' AS TIME ) AS VARCHAR )", "F051-05"),
     ("SELECT CAST ( CAST ( '2016-03-26 01:02:03' AS TIMESTAMP WITHOUT TIME ZONE ) AS VARCHAR )", "F051-05"),
 #    ("SELECT CAST ( CAST ( '01:02:03' AS TIME ) AS TIME )", "F051-05"),