Skip to content
stratasitePublic

About

No description, website, or topics provided.

Resources

Stars

10 stars

Watchers

0 watching

Forks

Repository files navigation

Ruby

DWH - Data Warehouse Adapter Library

A light weight library to connect, introspect, and query popular databases over a unified interface. This gem is intended for analtyical workloads. The library also provides database specific translations for common functions like date_trunc, date_add etc. The function tranlation is not comprehensive. But, it does provides good coverage for date handling, and some array handling as well.

Note

This is not an ORM nor will it cast types to ruby unless the underlying client does it out of the box. The goal here is to create an Architecture where new databases can be onboarded quickly.

Why do we need another database abstraction layer?

Libraries like Sequel are amazing and comprehensive. However, its broad coverage also makes it more laborious to add new databases. Especially, ones with only HTTP endpoints for Ruby. We seem to be having an explosion of databases recently and a light weight interface will allow us to integrate faster.

The adapter only has 5 core methods (6 including the connection method). A YAML settings controls how it interacts with a particular db. It is relatively fast to add a new db. See the Druid implementation as an example. And here is its corresponding YAML settings file.

Features

  • Unified Interface: Connect to multiple database types using the same API
  • SQL Function Translation: Automatically translates common SQL functions to database-specific syntax
  • Connection Pooling: Built-in connection pool management for high-performance applications
  • Rich Metadata: Extract table schemas, column information, and statistics

Supported Databases

  • Snowflake - High performance cloud warehouse
  • Trino (formerly Presto) - Distributed SQL query engine
  • Redshift - AWS data warehouse platform
  • AWS Athena - AWS big data warehouse
  • Apache Druid - Real-time analytics database
  • DuckDB - In-process analytical database
  • SQLite - Lightweight embedded database
  • PostgreSQL - Full-featured RDBMS with advanced SQL support
  • MySQL - Popular open-source database
  • SQL Server - Microsoft's enterprise database
  • ClickHouse - High performance analytical database
  • Databricks - Lakehouse SQL warehouse
  • Google BigQuery - Google Cloud serverless warehouse

Integrations Coming Soon

  • MotherDuck - Hosted DuckDB service

Quick Start

Install it yourself as:

gem install dwh

Connect and Execute a Basic Query

require 'dwh'

# Connect to Druid
druid = DWH.create(:druid, {
  host: 'localhost',
  port: 8080,
  protocol: 'http'
})

# basic query execution
results = druid.execute("SELECT * FROM web_sales", format: :csv)

# Connect to SQLite for local analytics
sqlite = DWH.create(:sqlite, {
  file: 'path/to/analytics.db'
})

# Query with optimized WAL mode enabled by default
results = sqlite.execute("SELECT * FROM sales_data", format: :array)

Core API

Standardized API across adapters:

connection
Creates a reusuable connection based on config hash passed in
tables(schema: nil, catalog: nil)
returns a list of tables from the default connection or from the specified schema and catalog
metadata(table_name, schema: nil, catalog: nil)
provides metadata about a table
stats(table_name, date_column: nil)
provides table row count and date range
execute(sql, format: :array, retries: 0)
runs a query and returns in given format
execute_stream(sql, io, stats: nil)
runs a query and streams it as csv into the given io

Tutorials and Guides

Testing

Certain databases have to be tested via docker. Those tests will try to launch docker compose services in test/support/compose*.yml

Run Unit Tests:

bundle exec rake test:unit

Run tests on RDBMS dbs:

bundle exec rake test:system:rdbms 

Run tests on druid:

bundle exec rake test:system:druid 

Cloud warehouse tests (test/system/cloud_*_test.rb) run against real accounts and skip unless configured through environment variables, so no credentials live in the repo:

Adapter Variables
Athena ATHENA_S3_OUTPUT, optional ATHENA_REGION, ATHENA_DATABASE; AWS credentials from the environment
BigQuery BIGQUERY_PROJECT, optional BIGQUERY_DATASET; GOOGLE_APPLICATION_CREDENTIALS or gcloud ADC. Creates its own fixture tables
Databricks DATABRICKS_HOST, DATABRICKS_WAREHOUSE, DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET
Redshift REDSHIFT_HOST, REDSHIFT_PASSWORD, optional REDSHIFT_USER, REDSHIFT_DATABASE, REDSHIFT_PORT
Snowflake SNOWFLAKE_ACCOUNT, SNOWFLAKE_PAT, optional SNOWFLAKE_DATABASE; key-pair test needs SNOWFLAKE_USER, SNOWFLAKE_PRIVATE_KEY
BIGQUERY_PROJECT=my-project BUNDLE_WITH=development bundle exec ruby -Itest test/system/cloud_bigquery_test.rb

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stratasite/dwh.

License

This project is available as open source under the terms of the MIT License.

Version

Current version: 0.6.3

About

No description, website, or topics provided.

Resources

Stars

10 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages