Exemple #1
0
    def from_string(s, all_types):
        """
        Parse string as condition object.

        Parameters
        ----------
        s: str
            String to parse.
        all_types: Dict[str, pyarrow.DataType]
            Mapping from all known columns to pyarrow types.

        Returns
        -------
        condition: Condition
            Parsed condition.

        Raises
        ------
        ValueError: If condition cannot be parsed.
        """
        m = re.match(
            pattern=r"""
            ^                    # anchor
            \s*                  # optional space
            \(?                  # optional open bracket
            \s*                  # optional space
            ([^!<>=\s]+)         # column name
            \s*                  # optional space
            (==|=|<=|<|>=|>|!=)  # operator
            ([^)=]+)             # value
            \)?                  # optional closing bracket
            \s*                  # optional space
            $                    # anchor
            """,
            string=s,
            flags=re.VERBOSE,
        )
        if not m:
            raise ValueError('Cannot parse condition "{s}"'.format(s=s))
        col, op, var = m.groups()

        col_obj = C(col)

        pa_type = all_types.get(col)
        if pa_type is None:
            raise ValueError(
                'Unknown column "{col}" in condition "{s}"'.format(col=col,
                                                                   s=s))
        var_f = get_str_to_python_converter(pa_type)
        var_obj = var_f(var.strip())

        if (op == "==") or (op == "="):
            return col_obj == var_obj
        elif op == "<=":
            return col_obj <= var_obj
        elif op == "<":
            return col_obj < var_obj
        elif op == ">=":
            return col_obj >= var_obj
        elif op == ">":
            return col_obj > var_obj
        elif op == "!=":
            return col_obj != var_obj
        else:
            raise RuntimeError("unreachable")
Exemple #2
0
def test_get_str_to_python_converter_unknown_type():
    pa_type = pa.struct([("f1", pa.int32()), ("f2", pa.bool_())])
    with pytest.raises(ValueError) as exc:
        get_str_to_python_converter(pa_type)
    assert str(exc.value) == "Cannot handle type struct<f1: int32, f2: bool>"
Exemple #3
0
def test_get_str_to_python_converter_failes(s, pa_type):
    converter = get_str_to_python_converter(pa_type)
    with pytest.raises(ValueError):
        converter(s)
Exemple #4
0
def test_get_str_to_python_converter_ok(s, pa_type, expected):
    converter = get_str_to_python_converter(pa_type)
    actual = converter(s)
    assert actual == expected
    assert isinstance(actual, type(expected))