|
| 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 | +# WindowSpec.R - WindowSpec class and methods implemented in S4 OO classes |
| 19 | + |
| 20 | +#' @include generics.R jobj.R column.R |
| 21 | +NULL |
| 22 | + |
| 23 | +#' @title S4 class that represents a WindowSpec |
| 24 | +#' @description WindowSpec can be created by using window.partitionBy() |
| 25 | +#' or window.orderBy() |
| 26 | +#' @rdname WindowSpec |
| 27 | +#' @seealso \link{window.partitionBy}, \link{window.orderBy} |
| 28 | +#' |
| 29 | +#' @param sws A Java object reference to the backing Scala WindowSpec |
| 30 | +#' @export |
| 31 | +setClass("WindowSpec", |
| 32 | + slots = list(sws = "jobj")) |
| 33 | + |
| 34 | +setMethod("initialize", "WindowSpec", function(.Object, sws) { |
| 35 | + .Object@sws <- sws |
| 36 | + .Object |
| 37 | +}) |
| 38 | + |
| 39 | +windowSpec <- function(sws) { |
| 40 | + stopifnot(class(sws) == "jobj") |
| 41 | + new("WindowSpec", sws) |
| 42 | +} |
| 43 | + |
| 44 | +#' @rdname show |
| 45 | +setMethod("show", "WindowSpec", |
| 46 | + function(object) { |
| 47 | + cat("WindowSpec", callJMethod(object@sws, "toString"), "\n") |
| 48 | + }) |
| 49 | + |
| 50 | +#' partitionBy |
| 51 | +#' |
| 52 | +#' Defines the partitioning columns in a WindowSpec. |
| 53 | +#' |
| 54 | +#' @param x a WindowSpec |
| 55 | +#' @return a WindowSpec |
| 56 | +#' @rdname partitionBy |
| 57 | +#' @name partitionBy |
| 58 | +#' @family windowspec_method |
| 59 | +#' @export |
| 60 | +#' @examples |
| 61 | +#' \dontrun{ |
| 62 | +#' partitionBy(ws, "col1", "col2") |
| 63 | +#' partitionBy(ws, df$col1, df$col2) |
| 64 | +#' } |
| 65 | +setMethod("partitionBy", |
| 66 | + signature(x = "WindowSpec"), |
| 67 | + function(x, col, ...) { |
| 68 | + stopifnot (class(col) %in% c("character", "Column")) |
| 69 | + |
| 70 | + if (class(col) == "character") { |
| 71 | + windowSpec(callJMethod(x@sws, "partitionBy", col, list(...))) |
| 72 | + } else { |
| 73 | + jcols <- lapply(list(col, ...), function(c) { |
| 74 | + c@jc |
| 75 | + }) |
| 76 | + windowSpec(callJMethod(x@sws, "partitionBy", jcols)) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | +#' orderBy |
| 81 | +#' |
| 82 | +#' Defines the ordering columns in a WindowSpec. |
| 83 | +#' |
| 84 | +#' @param x a WindowSpec |
| 85 | +#' @return a WindowSpec |
| 86 | +#' @rdname arrange |
| 87 | +#' @name orderBy |
| 88 | +#' @family windowspec_method |
| 89 | +#' @export |
| 90 | +#' @examples |
| 91 | +#' \dontrun{ |
| 92 | +#' orderBy(ws, "col1", "col2") |
| 93 | +#' orderBy(ws, df$col1, df$col2) |
| 94 | +#' } |
| 95 | +setMethod("orderBy", |
| 96 | + signature(x = "WindowSpec", col = "character"), |
| 97 | + function(x, col, ...) { |
| 98 | + windowSpec(callJMethod(x@sws, "orderBy", col, list(...))) |
| 99 | + }) |
| 100 | + |
| 101 | +#' @rdname arrange |
| 102 | +#' @name orderBy |
| 103 | +#' @export |
| 104 | +setMethod("orderBy", |
| 105 | + signature(x = "WindowSpec", col = "Column"), |
| 106 | + function(x, col, ...) { |
| 107 | + jcols <- lapply(list(col, ...), function(c) { |
| 108 | + c@jc |
| 109 | + }) |
| 110 | + windowSpec(callJMethod(x@sws, "orderBy", jcols)) |
| 111 | + }) |
| 112 | + |
| 113 | +#' rowsBetween |
| 114 | +#' |
| 115 | +#' Defines the frame boundaries, from `start` (inclusive) to `end` (inclusive). |
| 116 | +#' |
| 117 | +#' Both `start` and `end` are relative positions from the current row. For example, "0" means |
| 118 | +#' "current row", while "-1" means the row before the current row, and "5" means the fifth row |
| 119 | +#' after the current row. |
| 120 | +#' |
| 121 | +#' @param x a WindowSpec |
| 122 | +#' @param start boundary start, inclusive. |
| 123 | +#' The frame is unbounded if this is the minimum long value. |
| 124 | +#' @param end boundary end, inclusive. |
| 125 | +#' The frame is unbounded if this is the maximum long value. |
| 126 | +#' @return a WindowSpec |
| 127 | +#' @rdname rowsBetween |
| 128 | +#' @name rowsBetween |
| 129 | +#' @family windowspec_method |
| 130 | +#' @export |
| 131 | +#' @examples |
| 132 | +#' \dontrun{ |
| 133 | +#' rowsBetween(ws, 0, 3) |
| 134 | +#' } |
| 135 | +setMethod("rowsBetween", |
| 136 | + signature(x = "WindowSpec", start = "numeric", end = "numeric"), |
| 137 | + function(x, start, end) { |
| 138 | + # "start" and "end" should be long, due to serde limitation, |
| 139 | + # limit "start" and "end" as integer now |
| 140 | + windowSpec(callJMethod(x@sws, "rowsBetween", as.integer(start), as.integer(end))) |
| 141 | + }) |
| 142 | + |
| 143 | +#' rangeBetween |
| 144 | +#' |
| 145 | +#' Defines the frame boundaries, from `start` (inclusive) to `end` (inclusive). |
| 146 | +#' |
| 147 | +#' Both `start` and `end` are relative from the current row. For example, "0" means "current row", |
| 148 | +#' while "-1" means one off before the current row, and "5" means the five off after the |
| 149 | +#' current row. |
| 150 | + |
| 151 | +#' @param x a WindowSpec |
| 152 | +#' @param start boundary start, inclusive. |
| 153 | +#' The frame is unbounded if this is the minimum long value. |
| 154 | +#' @param end boundary end, inclusive. |
| 155 | +#' The frame is unbounded if this is the maximum long value. |
| 156 | +#' @return a WindowSpec |
| 157 | +#' @rdname rangeBetween |
| 158 | +#' @name rangeBetween |
| 159 | +#' @family windowspec_method |
| 160 | +#' @export |
| 161 | +#' @examples |
| 162 | +#' \dontrun{ |
| 163 | +#' rangeBetween(ws, 0, 3) |
| 164 | +#' } |
| 165 | +setMethod("rangeBetween", |
| 166 | + signature(x = "WindowSpec", start = "numeric", end = "numeric"), |
| 167 | + function(x, start, end) { |
| 168 | + # "start" and "end" should be long, due to serde limitation, |
| 169 | + # limit "start" and "end" as integer now |
| 170 | + windowSpec(callJMethod(x@sws, "rangeBetween", as.integer(start), as.integer(end))) |
| 171 | + }) |
| 172 | + |
| 173 | +# Note that over is a method of Column class, but it is placed here to |
| 174 | +# avoid Roxygen circular-dependency between class Column and WindowSpec. |
| 175 | + |
| 176 | +#' over |
| 177 | +#' |
| 178 | +#' Define a windowing column. |
| 179 | +#' |
| 180 | +#' @rdname over |
| 181 | +#' @name over |
| 182 | +#' @family colum_func |
| 183 | +#' @export |
| 184 | +setMethod("over", |
| 185 | + signature(x = "Column", window = "WindowSpec"), |
| 186 | + function(x, window) { |
| 187 | + column(callJMethod(x@jc, "over", window@sws)) |
| 188 | + }) |
0 commit comments