Skip to content

Commit

Permalink
Merge pull request #253 from mabel-dev/FIX/#252
Browse files Browse the repository at this point in the history
FIX/#252
  • Loading branch information
joocer authored Jul 4, 2022
2 parents 60d399d + 9d616c6 commit c9c555a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/Release Notes/Change Log.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [[#35](https://github.com/mabel-dev/opteryx/issues/35)] Table scan planning done during query planning. ([@joocer](https://github.com/joocer))
- [[#173](https://github.com/mabel-dev/opteryx/issues/173)] Data not found raises different errors under different scenarios. ([@joocer](https://github.com/joocer))

**Fixed**

- [[#252](https://github.com/mabel-dev/opteryx/issues/252)] Planner should gracefully convert byte strings to ascii strings. ([@joocer](https://github.com/joocer))

### [0.1.0] - 2022-07-02

Expand Down
5 changes: 5 additions & 0 deletions docs/SQL Reference/Working with SQL/10 Working with Dates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Working with Dates


- EXTRACT is usually faster than DATE_TRUNC but DATE_TRUNC is more functionally rich
- EXTRACT is preferred over the individual extractors (YEAR, MONTH, DAY etc)
4 changes: 2 additions & 2 deletions opteryx/engine/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pyarrow import compute

import opteryx
from opteryx.engine.functions import other_functions, date_functions
from opteryx.engine.functions import other_functions, date_functions, string_functions
from opteryx.exceptions import SqlError
from opteryx.third_party.date_trunc import date_trunc
from opteryx.utils.dates import parse_iso
Expand Down Expand Up @@ -151,7 +151,7 @@ def get_len(obj):
"UPPER": compute.utf8_upper, # UPPER(str) -> str
"LOWER": compute.utf8_lower, # LOWER(str) -> str
"TRIM": compute.utf8_trim_whitespace, # TRIM(str) -> str
"LEFT": _iterate_double_parameter(lambda x, y: str(x)[: int(y)]),
"LEFT": string_functions._string_slicer_left,
"RIGHT": _iterate_double_parameter(lambda x, y: str(x)[-int(y) :]),
# HASHING & ENCODING
"HASH": _iterate_single_parameter(lambda x: format(CityHash64(str(x)), "X")),
Expand Down
9 changes: 9 additions & 0 deletions opteryx/engine/functions/string_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy


def _string_slicer_left(arr, length):
length = int(length) # it's probably a float64
arr = arr.astype(str) # it's probably an array of objects
interim = arr.view((str, 1)).reshape(len(arr), -1)[:, 0:length]
return [numpy.array(interim).view((str, length)).flatten()]
7 changes: 6 additions & 1 deletion opteryx/engine/planner/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def copy(self):
def create_plan(self, sql: str = None, ast: dict = None):

if sql:

# if it's a byte string, convert to an ascii string
if isinstance(sql, bytes):
sql = sql.decode()

import sqloxide

# extract temporal filters, this isn't supported by sqloxide
Expand All @@ -116,7 +121,7 @@ def create_plan(self, sql: str = None, ast: dict = None):
# identifiers to start with _ (underscore) and $ (dollar sign)
# https://github.com/sqlparser-rs/sqlparser-rs/blob/main/src/dialect/mysql.rs
except ValueError as exception: # pragma: no cover
raise SqlError(exception)
raise SqlError from exception
else:
self._ast = ast

Expand Down
2 changes: 1 addition & 1 deletion opteryx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
2) we can import it in setup.py for the same reason
"""

__version__ = "0.1.1-beta.2"
__version__ = "0.1.1-beta.3"
2 changes: 2 additions & 0 deletions tests/sql_battery/test_battery_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@
("SELECT * FROM tests.data.nulls WHERE username NOT ILIKE 'BBC%' FOR '2000-01-01'", 21, 5),
("SELECT * FROM tests.data.nulls WHERE username ~ 'BBC.+' FOR '2000-01-01'", 3, 5),
("SELECT * FROM tests.data.nulls WHERE tweet ILIKE '%Trump%' FOR '2000-01-01'", 0, 5),
# BYTE-ARRAY FAILS #252
(b"SELECT * FROM $satellites", 177, 8),
]
# fmt:on

Expand Down

0 comments on commit c9c555a

Please sign in to comment.