snowflake

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 34 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package snowflake is a database/sql driver for Snowflake, which runs SQL over its SQL REST API, /api/v2/statements, with HTTPS and JSON. It registers the one name "snowflake", and takes a DSN of the form snowflake://user:key@<org>-<account>.snowflakecomputing.com/database/schema (D183).

db, err := sql.Open("snowflake", "snowflake://alice:"+key+"@org-acct.snowflakecomputing.com/db/public")

The password of the DSN is the private key of the user, as the base64url text of its PKCS8 DER bytes. The driver signs a token with it, with RS256 and the claims that Snowflake names, and signs a new one before the old one ends. The driver sends the token and never the key, and only to the host of the DSN.

The driver starts each statement with async=true and polls its handle until it ends, so that it can cancel the statement when the context ends or the caller closes the rows before the end. The server runs the whole statement before it sends the first partition, so an error comes before any row. The driver reads the first partition from the answer, and each later one with GET, in order and one at a time, one token at a time. The server binds each argument as a typed binding. The server has no session across two requests, so the driver has no transactions. docs/SNOWFLAKE.md holds what the driver knows about the server.

Index

Constants

View Source
const (
	// ErrCut is the error of a partition of a result that ends before the
	// count of rows that the server named for it. The server runs the whole
	// statement before it sends the first partition, so only a partition can
	// be cut (D21 and D183). After a row, it comes wrapped with
	// dbimp.ErrIncomplete (D107).
	ErrCut dbimp.Error = "the answer was cut short"
	// ErrCanceled is the error of a statement that the server ended with the
	// code 000604, when the context of the statement did not end. Someone
	// else canceled it, such as an administrator (measured).
	ErrCanceled dbimp.Error = "the statement was canceled"
	// ErrTimeout is the error of a statement that reached its timeout on the
	// server, which answers HTTP 408 and the code 000630 (measured).
	ErrTimeout dbimp.Error = "the statement reached its timeout"
)

The sentinel errors of the driver. Each one wraps into a failure with errors.Is (D6).

View Source
const Name = "snowflake"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the account, such as
	// <org>-<account>.snowflakecomputing.com.
	Host string
	// Port is the port of the server, 443 by default.
	Port int
	// User is the login user. The claims of the token name it in upper case.
	User string
	// Password is the private key of the user, as the base64url text of its
	// PKCS8 DER bytes (D183). The driver signs a token with it, and sends the
	// token and never the key.
	Password string
	// Database and Schema are the database and the schema of each statement,
	// which the driver sends in the body. Each one is optional.
	Database string
	Schema   string
	// Role is the role of each statement, and empty for the default role of
	// the user.
	Role string
	// Warehouse is the warehouse of each statement, and empty for the
	// default warehouse of the user.
	Warehouse string
	// TimeZone is the time zone of the session of each statement, which the
	// driver sends as the parameter TIMEZONE, such as UTC or Asia/Jakarta.
	// Empty leaves the zone of the account. A timestamp_ltz value takes this
	// zone too, and time.Local when it is empty. The name Local is time.Local.
	TimeZone string
	// Timeout is the time that the server gives each statement. The driver
	// rounds it up to whole seconds for the server. Zero sends none.
	Timeout time.Duration
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form snowflake://user:key@<org>-<account>.snowflakecomputing.com/database/schema?role=r (D27, D35 and D183). The password is the private key. The path holds the database and the schema, and each is optional. The keys are role, warehouse, timeout and timezone. The timeout is a duration with a unit, such as 60s or 1m, and a bare number is refused, and the driver refuses any other. The host must end in .snowflakecomputing.com, unless the DSN names a port or the host is an IP address, which a test of the driver uses for a fake server.

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

type Connector

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

Connector opens connections to one account. It owns its transport, and every connection shares it, with the token that the driver signs. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default. A private key that is not valid fails the first Connect.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

func (c *Connector) Connect(context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds nothing on the server, so it sends no request.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for Snowflake.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Code is code, such as 001003 for a syntax error, and empty when the
	// answer has none.
	Code string
	// SQLState is sqlState, such as 42000, and empty when the answer has
	// none. A wrong token has none.
	SQLState string
	// Message is message, or the text of the answer when it has none.
	Message string
	// Handle is statementHandle, and empty when the answer has none.
	Handle string
	// contains filtered or unexported fields
}

Error is an error that Snowflake reported. Every answer that is not a result has a JSON object with the code, the SQLSTATE and the message, with HTTP 422 for a statement that failed, HTTP 408 for one that reached its timeout, HTTP 400 for a request that the server refused, and HTTP 401 for a token that is not valid (measured). An error comes before any row, because the server runs the whole statement before it sends the first partition.

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is

func (err *Error) Is(target error) bool

Is reports whether target is the sentinel that the code of the error names, ErrCanceled or ErrTimeout, or dbimp.ErrAuthentication when the server refused the token: the code 390144, or HTTP 401 (D197). A statement that the role may not run, such as code 002003 with HTTP 422, does not match.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.

func WithDatabase

func WithDatabase(name string) Option

WithDatabase sets the database of one statement, as the path of the DSN does.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets a parameter of the session of one statement by its name, such as "QUERY_TAG", in parameters of the body. The value is written as text with fmt.Sprint. The server reads a parameter in the same way in each statement, and the parameter ends with the statement, because each request is its own session (measured).

A parameter that changes the text of a value is refused with dbimp.ErrNotSupported, because the driver reads each value in the form that docs/SNOWFLAKE.md names: DATE_OUTPUT_FORMAT, TIME_OUTPUT_FORMAT, TIMESTAMP_OUTPUT_FORMAT, TIMESTAMP_LTZ_OUTPUT_FORMAT, TIMESTAMP_NTZ_OUTPUT_FORMAT, TIMESTAMP_TZ_OUTPUT_FORMAT, BINARY_OUTPUT_FORMAT, GEOGRAPHY_OUTPUT_FORMAT and GEOMETRY_OUTPUT_FORMAT. MULTI_STATEMENT_COUNT is refused too, because the driver does not read the child statements that a request of several statements makes (D183). WithParameter("TIMEZONE", v) replaces the time zone of the DSN. A value of nil fails the statement with dbimp.ErrInvalidValue.

func WithReadonly

func WithReadonly(readonly bool) Option

WithReadonly asks that the statement write nothing. Snowflake has no such setting for one statement, so WithReadonly(true) fails the statement with dbimp.ErrNotSupported (D109).

func WithRole

func WithRole(name string) Option

WithRole sets the role of one statement, as the key role of the DSN does.

func WithSchema

func WithSchema(name string) Option

WithSchema sets the schema of one statement, as the path of the DSN does.

func WithTimeZone

func WithTimeZone(name string) Option

WithTimeZone sets the time zone of the session of one statement, as the parameter TIMEZONE, as the key timezone of the DSN does. A timestamp_ltz value of the statement takes this zone.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as timeout in the body, as the key timeout of the DSN does. The server counts whole seconds, so the driver rounds d up to the next second. The server stops a statement that runs longer with HTTP 408 and the code 000630 (measured). Zero sends no timeout, and the server uses its own.

func WithWarehouse

func WithWarehouse(name string) Option

WithWarehouse sets the warehouse of one statement, as the key warehouse of the DSN does.

Jump to

Keyboard shortcuts

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