Copilot commented on code in PR #468:
URL: https://github.com/apache/sedona-db/pull/468#discussion_r2656993137


##########
r/sedonadb/R/expression.R:
##########
@@ -0,0 +1,287 @@
+# 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.
+
+#' Create SedonaDB logical expressions
+#'
+#' @param column_name A column name
+#' @param qualifier An optional qualifier (e.g., table reference) that may be
+#'   used to disambiguate a specific reference
+#' @param function_name The name of the function to call. This name is resolved
+#'   from the context associated with `factory`.
+#' @param type A destination type into which `expr` should be cast.
+#' @param expr A SedonaDBExpr or object coercible to one with [as_sd_expr()].
+#' @param alias An alias to apply to `expr`.
+#' @param op Operator name for a binary expression. In general these follow
+#'   R function names (e.g., `>`, `<`, `+`, `-`).
+#' @param lhs,rhs Arguments to a binary expression
+#' @param factory A [sd_expr_factory()]. This factory wraps a SedonaDB context
+#'   and is used to resolve scalar functions and/or retrieve options.
+#'
+#' @returns An object of class SedonaDBExpr
+#' @export
+#'
+#' @examples
+#' sd_expr_column("foofy")
+#' sd_expr_literal(1L)
+#' sd_expr_scalar_function("abs", list(1L))
+#' sd_expr_cast(1L, nanoarrow::na_int64())
+#' sd_expr_alias(1L, "foofy")
+#'
+sd_expr_column <- function(column_name, qualifier = NULL, factory = 
sd_expr_factory()) {
+  factory$column(column_name, qualifier)
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_literal <- function(x, type = NULL, factory = sd_expr_factory()) {
+  as_sedonadb_literal(x, type = type, factory = factory)
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_binary <- function(op, lhs, rhs, factory = sd_expr_factory()) {
+  factory$binary(op, as_sd_expr(lhs), as_sd_expr(rhs))
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_negative <- function(expr, factory = sd_expr_factory()) {
+  as_sd_expr(expr, factory = factory)$negate()
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_scalar_function <- function(function_name, args, factory = 
sd_expr_factory()) {
+  args_as_expr <- lapply(args, as_sd_expr, factory = factory)
+  # Not sure why we need this exactly (something about savvy)
+  args_as_expr_ptr <- lapply(args_as_expr, "[[", ".ptr")
+  factory$scalar_function(function_name, args_as_expr_ptr)
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_aggregate_function <- function(function_name, args, ...,
+                                       na.rm = FALSE, distinct = FALSE, 
factory = sd_expr_factory()) {
+  args_as_expr <- lapply(args, as_sd_expr, factory = factory)
+  # Not sure why we need this exactly (something about savvy)
+  args_as_expr_ptr <- lapply(args_as_expr, "[[", ".ptr")
+  factory$aggregate_function(function_name, args_as_expr_ptr, na_rm = na.rm, 
distinct = distinct)
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_cast <- function(expr, type, factory = sd_expr_factory()) {
+  expr <- as_sd_expr(expr, factory = factory)
+  type <- nanoarrow::as_nanoarrow_schema(type)
+  expr$cast(type)
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_alias <- function(expr, alias, factory = sd_expr_factory()) {
+  expr <- as_sd_expr(expr, factory = factory)
+  expr$alias(alias)
+}
+
+#' @rdname sd_expr_column
+#' @export
+as_sd_expr <- function(x, factory = sd_expr_factory()) {
+  if (inherits(x, "SedonaDBExpr")) {
+    x
+  } else {
+    sd_expr_literal(x, factory = factory)
+  }
+}
+
+#' @rdname sd_expr_column
+#' @export
+is_sd_expr <- function(x) {
+  inherits(x, "SedonaDBExpr")
+}
+
+#' @rdname sd_expr_column
+#' @export
+sd_expr_factory <- function() {
+  SedonaDBExprFactory$new(ctx())
+}
+
+#' @export
+print.SedonaDBExpr <- function(x, ...) {
+  cat("<SedonaDBExpr>\n")
+  cat(x$display())
+  cat("\n")
+  invisible(x)
+}
+
+#' Evaluate an R expression into a SedonaDB expression
+#'
+#' @param expr An R expression (e.g., the result of `quote()`).
+#' @param expr_ctx An `sd_expr_ctx()`
+#'
+#' @returns A `SedonaDBExpr`
+#' @noRd
+sd_eval_expr <- function(expr, expr_ctx = sd_expr_ctx(env = env), env = 
parent.frame()) {
+  ensure_translations_registered()
+
+  rlang::try_fetch({
+    result <- sd_eval_expr_inner(expr, expr_ctx)
+    as_sd_expr(result, factory = factory)

Review Comment:
   Variable 'factory' is referenced but not defined in this function's scope. 
It should be extracted from 'expr_ctx$factory'.
   ```suggestion
       as_sd_expr(result, factory = expr_ctx$factory)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to