|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +CubeSQL is a SQL proxy server that enables SQL-based access to Cube.js semantic layer. It emulates the PostgreSQL wire protocol, allowing standard SQL clients and BI tools to query Cube.js deployments as if they were traditional databases. Note: MySQL protocol support has been deprecated and is no longer available. |
| 8 | + |
| 9 | +This is a Rust workspace containing three crates: |
| 10 | +- **cubesql**: Main SQL proxy server with query compilation and protocol emulation |
| 11 | +- **cubeclient**: Rust client library for Cube.js API communication |
| 12 | +- **pg-srv**: PostgreSQL wire protocol server implementation |
| 13 | + |
| 14 | +## Development Commands |
| 15 | + |
| 16 | +### Prerequisites |
| 17 | +```bash |
| 18 | +# Install required Rust toolchain (1.84.1) |
| 19 | +rustup update |
| 20 | + |
| 21 | +# Install snapshot testing tool |
| 22 | +cargo install cargo-insta |
| 23 | +``` |
| 24 | + |
| 25 | +### Core Build Commands |
| 26 | +```bash |
| 27 | +# Build all workspace members |
| 28 | +cargo build |
| 29 | + |
| 30 | +# Build release version |
| 31 | +cargo build --release |
| 32 | + |
| 33 | +# Format code |
| 34 | +cargo fmt |
| 35 | + |
| 36 | +# Run linting (note: many clippy rules are disabled) |
| 37 | +cargo clippy |
| 38 | +``` |
| 39 | + |
| 40 | +### Running CubeSQL Server |
| 41 | +```bash |
| 42 | +# Run with required environment variables |
| 43 | +CUBESQL_CUBE_URL=$CUBE_URL/cubejs-api \ |
| 44 | +CUBESQL_CUBE_TOKEN=$CUBE_TOKEN \ |
| 45 | +CUBESQL_LOG_LEVEL=debug \ |
| 46 | +CUBESQL_BIND_ADDR=0.0.0.0:4444 \ |
| 47 | +cargo run --bin cubesqld |
| 48 | + |
| 49 | +# Connect via PostgreSQL client |
| 50 | +psql -h 127.0.0.1 -p 4444 -U root |
| 51 | +``` |
| 52 | + |
| 53 | +### Testing Commands |
| 54 | +```bash |
| 55 | +# Run all unit tests |
| 56 | +cargo test |
| 57 | + |
| 58 | +# Run specific test module |
| 59 | +cargo test test_introspection |
| 60 | +cargo test test_udfs |
| 61 | + |
| 62 | +# Run integration tests (requires Cube.js instance) |
| 63 | +cargo test --test e2e |
| 64 | + |
| 65 | +# Review snapshot test changes |
| 66 | +cargo insta review |
| 67 | + |
| 68 | +# Run benchmarks |
| 69 | +cargo bench |
| 70 | +``` |
| 71 | + |
| 72 | +## Architecture Overview |
| 73 | + |
| 74 | +### Query Processing Pipeline |
| 75 | +1. **Protocol Layer**: Accepts PostgreSQL wire protocol connections |
| 76 | +2. **SQL Parser**: Modified sqlparser-rs parses incoming SQL queries |
| 77 | +3. **Query Rewriter**: egg-based rewrite engine transforms SQL to Cube.js queries |
| 78 | +4. **Compilation**: Generates Cube.js REST API calls or DataFusion execution plans |
| 79 | +5. **Execution**: DataFusion executes queries or proxies to Cube.js |
| 80 | +6. **Result Formatting**: Converts results back to wire protocol format |
| 81 | + |
| 82 | +### Key Components |
| 83 | + |
| 84 | +#### cubesql crate structure: |
| 85 | +- **`/compile`**: SQL compilation and query planning |
| 86 | + - `/engine`: DataFusion integration and query execution |
| 87 | + - `/rewrite`: egg-based query optimization rules |
| 88 | +- **`/sql`**: Database protocol implementations |
| 89 | + - `/postgres`: PostgreSQL system catalog emulation |
| 90 | + - `/database_variables`: Variable system for PostgreSQL protocol |
| 91 | +- **`/transport`**: Network transport and session management |
| 92 | +- **`/config`**: Configuration and service initialization |
| 93 | + |
| 94 | +#### Testing Approach: |
| 95 | +- **Unit Tests**: Inline tests in source files using `#[cfg(test)]` |
| 96 | +- **Integration Tests**: End-to-end tests in `/e2e` directory |
| 97 | +- **Snapshot Tests**: Extensive use of `insta` for SQL compilation snapshots |
| 98 | +- **BI Tool Tests**: Compatibility tests for Metabase, Tableau, PowerBI, etc. |
| 99 | + |
| 100 | +### Important Implementation Details |
| 101 | + |
| 102 | +1. **DataFusion Integration**: Uses forked Apache Arrow DataFusion for query execution |
| 103 | +2. **Rewrite Rules**: Complex SQL transformations using egg e-graph library |
| 104 | +3. **Protocol Emulation**: Implements enough of PostgreSQL protocol for BI tools |
| 105 | +4. **System Catalogs**: Emulates pg_catalog (PostgreSQL) |
| 106 | +5. **Variable Handling**: Supports SET/SHOW commands for protocol compatibility |
| 107 | + |
| 108 | +## Common Development Tasks |
| 109 | + |
| 110 | +### Adding New SQL Support |
| 111 | +1. Add parsing support in `/compile/parser` |
| 112 | +2. Create rewrite rules in `/compile/rewrite/rules` |
| 113 | +3. Add tests with snapshot expectations |
| 114 | +4. Update protocol-specific handling if needed |
| 115 | + |
| 116 | +### Debugging Query Compilation |
| 117 | +```bash |
| 118 | +# Enable detailed logging |
| 119 | +CUBESQL_LOG_LEVEL=trace cargo run --bin cubesqld |
| 120 | + |
| 121 | +# Check rewrite traces in logs |
| 122 | +# Look for "Rewrite" entries showing transformation steps |
| 123 | +``` |
| 124 | + |
| 125 | +### Working with Snapshots |
| 126 | +```bash |
| 127 | +# After making changes that affect SQL compilation |
| 128 | +cargo test |
| 129 | +cargo insta review # Review and accept/reject changes |
| 130 | +``` |
| 131 | + |
| 132 | +## Key Dependencies |
| 133 | + |
| 134 | +- **DataFusion**: Query execution engine (forked version with custom modifications) |
| 135 | +- **sqlparser-rs**: SQL parser (forked with CubeSQL-specific extensions) |
| 136 | +- **egg**: E-graph library for query optimization |
| 137 | +- **tokio**: Async runtime for network and I/O operations |
| 138 | +- **pgwire**: PostgreSQL wire protocol implementation |
| 139 | + |
| 140 | +## Important Notes |
| 141 | + |
| 142 | +- This codebase uses heavily modified forks of DataFusion and sqlparser-rs |
| 143 | +- Many clippy lints are disabled due to code generation and complex patterns |
| 144 | +- Integration tests require a running Cube.js instance |
| 145 | +- The rewrite engine is performance-critical and uses advanced optimization techniques |
| 146 | +- Protocol compatibility is paramount for BI tool support |
0 commit comments