Permalink
Browse files

Merge pull request #157 from umayer/156-sort-nested

issue 156: sort nested tables (Thanks to Uwe Mayer)
  • Loading branch information...
2 parents ade2ba1 + 95604c1 commit 6b15ac3593c68557bd681d7b5dc8ae8e7820a85c FrancescAlted committed Jun 13, 2012
Showing with 76 additions and 7 deletions.
  1. +8 −7 tables/table.py
  2. +68 −0 tables/tests/test_indexes.py
View
@@ -2628,13 +2628,14 @@ def _g_propIndexes(self, other):
"""Generate index in `other` table for every indexed column here."""
oldcols, newcols = self.colinstances, other.colinstances
for colname in newcols:
- oldcolindexed = oldcols[colname].is_indexed
- if oldcolindexed:
- oldcolindex = oldcols[colname].index
- newcol = newcols[colname]
- newcol.createIndex(
- kind=oldcolindex.kind, optlevel=oldcolindex.optlevel,
- filters=oldcolindex.filters, tmp_dir=None)
+ if (isinstance(oldcols[colname], Column)):
+ oldcolindexed = oldcols[colname].is_indexed
+ if oldcolindexed:
+ oldcolindex = oldcols[colname].index
+ newcol = newcols[colname]
+ newcol.createIndex(
+ kind=oldcolindex.kind, optlevel=oldcolindex.optlevel,
+ filters=oldcolindex.filters, tmp_dir=None)
def _g_copyWithStats(self, group, name, start, stop, step,
@@ -2318,6 +2318,72 @@ class readSortedIndex9(readSortedIndexTestCase):
optlevel = 9
+class Issue156TestBase(PyTablesTestCase):
+ # field name in table according to which test_copysort() sorts the table
+ sort_field = None
+
+ def setUp(self):
+ # create hdf5 file
+ self.filename = tempfile.mktemp(".hdf5")
+ self.file = openFile(self.filename, mode="w")
+
+ # create nested table
+ class Foo(IsDescription):
+ frame = UInt16Col()
+ class Bar(IsDescription):
+ code = UInt16Col()
+
+ table = self.file.createTable('/', 'foo', Foo, filters=Filters(3, 'zlib'), createparents=True)
+
+ self.file.flush()
+
+ # fill table with 10 random numbers
+ for k in xrange(10):
+ row = table.row
+ row['frame'] = numpy.random.random_integers(0, 2**16-1)
+ row['Bar/code'] = numpy.random.random_integers(0, 2**16-1)
+ row.append()
+
+ self.file.flush()
+
+
+ def tearDown(self):
+ self.file.close()
+ os.remove(self.filename)
+
+
+ def _copysort(self):
+ # copy table
+ oldNode = self.file.getNode('/foo')
+ # create completely sorted index on a main column
+ oldNode.colinstances[self.sort_field].createCSIndex()
+
+ # this fails on ade2ba123efd267fd31
+ try:
+ newNode = oldNode.copy(newname='foo2', overwrite=True, sortby=self.sort_field, checkCSI=True, propindexes=True)
+ except AttributeError as e:
+ self.fail("test_copysort1() raised AttributeError unexpectedly: \n"+str(e))
+
+ # check column is sorted
+ self.assertTrue( numpy.all( newNode.col(self.sort_field) == sorted( oldNode.col(self.sort_field) ) ) )
+ # check index is available
+ self.assertTrue( newNode.colindexes.has_key(self.sort_field) )
+ # check CSI was propagated
+ self.assertTrue( newNode.colindexes[self.sort_field].is_CSI )
+
+
+class Issue156_1(Issue156TestBase):
+ # sort by field from non nested entry
+ sort_field = 'frame'
+ test_copysort = Issue156TestBase._copysort
+
+class Issue156_2(Issue156TestBase):
+ # sort by field from nested entry
+ sort_field = 'Bar/code'
+ test_copysort = Issue156TestBase._copysort
+
+
+
#----------------------------------------------------------------------
def suite():
@@ -2351,6 +2417,8 @@ def suite():
theSuite.addTest(unittest.makeSuite(readSortedIndex3))
theSuite.addTest(unittest.makeSuite(readSortedIndex6))
theSuite.addTest(unittest.makeSuite(readSortedIndex9))
+ theSuite.addTest(unittest.makeSuite(Issue156_1))
+ theSuite.addTest(unittest.makeSuite(Issue156_2))
if heavy:
# These are too heavy for normal testing
theSuite.addTest(unittest.makeSuite(AI4bTestCase))

0 comments on commit 6b15ac3

Please sign in to comment.