Documentation
¶
Index ¶
- func CellInt64(v any) (int64, bool)
- func CellString(v any) (string, bool)
- func ExplainRows(ctx context.Context, db RowQuerier, sql string) (columns []string, rows [][]any, err error)
- func QueryCustomMetrics(ctx context.Context, db RowQuerier, dialect string, sqlQuery string, ...) ([]*scrapper.CustomMetricsRow, error)
- func QueryShape(ctx context.Context, db RowQuerier, dialect string, sql string) ([]*scrapper.QueryShapeColumn, error)
- func RunRawQuery(ctx context.Context, db RowQuerier, dialect string, sqlQuery string) (scrapper.RawQueryRowIterator, error)
- func ShapeColumns(dialect string, columnTypes []*sql.ColumnType) []*scrapper.QueryShapeColumn
- func ValueFromRaw(v any) scrapper.Value
- type RawDB
- type RowQuerier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CellInt64 ¶ added in v0.16.0
CellInt64 coerces a raw driver cell to int64, handling the signed/unsigned/ float/text shapes different drivers use for EXPLAIN numeric columns.
func CellString ¶ added in v0.16.0
CellString renders a raw driver cell (string, []byte, fmt.Stringer, ...) as a string. Used to pull JSON text out of a single EXPLAIN result cell.
func ExplainRows ¶ added in v0.16.0
func ExplainRows(ctx context.Context, db RowQuerier, sql string) (columns []string, rows [][]any, err error)
ExplainRows runs an EXPLAIN-style statement and returns the result column names plus every row as a slice of raw driver values. It is the shared plumbing behind the per-dialect EstimateQuery implementations that read an EXPLAIN result set.
The caller is responsible for passing a statement that only plans the query (EXPLAIN / EXPLAIN ESTIMATE / EXPLAIN USING JSON) and never one that executes it (no EXPLAIN ANALYZE) — EstimateQuery must not run the user's query.
func QueryCustomMetrics ¶
func QueryCustomMetrics(ctx context.Context, db RowQuerier, dialect string, sqlQuery string, args ...any) ([]*scrapper.CustomMetricsRow, error)
QueryCustomMetrics runs a metrics query. dialect is Scrapper.DialectType(), which decides what each column's native type means; a time is decoded by its column's kind (see scrapper.NormalizeTimeOfKind).
func QueryShape ¶ added in v0.8.3
func QueryShape(ctx context.Context, db RowQuerier, dialect string, sql string) ([]*scrapper.QueryShapeColumn, error)
QueryShape describes the columns sql returns without running it. dialect is Scrapper.DialectType(), which NativeValueKind needs to fill in each column's Kind.
func RunRawQuery ¶ added in v0.13.7
func RunRawQuery(ctx context.Context, db RowQuerier, dialect string, sqlQuery string) (scrapper.RawQueryRowIterator, error)
RunRawQuery executes sqlQuery and returns an iterator over typed rows. The iterator must be Close()'d by the caller; it also auto-closes on io.EOF.
Unlike QueryCustomMetrics this path preserves every column: segment* names are not sidelined, and string columns come back as StringValue rather than being re-parsed and collapsed to IgnoredValue. Unknown driver types fall back to StringValue(fmt.Sprint(v)) so nothing is silently dropped.
dialect is Scrapper.DialectType(). It decides what each column's native type means (Columns()[i].Kind), which is how each cell is decoded.
func ShapeColumns ¶ added in v0.21.0
func ShapeColumns(dialect string, columnTypes []*sql.ColumnType) []*scrapper.QueryShapeColumn
ShapeColumns describes the columns of a database/sql result as QueryShape and RunRawQuery report them: name, native type, position, the Kind the native type has on dialect (Scrapper.DialectType()), whether a float is single precision, and an exact numeric's precision and scale.
func ValueFromRaw ¶ added in v0.29.0
ValueFromRaw decodes one raw driver value, as a QueryMaps row holds it (read it with exec.QueryMapResult.Get), to a scrapper.Value. Read the result with the scrapper As* accessors.
Text is kept as text: a string, []byte or sql.RawBytes is a StringValue with exactly the bytes the driver returned. It is never read as a UUID (16 bytes of text are not a binary UUID, and 36 characters are not reformatted), as JSON or as a Postgres array. The one exception to byte-exact is invalid UTF-8, which is dropped, as RunRawQuery drops it. A binary cell (BLOB, bytea, BINARY) arrives as []byte too and is read as text the same way, so it is lossy here and in RunRawQuery alike: binary data is not supported.
SQL NULL is scrapper.NullValue, which no As* accessor reads. A Go type this does not know comes back as its fmt text, which AsString accepts, so a caller that must reject an unexpected driver type checks the raw cell's Go type before decoding it.
A QueryMaps row has no column type, so this is RunRawQuery's decoding with the column's kind unknown: an exact numeric the driver returns as text stays that text, a decimal type goes through float64 (only *big.Rat, exact by its type, is decoded exactly), and a UUID is a UUID only when the driver hands back a [16]byte or a uuid.UUID. A caller that needs a value typed by its column uses RunRawQuery.
What that means per driver, as measured on real warehouses:
- Snowflake returns every NUMBER, integers included, as text, so AsString reads it and AsInt64 does not. A VARIANT or OBJECT arrives as its JSON text, a StringValue.
- BigQuery returns DATE, TIME and DATETIME as civil values, decoded to text ("2024-03-15"), not a TimeValue. TIMESTAMP is a TimeValue.
- DuckDB (and SQL Server's UNIQUEIDENTIFIER) hand a UUID back as 16 raw bytes. Those are indistinguishable from 16 characters of text, so they decode to meaningless text. Cast the column to text in the query.
- MySQL and MariaDB return text as []byte, which is kept as text.
Types ¶
type RawDB ¶ added in v0.11.0
RawDB wraps a *sqlx.DB to satisfy RowQuerier without any enrichment. Use this only in tests; production code should pass an executor.
type RowQuerier ¶ added in v0.11.0
type RowQuerier interface {
QueryRows(ctx context.Context, q string, args ...interface{}) (*sqlx.Rows, error)
}
RowQuerier is the minimal interface for executing queries that return rows. It is satisfied by all StdSqlExecutor implementations (which add SQL comment enrichment, query tagging, and stats collection) and by RawDB for tests.