summaryrefslogtreecommitdiffstats
path: root/src/arrow/r/tests/testthat/test-expression.R
blob: c4aab718d9024d47fe86d8b80b2067e4540cca7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.


test_that("C++ expressions", {
  skip_if_not_available("dataset")
  f <- Expression$field_ref("f")
  expect_identical(f$field_name, "f")
  g <- Expression$field_ref("g")
  date <- Expression$scalar(as.Date("2020-01-15"))
  ts <- Expression$scalar(as.POSIXct("2020-01-17 11:11:11"))
  i64 <- Expression$scalar(bit64::as.integer64(42))
  time <- Expression$scalar(hms::hms(56, 34, 12))

  expect_r6_class(f == g, "Expression")
  expect_r6_class(f == 4, "Expression")
  expect_r6_class(f == "", "Expression")
  expect_r6_class(f == NULL, "Expression")
  expect_r6_class(f == date, "Expression")
  expect_r6_class(f == i64, "Expression")
  expect_r6_class(f == time, "Expression")
  # can't seem to make this work right now because of R Ops.method dispatch
  # expect_r6_class(f == as.Date("2020-01-15"), "Expression") # nolint
  expect_r6_class(f == ts, "Expression")
  expect_r6_class(f <= 2L, "Expression")
  expect_r6_class(f != FALSE, "Expression")
  expect_r6_class(f > 4, "Expression")
  expect_r6_class(f < 4 & f > 2, "Expression")
  expect_r6_class(f < 4 | f > 2, "Expression")
  expect_r6_class(!(f < 4), "Expression")
  expect_output(
    print(f > 4),
    "Expression\n(f > 4)",
    fixed = TRUE
  )
  expect_equal(
    f$type(schema(f = float64())),
    float64()
  )
  expect_equal(
    (f > 4)$type(schema(f = float64())),
    bool()
  )
  # Interprets that as a list type
  expect_r6_class(f == c(1L, 2L), "Expression")

  expect_error(
    Expression$create("add", 1, 2),
    "Expression arguments must be Expression objects"
  )
})

test_that("Field reference expression schemas and types", {
  x <- Expression$field_ref("x")

  # type() throws error when schema is NULL
  expect_error(x$type(), "schema")

  # type() returns type when schema is set
  x$schema <- Schema$create(x = int32())
  expect_equal(x$type(), int32())
})

test_that("Scalar expression schemas and types", {
  # type() works on scalars without setting the schema
  expect_equal(
    Expression$scalar("foo")$type(),
    arrow::string()
  )
  expect_equal(
    Expression$scalar(42L)$type(),
    int32()
  )
})

test_that("Expression schemas and types", {
  x <- Expression$field_ref("x")
  y <- Expression$field_ref("y")
  z <- Expression$scalar(42L)

  # type() throws error when both schemas are unset
  expect_error(
    Expression$create("add_checked", x, y)$type(),
    "schema"
  )

  # type() throws error when left schema is unset
  y$schema <- Schema$create(y = float64())
  expect_error(
    Expression$create("add_checked", x, y)$type(),
    "schema"
  )

  # type() throws error when right schema is unset
  x$schema <- Schema$create(x = int32())
  y$schema <- NULL
  expect_error(
    Expression$create("add_checked", x, y)$type(),
    "schema"
  )

  # type() returns type when both schemas are set
  y$schema <- Schema$create(y = float64())
  expect_equal(
    Expression$create("add_checked", x, y)$type(),
    float64()
  )

  # type() returns type when one arg has schema set and one is scalar
  expect_equal(
    Expression$create("add_checked", x, z)$type(),
    int32()
  )
})