|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# 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, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +#' @include generics.R column.R |
| 19 | +NULL |
| 20 | + |
| 21 | +#' @title S4 expression functions for DataFrame column(s) |
| 22 | +#' @description These are expression functions on DataFrame columns |
| 23 | + |
| 24 | +functions1 <- c( |
| 25 | + "abs", "acos", "approxCountDistinct", "ascii", "asin", "atan", |
| 26 | + "avg", "base64", "bin", "bitwiseNOT", "cbrt", "ceil", "cos", "cosh", "count", |
| 27 | + "crc32", "dayofmonth", "dayofyear", "exp", "explode", "expm1", "factorial", |
| 28 | + "first", "floor", "hex", "hour", "initcap", "isNaN", "last", "last_day", |
| 29 | + "length", "log", "log10", "log1p", "log2", "lower", "ltrim", "max", "md5", |
| 30 | + "mean", "min", "minute", "month", "negate", "quarter", "reverse", |
| 31 | + "rint", "round", "rtrim", "second", "sha1", "signum", "sin", "sinh", "size", |
| 32 | + "soundex", "sqrt", "sum", "sumDistinct", "tan", "tanh", "toDegrees", |
| 33 | + "toRadians", "to_date", "trim", "unbase64", "unhex", "upper", "weekofyear", |
| 34 | + "year") |
| 35 | +functions2 <- c( |
| 36 | + "atan2", "datediff", "hypot", "levenshtein", "months_between", "nanvl", "pmod") |
| 37 | + |
| 38 | +createFunction1 <- function(name) { |
| 39 | + setMethod(name, |
| 40 | + signature(x = "Column"), |
| 41 | + function(x) { |
| 42 | + jc <- callJStatic("org.apache.spark.sql.functions", name, x@jc) |
| 43 | + column(jc) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +createFunction2 <- function(name) { |
| 48 | + setMethod(name, |
| 49 | + signature(y = "Column"), |
| 50 | + function(y, x) { |
| 51 | + if (class(x) == "Column") { |
| 52 | + x <- x@jc |
| 53 | + } |
| 54 | + jc <- callJStatic("org.apache.spark.sql.functions", name, y@jc, x) |
| 55 | + column(jc) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +createFunctions <- function() { |
| 60 | + for (name in functions1) { |
| 61 | + createFunction1(name) |
| 62 | + } |
| 63 | + for (name in functions2) { |
| 64 | + createFunction2(name) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +createFunctions() |
| 69 | + |
| 70 | +#' @rdname functions |
| 71 | +#' @return Creates a Column class of literal value. |
| 72 | +setMethod("lit", signature("ANY"), |
| 73 | + function(x) { |
| 74 | + jc <- callJStatic("org.apache.spark.sql.functions", "lit", ifelse(class(x) == "Column", x@jc, x)) |
| 75 | + column(jc) |
| 76 | + }) |
| 77 | + |
| 78 | +#' Approx Count Distinct |
| 79 | +#' |
| 80 | +#' @rdname functions |
| 81 | +#' @return the approximate number of distinct items in a group. |
| 82 | +setMethod("approxCountDistinct", |
| 83 | + signature(x = "Column"), |
| 84 | + function(x, rsd = 0.95) { |
| 85 | + jc <- callJStatic("org.apache.spark.sql.functions", "approxCountDistinct", x@jc, rsd) |
| 86 | + column(jc) |
| 87 | + }) |
| 88 | + |
| 89 | +#' Count Distinct |
| 90 | +#' |
| 91 | +#' @rdname functions |
| 92 | +#' @return the number of distinct items in a group. |
| 93 | +setMethod("countDistinct", |
| 94 | + signature(x = "Column"), |
| 95 | + function(x, ...) { |
| 96 | + jcol <- lapply(list(...), function (x) { |
| 97 | + x@jc |
| 98 | + }) |
| 99 | + jc <- callJStatic("org.apache.spark.sql.functions", "countDistinct", x@jc, |
| 100 | + listToSeq(jcol)) |
| 101 | + column(jc) |
| 102 | + }) |
| 103 | + |
| 104 | +#' @rdname functions |
| 105 | +#' @return Concatenates multiple input string columns together into a single string column. |
| 106 | +setMethod("concat", |
| 107 | + signature(x = "Column"), |
| 108 | + function(x, ...) { |
| 109 | + jcols <- lapply(list(x, ...), function(x) { x@jc }) |
| 110 | + jc <- callJStatic("org.apache.spark.sql.functions", "concat", listToSeq(jcols)) |
| 111 | + column(jc) |
| 112 | + }) |
| 113 | + |
| 114 | +#' @rdname functions |
| 115 | +#' @return Returns the greatest value of the list of column names, skipping null values. |
| 116 | +#' This function takes at least 2 parameters. It will return null if all parameters are null. |
| 117 | +setMethod("greatest", |
| 118 | + signature(x = "Column"), |
| 119 | + function(x, ...) { |
| 120 | + stopifnot(length(list(...)) > 0) |
| 121 | + jcols <- lapply(list(x, ...), function(x) { x@jc }) |
| 122 | + jc <- callJStatic("org.apache.spark.sql.functions", "greatest", listToSeq(jcols)) |
| 123 | + column(jc) |
| 124 | + }) |
| 125 | + |
| 126 | +#' @rdname functions |
| 127 | +#' @return Returns the least value of the list of column names, skipping null values. |
| 128 | +#' This function takes at least 2 parameters. It will return null iff all parameters are null. |
| 129 | +setMethod("least", |
| 130 | + signature(x = "Column"), |
| 131 | + function(x, ...) { |
| 132 | + stopifnot(length(list(...)) > 0) |
| 133 | + jcols <- lapply(list(x, ...), function(x) { x@jc }) |
| 134 | + jc <- callJStatic("org.apache.spark.sql.functions", "least", listToSeq(jcols)) |
| 135 | + column(jc) |
| 136 | + }) |
| 137 | + |
| 138 | +#' @rdname functions |
| 139 | +#' @aliases ceil |
| 140 | +setMethod("ceiling", |
| 141 | + signature(x = "Column"), |
| 142 | + function(x) { |
| 143 | + ceil(x) |
| 144 | + }) |
| 145 | + |
| 146 | +#' @rdname functions |
| 147 | +#' @aliases signum |
| 148 | +setMethod("sign", signature(x = "Column"), |
| 149 | + function(x) { |
| 150 | + signum(x) |
| 151 | + }) |
| 152 | + |
| 153 | +#' @rdname functions |
| 154 | +#' @aliases countDistinct |
| 155 | +setMethod("n_distinct", signature(x = "Column"), |
| 156 | + function(x, ...) { |
| 157 | + countDistinct(x, ...) |
| 158 | + }) |
| 159 | + |
| 160 | +#' @rdname functions |
| 161 | +#' @aliases count |
| 162 | +setMethod("n", signature(x = "Column"), |
| 163 | + function(x) { |
| 164 | + count(x) |
| 165 | + }) |
0 commit comments