Skip to content

Commit 705a6d1

Browse files
committed
Added fix for regexp handling of null and non string data
1 parent 6225144 commit 705a6d1

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

bin/q

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ parser.add_option_group(query_option_group)
165165
#-----------------------------------------------
166166

167167
def regexp(regular_expression, data):
168-
return re.search(regular_expression, data) is not None
168+
if data is not None:
169+
if type(data) is not str:
170+
data = str(data)
171+
return re.search(regular_expression, data) is not None
172+
else:
173+
return False
169174

170175
class Sqlite3DBResults(object):
171176
def __init__(self,query_column_names,results):

test/test-suite

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ class BasicTests(AbstractQTestCase):
149149
self.assertTrue(e[1].startswith("Bad header row"))
150150
self.assertTrue("Column name cannot contain commas" in e[2])
151151

152+
self.cleanup(tmpfile)
153+
154+
def test_regexp_int_data_handling(self):
155+
tmpfile = self.create_file_with_data(sample_data_no_header)
156+
157+
cmd = '../bin/q -d , "select c2 from %s where regexp(\'^1\',c2)"' % tmpfile.name
158+
retcode, o, e = run_command(cmd)
159+
160+
self.assertEquals(retcode, 0)
161+
self.assertEquals(len(o), 1)
162+
self.assertEquals(len(e), 0)
163+
164+
self.assertEquals(o[0],"1")
165+
166+
self.cleanup(tmpfile)
167+
168+
def test_regexp_null_data_handling(self):
169+
tmpfile = self.create_file_with_data(sample_data_no_header)
170+
171+
cmd = '../bin/q -d , "select count(*) from %s where regexp(\'^\',c2)"' % tmpfile.name
172+
retcode, o, e = run_command(cmd)
173+
174+
self.assertEquals(retcode, 0)
175+
self.assertEquals(len(o), 1)
176+
self.assertEquals(len(e), 0)
177+
178+
self.assertEquals(o[0],"2")
179+
180+
self.cleanup(tmpfile)
181+
152182
def test_select_one_column(self):
153183
tmpfile = self.create_file_with_data(sample_data_no_header)
154184

0 commit comments

Comments
 (0)