Skip to content

Comments

Add intelligent SQL auto-completion with prioritization#3

Open
tobias-fire wants to merge 4 commits intotobias/syntax-highlightingfrom
feature/sql-autocompletion
Open

Add intelligent SQL auto-completion with prioritization#3
tobias-fire wants to merge 4 commits intotobias/syntax-highlightingfrom
feature/sql-autocompletion

Conversation

@tobias-fire
Copy link
Owner

Summary

Adds intelligent SQL auto-completion to the REPL with context-aware prioritization and usage tracking.

Features

1. SQL Auto-Completion

  • Tables: Completes table names (both short and schema-qualified)
  • Columns: Completes column names (both short and table-qualified)
  • Schemas: Completes schema names with trailing dot for exploration
  • Functions: Completes SQL function names with opening parenthesis

2. Intelligent Prioritization (5-tier system)

  • Priority 5000: Columns from tables in current query (highest)
  • Priority 4000: Tables and schemas
  • Priority 3000: Qualified columns from other tables
  • Priority 2000: Unqualified columns from other tables
  • Priority 1000: Functions
  • Priority 0-990: System schemas (information_schema, pg_catalog)

3. Context-Aware Suggestions

  • Analyzes current SQL statement to extract referenced tables
  • Prioritizes columns from tables already mentioned in the query
  • Adjusts priorities based on query context

4. Usage Tracking

  • Tracks last 10 queries to learn usage patterns
  • Frequently used items get higher priority within their class
  • Session-only tracking (not persisted)

5. Schema Exploration

  • Type schema_name. to see all tables in that schema
  • Type table_name. to see all columns in that table
  • Intelligent filtering based on partial input

Architecture

New Components

  • src/completion/usage_tracker.rs - Tracks table/column/function usage frequency
  • src/completion/priority_scorer.rs - Calculates priority scores (5-tier system)
  • src/completion/context_analyzer.rs - Extracts tables from SQL statements

Modified Components

  • src/completion/mod.rs - Integrated scoring and sorting
  • src/completion/schema_cache.rs - Added schemas and functions support
  • src/main.rs - Initialize UsageTracker
  • src/query.rs - Track successful queries
  • src/context.rs - Store usage tracker reference

Testing

  • 24 new unit tests for prioritization system
  • All 157 tests passing
  • Tested with information_schema exploration
  • Tested with function completion

Examples

No table context - typing SELECT u<Tab>:

[users, public.users, user_logs, users.user_id, user_id, ...]

Tables appear first, then columns.

With table context - typing SELECT * FROM users WHERE u<Tab>:

[user_id, username, updated_at, users, user_logs, ...]

Columns from users appear first.

Schema exploration - typing information_schema.<Tab>:

[information_schema.tables, information_schema.columns, ...]

Function completion - typing len<Tab>:

[length(, ...]

🤖 Generated with Claude Code

tobias-fire and others added 4 commits February 19, 2026 20:29
Implements regex-based syntax highlighting for SQL queries in the
interactive REPL mode with industry-standard color scheme.

Features:
- Keywords (SELECT, FROM, WHERE): Bright Blue
- Functions (COUNT, AVG): Bright Cyan
- Strings ('text'): Bright Yellow
- Numbers (42, 3.14): Bright Magenta
- Comments (-- text): Bright Black (gray)
- Operators: Default (subtle)

Configuration:
- Auto-enabled in interactive TTY mode
- Disabled via --no-color flag
- Respects NO_COLOR environment variable
- Auto-disabled for piped/redirected output

Implementation:
- Regex-based highlighting (no new dependencies)
- 13 comprehensive unit tests
- Graceful error handling
- Colorblind-accessible color scheme based on DuckDB, pgcli, and
  accessibility research

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Implement context-aware auto-completion that suggests table names and
column names from the database schema. The completion system queries
information_schema on startup and caches results for fast lookups.

Key features:
- Auto-complete table names and column names
- Async schema cache refresh (non-blocking startup)
- Support for message-based query response format
- Configurable via --no-completion and --completion-cache-ttl flags
- Runtime control with 'set completion = on/off'
- Manual refresh with \refresh command

Implementation details:
- New completion module with SqlCompleter, SchemaCache, and context detector
- Queries information_schema.tables, columns, and routines
- Thread-safe cache using Arc<RwLock<T>>
- Graceful error handling and fallback

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Implements context-aware, frequency-based suggestion ordering with schema exploration support. Tables and columns now appear based on query context, usage frequency, and relevance, with system schemas appropriately deprioritized. Schema names can be completed directly and typing 'schema.' shows all tables in that schema.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Implements auto-completion for SQL functions with lowest priority (below columns, above system schemas). Functions complete with opening parenthesis for immediate argument typing. Operators are filtered out using routine_type != 'OPERATOR' from information_schema.routines.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@tobias-fire tobias-fire force-pushed the feature/sql-autocompletion branch from 6a2b45c to 6a747d9 Compare February 19, 2026 19:30
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