sql

package
v0.0.0-...-bf013fd Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2016 License: Apache-2.0 Imports: 43 Imported by: 0

Documentation

Overview

Package sql provides the user-facing API for access to a Cockroach datastore. As the name suggests, the API is based around SQL, the same SQL you find in traditional RDMBS systems like Oracle, MySQL or Postgres. The core Cockroach system implements a distributed, transactional, monolithic sorted key-value map. The sql package builds on top of this core system adding parsing, query planning and query execution as well as defining the privilege model.

Databases and Tables

The two primary objects are databases and tables. A database is a namespace which holds a series of tables. Conceptually, a database can be viewed like a directory in a filesystem plus some additional metadata (privileges). A table is like a file on steroids: containing a structured layout of rows and columns along with secondary indexes.

Like a directory, a database has a name and metadata. The metadata is defined by the DatabaseDescriptor:

message DatabaseDescriptor {
  optional string name;
  optional uint32 id;
  optional PrivilegeDescriptor privileges;
}

Similarly, tables have a TableDescriptor:

message TableDescriptor {
  optional string name;
  optional uint32 id;
  repeated ColumnDescriptor columns;
  optional IndexDescriptor primary_index;
  repeated IndexDescriptor indexes;
  optional PrivilegeDescriptor privileges;
}

Both the database ID and the table ID are allocate from the same "ID space" and IDs are never reused.

The namespace in which databases and tables exist contains only two levels: the root level contains databases and the database level contains tables. The "system.namespace" and "system.descriptor" tables implement the mapping from database/table name to ID and from ID to descriptor:

CREATE TABLE system.namespace (
  "parentID" INT,
  "name"     CHAR,
  "id"       INT,
  PRIMARY KEY (parentID, name)
);

CREATE TABLE system.descriptor (
  "id"         INT PRIMARY KEY,
  "descriptor" BLOB
);

The reserved ID of 0 is used for the "root" of the namespace in which the databases reside. In order to lookup the ID of a database given its name, the system effectively does a query like:

SELECT id FROM system.namespace WHERE parentID = 0 AND name = <database-name>

And given a database/table ID, the system looks up the descriptor using a query like:

SELECT descriptor FROM system.descriptor WHERE id = <ID>

Primary Key Addressing

All of the SQL data stored in tables is mapped down to keys and values. This mapping is referred to as key addressing. All tables have a primary key, whether explicitly listed in the schema or automatically generated. Note that a primary key is unrelated to the core Cockroach key-value functionality and is instead referring to the primary key for a SQL table.

The primary key consists of one or more non-NULL columns from the table. For a given row of the table, the columns for the primary key are encoded into a single string using the routines in util/encoding. These routines allow for the encoding of NULL values, integers, floating pointer numbers and strings in such a way that lexicographic ordering of the encoded strings corresponds to the same ordering of the unencoded data. Using "system.namespace" as an example, the primary key columns would be encoded as:

/parentID/name

[Note that "/" is being used to disambiguate the components of the key. The actual encodings do not use "/"].

Before being stored in the monolothic key-value space, the encoded primary key columns are prefixed with the table ID and an ID indicating that the key corresponds to the primary index:

/TableID/PrimaryIndexID/parentID/name

The column data associated with a row in a table is stored within the primary index which is the index associated with the primary key. Every column has a unique ID (that is local to the table). The value for a column is stored at the key:

/TableID/PrimaryIndexID/parentID/name/ColumnID -> Value

A column containing a NULL value is not stored in the monolithic map. In order to detect rows which only contain NULL values in non-primary key columns, every row has a sentinel key indicating its existence. The sentinel key is simply the primary index key:

/TableID/PrimaryIndexID/parentID/name -> NULL

As an optimization, columns that are part of the primary key are not stored separately as their data can be decoded from the sentinel value.

Secondary Indexes

Despite not being a formal part of SQL, secondary indexes are one of its most powerful features. Secondary indexes are a level of indirection that allow quick lookup of a row using something other than the primary key. As an example, we can imagine creating a secondary index on the "system.namespace" table:

CREATE INDEX name ON system.namespace (name);

This would create a "name" index composed solely of the "name" column. The key addressing for this non-unique index looks like:

/TableId/SecondaryIndexID/name/parentID -> NULL

Notice that while the index is on "name", the key contains both "name" and "parentID". This is done to ensure that each row for a table has a unique key for the non-unique index. In general, for a non-unique index we encoded the index's columns followed by any primary key columns that have not already been mentioned. This effectively transforms any non-unique index into a unique index.

Let's suppose that we had instead defined the index as:

CREATE UNIQUE INDEX name ON system.namespace (name, id);

The key addressing for a unique index looks like:

/TableID/SecondaryID/name/ID -> /parentID

Unique index keys are defined like this so that a conditional put operation can fail if that key already exists for another row, thereby enforcing the uniqueness constraint. The value for a unique index is composed of any primary key columns that are not part of the index ("parentID" in this example).

Query Planning and Execution

Query planning is the system which takes a parsed SQL statement (described by an abstract syntax tree) and creates an execution plan which is itself a tree consisting of a set of scan, join, group, sort and projection operations. For the bulk of SQL statements, query planning is straightforward: the complexity lies in SELECT.

At one end of the performance spectrum, an implementation of SELECT can be straightforward: do a full scan of the (joined) tables in the FROM clause, filter rows based on the WHERE clause, group the resulting rows based on the GROUP BY clause, filter those rows using the HAVING clause, sort using the ORDER BY clause. There are a number of steps, but they all have well defined semantics and are mostly just an exercise in software engineering: retrieve the rows as quickly as possible and then send them through the pipeline of filtering, grouping, filtering and sorting.

At the other end of the performance spectrum, query planners attempt to take advantage of secondary indexes to limit the data retrieved, make joining of data between two tables easier and faster and to avoid the need to sort data by retrieving it in a sorted or partially sorted form. The details of how we implement this are in flux and will continue to be in flux for the foreseeable future. This section is intended to provide a high-level overview of a few of the techniques involved.

After parsing a SELECT query, the query planner performs semantic analysis to verify the queries correctness and to resolve names within the query to actual objects within the system. Let's consider the query:

SELECT id FROM system.namespace WHERE parentID = 0 AND name = 'test'

This query would look up the ID of the database named "test". The query planner needs to resolve the "system.namespace" qualified name in the FROM clause to the appropriate TableDescriptor. It also needs to resolve the "id", "parentID" and "name" column references to the appropriate column descriptions with the "system.namespace" TableDescriptor. Lastly, as part of semantic analysis, the query planner verifies that the expressions in the select targets and the WHERE clause are valid (e.g. the WHERE clause evaluates to a boolean).

From that starting point, the query planner then analyzes the GROUP BY and ORDER BY clauses, adding "hidden" targets for expressions used in those clauses that are not explicit targets of the query. In our example without a GROUP BY or ORDER BY clause we move straight to the next step: index selection. Index selection is the stage where the query planner selects the best index to scan and selects the start and end keys to use for scanning the index. Depending on the query, the query planner might even select multiple ranges to scan from an index or multiple ranges from different indexes.

How does the query planner decide which index to use and which range of the index to scan? We currently use a restricted form of value propagation in oder to determine the range of possible values for columns referenced in the WHERE clause. Using this range information, each index is examined to determine if it is a potential candidate and ranked according to its specificity. In addition to ranking indexes by the column value range information, they are ranked by how well they match the sorting required by the ORDER BY clause. Back to the example above, the range information would determine that:

parentID >= 0 AND parentID <= 0 AND name >= 'test' and name <= 'test

Notice that each column has a start and end value associated with it. Since there is only a single index on the "system.namespace" table, it is always selected. The start key is computed using the range information as:

/system.descriptor/primary/0/test

The end key is computed as:

/system.descriptor/primary/0/tesu

The "tesu" suffix is not a typo: the end key is computed as the "prefix end key" for the key "/TableID/PrimaryIndexId/0/test". This is done by incrementing the final byte of the key such that "t" becomes "u".

Our example query thus only scans two key-value pairs:

  /system.descriptor/primary/0/test    -> NULL
  /system.descriptor/primary/0/test/id -> <ID>

	Package sql is a generated protocol buffer package.

	It is generated from these files:
		cockroach/sql/privilege.proto
		cockroach/sql/structured.proto

	It has these top-level messages:
		UserPrivileges
		PrivilegeDescriptor
		ColumnType
		ColumnDescriptor
		IndexDescriptor
		DescriptorMutation
		TableDescriptor
		DatabaseDescriptor
		Descriptor

Index

Constants

View Source
const (

	// CodeUniquenessConstraintViolationError represents violations of uniqueness
	// constraints.
	CodeUniquenessConstraintViolationError string = "23505"
	// CodeTransactionAbortedError signals that the user tried to execute a
	// statement in the context of a SQL txn that's already aborted.
	CodeTransactionAbortedError string = "25P02"
	// CodeInternalError represents all internal cockroach errors, plus acts
	// as a catch-all for random errors for which we haven't implemented the
	// appropriate error code.
	CodeInternalError string = "XX000"

	// CodeRetriableError signals to the user that the SQL txn entered the
	// RESTART_WAIT state and that a RESTART statement should be issued.
	CodeRetriableError string = "CR000"
	// CodeTransactionCommittedError signals that the SQL txn is in the
	// COMMIT_WAIT state and a COMMIT statement should be issued.
	CodeTransactionCommittedError string = "CR001"
)
View Source
const (
	// PrimaryKeyIndexName is the name of the index for the primary key.
	PrimaryKeyIndexName = "primary"
)

Variables

View Source
var (
	// LeaseDuration is the mean duration a lease will be acquired for. The
	// actual duration is jittered in the range
	// [0.75,1.25]*LeaseDuration. Exported for testing purposes only.
	LeaseDuration = 5 * time.Minute
	// MinLeaseDuration is the minimum duration a lease will have remaining upon
	// acquisition. Exported for testing purposes only.
	MinLeaseDuration = time.Minute
)
View Source
var (
	ErrInvalidLengthPrivilege = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowPrivilege   = fmt.Errorf("proto: integer overflow")
)
View Source
var (
	ErrInvalidLengthStructured = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowStructured   = fmt.Errorf("proto: integer overflow")
)
View Source
var (

	// SystemAllowedPrivileges describes the privileges allowed for each
	// system object. No user may have more than those privileges, and
	// the root user must have exactly those privileges.
	// CREATE|DROP|ALL should always be denied.
	SystemAllowedPrivileges = map[ID]privilege.List{
		keys.SystemDatabaseID:  privilege.ReadData,
		keys.NamespaceTableID:  privilege.ReadData,
		keys.DescriptorTableID: privilege.ReadData,
		keys.UsersTableID:      privilege.ReadWriteData,
		keys.ZonesTableID:      privilege.ReadWriteData,
		keys.LeaseTableID:      privilege.ReadWriteData,
		keys.RangeEventTableID: privilege.ReadWriteData,
		keys.UITableID:         privilege.ReadWriteData,
	}

	// NumSystemDescriptors should be set to the number of system descriptors
	// above (SystemDB and each system table). This is used by tests which need
	// to know the number of system descriptors intended for installation; it starts at
	// 1 for the SystemDB descriptor created above, and is incremented by every
	// call to createSystemTable().
	NumSystemDescriptors = 1
)
View Source
var ColumnType_Kind_name = map[int32]string{
	0: "BOOL",
	1: "INT",
	2: "FLOAT",
	3: "DECIMAL",
	4: "DATE",
	5: "TIMESTAMP",
	6: "INTERVAL",
	7: "STRING",
	8: "BYTES",
}
View Source
var ColumnType_Kind_value = map[string]int32{
	"BOOL":      0,
	"INT":       1,
	"FLOAT":     2,
	"DECIMAL":   3,
	"DATE":      4,
	"TIMESTAMP": 5,
	"INTERVAL":  6,
	"STRING":    7,
	"BYTES":     8,
}
View Source
var DescriptorMutation_Direction_name = map[int32]string{
	0: "NONE",
	1: "ADD",
	2: "DROP",
}
View Source
var DescriptorMutation_Direction_value = map[string]int32{
	"NONE": 0,
	"ADD":  1,
	"DROP": 2,
}
View Source
var DescriptorMutation_State_name = map[int32]string{
	0: "UNKNOWN",
	1: "DELETE_ONLY",
	2: "WRITE_ONLY",
}
View Source
var DescriptorMutation_State_value = map[string]int32{
	"UNKNOWN":     0,
	"DELETE_ONLY": 1,
	"WRITE_ONLY":  2,
}
View Source
var IndexDescriptor_Direction_name = map[int32]string{
	0: "ASC",
	1: "DESC",
}
View Source
var IndexDescriptor_Direction_value = map[string]int32{
	"ASC":  0,
	"DESC": 1,
}

Functions

func AddEventLogToMetadataSchema

func AddEventLogToMetadataSchema(schema *MetadataSchema)

AddEventLogToMetadataSchema adds the event log table to the supplied MetadataSchema.

func GetZoneConfig

func GetZoneConfig(cfg config.SystemConfig, id uint32) (*config.ZoneConfig, error)

GetZoneConfig returns the zone config for the object with 'id'.

func MakeDescMetadataKey

func MakeDescMetadataKey(descID ID) roachpb.Key

MakeDescMetadataKey returns the key for the descriptor.

func MakeIndexKeyPrefix

func MakeIndexKeyPrefix(tableID ID, indexID IndexID) []byte

MakeIndexKeyPrefix returns the key prefix used for the index's data.

func MakeNameMetadataKey

func MakeNameMetadataKey(parentID ID, name string) roachpb.Key

MakeNameMetadataKey returns the key for the name. Pass name == "" in order to generate the prefix key to use to scan over all of the names for the specified parentID.

func MakeZoneKey

func MakeZoneKey(id ID) roachpb.Key

MakeZoneKey returns the key for 'id's entry in the system.zones table.

func NormalizeName

func NormalizeName(name string) string

NormalizeName normalizes to lowercase and Unicode Normalization Form C (NFC).

func SetKVBatchSize

func SetKVBatchSize(val int64) func()

SetKVBatchSize changes the kvFetcher batch size, and returns a function that restores it.

func TestDisableAsyncSchemaChangeExec

func TestDisableAsyncSchemaChangeExec() func()

TestDisableAsyncSchemaChangeExec is used in tests to disable the asynchronous execution of schema changes.

func TestDisableSyncSchemaChangeExec

func TestDisableSyncSchemaChangeExec() func()

TestDisableSyncSchemaChangeExec is used in tests to disable the synchronous execution of schema changes, so that the asynchronous schema changer can run the schema changes.

func TestDisableTableLeases

func TestDisableTableLeases() func()

TestDisableTableLeases disables table leases and returns a function that can be used to enable it.

Types

type ColumnDescriptor

type ColumnDescriptor struct {
	Name     string     `protobuf:"bytes,1,opt,name=name" json:"name"`
	ID       ColumnID   `protobuf:"varint,2,opt,name=id,casttype=ColumnID" json:"id"`
	Type     ColumnType `protobuf:"bytes,3,opt,name=type" json:"type"`
	Nullable bool       `protobuf:"varint,4,opt,name=nullable" json:"nullable"`
	// Default expression to use to populate the column on insert if no
	// value is provided.
	DefaultExpr *string `protobuf:"bytes,5,opt,name=default_expr,json=defaultExpr" json:"default_expr,omitempty"`
	Hidden      bool    `protobuf:"varint,6,opt,name=hidden" json:"hidden"`
}

func (*ColumnDescriptor) Descriptor

func (*ColumnDescriptor) Descriptor() ([]byte, []int)

func (*ColumnDescriptor) Marshal

func (m *ColumnDescriptor) Marshal() (data []byte, err error)

func (*ColumnDescriptor) MarshalTo

func (m *ColumnDescriptor) MarshalTo(data []byte) (int, error)

func (*ColumnDescriptor) ProtoMessage

func (*ColumnDescriptor) ProtoMessage()

func (*ColumnDescriptor) Reset

func (m *ColumnDescriptor) Reset()

func (*ColumnDescriptor) Size

func (m *ColumnDescriptor) Size() (n int)

func (*ColumnDescriptor) String

func (m *ColumnDescriptor) String() string

func (*ColumnDescriptor) Unmarshal

func (m *ColumnDescriptor) Unmarshal(data []byte) error

type ColumnID

type ColumnID uint32

ColumnID is a custom type for ColumnDescriptor IDs.

type ColumnType

type ColumnType struct {
	Kind ColumnType_Kind `protobuf:"varint,1,opt,name=kind,enum=cockroach.sql.ColumnType_Kind" json:"kind"`
	// BIT, INT, FLOAT, DECIMAL, CHAR and BINARY
	Width int32 `protobuf:"varint,2,opt,name=width" json:"width"`
	// FLOAT and DECIMAL.
	Precision int32 `protobuf:"varint,3,opt,name=precision" json:"precision"`
}

func (*ColumnType) Descriptor

func (*ColumnType) Descriptor() ([]byte, []int)

func (*ColumnType) Marshal

func (m *ColumnType) Marshal() (data []byte, err error)

func (*ColumnType) MarshalTo

func (m *ColumnType) MarshalTo(data []byte) (int, error)

func (*ColumnType) ProtoMessage

func (*ColumnType) ProtoMessage()

func (*ColumnType) Reset

func (m *ColumnType) Reset()

func (*ColumnType) SQLString

func (c *ColumnType) SQLString() string

SQLString returns the SQL string corresponding to the type.

func (*ColumnType) Size

func (m *ColumnType) Size() (n int)

func (*ColumnType) String

func (m *ColumnType) String() string

func (*ColumnType) Unmarshal

func (m *ColumnType) Unmarshal(data []byte) error

type ColumnType_Kind

type ColumnType_Kind int32

These mirror the types supported by the sql/parser. See sql/parser/types.go.

const (
	ColumnType_BOOL      ColumnType_Kind = 0
	ColumnType_INT       ColumnType_Kind = 1
	ColumnType_FLOAT     ColumnType_Kind = 2
	ColumnType_DECIMAL   ColumnType_Kind = 3
	ColumnType_DATE      ColumnType_Kind = 4
	ColumnType_TIMESTAMP ColumnType_Kind = 5
	ColumnType_INTERVAL  ColumnType_Kind = 6
	ColumnType_STRING    ColumnType_Kind = 7
	ColumnType_BYTES     ColumnType_Kind = 8
)

func (ColumnType_Kind) Enum

func (x ColumnType_Kind) Enum() *ColumnType_Kind

func (ColumnType_Kind) EnumDescriptor

func (ColumnType_Kind) EnumDescriptor() ([]byte, []int)

func (ColumnType_Kind) String

func (x ColumnType_Kind) String() string

func (*ColumnType_Kind) UnmarshalJSON

func (x *ColumnType_Kind) UnmarshalJSON(data []byte) error

type DatabaseDescriptor

type DatabaseDescriptor struct {
	Name       string               `protobuf:"bytes,1,opt,name=name" json:"name"`
	ID         ID                   `protobuf:"varint,2,opt,name=id,casttype=ID" json:"id"`
	Privileges *PrivilegeDescriptor `protobuf:"bytes,3,opt,name=privileges" json:"privileges,omitempty"`
}

DatabaseDescriptor represents a namespace (aka database) and is stored in a structured metadata key. The DatabaseDescriptor has a globally-unique ID shared with the TableDescriptor ID. Permissions are applied to all tables in the namespace.

func (*DatabaseDescriptor) Descriptor

func (*DatabaseDescriptor) Descriptor() ([]byte, []int)

func (*DatabaseDescriptor) GetID

func (m *DatabaseDescriptor) GetID() ID

func (*DatabaseDescriptor) GetName

func (m *DatabaseDescriptor) GetName() string

func (*DatabaseDescriptor) GetPrivileges

func (m *DatabaseDescriptor) GetPrivileges() *PrivilegeDescriptor

func (*DatabaseDescriptor) Marshal

func (m *DatabaseDescriptor) Marshal() (data []byte, err error)

func (*DatabaseDescriptor) MarshalTo

func (m *DatabaseDescriptor) MarshalTo(data []byte) (int, error)

func (*DatabaseDescriptor) ProtoMessage

func (*DatabaseDescriptor) ProtoMessage()

func (*DatabaseDescriptor) Reset

func (m *DatabaseDescriptor) Reset()

func (*DatabaseDescriptor) SetID

func (desc *DatabaseDescriptor) SetID(id ID)

SetID implements the descriptorProto interface.

func (*DatabaseDescriptor) SetName

func (desc *DatabaseDescriptor) SetName(name string)

SetName implements the descriptorProto interface.

func (*DatabaseDescriptor) Size

func (m *DatabaseDescriptor) Size() (n int)

func (*DatabaseDescriptor) String

func (m *DatabaseDescriptor) String() string

func (*DatabaseDescriptor) TypeName

func (desc *DatabaseDescriptor) TypeName() string

TypeName returns the plain type of this descriptor.

func (*DatabaseDescriptor) Unmarshal

func (m *DatabaseDescriptor) Unmarshal(data []byte) error

func (*DatabaseDescriptor) Validate

func (desc *DatabaseDescriptor) Validate() error

Validate validates that the database descriptor is well formed. Checks include validate the database name, and verifying that there is at least one read and write user.

type Descriptor

type Descriptor struct {
	// Types that are valid to be assigned to Union:
	//	*Descriptor_Table
	//	*Descriptor_Database
	Union isDescriptor_Union `protobuf_oneof:"union"`
}

Descriptor is a union type holding either a table or database descriptor.

func (*Descriptor) Descriptor

func (*Descriptor) Descriptor() ([]byte, []int)

func (*Descriptor) GetDatabase

func (m *Descriptor) GetDatabase() *DatabaseDescriptor

func (*Descriptor) GetID

func (desc *Descriptor) GetID() ID

GetID returns the ID of the descriptor.

func (*Descriptor) GetName

func (desc *Descriptor) GetName() string

GetName returns the Name of the descriptor.

func (*Descriptor) GetTable

func (m *Descriptor) GetTable() *TableDescriptor

func (*Descriptor) GetUnion

func (m *Descriptor) GetUnion() isDescriptor_Union

func (*Descriptor) Marshal

func (m *Descriptor) Marshal() (data []byte, err error)

func (*Descriptor) MarshalTo

func (m *Descriptor) MarshalTo(data []byte) (int, error)

func (*Descriptor) ProtoMessage

func (*Descriptor) ProtoMessage()

func (*Descriptor) Reset

func (m *Descriptor) Reset()

func (*Descriptor) Size

func (m *Descriptor) Size() (n int)

func (*Descriptor) String

func (m *Descriptor) String() string

func (*Descriptor) Unmarshal

func (m *Descriptor) Unmarshal(data []byte) error

func (*Descriptor) XXX_OneofFuncs

func (*Descriptor) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{})

XXX_OneofFuncs is for the internal use of the proto package.

type DescriptorMutation

type DescriptorMutation struct {
	// Types that are valid to be assigned to Descriptor_:
	//	*DescriptorMutation_Column
	//	*DescriptorMutation_Index
	Descriptor_ isDescriptorMutation_Descriptor_ `protobuf_oneof:"descriptor"`
	State       DescriptorMutation_State         `protobuf:"varint,3,opt,name=state,enum=cockroach.sql.DescriptorMutation_State" json:"state"`
	Direction   DescriptorMutation_Direction     `protobuf:"varint,4,opt,name=direction,enum=cockroach.sql.DescriptorMutation_Direction" json:"direction"`
	// The mutation id used to group mutations that should be applied together.
	// This is used for situations like creating a unique column, which
	// involve adding two mutations: one for the column, and another for the
	// unique constraint index.
	MutationID MutationID `protobuf:"varint,5,opt,name=mutation_id,json=mutationId,casttype=MutationID" json:"mutation_id"`
}

A DescriptorMutation represents a column or an index that has either been added or dropped and hasn't yet transitioned into a stable state: completely backfilled and visible, or completely deleted. A table descriptor in the middle of a schema change will have a DescriptorMutation FIFO queue containing each column/index descriptor being added or dropped.

func (*DescriptorMutation) Descriptor

func (*DescriptorMutation) Descriptor() ([]byte, []int)

func (*DescriptorMutation) GetColumn

func (m *DescriptorMutation) GetColumn() *ColumnDescriptor

func (*DescriptorMutation) GetDescriptor_

func (m *DescriptorMutation) GetDescriptor_() isDescriptorMutation_Descriptor_

func (*DescriptorMutation) GetIndex

func (m *DescriptorMutation) GetIndex() *IndexDescriptor

func (*DescriptorMutation) Marshal

func (m *DescriptorMutation) Marshal() (data []byte, err error)

func (*DescriptorMutation) MarshalTo

func (m *DescriptorMutation) MarshalTo(data []byte) (int, error)

func (*DescriptorMutation) ProtoMessage

func (*DescriptorMutation) ProtoMessage()

func (*DescriptorMutation) Reset

func (m *DescriptorMutation) Reset()

func (*DescriptorMutation) Size

func (m *DescriptorMutation) Size() (n int)

func (*DescriptorMutation) String

func (m *DescriptorMutation) String() string

func (*DescriptorMutation) Unmarshal

func (m *DescriptorMutation) Unmarshal(data []byte) error

func (*DescriptorMutation) XXX_OneofFuncs

func (*DescriptorMutation) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{})

XXX_OneofFuncs is for the internal use of the proto package.

type DescriptorMutation_Column

type DescriptorMutation_Column struct {
	Column *ColumnDescriptor `protobuf:"bytes,1,opt,name=column,oneof"`
}

func (*DescriptorMutation_Column) MarshalTo

func (m *DescriptorMutation_Column) MarshalTo(data []byte) (int, error)

func (*DescriptorMutation_Column) Size

func (m *DescriptorMutation_Column) Size() (n int)

type DescriptorMutation_Direction

type DescriptorMutation_Direction int32

Direction of mutation.

const (
	// Not used.
	DescriptorMutation_NONE DescriptorMutation_Direction = 0
	// Descriptor is being added.
	DescriptorMutation_ADD DescriptorMutation_Direction = 1
	// Descriptor is being dropped.
	DescriptorMutation_DROP DescriptorMutation_Direction = 2
)

func (DescriptorMutation_Direction) Enum

func (DescriptorMutation_Direction) EnumDescriptor

func (DescriptorMutation_Direction) EnumDescriptor() ([]byte, []int)

func (DescriptorMutation_Direction) String

func (*DescriptorMutation_Direction) UnmarshalJSON

func (x *DescriptorMutation_Direction) UnmarshalJSON(data []byte) error

type DescriptorMutation_Index

type DescriptorMutation_Index struct {
	Index *IndexDescriptor `protobuf:"bytes,2,opt,name=index,oneof"`
}

func (*DescriptorMutation_Index) MarshalTo

func (m *DescriptorMutation_Index) MarshalTo(data []byte) (int, error)

func (*DescriptorMutation_Index) Size

func (m *DescriptorMutation_Index) Size() (n int)

type DescriptorMutation_State

type DescriptorMutation_State int32

A descriptor within a mutation is unavailable for reads, writes and deletes. It is only available for implicit (internal to the database) writes and deletes depending on the state of the mutation.

const (
	// Not used.
	DescriptorMutation_UNKNOWN DescriptorMutation_State = 0
	// Operations can use this invisible descriptor to implicitly
	// delete entries.
	// Column: A descriptor in this state is invisible to
	// INSERT and UPDATE. DELETE must delete a column in this state.
	// Index: A descriptor in this state is invisible to an INSERT.
	// UPDATE must delete the old value of the index but doesn't write
	// the new value. DELETE must delete the index.
	//
	// When deleting a descriptor, all descriptor related data
	// (column or index data) can only be mass deleted once
	// all the nodes have transitioned to the DELETE_ONLY state.
	DescriptorMutation_DELETE_ONLY DescriptorMutation_State = 1
	// Operations can use this invisible descriptor to implicitly
	// write and delete entries.
	// Column: INSERT will populate this column with the default
	// value. UPDATE ignores this descriptor. DELETE must delete
	// the column.
	// Index: INSERT, UPDATE and DELETE treat this index like any
	// other index.
	//
	// When adding a descriptor, all descriptor related data
	// (column default or index data) can only be backfilled once
	// all nodes have transitioned into the WRITE_ONLY state.
	DescriptorMutation_WRITE_ONLY DescriptorMutation_State = 2
)

func (DescriptorMutation_State) Enum

func (DescriptorMutation_State) EnumDescriptor

func (DescriptorMutation_State) EnumDescriptor() ([]byte, []int)

func (DescriptorMutation_State) String

func (x DescriptorMutation_State) String() string

func (*DescriptorMutation_State) UnmarshalJSON

func (x *DescriptorMutation_State) UnmarshalJSON(data []byte) error

type DescriptorStatus

type DescriptorStatus int

DescriptorStatus is the status for a descriptor.

const (

	// DescriptorAbsent for a descriptor that doesn't exist.
	DescriptorAbsent DescriptorStatus
	// DescriptorIncomplete for a descriptor that is a part of a
	// schema change, and is still being processed.
	DescriptorIncomplete
	// DescriptorActive for a descriptor that is completely active
	// for read/write and delete operations.
	DescriptorActive
)

type DescriptorVersion

type DescriptorVersion uint32

DescriptorVersion is a custom type for TableDescriptor Versions.

type Descriptor_Database

type Descriptor_Database struct {
	Database *DatabaseDescriptor `protobuf:"bytes,2,opt,name=database,oneof"`
}

func (*Descriptor_Database) MarshalTo

func (m *Descriptor_Database) MarshalTo(data []byte) (int, error)

func (*Descriptor_Database) Size

func (m *Descriptor_Database) Size() (n int)

type Descriptor_Table

type Descriptor_Table struct {
	Table *TableDescriptor `protobuf:"bytes,1,opt,name=table,oneof"`
}

func (*Descriptor_Table) MarshalTo

func (m *Descriptor_Table) MarshalTo(data []byte) (int, error)

func (*Descriptor_Table) Size

func (m *Descriptor_Table) Size() (n int)

type EventLogType

type EventLogType string

EventLogType represents an event type that can be recorded in the event log.

const (
	// EventLogCreateDatabase is recorded when a database is created.
	EventLogCreateDatabase EventLogType = "create_database"
	// EventLogDropDatabase is recorded when a database is dropped.
	EventLogDropDatabase EventLogType = "drop_database"
	// EventLogCreateTable is recorded when a table is created.
	EventLogCreateTable EventLogType = "create_table"
	// EventLogDropTable is recorded when a table is dropped.
	EventLogDropTable EventLogType = "drop_table"
	// EventLogNodeJoin is recorded when a node joins the cluster.
	EventLogNodeJoin EventLogType = "node_join"
	// EventLogNodeRestart is recorded when an existing node rejoins the cluster
	// after being offline.
	EventLogNodeRestart EventLogType = "node_restart"
)

type EventLogger

type EventLogger struct {
	InternalExecutor
}

An EventLogger exposes methods used to record events to the event table.

func MakeEventLogger

func MakeEventLogger(leaseMgr *LeaseManager) EventLogger

MakeEventLogger constructs a new EventLogger. A LeaseManager is required in order to correctly execute SQL statements.

func (EventLogger) InsertEventRecord

func (ev EventLogger) InsertEventRecord(txn *client.Txn, eventType EventLogType, targetID, reportingID int32, info interface{}) *roachpb.Error

InsertEventRecord inserts a single event into the event log as part of the provided transaction.

type Executor

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

An Executor executes SQL statements. Executor is thread-safe.

func NewExecutor

func NewExecutor(ctx ExecutorContext, stopper *stop.Stopper, registry *metric.Registry) *Executor

NewExecutor creates an Executor and registers a callback on the system config.

func (*Executor) ExecuteStatements

func (e *Executor) ExecuteStatements(
	ctx context.Context, session *Session, stmts string,
	params []parser.Datum) StatementResults

ExecuteStatements executes the given statement(s) and returns a response. On error, the returned integer is an HTTP error code.

func (*Executor) Prepare

func (e *Executor) Prepare(ctx context.Context, query string, session *Session, args parser.MapArgs) (
	[]ResultColumn, *roachpb.Error)

Prepare returns the result types of the given statement. Args may be a partially populated val args map. Prepare will populate the missing val args. The column result types are returned (or nil if there are no results).

func (*Executor) Registry

func (e *Executor) Registry() *metric.Registry

Registry returns a registry with the metrics tracked by this executor, which can be used to access its stats or be added to another registry.

func (*Executor) SetNodeID

func (e *Executor) SetNodeID(nodeID roachpb.NodeID)

SetNodeID sets the node ID for the SQL server. This method must be called before actually using the Executor.

type ExecutorContext

type ExecutorContext struct {
	DB           *client.DB
	Gossip       *gossip.Gossip
	LeaseManager *LeaseManager
	Clock        *hlc.Clock

	TestingKnobs *ExecutorTestingKnobs
}

An ExecutorContext encompasses the auxiliary objects and configuration required to create an executor. All fields holding a pointer or an interface are required to create a Executor; the rest will have sane defaults set if omitted.

type ExecutorTestingKnobs

type ExecutorTestingKnobs struct {
	// WaitForGossipUpdate causes metadata-mutating operations to wait
	// for the new metadata to back-propagate through gossip.
	WaitForGossipUpdate bool

	// CheckStmtStringChange causes Executor.execStmtsInCurrentTxn to verify
	// that executed statements are not modified during execution.
	CheckStmtStringChange bool

	// FixTxnPriority causes transaction priority values to be hardcoded (for
	// each priority level) to avoid the randomness in the normal generation.
	FixTxnPriority bool
}

ExecutorTestingKnobs is part of the context used to control parts of the system during testing.

type FormatVersion

type FormatVersion uint32

FormatVersion is a custom type for TableDescriptor versions of the sql to key:value mapping.

const (

	// BaseFormatVersion corresponds to the encoding described in
	// https://www.cockroachlabs.com/blog/sql-in-cockroachdb-mapping-table-data-to-key-value-storage/.
	BaseFormatVersion FormatVersion
)

func (FormatVersion) String

func (i FormatVersion) String() string

type ID

type ID uint32

ID is a custom type for {Database,Table}Descriptor IDs.

type IndexDescriptor

type IndexDescriptor struct {
	Name   string  `protobuf:"bytes,1,opt,name=name" json:"name"`
	ID     IndexID `protobuf:"varint,2,opt,name=id,casttype=IndexID" json:"id"`
	Unique bool    `protobuf:"varint,3,opt,name=unique" json:"unique"`
	// An ordered list of column names of which the index is comprised. This list
	// parallels the column_ids list. If duplicating the storage of the column
	// names here proves to be prohibitive, we could clear this field before
	// saving and reconstruct it after loading.
	ColumnNames []string `protobuf:"bytes,4,rep,name=column_names,json=columnNames" json:"column_names,omitempty"`
	// Parallel with column_names - the sort direction of each column.
	ColumnDirections []IndexDescriptor_Direction `` /* 148-byte string literal not displayed */
	// An ordered list of column names which the index stores in
	// addition to the columns which are explicitly part of the index.
	StoreColumnNames []string `protobuf:"bytes,5,rep,name=store_column_names,json=storeColumnNames" json:"store_column_names,omitempty"`
	// An ordered list of column ids of which the index is comprised. This list
	// parallels the column_names list.
	ColumnIDs []ColumnID `protobuf:"varint,6,rep,name=column_ids,json=columnIds,casttype=ColumnID" json:"column_ids,omitempty"`
	// An ordered list of implicit column ids associated with the index. For
	// non-unique indexes, these columns will be appended to the key. For unique
	// indexes these columns will be stored in the value. The extra column IDs is
	// computed as PrimaryIndex.column_ids - column_ids. For the primary index
	// the list will be empty.
	// The distinction about whether the columns are written in the key or the value
	// comes because we want to always do writes using a single operation - this
	// way for unique indexes we can do a conditional put on the key.
	ImplicitColumnIDs []ColumnID `` /* 126-byte string literal not displayed */
}

func (*IndexDescriptor) Descriptor

func (*IndexDescriptor) Descriptor() ([]byte, []int)

func (*IndexDescriptor) Marshal

func (m *IndexDescriptor) Marshal() (data []byte, err error)

func (*IndexDescriptor) MarshalTo

func (m *IndexDescriptor) MarshalTo(data []byte) (int, error)

func (*IndexDescriptor) ProtoMessage

func (*IndexDescriptor) ProtoMessage()

func (*IndexDescriptor) Reset

func (m *IndexDescriptor) Reset()

func (*IndexDescriptor) Size

func (m *IndexDescriptor) Size() (n int)

func (*IndexDescriptor) String

func (m *IndexDescriptor) String() string

func (*IndexDescriptor) Unmarshal

func (m *IndexDescriptor) Unmarshal(data []byte) error

type IndexDescriptor_Direction

type IndexDescriptor_Direction int32

The direction of a column in the index.

const (
	IndexDescriptor_ASC  IndexDescriptor_Direction = 0
	IndexDescriptor_DESC IndexDescriptor_Direction = 1
)

func (IndexDescriptor_Direction) Enum

func (IndexDescriptor_Direction) EnumDescriptor

func (IndexDescriptor_Direction) EnumDescriptor() ([]byte, []int)

func (IndexDescriptor_Direction) String

func (x IndexDescriptor_Direction) String() string

func (*IndexDescriptor_Direction) UnmarshalJSON

func (x *IndexDescriptor_Direction) UnmarshalJSON(data []byte) error

type IndexID

type IndexID uint32

IndexID is a custom type for IndexDescriptor IDs.

type InternalExecutor

type InternalExecutor struct {
	LeaseManager *LeaseManager
}

InternalExecutor can be used internally by cockroach to execute SQL statements without needing to open a SQL connection. InternalExecutor assumes that the caller has access to a cockroach KV client to handle connection and transaction management.

func (InternalExecutor) ExecuteStatementInTransaction

func (ie InternalExecutor) ExecuteStatementInTransaction(
	txn *client.Txn, statement string, params ...interface{},
) (int, *roachpb.Error)

ExecuteStatementInTransaction executes the supplied SQL statement as part of the supplied transaction. Statements are currently executed as the root user.

func (InternalExecutor) GetTableSpan

func (ie InternalExecutor) GetTableSpan(user string, txn *client.Txn, dbName, tableName string) (roachpb.Span, *roachpb.Error)

GetTableSpan gets the key span for a SQL table, including any indices.

type LeaseManager

type LeaseManager struct {
	LeaseStore
	// contains filtered or unexported fields
}

LeaseManager manages acquiring and releasing per-table leases. Exported only for testing.

func NewLeaseManager

func NewLeaseManager(nodeID uint32, db client.DB, clock *hlc.Clock) *LeaseManager

NewLeaseManager creates a new LeaseManager.

func (*LeaseManager) Acquire

func (m *LeaseManager) Acquire(txn *client.Txn, tableID ID, version DescriptorVersion) (*LeaseState, *roachpb.Error)

Acquire acquires a read lease for the specified table ID. If version is non-zero the lease is grabbed for the specified version. Otherwise it is grabbed for the most recent version of the descriptor that the lease manager knows about.

func (*LeaseManager) RefreshLeases

func (m *LeaseManager) RefreshLeases(s *stop.Stopper, db *client.DB, gossip *gossip.Gossip)

RefreshLeases starts a goroutine that refreshes the lease manager leases for tables received in the latest system configuration via gossip.

func (*LeaseManager) Release

func (m *LeaseManager) Release(lease *LeaseState) error

Release releases a previously acquired read lease.

type LeaseState

type LeaseState struct {
	TableDescriptor
	// contains filtered or unexported fields
}

LeaseState holds the state for a lease. Exported only for testing.

func (*LeaseState) Expiration

func (s *LeaseState) Expiration() time.Time

Expiration returns the expiration time of the lease.

func (*LeaseState) Refcount

func (s *LeaseState) Refcount() int

Refcount returns the reference count of the lease.

func (*LeaseState) String

func (s *LeaseState) String() string

type LeaseStore

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

LeaseStore implements the operations for acquiring and releasing leases and publishing a new version of a descriptor. Exported only for testing.

func (LeaseStore) Acquire

func (s LeaseStore) Acquire(txn *client.Txn, tableID ID, minVersion DescriptorVersion) (*LeaseState, *roachpb.Error)

Acquire a lease on the most recent version of a table descriptor.

func (LeaseStore) Publish

func (s LeaseStore) Publish(tableID ID, update func(*TableDescriptor) error) *roachpb.Error

Publish a new version of a table descriptor. The update closure may be called multiple times if retries occur: make sure it does not have side effects.

func (LeaseStore) Release

func (s LeaseStore) Release(lease *LeaseState) error

Release a previously acquired table descriptor lease.

type MetadataSchema

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

MetadataSchema is used to construct the initial sql schema for a new CockroachDB cluster being bootstrapped. Tables and databases must be installed on the underlying persistent storage before a cockroach store can start running correctly, thus requiring this special initialization.

func MakeMetadataSchema

func MakeMetadataSchema() MetadataSchema

MakeMetadataSchema constructs a new MetadataSchema value which constructs the "system" database.

func (*MetadataSchema) AddDescriptor

func (ms *MetadataSchema) AddDescriptor(parentID ID, desc descriptorProto)

AddDescriptor adds a new descriptor to the system schema. Used only for SystemConfig tables and databases. Prefer AddTable for most uses.

func (*MetadataSchema) AddTable

func (ms *MetadataSchema) AddTable(id ID, definition string, privileges privilege.List)

AddTable adds a new table to the system database.

func (MetadataSchema) DescriptorCount

func (ms MetadataSchema) DescriptorCount() int

DescriptorCount returns the number of descriptors that will be created by this schema. This value is needed to automate certain tests.

func (MetadataSchema) GetInitialValues

func (ms MetadataSchema) GetInitialValues() []roachpb.KeyValue

GetInitialValues returns the set of initial K/V values which should be added to a bootstrapping CockroachDB cluster in order to create the tables contained in the schema.

func (MetadataSchema) MaxTableID

func (ms MetadataSchema) MaxTableID() ID

MaxTableID returns the highest table ID of any system table. This value is needed to automate certain tests.

func (MetadataSchema) TableCount

func (ms MetadataSchema) TableCount() int

TableCount returns the number of non-system config tables in the system database. This value is needed to automate certain tests.

type MutationID

type MutationID uint32

MutationID is custom type for TableDescriptor mutations.

type PrivilegeDescriptor

type PrivilegeDescriptor struct {
	Users []*UserPrivileges `protobuf:"bytes,1,rep,name=users" json:"users,omitempty"`
}

PrivilegeDescriptor describes a list of users and attached privileges. The list should be sorted by user for fast access.

func NewDefaultPrivilegeDescriptor

func NewDefaultPrivilegeDescriptor() *PrivilegeDescriptor

NewDefaultPrivilegeDescriptor returns a privilege descriptor with ALL privileges for the root user.

func NewPrivilegeDescriptor

func NewPrivilegeDescriptor(user string, priv privilege.List) *PrivilegeDescriptor

NewPrivilegeDescriptor returns a privilege descriptor for the given user with the specified list of privileges.

func (*PrivilegeDescriptor) CheckPrivilege

func (p *PrivilegeDescriptor) CheckPrivilege(user string, priv privilege.Kind) bool

CheckPrivilege returns true if 'user' has 'privilege' on this descriptor.

func (*PrivilegeDescriptor) Descriptor

func (*PrivilegeDescriptor) Descriptor() ([]byte, []int)

func (*PrivilegeDescriptor) Grant

func (p *PrivilegeDescriptor) Grant(user string, privList privilege.List)

Grant adds new privileges to this descriptor for a given list of users. TODO(marc): if all privileges other than ALL are set, should we collapse them into ALL?

func (*PrivilegeDescriptor) Marshal

func (m *PrivilegeDescriptor) Marshal() (data []byte, err error)

func (*PrivilegeDescriptor) MarshalTo

func (m *PrivilegeDescriptor) MarshalTo(data []byte) (int, error)

func (*PrivilegeDescriptor) ProtoMessage

func (*PrivilegeDescriptor) ProtoMessage()

func (*PrivilegeDescriptor) Reset

func (m *PrivilegeDescriptor) Reset()

func (*PrivilegeDescriptor) Revoke

func (p *PrivilegeDescriptor) Revoke(user string, privList privilege.List)

Revoke removes privileges from this descriptor for a given list of users.

func (*PrivilegeDescriptor) Show

Show returns the list of {username, privileges} sorted by username. 'privileges' is a string of comma-separated sorted privilege names.

func (*PrivilegeDescriptor) Size

func (m *PrivilegeDescriptor) Size() (n int)

func (*PrivilegeDescriptor) String

func (m *PrivilegeDescriptor) String() string

func (*PrivilegeDescriptor) Unmarshal

func (m *PrivilegeDescriptor) Unmarshal(data []byte) error

func (*PrivilegeDescriptor) Validate

func (p *PrivilegeDescriptor) Validate(id ID) error

Validate is called when writing a database or table descriptor. It takes the descriptor ID which is used to determine if it belongs to a system descriptor, in which case the maximum set of allowed privileges is looked up and applied.

type Result

type Result struct {
	PErr *roachpb.Error
	// The type of statement that the result is for.
	Type parser.StatementType
	// The tag of the statement that the result is for.
	PGTag string
	// RowsAffected will be populated if the statement type is "RowsAffected".
	RowsAffected int
	// Columns will be populated if the statement type is "Rows". It will contain
	// the names and types of the columns returned in the result set in the order
	// specified in the SQL statement. The number of columns will equal the number
	// of values in each Row.
	Columns []ResultColumn
	// Rows will be populated if the statement type is "Rows". It will contain
	// the result set of the result.
	// TODO(nvanbenschoten): Can this be streamed from the planNode?
	Rows []ResultRow
}

Result corresponds to the execution of a single SQL statement.

type ResultColumn

type ResultColumn struct {
	Name string
	Typ  parser.Datum
	// contains filtered or unexported fields
}

ResultColumn contains the name and type of a SQL "cell".

type ResultList

type ResultList []Result

ResultList represents a list of results for a list of SQL statements. There is one result object per SQL statement in the request.

type ResultRow

type ResultRow struct {
	Values []parser.Datum
}

ResultRow is a collection of values representing a row in a result.

type SchemaChangeManager

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

SchemaChangeManager processes pending schema changes seen in gossip updates. Most schema changes are executed synchronously by the node that created the schema change. If the node dies while processing the schema change this manager acts as a backup execution mechanism.

func NewSchemaChangeManager

func NewSchemaChangeManager(db client.DB, gossip *gossip.Gossip, leaseMgr *LeaseManager) *SchemaChangeManager

NewSchemaChangeManager returns a new SchemaChangeManager.

func (*SchemaChangeManager) Start

func (s *SchemaChangeManager) Start(stopper *stop.Stopper)

Start starts a goroutine that runs outstanding schema changes for tables received in the latest system configuration via gossip.

type SchemaChanger

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

SchemaChanger is used to change the schema on a table.

func NewSchemaChangerForTesting

func NewSchemaChangerForTesting(
	tableID ID, mutationID MutationID, nodeID roachpb.NodeID, db client.DB, leaseMgr *LeaseManager,
) SchemaChanger

NewSchemaChangerForTesting only for tests.

func (*SchemaChanger) AcquireLease

AcquireLease acquires a schema change lease on the table if an unexpired lease doesn't exist. It returns the lease.

func (*SchemaChanger) ExtendLease

ExtendLease for the current leaser.

func (*SchemaChanger) IsDone

func (sc *SchemaChanger) IsDone() (bool, error)

IsDone returns true if the work scheduled for the schema changer is complete.

func (*SchemaChanger) MaybeIncrementVersion

func (sc *SchemaChanger) MaybeIncrementVersion() *roachpb.Error

MaybeIncrementVersion increments the version if needed.

func (*SchemaChanger) ReleaseLease

func (sc *SchemaChanger) ReleaseLease(lease TableDescriptor_SchemaChangeLease) error

ReleaseLease the table lease if it is the one registered with the table descriptor.

func (*SchemaChanger) RunStateMachineBeforeBackfill

func (sc *SchemaChanger) RunStateMachineBeforeBackfill() error

RunStateMachineBeforeBackfill moves the state machine forward and wait to ensure that all nodes are seeing the latest version of the table.

type Session

type Session struct {
	Database string
	User     string
	Syntax   int32

	// Info about the open transaction (if any).
	TxnState txnState

	Timezone              isSessionTimezone
	DefaultIsolationLevel roachpb.IsolationType
	Trace                 trace.Trace
	// contains filtered or unexported fields
}

Session contains the state of a SQL client connection. Create instances using NewSession().

func NewSession

func NewSession(args SessionArgs, e *Executor, remote net.Addr) *Session

NewSession creates and initializes new Session object. remote can be nil.

func (*Session) Finish

func (s *Session) Finish()

Finish releases resources held by the Session.

type SessionArgs

type SessionArgs struct {
	Database string
	User     string
}

SessionArgs contains arguments for creating a new Session with NewSession().

type SessionLocation

type SessionLocation struct {
	Location string
}

SessionLocation ...

type SessionOffset

type SessionOffset struct {
	Offset int64
}

SessionOffset ...

type StatementResults

type StatementResults struct {
	ResultList
	// Indicates that after parsing, the request contained 0 non-empty statements.
	Empty bool
}

StatementResults represents a list of results from running a batch of SQL statements, plus some meta info about the batch.

type TableDescriptor

type TableDescriptor struct {
	Name string `protobuf:"bytes,1,opt,name=name" json:"name"`
	ID   ID     `protobuf:"varint,3,opt,name=id,casttype=ID" json:"id"`
	// ID of the parent database.
	ParentID ID `protobuf:"varint,4,opt,name=parent_id,json=parentId,casttype=ID" json:"parent_id"`
	// Monotonically increasing version of the table descriptor.
	//
	// Invariants:
	// 1. not more than two subsequent versions of the table
	// descriptor can be leased. This is to make the system
	// easy to reason about, by permiting mutation state
	// changes (reflected in the next version), only when the existing
	// state (reflected in the current version) is present on all
	// outstanding unexpired leases.
	// 2. A schema change command (ALTER, RENAME, etc) never directly
	// increments the version. This allows the command to execute without
	// waiting for the entire cluster to converge to a single version
	// preventing weird deadlock situations. For instance, a transaction
	// with a schema change command might use a descriptor lease that is
	// at version: v - 1, and therefore deadlock when it tries to wait
	// for version: v, in the process of incrementing it to v + 1.
	// Therefore, a schema change command never increments the version,
	// and instead, sets the up_version boolean to notify the schema
	// changer execution engine that runs a future transaction to
	// increment the version.
	//
	// The schema change commands must therefore make *safe* modifications
	// to the table descriptor, such as scheduling long running schema
	// changes through mutations for future execution, or making simple
	// schema changes like RENAME that only modify the table descriptor in a
	// single transaction.
	//
	// Multiple schema changes in the same transaction set up_version.
	// The actual schema change execution that follows a schema change
	// command sees the up_version boolean set, and increments the
	// table version after ensuring that there are no unexpired leases
	// for version - 1. The schema change execution must increment
	// the version before executing future state changes, to ensure
	// that the scheduled mutations made by the original commands are
	// visible on all leases. Multiple schema change mutations can be
	// grouped together on a particular version increment.
	//
	// If schema change commands are safe to run without incrementing
	// the version, why do it later on? We increment the version
	// to ensure that all the nodes renew their leases with the new version
	// and get to see what the schema change command has done quickly.
	Version DescriptorVersion `protobuf:"varint,5,opt,name=version,casttype=DescriptorVersion" json:"version"`
	// See comment above.
	UpVersion bool `protobuf:"varint,6,opt,name=up_version,json=upVersion" json:"up_version"`
	// Last modification time of the table descriptor.
	ModificationTime cockroach_roachpb1.Timestamp `protobuf:"bytes,7,opt,name=modification_time,json=modificationTime" json:"modification_time"`
	Columns          []ColumnDescriptor           `protobuf:"bytes,8,rep,name=columns" json:"columns"`
	// next_column_id is used to ensure that deleted column ids are not reused.
	NextColumnID ColumnID        `protobuf:"varint,9,opt,name=next_column_id,json=nextColumnId,casttype=ColumnID" json:"next_column_id"`
	PrimaryIndex IndexDescriptor `protobuf:"bytes,10,opt,name=primary_index,json=primaryIndex" json:"primary_index"`
	// indexes are all the secondary indexes.
	Indexes []IndexDescriptor `protobuf:"bytes,11,rep,name=indexes" json:"indexes"`
	// next_index_id is used to ensure that deleted index ids are not reused.
	NextIndexID IndexID              `protobuf:"varint,12,opt,name=next_index_id,json=nextIndexId,casttype=IndexID" json:"next_index_id"`
	Privileges  *PrivilegeDescriptor `protobuf:"bytes,13,opt,name=privileges" json:"privileges,omitempty"`
	// Columns or indexes being added or deleted in a FIFO order.
	Mutations []DescriptorMutation               `protobuf:"bytes,14,rep,name=mutations" json:"mutations"`
	Lease     *TableDescriptor_SchemaChangeLease `protobuf:"bytes,15,opt,name=lease" json:"lease,omitempty"`
	// An id for the next group of mutations to be applied together.
	NextMutationID MutationID `protobuf:"varint,16,opt,name=next_mutation_id,json=nextMutationId,casttype=MutationID" json:"next_mutation_id"`
	// format_version declares which sql to key:value mapping is being used to
	// represent the data in this table.
	FormatVersion FormatVersion `protobuf:"varint,17,opt,name=format_version,json=formatVersion,casttype=FormatVersion" json:"format_version"`
}

A TableDescriptor represents a table and is stored in a structured metadata key. The TableDescriptor has a globally-unique ID, while its member {Column,Index}Descriptors have locally-unique IDs.

func (*TableDescriptor) AddColumn

func (desc *TableDescriptor) AddColumn(col ColumnDescriptor)

AddColumn adds a column to the table.

func (*TableDescriptor) AddIndex

func (desc *TableDescriptor) AddIndex(idx IndexDescriptor, primary bool) error

AddIndex adds an index to the table.

func (*TableDescriptor) AllocateIDs

func (desc *TableDescriptor) AllocateIDs() error

AllocateIDs allocates column and index ids for any column or index which has an ID of 0.

func (*TableDescriptor) Descriptor

func (*TableDescriptor) Descriptor() ([]byte, []int)

func (*TableDescriptor) FindActiveColumnByName

func (desc *TableDescriptor) FindActiveColumnByName(name string) (ColumnDescriptor, error)

FindActiveColumnByName finds an active column with the specified name.

func (*TableDescriptor) FindColumnByID

func (desc *TableDescriptor) FindColumnByID(id ColumnID) (*ColumnDescriptor, error)

FindColumnByID finds the active column with specified ID.

func (*TableDescriptor) FindColumnByName

func (desc *TableDescriptor) FindColumnByName(name string) (DescriptorStatus, int, error)

FindColumnByName finds the column with the specified name. It returns DescriptorStatus for the column, and an index into either the columns (status == DescriptorActive) or mutations (status == DescriptorIncomplete).

func (*TableDescriptor) FindIndexByID

func (desc *TableDescriptor) FindIndexByID(id IndexID) (*IndexDescriptor, error)

FindIndexByID finds the active index with specified ID.

func (*TableDescriptor) FindIndexByName

func (desc *TableDescriptor) FindIndexByName(name string) (DescriptorStatus, int, error)

FindIndexByName finds the index with the specified name. It returns DescriptorStatus for the index, and an index into either the indexes (status == DescriptorActive) or mutations (status == DescriptorIncomplete).

func (*TableDescriptor) GetColumns

func (m *TableDescriptor) GetColumns() []ColumnDescriptor

func (*TableDescriptor) GetFormatVersion

func (m *TableDescriptor) GetFormatVersion() FormatVersion

func (*TableDescriptor) GetID

func (m *TableDescriptor) GetID() ID

func (*TableDescriptor) GetIndexes

func (m *TableDescriptor) GetIndexes() []IndexDescriptor

func (*TableDescriptor) GetLease

func (*TableDescriptor) GetModificationTime

func (m *TableDescriptor) GetModificationTime() cockroach_roachpb1.Timestamp

func (*TableDescriptor) GetMutations

func (m *TableDescriptor) GetMutations() []DescriptorMutation

func (*TableDescriptor) GetName

func (m *TableDescriptor) GetName() string

func (*TableDescriptor) GetNextColumnID

func (m *TableDescriptor) GetNextColumnID() ColumnID

func (*TableDescriptor) GetNextIndexID

func (m *TableDescriptor) GetNextIndexID() IndexID

func (*TableDescriptor) GetNextMutationID

func (m *TableDescriptor) GetNextMutationID() MutationID

func (*TableDescriptor) GetParentID

func (m *TableDescriptor) GetParentID() ID

func (*TableDescriptor) GetPrimaryIndex

func (m *TableDescriptor) GetPrimaryIndex() IndexDescriptor

func (*TableDescriptor) GetPrivileges

func (m *TableDescriptor) GetPrivileges() *PrivilegeDescriptor

func (*TableDescriptor) GetUpVersion

func (m *TableDescriptor) GetUpVersion() bool

func (*TableDescriptor) GetVersion

func (m *TableDescriptor) GetVersion() DescriptorVersion

func (*TableDescriptor) Marshal

func (m *TableDescriptor) Marshal() (data []byte, err error)

func (*TableDescriptor) MarshalTo

func (m *TableDescriptor) MarshalTo(data []byte) (int, error)

func (*TableDescriptor) ProtoMessage

func (*TableDescriptor) ProtoMessage()

func (*TableDescriptor) Reset

func (m *TableDescriptor) Reset()

func (*TableDescriptor) SetID

func (desc *TableDescriptor) SetID(id ID)

SetID implements the descriptorProto interface.

func (*TableDescriptor) SetName

func (desc *TableDescriptor) SetName(name string)

SetName implements the descriptorProto interface.

func (*TableDescriptor) Size

func (m *TableDescriptor) Size() (n int)

func (*TableDescriptor) String

func (m *TableDescriptor) String() string

func (*TableDescriptor) TypeName

func (desc *TableDescriptor) TypeName() string

TypeName returns the plain type of this descriptor.

func (*TableDescriptor) Unmarshal

func (m *TableDescriptor) Unmarshal(data []byte) error

func (*TableDescriptor) Validate

func (desc *TableDescriptor) Validate() error

Validate validates that the table descriptor is well formed. Checks include validating the table, column and index names, verifying that column names and index names are unique and verifying that column IDs and index IDs are consistent.

func (*TableDescriptor) VisibleColumns

func (desc *TableDescriptor) VisibleColumns() []ColumnDescriptor

VisibleColumns returns all non hidden columns.

type TableDescriptor_SchemaChangeLease

type TableDescriptor_SchemaChangeLease struct {
	NodeID github_com_cockroachdb_cockroach_roachpb.NodeID `protobuf:"varint,1,opt,name=node_id,json=nodeId,casttype=github.com/cockroachdb/cockroach/roachpb.NodeID" json:"node_id"`
	// Nanoseconds since the Unix epoch.
	ExpirationTime int64 `protobuf:"varint,2,opt,name=expiration_time,json=expirationTime" json:"expiration_time"`
}

The schema update lease. A single goroutine across a cockroach cluster can own it, and will execute pending schema changes for this table. Since the execution of a pending schema change is through transactions, it is legal for more than one goroutine to attempt to execute it. This lease reduces write contention on the schema change.

func (*TableDescriptor_SchemaChangeLease) Descriptor

func (*TableDescriptor_SchemaChangeLease) Descriptor() ([]byte, []int)

func (*TableDescriptor_SchemaChangeLease) Marshal

func (m *TableDescriptor_SchemaChangeLease) Marshal() (data []byte, err error)

func (*TableDescriptor_SchemaChangeLease) MarshalTo

func (m *TableDescriptor_SchemaChangeLease) MarshalTo(data []byte) (int, error)

func (*TableDescriptor_SchemaChangeLease) ProtoMessage

func (*TableDescriptor_SchemaChangeLease) ProtoMessage()

func (*TableDescriptor_SchemaChangeLease) Reset

func (*TableDescriptor_SchemaChangeLease) Size

func (m *TableDescriptor_SchemaChangeLease) Size() (n int)

func (*TableDescriptor_SchemaChangeLease) String

func (*TableDescriptor_SchemaChangeLease) Unmarshal

func (m *TableDescriptor_SchemaChangeLease) Unmarshal(data []byte) error

type TxnStateEnum

type TxnStateEnum int

TxnStateEnum represents the state of a SQL txn.

const (
	// No txn is in scope. Either there never was one, or it got committed/rolled back.
	NoTxn TxnStateEnum = iota
	// A txn is in scope.
	Open
	// The txn has encoutered a (non-retriable) error.
	// Statements will be rejected until a COMMIT/ROLLBACK is seen.
	Aborted
	// The txn has encoutered a retriable error.
	// Statements will be rejected until a RESTART_TRANSACTION is seen.
	RestartWait
	// The KV txn has been committed successfully through a RELEASE.
	// Statements are rejected until a COMMIT is seen.
	CommitWait
)

func (TxnStateEnum) String

func (i TxnStateEnum) String() string

type UserPrivilegeString

type UserPrivilegeString struct {
	User       string
	Privileges string
}

UserPrivilegeString is a pair of strings describing the privileges for a given user.

type UserPrivileges

type UserPrivileges struct {
	User string `protobuf:"bytes,1,opt,name=user" json:"user"`
	// privileges is a bitfield of 1<<Privilege values.
	Privileges uint32 `protobuf:"varint,2,opt,name=privileges" json:"privileges"`
}

UserPrivileges describes the list of privileges available for a given user.

func (*UserPrivileges) Descriptor

func (*UserPrivileges) Descriptor() ([]byte, []int)

func (*UserPrivileges) Marshal

func (m *UserPrivileges) Marshal() (data []byte, err error)

func (*UserPrivileges) MarshalTo

func (m *UserPrivileges) MarshalTo(data []byte) (int, error)

func (*UserPrivileges) ProtoMessage

func (*UserPrivileges) ProtoMessage()

func (*UserPrivileges) Reset

func (m *UserPrivileges) Reset()

func (*UserPrivileges) Size

func (m *UserPrivileges) Size() (n int)

func (*UserPrivileges) String

func (m *UserPrivileges) String() string

func (*UserPrivileges) Unmarshal

func (m *UserPrivileges) Unmarshal(data []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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