MCPcopy Create free account
hub / github.com/PyTables/PyTables / _compile_condition

Method _compile_condition

tables/table.py:1395–1453  ·  view source on GitHub ↗

Compile the `condition` and extract usable index conditions. This method returns an instance of ``CompiledCondition``. See the ``compile_condition()`` function in the ``conditions`` module for more information about the compilation process. This method makes use of

(
        self,
        condition: str,
        condvars: dict[str, Column | np.ndarray],
    )

Source from the content-addressed store, hash-verified

1393 return condkey
1394
1395 def _compile_condition(
1396 self,
1397 condition: str,
1398 condvars: dict[str, Column | np.ndarray],
1399 ) -> CompiledCondition:
1400 """Compile the `condition` and extract usable index conditions.
1401
1402 This method returns an instance of ``CompiledCondition``. See
1403 the ``compile_condition()`` function in the ``conditions``
1404 module for more information about the compilation process.
1405
1406 This method makes use of the condition cache when possible.
1407
1408 """
1409 # Look up the condition in the condition cache.
1410 condcache = self._condition_cache
1411 condkey = self._get_condition_key(condition, condvars)
1412 compiled = condcache.get(condkey)
1413 if compiled:
1414 return compiled.with_replaced_vars(condvars) # bingo!
1415
1416 # Bad luck, the condition must be parsed and compiled.
1417 # Fortunately, the key provides some valuable information. ;)
1418 condition, colnames, varnames, colpaths, vartypes = condkey
1419
1420 # Extract more information from referenced columns.
1421
1422 # start with normal variables
1423 typemap = dict(list(zip(varnames, vartypes)))
1424 indexedcols = []
1425 for colname in colnames:
1426 col = condvars[colname]
1427
1428 # Extract types from *all* the given variables.
1429 coltype = col.dtype.type
1430 typemap[colname] = _nxtype_from_nptype[coltype]
1431
1432 # Get the set of columns with usable indexes.
1433 if (
1434 self._enabled_indexing_in_queries # no in-kernel searches
1435 and self.colindexed[col.pathname]
1436 and not col.index.dirty
1437 ):
1438 indexedcols.append(colname)
1439
1440 indexedcols = frozenset(indexedcols)
1441 # Now let ``compile_condition()`` do the Numexpr-related job.
1442 compiled = compile_condition(condition, typemap, indexedcols)
1443
1444 # Check that there actually are columns in the condition.
1445 if not set(compiled.parameters).intersection(set(colnames)):
1446 raise ValueError(
1447 f"there are no columns taking part in "
1448 f"condition ``{condition}``"
1449 )
1450
1451 # Store the compiled condition in the cache and return it.
1452 condcache[condkey] = compiled

Callers 2

_whereMethod · 0.95

Calls 5

_get_condition_keyMethod · 0.95
compile_conditionFunction · 0.85
with_replaced_varsMethod · 0.80
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected