Skip to content

Casting#1126

Merged
suketa merged 2 commits intomainfrom
casting
Mar 3, 2026
Merged

Casting#1126
suketa merged 2 commits intomainfrom
casting

Conversation

@suketa
Copy link
Owner

@suketa suketa commented Mar 3, 2026

Summary by CodeRabbit

  • New Features

    • Added casting functionality to convert values to integer, bigint, float, varchar, timestamp, and date types, with error handling for unsupported conversions.
  • Tests

    • Added comprehensive test coverage for casting operations across all supported types and input formats.

@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2026

📝 Walkthrough

Walkthrough

A new casting feature is added to DuckDB, allowing users to convert values between types (integer, float, varchar, timestamp, date). The implementation delegates to existing converter utilities with comprehensive test coverage and minor exception-handling refactoring in converter methods.

Changes

Cohort / File(s) Summary
Casting Feature
lib/duckdb/casting.rb, lib/duckdb.rb
New public cast(value, type) method supporting integer, float, varchar, timestamp, and date conversions. Raises ArgumentError for unsupported types. Delegates timestamp/date parsing to existing converter utilities.
Converter Utilities
lib/duckdb/converter.rb
Refactored exception handling in _parse_date and _parse_time methods: moved rescue blocks outside control structures and added DateTime support in _parse_time.
Testing & Documentation
test/duckdb_test/casting_test.rb, CHANGELOG.md
New comprehensive test module validating casting across all supported types with edge cases (invalid inputs, multiple input formats for timestamps/dates). Changelog entry documenting new public method.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hop, hop! New casting spree,
Values transform with glee!
Int to float, string to time,
DuckDB's magic—perfectly sublime!
Tests in place, no bugs to see,
Burrow's code flows wild and free! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Casting' is too vague and generic. While it loosely relates to the changeset (adding a cast feature), it does not clearly summarize the main change. A more specific title would better communicate the actual functionality being added. Consider using a more descriptive title such as 'Add DuckDB.cast method for value type conversion' to clearly communicate the feature being introduced.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch casting

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

@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: 1

🧹 Nitpick comments (2)
lib/duckdb/casting.rb (1)

17-18: Minor: Consider using value.inspect for consistency.

The error message uses #{value} directly, but Converter methods use #{value.inspect} for better debugging output (e.g., distinguishing nil from "nil", showing string quotes).

Suggested change
-        raise ArgumentError, "Unsupported type: #{type} for value: #{value}"
+        raise ArgumentError, "Unsupported type: #{type} for value: #{value.inspect}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/duckdb/casting.rb` around lines 17 - 18, The error raise in
lib/duckdb/casting.rb uses #{value} which is inconsistent with other Converter
methods; update the else branch that raises ArgumentError to interpolate the
inspected representation of the value (#{value.inspect}) so the message becomes:
raise ArgumentError, "Unsupported type: #{type} for value: #{value.inspect}";
ensure this change is applied in the same method/class that handles type
conversion (Converter or the casting method) to keep debug output consistent.
test/duckdb_test/casting_test.rb (1)

58-61: Consider adding a test for invalid date input.

For consistency with test_cast_timestamp_invalid, consider adding a test case verifying that invalid date strings raise ArgumentError.

Suggested test
def test_cast_date_invalid
  assert_raises(ArgumentError) { DuckDB.cast('invalid date', :date) }
end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/duckdb_test/casting_test.rb` around lines 58 - 61, Add a new unit test
that mirrors test_cast_timestamp_invalid by asserting that DuckDB.cast raises
ArgumentError for invalid date input; specifically, create a test method (e.g.,
test_cast_date_invalid) that calls DuckDB.cast with a bad string like 'invalid
date' and asserts it raises ArgumentError so test_cast_date and DuckDB.cast's
date handling are covered for failure cases.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/duckdb_test/casting_test.rb`:
- Around line 48-52: The assertion in test_cast_timestamp can fail in non-UTC
environments because DateTime.new(...).to_time uses the local TZ; update the
test to normalize the DateTime conversion to UTC before casting and comparison:
convert the DateTime to a Time in UTC (or construct the expected Time in UTC) so
that DuckDB.cast(DateTime.new(...)) is compared against a UTC Time; modify the
call references in test_cast_timestamp and the DuckDB.cast assertion to use the
UTC-normalized Time to avoid timezone-dependent failures.

---

Nitpick comments:
In `@lib/duckdb/casting.rb`:
- Around line 17-18: The error raise in lib/duckdb/casting.rb uses #{value}
which is inconsistent with other Converter methods; update the else branch that
raises ArgumentError to interpolate the inspected representation of the value
(#{value.inspect}) so the message becomes: raise ArgumentError, "Unsupported
type: #{type} for value: #{value.inspect}"; ensure this change is applied in the
same method/class that handles type conversion (Converter or the casting method)
to keep debug output consistent.

In `@test/duckdb_test/casting_test.rb`:
- Around line 58-61: Add a new unit test that mirrors
test_cast_timestamp_invalid by asserting that DuckDB.cast raises ArgumentError
for invalid date input; specifically, create a test method (e.g.,
test_cast_date_invalid) that calls DuckDB.cast with a bad string like 'invalid
date' and asserts it raises ArgumentError so test_cast_date and DuckDB.cast's
date handling are covered for failure cases.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc40f0d and 2aadcac.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • lib/duckdb.rb
  • lib/duckdb/casting.rb
  • lib/duckdb/converter.rb
  • test/duckdb_test/casting_test.rb

Comment on lines +48 to +52
def test_cast_timestamp
assert_equal(Time.new(2024, 1, 2, 3, 4, 5), DuckDB.cast('2024-01-02 03:04:05', :timestamp))
assert_equal(Time.new(2024, 1, 2, 3, 4, 5), DuckDB.cast(Time.new(2024, 1, 2, 3, 4, 5), :timestamp))
assert_equal(Time.new(2024, 1, 2, 3, 4, 5, '+0000'), DuckDB.cast(DateTime.new(2024, 1, 2, 3, 4, 5), :timestamp))
end
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Potential timezone-dependent test failure.

Line 51 asserts equality with Time.new(2024, 1, 2, 3, 4, 5, '+0000'), but DateTime.new(2024, 1, 2, 3, 4, 5).to_time converts to the local timezone, which may differ from UTC. This test could fail in non-UTC environments.

Proposed fix
-      assert_equal(Time.new(2024, 1, 2, 3, 4, 5, '+0000'), DuckDB.cast(DateTime.new(2024, 1, 2, 3, 4, 5), :timestamp))
+      dt = DateTime.new(2024, 1, 2, 3, 4, 5)
+      assert_equal(dt.to_time, DuckDB.cast(dt, :timestamp))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/duckdb_test/casting_test.rb` around lines 48 - 52, The assertion in
test_cast_timestamp can fail in non-UTC environments because
DateTime.new(...).to_time uses the local TZ; update the test to normalize the
DateTime conversion to UTC before casting and comparison: convert the DateTime
to a Time in UTC (or construct the expected Time in UTC) so that
DuckDB.cast(DateTime.new(...)) is compared against a UTC Time; modify the call
references in test_cast_timestamp and the DuckDB.cast assertion to use the
UTC-normalized Time to avoid timezone-dependent failures.

@suketa suketa merged commit 74fe4b5 into main Mar 3, 2026
40 of 41 checks passed
@suketa suketa deleted the casting branch March 3, 2026 11:38
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