Cannot process string literals and references as operands in ConditionComparison
In two cases where ConditionComparison
is used, there is ambiguity in the grammar and then in the interpreter affecting using string literals and references as operands. They are parsed and then interpreted as column names. In case of Calculator
type, this leads to a crash with key error (see two minimal examples below). Numerical and boolean literals work as expected.
Input:
calc = Calculator vasp == 5.4.4 (
(algo: 'Fast', 'VeryFast'),
(encut: 400.0, 350.) [eV]
)
limit = 350 [eV]
calc_1 = calc where encut > limit
print(calc_1)
Output:
Traceback (most recent call last):
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3805, in get_loc
return self._engine.get_loc(casted_key)
File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'limit'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "python-3.10.12/bin/texts", line 8, in <module>
sys.exit(texts())
File "vre-language/src/virtmat/language/cli/__init__.py", line 28, in texts
clargs.func(clargs)
File "vre-language/src/virtmat/language/cli/run_model.py", line 156, in main
evaluate_prog(prog)
File "vre-language/src/virtmat/language/utilities/errors.py", line 304, in wrapper
process_error(err)
File "vre-language/src/virtmat/language/utilities/errors.py", line 292, in process_error
raise err.__cause__
File "vre-language/src/virtmat/language/utilities/errors.py", line 207, in wrapper
return func(*args, **kwargs)
File "vre-language/src/virtmat/language/utilities/typemap.py", line 117, in wrapper
return checktype_(func(obj), obj.type_)
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 52, in program_value
vals = [p.value for p in get_children_of_type('Print', self) if p.value]
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 52, in <listcomp>
vals = [p.value for p in get_children_of_type('Print', self) if p.value]
File "vre-language/src/virtmat/language/utilities/errors.py", line 304, in wrapper
process_error(err)
File "vre-language/src/virtmat/language/utilities/errors.py", line 292, in process_error
raise err.__cause__
File "vre-language/src/virtmat/language/utilities/errors.py", line 207, in wrapper
return func(*args, **kwargs)
File "vre-language/src/virtmat/language/utilities/typemap.py", line 117, in wrapper
return checktype_(func(obj), obj.type_)
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 417, in condition_comparison_value
column_right = qobj_ref_val[self.column_right] if self.column_right else None
File "vre-language/src/virtmat/language/utilities/amml.py", line 339, in __getitem__
return super().__getitem__(key)
File "vre-language/src/virtmat/language/utilities/amml.py", line 308, in __getitem__
return self.parameters[key]
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/frame.py", line 4102, in __getitem__
indexer = self.columns.get_loc(key)
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3812, in get_loc
raise KeyError(key) from err
KeyError: 'limit'
Input:
calc = Calculator vasp == 5.4.4 (
(algo: 'Fast', 'VeryFast'),
(encut: 400.0, 350.) [eV]
)
calc_2 = calc where algo == 'Fast'
print(calc_2)
Output:
Traceback (most recent call last):
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3805, in get_loc
return self._engine.get_loc(casted_key)
File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Fast'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "python-3.10.12/bin/texts", line 8, in <module>
sys.exit(texts())
File "vre-language/src/virtmat/language/cli/__init__.py", line 28, in texts
clargs.func(clargs)
File "vre-language/src/virtmat/language/cli/run_model.py", line 156, in main
evaluate_prog(prog)
File "vre-language/src/virtmat/language/utilities/errors.py", line 304, in wrapper
process_error(err)
File "vre-language/src/virtmat/language/utilities/errors.py", line 292, in process_error
raise err.__cause__
File "vre-language/src/virtmat/language/utilities/errors.py", line 207, in wrapper
return func(*args, **kwargs)
File "vre-language/src/virtmat/language/utilities/typemap.py", line 117, in wrapper
return checktype_(func(obj), obj.type_)
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 52, in program_value
vals = [p.value for p in get_children_of_type('Print', self) if p.value]
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 52, in <listcomp>
vals = [p.value for p in get_children_of_type('Print', self) if p.value]
File "vre-language/src/virtmat/language/utilities/errors.py", line 304, in wrapper
process_error(err)
File "vre-language/src/virtmat/language/utilities/errors.py", line 292, in process_error
raise err.__cause__
File "vre-language/src/virtmat/language/utilities/errors.py", line 207, in wrapper
return func(*args, **kwargs)
File "vre-language/src/virtmat/language/utilities/typemap.py", line 117, in wrapper
return checktype_(func(obj), obj.type_)
File "vre-language/src/virtmat/language/interpreter/instant_executor.py", line 417, in condition_comparison_value
column_right = qobj_ref_val[self.column_right] if self.column_right else None
File "vre-language/src/virtmat/language/utilities/amml.py", line 339, in __getitem__
return super().__getitem__(key)
File "vre-language/src/virtmat/language/utilities/amml.py", line 308, in __getitem__
return self.parameters[key]
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/frame.py", line 4102, in __getitem__
indexer = self.columns.get_loc(key)
File "python-3.10.12/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3812, in get_loc
raise KeyError(key) from err
KeyError: 'Fast'
If we replace the Calculator
with Table
:
calc = (
(lreal: true, false),
(encut: 400.0, 350.) [eV]
)
limit = 350 [eV]
calc_1 = calc where encut > limit
print(calc_1)
we get this error message:
Value error: /mnt/data/ubuntu/work/vre-language/examples/8.vm:7:21 --> encut > limit <--
column "limit" not found in Table "calc"
which is basically the same problem. The lack of static check in Calculator causes a crash at run time. Similarly,
calc = (
(algo: 'Fast', 'VeryFast'),
(encut: 400.0, 350.) [eV]
)
calc_1 = calc where algo == 'Fast'
print(calc_1)
causes:
Value error: /mnt/data/ubuntu/work/vre-language/examples/8.vm:6:21 --> algo == 'Fast' <--
column "Fast" not found in Table "calc"