tabletype

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package tabletype is the one model of a column type the platform declares a table with or writes a typed file from (#1833): the Trino types a registered table's columns carry, the parse of the type text Trino reports for a query's columns, and the inference of a column's type from JSON values.

It knows nothing about registrations, files or connectors. A JSON-lines registration and a script's Parquet export infer their types here, so the two cannot disagree about what a column of numbers is, and a Parquet file's columns and a trino_export's columns are rendered through the same Type.

Index

Constants

View Source
const DeclaredTimestampPrecision = 6

DeclaredTimestampPrecision is the precision every TIMESTAMP column is declared with; see Timestamp.

Variables

View Source
var ErrTooDeep = fmt.Errorf("a value is nested more than %d levels deep", maxDepth)

ErrTooDeep is returned for a JSON value nested beyond what a table's type can be declared with.

Functions

func CheckFieldName

func CheckFieldName(name string) error

CheckFieldName refuses a ROW field name the Hive metastore cannot store.

A table's nested types are kept in the metastore as Hive type text ("struct<a:bigint>"), whose parser admits a field name made of letters, digits, '_', '.', '$' and ' ' and nothing else. Trino creates a table whose ROW declares any other name, and every later statement on it -- a SELECT, a DROP -- fails with "Could not read table schema" (observed on Trino 453), so a registration refused here is a table nobody is left unable to remove. A top-level column is not held to this: its name is stored on its own.

func CheckName

func CheckName(name string) error

CheckName refuses a name a Hive column or ROW field cannot carry, or one the JSON reader would leave empty on every row. It is the rule for a JSON-lines key at any depth and for a Parquet column or field.

func DecodeJSON

func DecodeJSON(data []byte) (any, error)

DecodeJSON decodes one JSON value into the values the inference reads: nil, bool, json.Number, string, []any and *Object. A second value after the first is an error, and so is one nested deeper than maxDepth.

func IsTrailing

func IsTrailing(err error) bool

IsTrailing reports whether DecodeJSON refused a second value after the first.

func QuoteName

func QuoteName(name string) string

QuoteName renders a name as a Trino delimited identifier.

Types

type Column

type Column struct {
	Name string
	Type Type
}

Column is one column an inference declares.

type Field

type Field struct {
	Name string
	Type Type
}

Field is one field of a ROW.

type Inferrer

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

Inferrer reads the type of every column across a sequence of records, every record rather than a sample: one value on the last line that does not fit what the first thousand said is a query that fails on the last line.

The rules, per key, ignoring null and an absent key:

  • every value a JSON boolean: BOOLEAN;
  • every value an integer within int64: BIGINT;
  • any number with a fraction or an exponent, alone or among integers: DOUBLE;
  • anything else a scalar can be -- strings, a mix of kinds, an integer too large for BIGINT, nothing but nulls: VARCHAR, which is the text of the value and loses nothing;
  • objects: a ROW over the union of their keys, each field inferred the same way; lists: an ARRAY of what their elements infer to.

A key whose values cannot be one type is refused, naming the key, because no declaration reads both: an object, a list and a scalar are three shapes, and a key holding any two of them in different records is a conflict. Two scalars are not -- they meet at VARCHAR.

func NewInferrer

func NewInferrer() *Inferrer

NewInferrer returns an Inferrer with no columns.

func (*Inferrer) Columns

func (in *Inferrer) Columns() ([]Column, error)

Columns returns the columns in first-seen order with the type each infers to.

func (*Inferrer) Len

func (in *Inferrer) Len() int

Len is how many columns the records have named so far.

func (*Inferrer) Observe

func (in *Inferrer) Observe(names []string, values []any) error

Observe takes one record: its column names, in the order written, and the value under each. A name seen for the first time adds a column at the end. Values are what DecodeJSON produces.

type Kind

type Kind string

Kind names a type. The declarable kinds are the ones a Hive table over a JSON-lines or Parquet file can be declared with; the source-only kinds are what a query can return that no registration declares, and a writer stores them as text.

const (
	Boolean   Kind = "BOOLEAN"
	Tinyint   Kind = "TINYINT"
	Smallint  Kind = "SMALLINT"
	Integer   Kind = "INTEGER"
	Bigint    Kind = "BIGINT"
	Real      Kind = "REAL"
	Double    Kind = "DOUBLE"
	Decimal   Kind = "DECIMAL"
	Varchar   Kind = "VARCHAR"
	Varbinary Kind = "VARBINARY"
	Date      Kind = "DATE"
	// Timestamp is declared at microseconds, TIMESTAMP(6), whatever the
	// precision its source had: a Hive table's timestamps are read at the
	// catalog's hive.timestamp-precision, the connector refuses a column
	// declared at any other ("Incorrect timestamp precision for timestamp(3);
	// the configured precision is MICROSECONDS"), and MICROSECONDS is the
	// setting a catalog reads a Parquet file's timestamps exactly at.
	Timestamp Kind = "TIMESTAMP"
	Array     Kind = "ARRAY"
	Map       Kind = "MAP"
	Row       Kind = "ROW"
)

The declarable kinds.

const (
	// TimestampTZ is a timestamp with a time zone. A Parquet file keeps the
	// instant and not the zone.
	TimestampTZ Kind = "TIMESTAMP WITH TIME ZONE"
	// Text is any other type a query returns as text -- CHAR, TIME, JSON,
	// UUID, IPADDRESS, an interval -- which a file stores as a string.
	Text Kind = "TEXT"
)

The source-only kinds.

type Object

type Object struct {
	Keys   []string
	Values []any
}

Object is a JSON object with its keys in the order they were written. Decoding into a Go map loses that order, and the order is what a table's columns and a ROW's fields are declared in.

func (*Object) Get

func (o *Object) Get(key string) (any, bool)

Get returns the value under a key, and whether the object holds one.

type Type

type Type struct {
	Kind      Kind
	Precision int
	Scale     int
	Elem      *Type
	Key       *Type
	Fields    []Field
	Source    string
}

Type is one column type. Precision and Scale describe a DECIMAL, and Precision a source TIMESTAMP's fractional digits; Elem is an ARRAY's element or a MAP's value, Key a MAP's key, and Fields a ROW's fields in order. Source is the type text a query reported, kept for a Text type so a writer can say what it stored as text.

func ArrayOf

func ArrayOf(elem Type) Type

ArrayOf returns an ARRAY of elem.

func DecimalOf

func DecimalOf(precision, scale int) Type

DecimalOf returns a DECIMAL(precision, scale).

func MapOf

func MapOf(key, value Type) Type

MapOf returns a MAP from key to value.

func Parse

func Parse(text string) (Type, error)

Parse reads the type text Trino reports for a query column -- "bigint", "decimal(12,2)", "timestamp(6) with time zone", "array(row(a bigint, "b c" varchar))" -- into a Type. A type with no Parquet form of its own (TIME, JSON, UUID, an interval, anything this parse does not know) is Text, which a writer stores as a string, so a query never fails to export over one column.

The text is case-insensitive and a ROW field name may be quoted. Precision and scale a caller learned elsewhere -- a top-level DECIMAL reported as the bare "DECIMAL" with its numbers beside it -- are applied with WithPrecision.

func RowOf

func RowOf(fields ...Field) Type

RowOf returns a ROW of fields.

func Scalar

func Scalar(kind Kind) Type

Scalar returns a Type of a kind that takes no arguments.

func (Type) Nested

func (t Type) Nested() bool

Nested reports whether the type holds other types.

func (Type) SQL

func (t Type) SQL() string

SQL renders the type as a CREATE TABLE declares it. A ROW's field names are quoted, because they come from a file somebody wrote.

func (Type) WithPrecision

func (t Type) WithPrecision(precision, scale int) Type

WithPrecision applies a precision and scale reported beside a bare type name: a DECIMAL's two numbers, or a TIMESTAMP's fractional digits. A type that already carries its own is returned unchanged.

Jump to

Keyboard shortcuts

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