Skip to content

Commit ca429c3

Browse files
authored
* Fix for issue pandas-dev#169
1 parent 537927f commit ca429c3

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Bugfix: #147 Add get_info method to ChunkStore
88
* Bugfix: Periodically re-cache the library.quota to pick up any changes
99
* Bugfix: #166 Add index on SHA for ChunkStore
10+
* Bugfix: #169 Dtype mismatch in chunkstore updates
1011

1112
### 1.25 (2016-05-23)
1213

arctic/chunkstore/chunkstore.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def write(self, symbol, item, chunk_size):
167167
raise Exception("Can only chunk Series and DataFrames")
168168

169169
previous_shas = []
170-
if self._get_symbol_info(symbol):
170+
sym = self._get_symbol_info(symbol)
171+
if sym:
171172
previous_shas = set([Binary(x['sha']) for x in self._collection.find({'symbol': symbol},
172173
projection={'sha': True, '_id': False},
173174
)])
@@ -177,6 +178,9 @@ def write(self, symbol, item, chunk_size):
177178

178179
for start, end, record in self.chunker.to_chunks(item, chunk_size):
179180
r, dtype = serialize(record, string_max_len=self.STRING_MAX)
181+
# if symbol exists, dtypes better match
182+
if sym and str(dtype) != sym['dtype']:
183+
raise Exception('Dtype mismatch - cannot write chunk')
180184
records.append(r)
181185
ranges.append((start, end))
182186

@@ -266,6 +270,8 @@ def append(self, symbol, item):
266270
sym = self._get_symbol_info(symbol)
267271
continue
268272
r, dtype = serialize(record, string_max_len=self.STRING_MAX)
273+
if str(dtype) != sym['dtype']:
274+
raise Exception("Dtype mismatch - cannot append")
269275
records.append(r)
270276
ranges.append((start, end))
271277

@@ -321,6 +327,7 @@ def update(self, symbol, item):
321327
if not sym:
322328
raise NoDataFoundException("Symbol does not exist. Cannot update")
323329

330+
324331
records = []
325332
ranges = []
326333
orig_ranges = []
@@ -337,7 +344,9 @@ def update(self, symbol, item):
337344
else:
338345
orig_ranges.append((None, None))
339346

340-
r, _ = serialize(record, string_max_len=self.STRING_MAX)
347+
r, dtype = serialize(record, string_max_len=self.STRING_MAX)
348+
if str(dtype) != sym['dtype']:
349+
raise Exception('Dtype mismatch - cannot update')
341350
records.append(r)
342351
ranges.append((start, end))
343352

tests/integration/chunkstore/test_chunkstore.py

+22
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,25 @@ def test_get_info(chunkstore_lib):
616616
'type': u'df',
617617
'size': 72}
618618
assert(chunkstore_lib.get_info('test_df') == info)
619+
620+
621+
def test_dtype_mismatch_error(chunkstore_lib):
622+
s = pd.Series([1], index=pd.date_range('2016-01-01', '2016-01-01', name='date'), name='vals')
623+
624+
# Write with an int
625+
chunkstore_lib.write('test', s, 'D')
626+
627+
# Update with a float
628+
with pytest.raises(Exception) as e:
629+
chunkstore_lib.update('test', s * 1.0)
630+
assert('Dtype mismatch' in str(e))
631+
632+
with pytest.raises(Exception) as e:
633+
chunkstore_lib.write('test', s * 1.0, 'D')
634+
assert('Dtype mismatch' in str(e))
635+
636+
with pytest.raises(Exception) as e:
637+
chunkstore_lib.append('test', s * 1.0)
638+
assert('Dtype mismatch' in str(e))
639+
640+
assert_series_equal(s, chunkstore_lib.read('test'))

0 commit comments

Comments
 (0)