schema

package
v3.10.6 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MPL-2.0 Imports: 23 Imported by: 37

Documentation

Overview

Package schema defines types supported by tables in source plugins

Index

Constants

View Source
const (
	MetadataUnique         = "cq:extension:unique"
	MetadataPrimaryKey     = "cq:extension:primary_key"
	MetadataConstraintName = "cq:extension:constraint_name"
	MetadataIncremental    = "cq:extension:incremental"

	MetadataTrue             = "true"
	MetadataFalse            = "false"
	MetadataTableName        = "cq:table_name"
	MetadataTableDescription = "cq:table_description"
)

Variables

View Source
var CqIDColumn = Column{
	Name:        "_cq_id",
	Type:        types.ExtensionTypes.UUID,
	Description: "Internal CQ ID of the row",
	NotNull:     true,
	Unique:      true,
}

These columns are managed and populated by the source plugins

View Source
var CqParentIDColumn = Column{
	Name:          "_cq_parent_id",
	Type:          types.ExtensionTypes.UUID,
	Description:   "Internal CQ ID of the parent row",
	Resolver:      parentCqUUIDResolver(),
	IgnoreInTests: true,
}
View Source
var CqSourceNameColumn = Column{
	Name:        "_cq_source_name",
	Type:        arrow.BinaryTypes.String,
	Description: "Internal CQ row that references the source plugin name data was retrieved",
}
View Source
var CqSyncTimeColumn = Column{
	Name:        "_cq_sync_time",
	Type:        arrow.FixedWidthTypes.Timestamp_us,
	Description: "Internal CQ row of when sync was started (this will be the same for all rows in a single fetch)",
}

These columns are managed and populated by the destination plugin.

Functions

func GenTestData

func GenTestData(table *Table, opts GenTestDataOptions) []arrow.Record

GenTestData generates a slice of arrow.Records with the given schema and options.

func ValidColumnName

func ValidColumnName(name string) bool

func ValidateTable

func ValidateTable(t *Table) error

Types

type ClientMeta

type ClientMeta interface {
	ID() string
}

type Column

type Column struct {
	// Name of column
	Name string
	// Value Type of column i.e String, UUID etc'
	Type arrow.DataType
	// Description about column, this description is added as a comment in the database
	Description string
	// Column Resolver allows to set your own data for a column; this can be an API call, setting multiple embedded values, etc
	Resolver ColumnResolver

	// IgnoreInTests is used to skip verifying the column is non-nil in integration tests.
	// By default, integration tests perform a fetch for all resources in cloudquery's test account, and
	// verify all columns are non-nil.
	// If IgnoreInTests is true, verification is skipped for this column.
	// Used when it is hard to create a reproducible environment with this column being non-nil (e.g. various error columns).
	IgnoreInTests bool

	// PrimaryKey requires the destinations supporting this to include this column into the primary key
	PrimaryKey bool
	// NotNull requires the destinations supporting this to mark this column as non-nullable
	NotNull bool
	// IncrementalKey is a flag that indicates if the column is used as part of an incremental key.
	// It is mainly used for documentation purposes, but may also be used as part of ensuring that
	// migrations are done correctly.
	IncrementalKey bool
	// Unique requires the destinations supporting this to mark this column as unique
	Unique bool
}

Column definition for Table

func NewColumnFromArrowField

func NewColumnFromArrowField(f arrow.Field) Column

NewColumnFromArrowField creates a new Column from an arrow.Field arrow.Field is a low-level representation of a CloudQuery column that can be sent over the wire in a cross-language way.

func TestSourceColumns added in v3.2.0

func TestSourceColumns(testOpts TestSourceOptions) []Column

TestSourceColumns returns columns for all Arrow types and composites thereof. TestSourceOptions controls which types are included.

func (Column) String

func (c Column) String() string

func (Column) ToArrowField

func (c Column) ToArrowField() arrow.Field

type ColumnList

type ColumnList []Column

func (ColumnList) Get

func (c ColumnList) Get(name string) *Column

func (ColumnList) Index

func (c ColumnList) Index(col string) int

func (ColumnList) Names

func (c ColumnList) Names() []string

func (ColumnList) String

func (c ColumnList) String() string

type ColumnResolver

type ColumnResolver func(ctx context.Context, meta ClientMeta, resource *Resource, c Column) error

ColumnResolver is called for each row received in TableResolver's data fetch. execution holds all relevant information regarding execution as well as the Column called. resource holds the current row we are resolving the column for.

func ParentColumnResolver

func ParentColumnResolver(name string) ColumnResolver

ParentColumnResolver resolves a column from the parent's table data, if name isn't set the column will be set to null

func PathResolver

func PathResolver(path string) ColumnResolver

PathResolver resolves a field in the Resource.Item

Examples: PathResolver("Field") PathResolver("InnerStruct.Field") PathResolver("InnerStruct.InnerInnerStruct.Field")

type GenTestDataOptions

type GenTestDataOptions struct {
	// SourceName is the name of the source to set in the source_name column.
	SourceName string
	// SyncTime is the time to set in the sync_time column.
	SyncTime time.Time
	// MaxRows is the number of rows to generate.
	// Rows alternate between not containing null values and containing only null values.
	// (Only columns that are nullable according to the schema will be null)
	MaxRows int
	// StableUUID is the UUID to use for all rows. If set to uuid.Nil, a new UUID will be generated
	StableUUID uuid.UUID
	// StableTime is the time to use for all rows other than sync time. If set to time.Time{}, a new time will be generated
	StableTime    time.Time
	TimePrecision time.Duration
	Seed          int64
}

GenTestDataOptions are options for generating test data

type LengthTableValidator

type LengthTableValidator struct{}

func (LengthTableValidator) Validate

func (LengthTableValidator) Validate(t *Table) error

type Multiplexer

type Multiplexer func(meta ClientMeta) []ClientMeta

type Resource

type Resource struct {
	// Original resource item that wa from prior resolve
	Item any
	// Set if this is an embedded table
	Parent *Resource
	// internal fields
	Table *Table
	// contains filtered or unexported fields
}

Resource represents a row in it's associated table, it carries a reference to the original item, and automatically generates an Id based on Table's Columns. Resource data can be accessed by the Get and Set methods

func NewResourceData

func NewResourceData(t *Table, parent *Resource, item any) *Resource

func (*Resource) CalculateCQID

func (r *Resource) CalculateCQID(deterministicCQID bool) error

func (*Resource) Get

func (r *Resource) Get(columnName string) scalar.Scalar

func (*Resource) GetItem

func (r *Resource) GetItem() any

func (*Resource) GetValues added in v3.3.0

func (r *Resource) GetValues() scalar.Vector

func (*Resource) Set

func (r *Resource) Set(columnName string, value any) error

Set sets a column with value. This does validation and conversion to one of concrete it returns an error just for backward compatibility and panics in case it fails

func (*Resource) SetItem

func (r *Resource) SetItem(item any)

Override original item (this is useful for apis that follow list/details pattern)

func (*Resource) Validate

func (r *Resource) Validate() error

Validates that all primary keys have values.

type Resources

type Resources []*Resource

func (Resources) ColumnNames

func (rr Resources) ColumnNames() []string

func (Resources) TableName

func (rr Resources) TableName() string

type RowResolver

type RowResolver func(ctx context.Context, meta ClientMeta, resource *Resource) error

type Schemas

type Schemas []*arrow.Schema

func NewSchemasFromBytes added in v3.3.0

func NewSchemasFromBytes(b [][]byte) (Schemas, error)

func (Schemas) Encode added in v3.3.0

func (s Schemas) Encode() ([][]byte, error)

func (Schemas) Len

func (s Schemas) Len() int

func (Schemas) SchemaByName

func (s Schemas) SchemaByName(name string) *arrow.Schema

type SyncSummary

type SyncSummary struct {
	Resources uint64
	Errors    uint64
	Panics    uint64
}

This is deprecated

type Table

type Table struct {
	// Name of table
	Name string
	// Title to be used in documentation (optional: will be generated from name if not set)
	Title string
	// table description
	Description string
	// Columns are the set of fields that are part of this table
	Columns ColumnList
	// Relations are a set of related tables defines
	Relations Tables
	// Transform
	Transform Transform
	// Resolver is the main entry point to fetching table data and
	Resolver TableResolver
	// Multiplex returns re-purposed meta clients. The sdk will execute the table with each of them
	Multiplex Multiplexer
	// PostResourceResolver is called after all columns have been resolved, but before the Resource is sent to be inserted. The ordering of resolvers is:
	//  (Table) Resolver → PreResourceResolver → ColumnResolvers → PostResourceResolver
	PostResourceResolver RowResolver
	// PreResourceResolver is called before all columns are resolved but after Resource is created. The ordering of resolvers is:
	//  (Table) Resolver → PreResourceResolver → ColumnResolvers → PostResourceResolver
	PreResourceResolver RowResolver
	// IsIncremental is a flag that indicates if the table is incremental or not. This flag mainly affects how the table is
	// documented.
	IsIncremental bool

	// IgnoreInTests is used to exclude a table from integration tests.
	// By default, integration tests fetch all resources from cloudquery's test account, and verify all tables
	// have at least one row.
	// When IgnoreInTests is true, integration tests won't fetch from this table.
	// Used when it is hard to create a reproducible environment with a row in this table.
	IgnoreInTests bool

	// Parent is the parent table in case this table is called via parent table (i.e. relation)
	Parent *Table

	PkConstraintName string
}

func NewTableFromArrowSchema

func NewTableFromArrowSchema(sc *arrow.Schema) (*Table, error)

Create a CloudQuery Table abstraction from an arrow schema arrow schema is a low level representation of a table that can be sent over the wire in a cross-language way

func TestTable

func TestTable(name string, testOpts TestSourceOptions) *Table

TestTable returns a table with columns of all types. Useful for destination testing purposes

func (*Table) Column

func (t *Table) Column(name string) *Column

func (*Table) Copy

func (t *Table) Copy(parent *Table) *Table

func (*Table) GetChanges

func (t *Table) GetChanges(old *Table) []TableColumnChange

Get Changes returns changes between two tables when t is the new one and old is the old one.

func (*Table) IncrementalKeys

func (t *Table) IncrementalKeys() []string

func (*Table) OverwriteOrAddColumn

func (t *Table) OverwriteOrAddColumn(column *Column)

If the column with the same name exists, overwrites it. Otherwise, adds the column to the beginning of the table.

func (*Table) PrimaryKeys

func (t *Table) PrimaryKeys() []string

func (*Table) PrimaryKeysIndexes

func (t *Table) PrimaryKeysIndexes() []int

func (*Table) TableNames

func (t *Table) TableNames() []string

func (*Table) ToArrowSchema

func (t *Table) ToArrowSchema() *arrow.Schema

func (*Table) ValidateColumnNames

func (t *Table) ValidateColumnNames() error

func (*Table) ValidateDuplicateColumns

func (t *Table) ValidateDuplicateColumns() error

func (*Table) ValidateName

func (t *Table) ValidateName() error

type TableColumnChange

type TableColumnChange struct {
	Type       TableColumnChangeType
	ColumnName string
	Current    Column
	Previous   Column
}

func (TableColumnChange) String

func (t TableColumnChange) String() string

type TableColumnChangeType

type TableColumnChangeType int
const (
	TableColumnChangeTypeUnknown TableColumnChangeType = iota
	TableColumnChangeTypeAdd
	TableColumnChangeTypeUpdate
	TableColumnChangeTypeRemove
)

func (TableColumnChangeType) String

func (t TableColumnChangeType) String() string

type TableResolver

type TableResolver func(ctx context.Context, meta ClientMeta, parent *Resource, res chan<- any) error

TableResolver is the main entry point when a table is sync is called.

Table resolver has 3 main arguments: - meta(ClientMeta): is the client returned by the plugin.Provider Configure call - parent(Resource): resource is the parent resource in case this table is called via parent table (i.e. relation) - res(chan any): is a channel to pass results fetched by the TableResolver

type TableValidator

type TableValidator interface {
	Validate(t *Table) error
}

type Tables

type Tables []*Table

func NewTablesFromArrowSchemas added in v3.3.0

func NewTablesFromArrowSchemas(schemas []*arrow.Schema) (Tables, error)

func NewTablesFromBytes added in v3.3.0

func NewTablesFromBytes(b [][]byte) (Tables, error)

func (Tables) FilterDfs

func (tt Tables) FilterDfs(tables, skipTables []string, skipDependentTables bool) (Tables, error)

func (Tables) FilterDfsFunc

func (tt Tables) FilterDfsFunc(include, exclude func(*Table) bool, skipDependentTables bool) Tables

func (Tables) FlattenTables

func (tt Tables) FlattenTables() Tables

func (Tables) Get

func (tt Tables) Get(name string) *Table

Get returns a table by name. Returns top-level tables and relations.

func (Tables) GetTopLevel

func (tt Tables) GetTopLevel(name string) *Table

GetTopLevel returns a table by name. Only returns the table if it is in top-level list.

func (Tables) TableNames

func (tt Tables) TableNames() []string

func (Tables) ToArrowSchemas

func (tt Tables) ToArrowSchemas() Schemas

func (Tables) ValidateColumnNames

func (tt Tables) ValidateColumnNames() error

func (Tables) ValidateDuplicateColumns

func (tt Tables) ValidateDuplicateColumns() error

func (Tables) ValidateDuplicateTables

func (tt Tables) ValidateDuplicateTables() error

func (Tables) ValidateTableNames

func (tt Tables) ValidateTableNames() error

type TestSourceOptions added in v3.2.0

type TestSourceOptions struct {
	SkipDates      bool
	SkipDecimals   bool
	SkipDurations  bool
	SkipIntervals  bool
	SkipLargeTypes bool // e.g. large binary, large string
	SkipLists      bool // lists of all primitive types. Lists that were supported by CQTypes are always included.
	SkipMaps       bool
	SkipStructs    bool
	SkipTimes      bool // time of day types
	SkipTimestamps bool // timestamp types. Microsecond timestamp is always be included, regardless of this setting.
	TimePrecision  time.Duration
}

TestSourceOptions controls which types are included by TestSourceColumns.

type Transform

type Transform func(table *Table) error

type ValidationError

type ValidationError struct {
	Err   error
	Msg   string
	Type  arrow.DataType
	Value any
}

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) MaskedError

func (e *ValidationError) MaskedError() string

this prints the error without the value

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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