Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
Check for negative values before doing substr (#32)
Browse files Browse the repository at this point in the history
* 1629: Check for negative values before doing substr

* 1629: Fix lint and unit test

* 1629: Reuse substring method from ibis.backends.base.sql.string
  • Loading branch information
renato2099 authored Apr 27, 2021
1 parent b6bbfbe commit d515184
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ibis_bigquery/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ def _string_right(translator, expr):
)


def _string_substring(translator, expr):
op = expr.op()
arg, start, length = op.args
if length.op().value < 0:
raise ValueError('Length parameter should not be a negative value.')

base_substring = operation_registry[ops.Substring]
base_substring(translator, expr)


def _array_literal_format(expr):
return str(list(expr.op().value))

Expand Down Expand Up @@ -418,6 +428,7 @@ def _formatter(translator, expr):
ops.StringJoin: _string_join,
ops.StringAscii: _string_ascii,
ops.StringFind: _string_find,
ops.Substring: _string_substring,
ops.StrRight: _string_right,
ops.Repeat: fixed_arity('REPEAT', 2),
ops.RegexSearch: _regex_search,
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ def test_binary():
assert result == expected


def test_substring():
t = ibis.table([('value', 'string')], name='t')
expr = t["value"].substr(3, -1)
with pytest.raises(Exception) as exception_info:
ibis_bigquery.compile(expr)

expected = 'Length parameter should not be a negative value.'
assert str(exception_info.value) == expected


def test_bucket():
t = ibis.table([('value', 'double')], name='t')
buckets = [0, 1, 3]
Expand Down

0 comments on commit d515184

Please sign in to comment.