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

Commit d515184

Browse files
authored
Check for negative values before doing substr (#32)
* 1629: Check for negative values before doing substr * 1629: Fix lint and unit test * 1629: Reuse substring method from ibis.backends.base.sql.string
1 parent b6bbfbe commit d515184

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

ibis_bigquery/compiler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ def _string_right(translator, expr):
274274
)
275275

276276

277+
def _string_substring(translator, expr):
278+
op = expr.op()
279+
arg, start, length = op.args
280+
if length.op().value < 0:
281+
raise ValueError('Length parameter should not be a negative value.')
282+
283+
base_substring = operation_registry[ops.Substring]
284+
base_substring(translator, expr)
285+
286+
277287
def _array_literal_format(expr):
278288
return str(list(expr.op().value))
279289

@@ -418,6 +428,7 @@ def _formatter(translator, expr):
418428
ops.StringJoin: _string_join,
419429
ops.StringAscii: _string_ascii,
420430
ops.StringFind: _string_find,
431+
ops.Substring: _string_substring,
421432
ops.StrRight: _string_right,
422433
ops.Repeat: fixed_arity('REPEAT', 2),
423434
ops.RegexSearch: _regex_search,

tests/unit/test_compiler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ def test_binary():
258258
assert result == expected
259259

260260

261+
def test_substring():
262+
t = ibis.table([('value', 'string')], name='t')
263+
expr = t["value"].substr(3, -1)
264+
with pytest.raises(Exception) as exception_info:
265+
ibis_bigquery.compile(expr)
266+
267+
expected = 'Length parameter should not be a negative value.'
268+
assert str(exception_info.value) == expected
269+
270+
261271
def test_bucket():
262272
t = ibis.table([('value', 'double')], name='t')
263273
buckets = [0, 1, 3]

0 commit comments

Comments
 (0)