Skip to content

Conversation

@otegami
Copy link
Contributor

@otegami otegami commented Mar 30, 2025

refs: GH-690

In this PR, we support interfaces for Enum type in DuckDB::Column#logical_type about the following.

This is one of the steps for supporting the duckdb_logical_column_type C API.

Summary by CodeRabbit

  • New Features
    • Introduced enhanced support for enumeration types, enabling querying of type information, dictionary sizes, and iteration over dictionary values.
  • Tests
    • Expanded test coverage to validate the new enumeration and numeric type functionalities, ensuring robust and reliable behavior.

@coderabbitai
Copy link

coderabbitai bot commented Mar 30, 2025

Walkthrough

The changes add support for enum types within the DuckDB::LogicalType class. New methods have been introduced in both the C extension and Ruby interface to handle enum-specific functionality. These methods include retrieving an internal type, obtaining the dictionary size, accessing a dictionary value by index, and iterating over dictionary values. Additionally, test cases have been added to verify functionality for both enum and decimal types.

Changes

File(s) Change Summary
CHANGELOG.md Updated the "Unreleased" section with details on enum support in DuckDB::LogicalType and the addition of new related methods.
ext/duckdb/…/logical_type.c & lib/duckdb/…/logical_type.rb Added support for enum types. In the C extension, introduced static functions: duckdb_logical_type__internal_type, duckdb_logical_type_dictionary_size, and duckdb_logical_type_dictionary_value_at. In the Ruby file, added internal_type and each_dictionary_value methods.
test/duckdb_test/…/logical_type_test.rb Introduced four new test methods to verify that the enum and decimal types return the expected internal type, correct dictionary size, and accurate dictionary values.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Ruby Client
    participant LT as LogicalType (Ruby)
    participant Ext as C Extension
    participant DB as DuckDB Engine

    Client->>LT: call internal_type
    LT->>Ext: invoke _internal_type method
    Ext->>DB: call duckdb_get_type_id (and duckdb_enum_internal_type/duckdb_decimal_internal_type)
    DB-->>Ext: return internal type identifier
    Ext-->>LT: return type identifier
    LT-->>Client: return type as symbol

    Client->>LT: call each_dictionary_value (with block)
    LT->>Ext: obtain dictionary_size
    Ext->>DB: fetch enum dictionary size
    DB-->>Ext: return dictionary size
    LT->>Ext: iterative call to dictionary_value_at(index)
    Ext->>DB: fetch enum dictionary value for index
    DB-->>Ext: return dictionary value
    Ext-->>LT: yield dictionary value
    LT-->>Client: yield each dictionary value
Loading

Suggested reviewers

  • suketa

Poem

I'm a bunny in the code field, hopping along with delight,
New enum support in my meadow makes everything so bright.
Methods sprout like carrots in rows, neat and true,
Tests ensure each value shines like morning dew.
With a twitch of my nose, I celebrate this update in flight!
🐰🌟

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 0

🧹 Nitpick comments (1)
lib/duckdb/logical_type.rb (1)

22-38: Fix typo in documentation example

The implementation of internal_type looks good, but there's a small typo in the documentation example.

-    #   ernum_col = users.columns.find { |col| col.name == 'enum_col' }
+    #   enum_col = users.columns.find { |col| col.name == 'enum_col' }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b9d767 and 043fff1.

📒 Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • ext/duckdb/logical_type.c (3 hunks)
  • lib/duckdb/logical_type.rb (2 hunks)
  • test/duckdb_test/logical_type_test.rb (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
test/duckdb_test/logical_type_test.rb (1)
lib/duckdb/logical_type.rb (4)
  • type (4-149)
  • type (17-20)
  • internal_type (35-38)
  • each_dictionary_value (142-148)
🪛 LanguageTool
CHANGELOG.md

[uncategorized] ~6-~6: Loose punctuation mark.
Context: ... - DuckDB::LogicalType#internal_type, DuckDB::LogicalType#dictionary_size, ...

(UNLIKELY_OPENING_PUNCTUATION)

⏰ Context from checks skipped due to timeout of 90000ms (17)
  • GitHub Check: test (mswin, 1.1.3)
  • GitHub Check: test (mingw, 1.1.1)
  • GitHub Check: test (head, 1.1.1)
  • GitHub Check: test (mswin, 1.1.1)
  • GitHub Check: test (3.4.1, 1.2.1)
  • GitHub Check: test (head, 1.1.1)
  • GitHub Check: test (mingw, 1.1.3)
  • GitHub Check: test (ucrt, 1.1.1)
  • GitHub Check: test (head, 1.2.1)
  • GitHub Check: test (head, 1.1.3)
  • GitHub Check: test (ucrt, 1.2.1)
  • GitHub Check: test (3.3.6, 1.1.1)
  • GitHub Check: test (3.2.6, 1.1.1)
  • GitHub Check: test (3.3.6, 1.2.1)
  • GitHub Check: test (3.3.6, 1.1.3)
  • GitHub Check: test (3.1.6, 1.1.1)
  • GitHub Check: test (3.1.6, 1.2.1)
🔇 Additional comments (10)
CHANGELOG.md (1)

5-8: Clear documentation of new features

The changelog entry properly documents the new Enum type support in the DuckDB::LogicalType class and lists all the newly available methods. This provides good visibility for users about what's changed.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~6-~6: Loose punctuation mark.
Context: ... - DuckDB::LogicalType#internal_type, DuckDB::LogicalType#dictionary_size, ...

(UNLIKELY_OPENING_PUNCTUATION)

test/duckdb_test/logical_type_test.rb (4)

121-124: Test for decimal internal type looks good

This test properly verifies that the internal type of a decimal column is :integer, which matches the expected behavior.


223-226: Test for enum internal type looks good

This test correctly verifies that the internal type of an enum column is :utinyint, which is expected based on the DuckDB implementation.


228-231: Test for enum dictionary size looks good

This test verifies that the dictionary size of the enum column matches the number of values defined in the CREATE TYPE statement (4 values: 'sad', 'ok', 'happy', '𝘾𝝾օɭ 😎').


233-238: Test for enum dictionary values looks good

This test properly verifies that all dictionary values can be retrieved and match the expected values from the CREATE TYPE statement. It also tests that Unicode characters and emojis are handled correctly.

lib/duckdb/logical_type.rb (1)

128-148: Well-implemented iterator method for enum dictionary values

The each_dictionary_value method follows Ruby's enumeration patterns correctly:

  1. Returns an Enumerator when no block is given
  2. Yields each value when a block is provided
  3. Properly uses the underlying dictionary_size and dictionary_value_at methods

This implementation is consistent with other enumeration methods in the class.

ext/duckdb/logical_type.c (4)

301-321: Good handling of different logical type cases

The implementation of duckdb_logical_type__internal_type correctly handles both Decimal and Enum types by:

  1. Extracting the logical type from the Ruby object
  2. Determining the type ID
  3. Calling the appropriate function based on the type
  4. Defaulting to DUCKDB_TYPE_INVALID for unsupported types

This approach ensures proper type handling and error cases.


330-334: Simple and effective implementation of dictionary size

The method directly leverages the DuckDB C API to retrieve the dictionary size for the enum type.


343-358: Proper memory management for dictionary values

The implementation correctly:

  1. Extracts the logical type from the Ruby object
  2. Converts the index from Ruby to C
  3. Gets the dictionary value using the DuckDB C API
  4. Raises an error if the value is NULL
  5. Creates a new Ruby string with UTF-8 encoding
  6. Frees the memory allocated by the DuckDB C API

This approach prevents memory leaks and handles errors appropriately.


392-394: Proper registration of new methods

The implementation correctly registers the new methods with the Ruby class, making them accessible from Ruby code.

Copy link
Owner

@suketa suketa left a comment

Choose a reason for hiding this comment

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

LGTM

@suketa suketa merged commit ea01a32 into suketa:main Mar 31, 2025
58 checks passed
@otegami otegami deleted the support-enum-in-logical-type branch April 1, 2025 00:18
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.

2 participants