def test_missing_reference():
    expr = Expressions.and_(Expressions.equal("t", 5),
                            Expressions.equal("x", 7))
    try:
        Binder.bind(STRUCT, expr)
    except ice_ex.ValidationException as e:
        assert "Cannot find field 't' in struct" in "{}".format(e)
def test_multiple_references(assert_all_bound):
    expr = Expressions.or_(
        Expressions.and_(Expressions.equal("x", 7),
                         Expressions.less_than("y", 100)),
        Expressions.greater_than("z", -100))

    assert_all_bound("Multiple references", Binder.bind(STRUCT, expr))
def test_basic_simplification(assert_and_unwrap):
    # Should simplify or expression to alwaysTrue
    assert Expressions.always_true() == Binder.bind(
        STRUCT,
        Expressions.or_(Expressions.less_than("y", 100),
                        Expressions.greater_than("z", -9999999999)))
    # Should simplify or expression to alwaysfalse
    assert Expressions.always_false() == Binder.bind(
        STRUCT,
        Expressions.and_(Expressions.less_than("y", 100),
                         Expressions.less_than("z", -9999999999)))

    bound = Binder.bind(
        STRUCT,
        Expressions.not_(Expressions.not_(Expressions.less_than("y", 100))))
    pred = assert_and_unwrap(bound, None)
    assert 1 == pred.ref.field_id
def test_not(assert_all_bound, assert_and_unwrap):
    expr = Expressions.not_(Expressions.equal("x", 7))
    bound_expr = Binder.bind(STRUCT, expr)
    assert_all_bound("Not", bound_expr)

    not_ = assert_and_unwrap(bound_expr, Not)

    child = assert_and_unwrap(not_.child, None)
    # should bind x correctly
    assert 0 == child.ref.field_id
def test_or(assert_all_bound, assert_and_unwrap):
    expr = Expressions.or_(Expressions.greater_than("z", -100),
                           Expressions.less_than("y", 100))
    bound_expr = Binder.bind(STRUCT, expr)
    assert_all_bound("Or", bound_expr)

    or_ = assert_and_unwrap(bound_expr, Or)

    left = assert_and_unwrap(or_.left, None)
    # should bind z correctly
    assert 2 == left.ref.field_id
    right = assert_and_unwrap(or_.right, None)
    # should bind y correctly
    assert 1 == right.ref.field_id
def test_and(assert_all_bound, assert_and_unwrap):
    expr = Expressions.and_(Expressions.equal("x", 7),
                            Expressions.less_than("y", 100))
    bound_expr = Binder.bind(STRUCT, expr)
    assert_all_bound("And", bound_expr)

    and_ = assert_and_unwrap(bound_expr, And)

    left = assert_and_unwrap(and_.left, None)
    # should bind x correctly
    assert 0 == left.ref.field_id
    right = assert_and_unwrap(and_.right, None)
    # should bind y correctly
    assert 1 == right.ref.field_id
    def _lazy_column_projection(self):
        if "*" in self.selected_columns:
            if len(self.minused_cols) == 0:
                return self._schema
            self.selected_columns = [field.name for field in self._schema.as_struct().fields]
            final_selected_cols = [column for column in self.selected_columns if column not in self.minused_cols]
        else:
            final_selected_cols = self.selected_columns

        required_field_ids = set()
        required_field_ids.update(Binder.bound_references(self.table.schema().as_struct(),
                                                          [self._row_filter],
                                                          self._case_sensitive))

        if self._case_sensitive:
            selected_ids = get_projected_ids(self.table.schema().select(final_selected_cols))
        else:
            selected_ids = get_projected_ids(self.table.schema().case_insensitive_select(final_selected_cols))

        required_field_ids.update(selected_ids)

        return select(self.table.schema(), required_field_ids)
def test_case_sensitive_reference():
    with raises(ice_ex.ValidationException):
        expr = Expressions.not_(Expressions.equal("X", 7))
        Binder.bind(STRUCT, expr, case_sensitive=True)
def test_case_insensitive_reference(assert_all_bound):
    expr = Expressions.not_(Expressions.equal("X", 7))
    assert_all_bound("Single reference",
                     Binder.bind(STRUCT, expr, case_sensitive=False))
def test_single_reference(assert_all_bound):
    expr = Expressions.not_(Expressions.equal("x", 7))
    assert_all_bound("Single reference", Binder.bind(STRUCT, expr))
def test_bound_expression_fails():
    with raises(RuntimeError):
        expr = Expressions.not_(Expressions.equal("x", 7))
        Binder.bind(STRUCT, Binder.bind(STRUCT, expr))
def test_always_false():
    assert Expressions.always_false() == Binder.bind(
        STRUCT, Expressions.always_false())