Skip to content

Add DuckDB::ScalarFunctionSet#add#1223

Merged
suketa merged 2 commits intomainfrom
implement-scalar-function-set-add
Mar 29, 2026
Merged

Add DuckDB::ScalarFunctionSet#add#1223
suketa merged 2 commits intomainfrom
implement-scalar-function-set-add

Conversation

@suketa
Copy link
Copy Markdown
Owner

@suketa suketa commented Mar 29, 2026

Summary

Partial implementation of #1221 — adds the add method to ScalarFunctionSet.

Changes

  • ScalarFunctionSet#add(scalar_function): adds a ScalarFunction overload to the set. Raises TypeError for non-ScalarFunction arguments. Wraps duckdb_add_scalar_function_to_set.
  • ScalarFunction.create: name: is now optional (defaults to nil), so functions can be created without a name for use inside a set (the set provides the name).
  • Tests: multiple overload scenarios — different parameter types, different parameter counts, varargs.

What's next

  • Connection#register_scalar_function_set(set)

Summary by CodeRabbit

  • New Features

    • Added an add method to scalar function sets to register additional overloads, validating inputs and returning the same set for chaining.
    • Scalar function creation now accepts an optional name, allowing nameless overloads to be created and grouped into sets.
    • Supports registering multiple overload variants (different parameter types/counts and varargs).
  • Tests

    • Added tests covering validation, chaining behavior, and overload registration scenarios.

- Add _add C method wrapping duckdb_add_scalar_function_to_set
- Add ScalarFunctionSet#add Ruby method with TypeError guard
- Make name: optional (default nil) in ScalarFunction.create so
  functions can be created without a name for use in a set
- Add tests for multiple overloads (different types, counts, varargs)

Co-authored-by: Copilot <[email protected]>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 590572aa-3474-4720-9d29-ad2fffd56f61

📥 Commits

Reviewing files that changed from the base of the PR and between cff9a04 and 4159814.

📒 Files selected for processing (2)
  • lib/duckdb/scalar_function.rb
  • test/duckdb_test/scalar_function_set_test.rb
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/duckdb_test/scalar_function_set_test.rb

📝 Walkthrough

Walkthrough

Adds a public DuckDB::ScalarFunctionSet#add that registers a DuckDB::ScalarFunction overload into a set (via a new C wrapper), and makes DuckDB::ScalarFunction.create accept an optional name: (allowing nameless overloads); also reorders keyword parameters to place return_type: first.

Changes

Cohort / File(s) Summary
Changelog
CHANGELOG.md
Documents ScalarFunctionSet#add and optional name for ScalarFunction.create.
C extension
ext/duckdb/scalar_function_set.c
Added private _add wrapper that extracts rubyDuckDBScalarFunction, calls duckdb_add_scalar_function_to_set, and raises eDuckDBError on DuckDB error (duplicate overload).
Ruby core
lib/duckdb/scalar_function.rb, lib/duckdb/scalar_function_set.rb
ScalarFunction.create signature reordered (return_type: first) and name: made optional; sf.name set only if provided. Added public ScalarFunctionSet#add with type validation delegating to _add.
Tests
test/duckdb_test/scalar_function_set_test.rb
Added tests for #add: accepts ScalarFunction, chainable return (self), raises TypeError for wrong types, and covers overload registration (duplicates, multiple overloads, differing arity, varargs).

Sequence Diagram(s)

sequenceDiagram
    participant Ruby as Ruby caller
    participant CExt as C extension (_add)
    participant DuckDB as DuckDB C API

    Ruby->>CExt: ScalarFunctionSet#add(scalar_function)
    CExt->>CExt: extract rubyDuckDBScalarFunction
    CExt->>DuckDB: duckdb_add_scalar_function_to_set(set, scalar_function)
    DuckDB-->>CExt: success / DuckDBError
    alt success
        CExt-->>Ruby: return self
    else error
        CExt-->>Ruby: raise eDuckDBError (duplicate overload)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

Poem

🐰 A hop, a function joins the set,
Nameless or named, no need to fret.
C calls the core, the overloads play,
Chainable returns to brighten the day. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add DuckDB::ScalarFunctionSet#add' clearly and concisely summarizes the primary change in the pull request: the addition of the add method to the ScalarFunctionSet class.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch implement-scalar-function-set-add

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
ext/duckdb/scalar_function_set.c (1)

47-48: Add a call-seq: doc block for the new C-exposed method.

The new function is documented as :nodoc: only; this file’s C API style expects call-seq: blocks.

Proposed patch
-/* :nodoc: */
+/*
+ * call-seq:
+ *   _add(scalar_function) -> self
+ *
+ * Internal API: adds a DuckDB::ScalarFunction to the set.
+ */
 static VALUE rbduckdb_scalar_function_set__add(VALUE self, VALUE scalar_function) {
As per coding guidelines `ext/duckdb/**/*.c`: “All C functions should use comment blocks with `call-seq:` for documentation”.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ext/duckdb/scalar_function_set.c` around lines 47 - 48, Add a C-style
documentation comment block with a call-seq: above the new function
rbduckdb_scalar_function_set__add; specifically, insert a comment that includes
"call-seq: scalar_function_set.add(scalar_function)" (or similar Ruby-facing
signature), a short one-line description of what the method does, and notes
about the parameter (scalar_function) and return value, matching the file's
existing C API comment style used elsewhere for functions like
rbduckdb_scalar_function_set__add; ensure the block appears immediately above
the static VALUE rbduckdb_scalar_function_set__add(VALUE self, VALUE
scalar_function) declaration and follows the same formatting conventions as
other ext/duckdb/*.c comment blocks.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ext/duckdb/scalar_function_set.c`:
- Around line 55-57: The error message raised when
duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function)
returns DuckDBError is overly specific ("duplicate overload?") though the branch
can represent any failure; change the rb_raise call to use a neutral,
non-speculative message (e.g., "failed to add scalar function to set") while
preserving the same exception type eDuckDBError and keeping the existing call
site (the block that checks the return of duckdb_add_scalar_function_to_set for
DuckDBError and calls rb_raise).

In `@lib/duckdb/scalar_function.rb`:
- Around line 66-67: Update the YARD documentation for the scalar function
constructor (the method with signature including return_type:, name: nil,
parameter_type:, parameter_types:, varargs_type:, null_handling:) to mark the
name: parameter as optional and adjust any examples to show calling the
constructor without providing name; update the `@param` description for name to
reflect it can be omitted and clarify behavior when omitted (e.g., autogenerated
or inferred), and make the corresponding change in the second doc block that
documents the same constructor/overload.

---

Nitpick comments:
In `@ext/duckdb/scalar_function_set.c`:
- Around line 47-48: Add a C-style documentation comment block with a call-seq:
above the new function rbduckdb_scalar_function_set__add; specifically, insert a
comment that includes "call-seq: scalar_function_set.add(scalar_function)" (or
similar Ruby-facing signature), a short one-line description of what the method
does, and notes about the parameter (scalar_function) and return value, matching
the file's existing C API comment style used elsewhere for functions like
rbduckdb_scalar_function_set__add; ensure the block appears immediately above
the static VALUE rbduckdb_scalar_function_set__add(VALUE self, VALUE
scalar_function) declaration and follows the same formatting conventions as
other ext/duckdb/*.c comment blocks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f322c948-2cd9-41bb-98ab-05a1a2c3c4ce

📥 Commits

Reviewing files that changed from the base of the PR and between 0866bd7 and cff9a04.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • ext/duckdb/scalar_function_set.c
  • lib/duckdb/scalar_function.rb
  • lib/duckdb/scalar_function_set.rb
  • test/duckdb_test/scalar_function_set_test.rb

Comment on lines +55 to +57
if (duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function) == DuckDBError) {
rb_raise(eDuckDBError, "failed to add scalar function to set (duplicate overload?)");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Avoid narrowing the failure reason in the raised error.

Line 56 suggests duplicate overload as the cause, but this branch handles any DuckDBError. A neutral message is safer and less misleading.

Proposed patch
-    if (duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function) == DuckDBError) {
-        rb_raise(eDuckDBError, "failed to add scalar function to set (duplicate overload?)");
-    }
+    if (duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function) == DuckDBError) {
+        rb_raise(eDuckDBError, "failed to add scalar function to set");
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function) == DuckDBError) {
rb_raise(eDuckDBError, "failed to add scalar function to set (duplicate overload?)");
}
if (duckdb_add_scalar_function_to_set(p->scalar_function_set, sf->scalar_function) == DuckDBError) {
rb_raise(eDuckDBError, "failed to add scalar function to set");
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ext/duckdb/scalar_function_set.c` around lines 55 - 57, The error message
raised when duckdb_add_scalar_function_to_set(p->scalar_function_set,
sf->scalar_function) returns DuckDBError is overly specific ("duplicate
overload?") though the branch can represent any failure; change the rb_raise
call to use a neutral, non-speculative message (e.g., "failed to add scalar
function to set") while preserving the same exception type eDuckDBError and
keeping the existing call site (the block that checks the return of
duckdb_add_scalar_function_to_set for DuckDBError and calls rb_raise).

@suketa suketa force-pushed the implement-scalar-function-set-add branch from cff9a04 to 4159814 Compare March 29, 2026 14:30
@suketa suketa merged commit c7a39fe into main Mar 29, 2026
41 checks passed
@suketa suketa deleted the implement-scalar-function-set-add branch March 29, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant