R/filter.R

Defines functions duckplyr_filter filter.duckplyr_df

# Generated by 02-duckplyr_df-methods.R

filter.duckplyr_df <- function(.data, ..., .by = NULL, .preserve = FALSE) {
  force(.data)

  dots <- dplyr_quosures(...)
  check_filter(dots)

  by <- enquo(.by)

  rel_try(list(name = "filter", x = .data, args = try_list(dots = dots, by = by, preserve = .preserve)),
    "Can't use relational with zero-column result set." = (length(.data) == 0),
    "Can't use relational without filter conditions." = (length(dots) == 0),
    "Can't use relational with grouped operation." = (!quo_is_null(by)), # (length(by$names) > 0),
    {
      exprs <- rel_translate_dots(dots, .data)
      rel <- duckdb_rel_from_df(.data)

      # FIXME: Seems to be necessary only if alternations `|` are used in `exprs`.
      # Add only then?
      rel <- oo_prep(rel)

      rel <- rel_filter(rel, exprs)

      out_rel <- oo_restore(rel)

      out <- rel_to_df(out_rel)
      out <- dplyr_reconstruct(out, .data)
      return(out)
    }
  )

  # dplyr forward
  filter <- dplyr$filter.data.frame
  out <- filter(.data, ..., .by = {{ .by }}, .preserve = .preserve)
  return(out)

  # dplyr implementation
  dots <- dplyr_quosures(...)
  check_filter(dots)

  by <- compute_by(
    by = {{ .by }},
    data = .data,
    by_arg = ".by",
    data_arg = ".data"
  )

  loc <- filter_rows(.data, dots, by)
  dplyr_row_slice(.data, loc, preserve = .preserve)
}

duckplyr_filter <- function(.data, ...) {
  try_fetch(
    .data <- as_duckplyr_df(.data),
    error = function(e) {
      testthat::skip(conditionMessage(e))
    }
  )
  out <- filter(.data, ...)
  class(out) <- setdiff(class(out), "duckplyr_df")
  out
}
duckdblabs/duckplyr documentation built on Nov. 6, 2024, 10 p.m.