1# Licensed to the Apache Software Foundation (ASF) under one 2# or more contributor license agreements. See the NOTICE file 3# distributed with this work for additional information 4# regarding copyright ownership. The ASF licenses this file 5# to you under the Apache License, Version 2.0 (the 6# "License"); you may not use this file except in compliance 7# with the License. You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, 12# software distributed under the License is distributed on an 13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14# KIND, either express or implied. See the License for the 15# specific language governing permissions and limitations 16# under the License. 17 18context("Expressions") 19 20test_that("Can create an expression", { 21 expect_is(build_array_expression(">", Array$create(1:5), 4), "array_expression") 22}) 23 24test_that("as.vector(array_expression)", { 25 expect_equal(as.vector(build_array_expression(">", Array$create(1:5), 4)), c(FALSE, FALSE, FALSE, FALSE, TRUE)) 26}) 27 28test_that("array_expression print method", { 29 expect_output( 30 print(build_array_expression(">", Array$create(1:5), 4)), 31 # Not ideal but it is informative 32 "greater(<Array>, 4)", 33 fixed = TRUE 34 ) 35}) 36 37test_that("C++ expressions", { 38 f <- Expression$field_ref("f") 39 g <- Expression$field_ref("g") 40 date <- Expression$scalar(as.Date("2020-01-15")) 41 ts <- Expression$scalar(as.POSIXct("2020-01-17 11:11:11")) 42 i64 <- Expression$scalar(bit64::as.integer64(42)) 43 time <- Expression$scalar(hms::hms(56, 34, 12)) 44 45 expect_is(f == g, "Expression") 46 expect_is(f == 4, "Expression") 47 expect_is(f == "", "Expression") 48 expect_is(f == NULL, "Expression") 49 expect_is(f == date, "Expression") 50 expect_is(f == i64, "Expression") 51 expect_is(f == time, "Expression") 52 # can't seem to make this work right now because of R Ops.method dispatch 53 # expect_is(f == as.Date("2020-01-15"), "Expression") 54 expect_is(f == ts, "Expression") 55 expect_is(f <= 2L, "Expression") 56 expect_is(f != FALSE, "Expression") 57 expect_is(f > 4, "Expression") 58 expect_is(f < 4 & f > 2, "Expression") 59 expect_is(f < 4 | f > 2, "Expression") 60 expect_is(!(f < 4), "Expression") 61 expect_output( 62 print(f > 4), 63 'Expression\n(f > 4)', 64 fixed = TRUE 65 ) 66 # Interprets that as a list type 67 expect_is(f == c(1L, 2L), "Expression") 68}) 69 70test_that("Can create an expression", { 71 a <- Array$create(as.numeric(1:5)) 72 expr <- array_expression("cast", a, options = list(to_type = int32())) 73 expect_is(expr, "array_expression") 74 expect_equal(eval_array_expression(expr), Array$create(1:5)) 75 76 b <- Array$create(0.5:4.5) 77 bad_expr <- array_expression("cast", b, options = list(to_type = int32())) 78 expect_is(bad_expr, "array_expression") 79 expect_error( 80 eval_array_expression(bad_expr), 81 "Invalid: Float value .* was truncated converting" 82 ) 83 expr <- array_expression("cast", b, options = list(to_type = int32(), allow_float_truncate = TRUE)) 84 expect_is(expr, "array_expression") 85 expect_equal(eval_array_expression(expr), Array$create(0:4)) 86}) 87