Skip to content

Commit 3e1d9aa

Browse files
authored
Merge pull request #1 from chdb-io/dev
Add Ruby bindings for chDB C++ library
2 parents d66c9a4 + a35a5ae commit 3e1d9aa

34 files changed

+1287
-162
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
*.so
2+
*.bundle
3+
*.swp
4+
*.log
5+
*.o
6+
7+
Gemfile.lock
8+
.rspec_status
9+
.ruby-version
10+
.vscode
11+
12+
doc/
13+
deps/
14+
gems/
15+
issues/
16+
pkg/
17+
ports/
18+
tmp/
19+
vendor/
20+
test/
21+
testdb/
22+
ext/chdb/include/
23+
ext/chdb/lib/

.rspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--format documentation
2+
--color
3+
--require spec_helper
4+
--order rand

.rubocop.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
AllCops:
2+
Exclude:
3+
- 'vendor/**/*'
4+
- 'spec/**/*'
5+
- 'tmp/**/*'
6+
- 'ext/**/extconf.rb'
7+
8+
NewCops: enable
9+
10+
TargetRubyVersion: 3.1
11+
12+
AutoCorrect: true
13+
14+
SuggestExtensions: false
15+
16+
Style:
17+
StringLiterals:
18+
EnforcedStyle: double_quotes
19+
20+
ParameterLists:
21+
EnforcedStyle: compact
22+
23+
RedundantSelf:
24+
Enabled: true
25+
26+
ModifierForm:
27+
AllowModifierForm: true
28+
29+
Layout:
30+
LineLength:
31+
Max: 120
32+
33+
FirstParameterIndentation:
34+
Enabled: true
35+
EnforcedStyle: indented
36+
37+
Indentation:
38+
Width: 2
39+
40+
Metrics:
41+
ClassLength:
42+
Max: 200
43+
44+
MethodLength:
45+
Max: 20
46+
47+
ParameterLists:
48+
Max: 5
49+
50+
Lint/AmbiguousOperator:
51+
Enabled: false
52+
53+
Style/Documentation:
54+
Enabled: false
55+
56+
Style/AccessModifierDeclarations:
57+
Enabled: false

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# chdb-ruby Changelog
2+
3+
## [Unreleased]

Gemfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gemspec
6+
7+
group :test do
8+
gem 'rake-compiler', '1.2.9'
9+
gem 'rake-compiler-dock', '1.9.1'
10+
gem 'rspec', '3.12.0'
11+
end
12+
13+
group :development do
14+
gem 'rdoc', '6.12.0'
15+
gem 'rubocop', '1.74.0', require: false
16+
end

INSTALLATION.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
# Installation and Using chdb-ruby extensions
3+
4+
This document will help you install the `chdb-ruby` ruby gem.
5+
6+
## Installation
7+
8+
### Native Gems
9+
10+
Native (precompiled) gems are available for recent Ruby versions on these platforms:
11+
12+
- `aarch64-linux`
13+
- `arm64-darwin`
14+
- `x86_64-linux`
15+
- `x86_64-darwin`
16+
17+
If you are using one of these Ruby versions on one of these platforms, the native gem is the recommended way to install chdb-ruby.
18+
19+
`chdb-ruby` gem does not provide a pure Ruby implementation. Installation will fail if your platform is unsupported.
20+
21+
## Post-Installation: Setting Up libchdb C++ Library
22+
23+
After installing the `chdb-ruby` gem, you must also install the `libchdb` C++ library locally. If the library path is not in your system's default search paths, you'll need to configure the runtime library loading path.
24+
25+
### 1. Download the C++ Library
26+
27+
You can either:
28+
- Use the automated installation script:
29+
```bash
30+
curl -sSL https://github.com/chdb-io/chdb-io.github.io/blob/main/install_libchdb.sh | bash
31+
```
32+
33+
- Or manually download from chdb releases(example for arm64-darwin (v3.12)):
34+
```bash
35+
wget https://github.com/chdb-io/chdb/releases/download/v3.12/macos-arm64-libchdb.tar.gz
36+
tar -xzf macos-arm64-libchdb.tar.gz
37+
```
38+
39+
### 2. Configure Library Path
40+
- MacOS:
41+
```bash
42+
export DYLD_LIBRARY_PATH="/path/to/libchdb:$DYLD_LIBRARY_PATH"
43+
```
44+
(Add to your shell config file like ~/.zshrc for persistence)
45+
46+
- Linux:
47+
```bash
48+
export LD_LIBRARY_PATH="/path/to/libchdb:$LD_LIBRARY_PATH"
49+
```
50+
51+
### 3. Verify Installation
52+
- Ruby:
53+
```bash
54+
require 'chdb'
55+
```
56+
57+
- Troubleshooting(If you get "Library not loaded" errors):
58+
- Verify the path in DYLD_LIBRARY_PATH/LD_LIBRARY_PATH is correct
59+
- Ensure you downloaded the right version for your platform

LICENSE.txt renamed to LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Copyright 2025 chDB, Inc.
188188
same "printed page" as the copyright notice for easier
189189
identification within third-party archives.
190190

191-
Copyright 2023 chDB, Inc.
191+
Copyright 2025 chDB, Inc.
192192

193193
Licensed under the Apache License, Version 2.0 (the "License");
194194
you may not use this file except in compliance with the License.

README.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,119 @@
44

55
[![chDB-ruby](https://github.com/chdb-io/chdb-ruby/actions/workflows/chdb.yml/badge.svg)](https://github.com/chdb-io/chdb-ruby/actions/workflows/chdb.yml)
66

7-
# chdb-ruby
8-
[chDB](https://github.com/chdb-io/chdb) ruby bindings and chDB cli.
7+
# Ruby Interface for chdb
8+
9+
## Overview
10+
11+
This library allows Ruby programs to use the chDB embedded analytical database (https://github.com/chdb-io/chdb).
12+
13+
**Designed with SQLite3-compatible API style** - If you're familiar with Ruby's sqlite3 gem, you'll feel right at home with chdb-ruby's similar interface design.
14+
15+
Note that this module is only compatible with ChDB 3.0.0 or newer.
16+
17+
* Source code: https://github.com/chdb-io/chdb-ruby
18+
* Download: http://rubygems.org/gems/chdb-ruby
19+
* Documentation: https://clickhouse.com/docs/chdb
20+
21+
## Quick start
22+
23+
``` ruby
24+
require 'chdb'
25+
26+
# Open a database
27+
db = ChDB::Database.new('test_db', results_as_hash: true)
28+
29+
# Create a table
30+
rows = db.execute <<-SQL
31+
CREATE TABLE test_table(
32+
id Int32,
33+
name String)
34+
ENGINE = MergeTree()
35+
ORDER BY id);
36+
SQL
37+
38+
# Execute a few inserts
39+
{
40+
1 => 'Alice',
41+
2 => 'Bob'
42+
}.each do |pair|
43+
db.execute 'INSERT INTO test_table VALUES ( ?, ? )', pair
44+
end
45+
46+
# Find a few rows
47+
db.execute('SELECT * FROM test_table ORDER BY id') do |row|
48+
p row
49+
end
50+
# [{ 'id' => '1', 'name' => 'Alice' },
51+
# { 'id' => '2', 'name' => 'Bob' }]
52+
53+
# Open another database
54+
db = ChDB::Database.new 'test2.db'
55+
56+
# Create another table
57+
rows = db.execute <<-SQL
58+
CREATE TABLE test2_table(
59+
id Int32,
60+
name String)
61+
ENGINE = MergeTree()
62+
ORDER BY id");
63+
SQL
64+
65+
# Execute inserts with parameter markers
66+
db.execute('INSERT INTO test2_table (id, name)
67+
VALUES (?, ?)', [3, 'Charlie'])
68+
69+
db.execute2('SELECT * FROM test2_table') do |row|
70+
p row
71+
end
72+
# [['id', 'name'], [3, 'Charlie']],
73+
```
74+
75+
## Thread Safety
76+
77+
When using `ChDB::Database.new` to open a session, all read/write operations within that session are thread-safe. However, currently only one active session is allowed per process. Therefore, when you need to open another session, you must first close the previous session.
78+
79+
For example, the following code is fine because only the database
80+
instance is shared among threads:
81+
82+
```ruby
83+
require 'chdb'
84+
85+
db = ChDB::Database.new ":memory:'
86+
87+
latch = Queue.new
88+
89+
ts = 10.times.map {
90+
Thread.new {
91+
latch.pop
92+
db.execute 'SELECT 1'
93+
}
94+
}
95+
10.times { latch << nil }
96+
97+
p ts.map(&:value)
98+
```
99+
100+
Other instances can be shared among threads, but they require that you provide
101+
your own locking for thread safety. For example, `ChDB::Statement` objects
102+
(prepared statements) are mutable, so applications must take care to add
103+
appropriate locks to avoid data race conditions when sharing these objects
104+
among threads.
105+
106+
It is generally recommended that if applications want to share a database among
107+
threads, they _only_ share the database instance object. Other objects are
108+
fine to share, but may require manual locking for thread safety.
109+
110+
## Support
111+
112+
### Installation
113+
114+
If you're having trouble with installation, please first read [`INSTALLATION.md`](./INSTALLATION.md).
115+
116+
### Bug reports
117+
118+
You can file the bug at the [github issues page](https://github.com/chdb-io/chdb-ruby/issues).
119+
120+
## License
121+
122+
This library is licensed under `Apache License 2.0`, see [`LICENSE`](./LICENSE).

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
require 'bundler'
4+
CHDB_SPEC = Bundler.load_gemspec('chdb.gemspec')
5+
6+
task default: %i[rubocop compile test]

chdb.gemspec

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
begin
4+
require_relative 'lib/chdb/version'
5+
rescue LoadError
6+
puts 'WARNING: could not load ChDB::VERSION'
7+
end
8+
9+
Gem::Specification.new do |s|
10+
s.name = 'chdb-ruby'
11+
s.version = defined?(ChDB::VERSION) ? ChDB::VERSION : '0.0.0'
12+
13+
s.summary = 'Ruby library to interface with the chDB database engine (https://clickhouse.com/docs/chdb).'
14+
s.description = <<~TEXT
15+
Ruby library to interface with the chDB database engine (https://clickhouse.com/docs/chdb). Precompiled
16+
binaries are available for common platforms for recent versions of Ruby.
17+
TEXT
18+
19+
s.authors = ['Xiaozhe Yu', 'Auxten Wang']
20+
21+
s.licenses = ['Apache-2.0']
22+
23+
s.required_ruby_version = Gem::Requirement.new('>= 3.1')
24+
25+
s.homepage = 'https://github.com/chdb-io/chdb-ruby'
26+
s.metadata = {
27+
'homepage_uri' => 'https://github.com/chdb-io/chdb-ruby',
28+
'bug_tracker_uri' => 'https://github.com/chdb-io/chdb-ruby/issues',
29+
'changelog_uri' => 'https://github.com/chdb-io/chdb-ruby/blob/main/CHANGELOG.md',
30+
'source_code_uri' => 'https://github.com/chdb-io/chdb-ruby',
31+
32+
# https://guides.rubygems.org/mfa-requirement-opt-in/
33+
'rubygems_mfa_required' => 'true'
34+
}
35+
36+
s.files = [
37+
# 'CHANGELOG.md',
38+
'INSTALLATION.md',
39+
'LICENSE',
40+
'README.md',
41+
'lib/chdb.rb',
42+
'lib/chdb/constants.rb',
43+
'lib/chdb/data_path.rb',
44+
'lib/chdb/database.rb',
45+
'lib/chdb/errors.rb',
46+
'lib/chdb/local_result.rb',
47+
'lib/chdb/parameter_binding.rb',
48+
'lib/chdb/result_handler.rb',
49+
'lib/chdb/result_set.rb',
50+
'lib/chdb/sql_processor.rb',
51+
'lib/chdb/statement.rb',
52+
'lib/chdb/version_info.rb',
53+
'lib/chdb/version.rb'
54+
]
55+
56+
s.rdoc_options = ['--main', 'README.md']
57+
58+
s.add_dependency 'csv', '~> 3.1'
59+
60+
# s.extensions << 'ext/chdb/extconf.rb'
61+
end

dependencies.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chdb:
2+
version: "3.1.2"

0 commit comments

Comments
 (0)