File: test-expression.R

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (154 lines) | stat: -rw-r--r-- 4,869 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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")

  # Non-Expression inputs are wrapped in Expression$scalar()
  expect_equal(
    Expression$create("add", 1, 2),
    Expression$create("add", Expression$scalar(1), Expression$scalar(2))
  )
})

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("Nested field refs", {
  x <- Expression$field_ref("x")
  nested <- x$y
  expect_r6_class(nested, "Expression")
  expect_r6_class(x[["y"]], "Expression")
  expect_r6_class(nested$z, "Expression")
  expect_error(Expression$scalar(42L)$y, "Cannot extract a field from an Expression of 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()
  )
})

test_that("Nested field ref types", {
  nested <- Expression$field_ref("x")$y
  schm <- schema(x = struct(y = int32(), z = double()))
  expect_equal(nested$type(schm), int32())
  # implicit casting and schema propagation
  x <- Expression$field_ref("x")
  x$schema <- schm
  expect_equal((x$y * 2)$type(), int32())
})

test_that("Nested field from a non-field-ref (struct_field kernel)", {
  x <- Expression$scalar(tibble::tibble(a = 1, b = "two"))
  expect_true(inherits(x$a, "Expression"))
  expect_equal(x$a$type(), float64())
  expect_error(x$c, "field 'c' not found in struct<a: double, b: string>")
})