def create_indexes(self, colname, ncolname, extracolname): if not self.indexed: return try: kind = self.kind vprint("* Indexing ``%s`` columns. Type: %s." % (colname, kind)) for acolname in [colname, ncolname, extracolname]: acolumn = self.table.colinstances[acolname] acolumn.create_index(kind=self.kind, optlevel=self.optlevel, _blocksizes=small_blocksizes, _testmode=True) except TypeError as te: if self.colNotIndexable_re.search(str(te)): raise common.SkipTest( "Columns of this type can not be indexed.") raise except NotImplementedError: raise common.SkipTest( "Indexing columns of this type is not supported yet.")
class BaseTableQueryTestCase(common.TempFileMixin, common.PyTablesTestCase): """ Base test case for querying tables. Sub-classes must define the following attributes: ``tableDescription`` The description of the table to be created. ``shape`` The shape of data fields in the table. ``nrows`` The number of data rows to be generated for the table. Sub-classes may redefine the following attributes: ``indexed`` Whether columns shall be indexed, if possible. Default is not to index them. ``optlevel`` The level of optimisation of column indexes. Default is 0. """ indexed = False optlevel = 0 colNotIndexable_re = re.compile(r"\bcan not be indexed\b") condNotBoolean_re = re.compile(r"\bdoes not have a boolean type\b") def createIndexes(self, colname, ncolname, extracolname): if not self.indexed: return try: kind = self.kind vprint("* Indexing ``%s`` columns. Type: %s." % (colname, kind)) for acolname in [colname, ncolname, extracolname]: acolumn = self.table.colinstances[acolname] acolumn.createIndex(kind=self.kind, optlevel=self.optlevel, _blocksizes=small_blocksizes, _testmode=True) except TypeError, te: if self.colNotIndexable_re.search(str(te)): raise common.SkipTest( "Columns of this type can not be indexed.") raise except NotImplementedError: raise common.SkipTest( "Indexing columns of this type is not supported yet.")
def newmethod(self, *args, **kwargs): if not t.is_pro: raise common.SkipTest("Indexing is not supported.") return oldmethod(self, *args, **kwargs)
def test_method(self): vprint("* Condition is ``%s``." % cond) # Replace bitwise operators with their logical counterparts. pycond = cond for (ptop, pyop) in [('&', 'and'), ('|', 'or'), ('~', 'not')]: pycond = pycond.replace(ptop, pyop) pycond = compile(pycond, '<string>', 'eval') table = self.table self.createIndexes(colname, ncolname, 'c_idxextra') table_slice = dict(start=1, stop=table.nrows - 5, step=3) rownos, fvalues = None, None # Test that both simple and nested columns work as expected. # Knowing how the table is filled, results must be the same. for acolname in [colname, ncolname]: # First the reference Python version. pyrownos, pyfvalues, pyvars = [], [], condvars.copy() for row in table.iterrows(**table_slice): pyvars[colname] = row[acolname] pyvars['c_extra'] = row['c_extra'] pyvars['c_idxextra'] = row['c_idxextra'] try: isvalidrow = eval(pycond, {}, pyvars) except TypeError: raise common.SkipTest( "The Python type does not support the operation.") if isvalidrow: pyrownos.append(row.nrow) pyfvalues.append(row[acolname]) pyrownos = numpy.array(pyrownos) # row numbers already sorted pyfvalues = numpy.array(pyfvalues, dtype=sctype) pyfvalues.sort() vprint("* %d rows selected by Python from ``%s``." % (len(pyrownos), acolname)) if rownos is None: rownos = pyrownos # initialise reference results fvalues = pyfvalues else: self.assert_(numpy.all(pyrownos == rownos)) # check self.assert_(numpy.all(pyfvalues == fvalues)) # Then the in-kernel or indexed version. ptvars = condvars.copy() ptvars[colname] = table.colinstances[acolname] ptvars['c_extra'] = table.colinstances['c_extra'] ptvars['c_idxextra'] = table.colinstances['c_idxextra'] try: isidxq = table.willQueryUseIndexing(cond, ptvars) # Query twice to trigger possible query result caching. ptrownos = [ table.getWhereList(cond, condvars, sort=True, **table_slice) for _ in range(2) ] ptfvalues = [ table.readWhere(cond, condvars, field=acolname, **table_slice) for _ in range(2) ] except TypeError, te: if self.condNotBoolean_re.search(str(te)): raise common.SkipTest("The condition is not boolean.") raise except NotImplementedError: raise common.SkipTest( "The PyTables type does not support the operation.")
for acolname in [colname, ncolname, extracolname]: acolumn = self.table.colinstances[acolname] acolumn.createIndex(kind=self.kind, optlevel=self.optlevel, _blocksizes=small_blocksizes, _testmode=True) except TypeError, te: if self.colNotIndexable_re.search(str(te)): raise common.SkipTest( "Columns of this type can not be indexed.") raise except tables.NoIndexingError: raise common.SkipTest("Indexing is not supported.") except NotImplementedError: raise common.SkipTest( "Indexing columns of this type is not supported yet.") def setUp(self): super(BaseTableQueryTestCase, self).setUp() self.table = table = self.h5file.createTable('/', 'test', self.tableDescription, expectedrows=self.nrows) fill_table(table, self.shape, self.nrows) class ScalarTableMixin: tableDescription = TableDescription shape = ()