duckdb

package
v0.2.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README


title: 'DuckDB'

DuckDB connector allows querying DuckDB databases, which is an embedded analytical database similar to SQLite but optimized for OLAP workloads.

Config Schema

Field Type Required Description
type string yes constant: duckdb
hosts string[] no* List of paths (only first path is used)
database string no* Database file name, will be opened in readonly mode
init_sql string no SQL commands to execute on connection initialization (e.g. installing extensions, attaching databases)
memory boolean no If true, uses an in-memory database
conn_string string no Direct connection string, overrides other parameters

Config Examples

  1. Using directory path in hosts with initialization SQL:
connection:
  type: duckdb
  hosts:
    - ./data    # relative path to directory
  database: analytics.duckdb
  init_sql: |
    FORCE INSTALL aws FROM core_nightly;
    FORCE INSTALL httpfs FROM core_nightly;
    FORCE INSTALL iceberg FROM core_nightly;
    CREATE TABLE weather AS
            SELECT * FROM read_csv_auto('https://raw.githubusercontent.com/duckdb/duckdb-web/main/data/weather.csv');
  1. Using full file path in hosts:
connection:
  type: duckdb
  hosts:
    - /absolute/path/to/analytics.duckdb    # Unix-style path
    # or
    - C:/Users/MyUser/data/analytics.duckdb  # Windows-style path
  1. Using relative file path:
connection:
  type: duckdb
  hosts:
    - ./data/analytics.duckdb
  1. Using current directory:
connection:
  type: duckdb
  hosts:
    - .
  database: analytics.duckdb
  1. Using in-memory mode (recommended format):
connection:
  type: duckdb
  memory: true
  1. Using in-memory mode with direct connection string:
connection:
  type: duckdb
  conn_string: ":memory:"
  1. Using empty connection section (defaults to in-memory):
connection:

Running Discovery and API

You can also pass connection string as parameter:

File-based connection strings

Using absolute path on Linux:

./gateway discover --ai-provider gemin --connection-string "duckdb:///absolute/path/to/duckdb-demo.duckdb"

or on Windows

.\gateway discover --ai-provider gemini --connection-string "duckdb://C:/path/duckdb-demo.duckdb"

In-memory connection strings

./gateway discover --ai-provider openai --connection-string "duckdb://:memory:"

Start server, it will use gateway.yaml generated from prev step:

./gateway start

Path Resolution

The final database path is determined as follows:

  1. If conn_string is provided: uses it directly
  2. If memory is true: uses in-memory database (:memory:)
  3. If hosts[0] and database are provided: hosts[0]/database
  4. If only hosts[0] is provided: uses it as the complete path
  5. If only database is provided: uses it as a local path

Safety Features

For security reasons, the connector automatically adds the following safety guard rails to connection strings:

  1. For all file-based databases (non-memory), the access_mode=READ_ONLY parameter is applied to prevent write operations
  2. For all database connections, allow_community_extensions=false is added to prevent loading potentially unsafe extensions
  3. These parameters are automatically added as query parameters (after ? or & as appropriate) to the connection string

Memory databases (:memory: or memory=true) do not have the READ_ONLY restriction, but still have community extensions disabled.

Notes

  • DuckDB is an embedded database, so no server setup is required
  • Only the first path in hosts is used (others are ignored)
  • Both forward slashes / and backslashes \ are supported for Windows paths
  • Relative paths are resolved relative to the current working directory
  • File-based databases are opened in read-only mode by default
  • For in-memory databases, use the memory: true flag or conn_string: ":memory:"
  • In-memory databases still create temporary files for persistence, which is normal behavior
  • The init_sql field allows executing multiple SQL commands on connection initialization, separated by semicolons

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Hosts      []string `json:"hosts" yaml:"hosts"`             // List of database file paths
	Database   string   `json:"database" yaml:"database"`       // Database file name
	ReadOnly   bool     `json:"read_only" yaml:"read_only"`     // Whether to open database in read-only mode
	Memory     bool     `json:"memory" yaml:"memory"`           // Whether to create an in-memory database
	ConnString string   `json:"conn_string" yaml:"conn_string"` // Direct connection string
	InitSQL    string   `json:"init_sql" yaml:"init_sql"`       // SQL commands to execute on connection initialization
}

Config represents the configuration for a DuckDB connection

func (Config) ConnectionString

func (c Config) ConnectionString() string

ConnectionString generates a connection string for DuckDB

func (Config) Doc

func (c Config) Doc() string

Doc returns documentation about the configuration

func (Config) ExtraPrompt

func (c Config) ExtraPrompt() []string

ExtraPrompt returns additional prompt information for the configuration

func (Config) Readonly added in v0.2.1

func (c Config) Readonly() bool

func (Config) Type

func (c Config) Type() string

Type returns the type of the connector

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface

func (Config) Validate

func (c Config) Validate() error

Validate checks if the configuration is valid

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector implements the connectors.Connector interface for DuckDB

func (Connector) Config

func (c Connector) Config() connectors.Config

func (Connector) Discovery

func (c Connector) Discovery(ctx context.Context, tablesList []string) ([]model.Table, error)

func (*Connector) GuessColumnType

func (c *Connector) GuessColumnType(sqlType string) model.ColumnType

GuessColumnType implements TypeGuesser interface for DuckDB

func (*Connector) InferQuery

func (c *Connector) InferQuery(ctx context.Context, query string) ([]model.ColumnSchema, error)

InferQuery implements the Connector interface

func (Connector) LoadsColumns

func (c Connector) LoadsColumns(ctx context.Context, tableName string) ([]model.ColumnSchema, error)

func (Connector) Ping

func (c Connector) Ping(ctx context.Context) error

func (Connector) Query

func (c Connector) Query(ctx context.Context, endpoint model.Endpoint, params map[string]any) ([]map[string]any, error)

func (Connector) Sample

func (c Connector) Sample(ctx context.Context, table model.Table) ([]map[string]any, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL