Skip to content

Commit

Permalink
opt: test catalog support for virtual columns
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
RaduBerinde committed Dec 7, 2020
1 parent e225817 commit 2045ba2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
15 changes: 10 additions & 5 deletions pkg/sql/opt/cat/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ const (
// VirtualInverted columns are implicit columns that are used by inverted
// indexes.
VirtualInverted
// VirtualComputed columns are non-stored computed columns that are used by
// expression-based indexes.
// VirtualComputed columns are non-stored computed columns.
VirtualComputed
)

Expand Down Expand Up @@ -228,15 +227,21 @@ func (c *Column) InitVirtualInverted(
// InitVirtualComputed is used by catalog implementations to populate a
// VirtualComputed Column. It should not be used anywhere else.
func (c *Column) InitVirtualComputed(
ordinal int, name tree.Name, datumType *types.T, nullable bool, computedExpr string,
ordinal int,
stableID StableID,
name tree.Name,
datumType *types.T,
nullable bool,
hidden bool,
computedExpr string,
) {
c.ordinal = ordinal
c.stableID = 0
c.stableID = stableID
c.name = name
c.kind = VirtualComputed
c.datumType = datumType
c.nullable = nullable
c.hidden = true
c.hidden = hidden
c.defaultExpr = ""
c.computedExpr = computedExpr
c.invertedSourceColumnOrdinal = -1
Expand Down
8 changes: 6 additions & 2 deletions pkg/sql/opt/cat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ func formatColumn(col *Column, buf *bytes.Buffer) {
fmt.Fprintf(buf, " not null")
}
if col.IsComputed() {
fmt.Fprintf(buf, " as (%s) stored", col.ComputedExprStr())
if col.Kind() == VirtualComputed {
fmt.Fprintf(buf, " as (%s) virtual", col.ComputedExprStr())
} else {
fmt.Fprintf(buf, " as (%s) stored", col.ComputedExprStr())
}
}
if col.HasDefault() {
fmt.Fprintf(buf, " default (%s)", col.DefaultExprStr())
Expand All @@ -322,7 +326,7 @@ func formatColumn(col *Column, buf *bytes.Buffer) {
case VirtualInverted:
fmt.Fprintf(buf, " [virtual-inverted]")
case VirtualComputed:
fmt.Fprintf(buf, " [virtual-computed]")
// No need to show anything more (it already shows up as virtual).
}
}

Expand Down
36 changes: 25 additions & 11 deletions pkg/sql/opt/testutils/testcat/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,29 @@ func (tt *Table) addColumn(def *tree.ColumnTableDef) {
}

var col cat.Column
col.InitNonVirtual(
ordinal,
cat.StableID(1+ordinal),
name,
kind,
typ,
nullable,
false, /* hidden */
defaultExpr,
computedExpr,
)
if def.Computed.Virtual {
col.InitVirtualComputed(
ordinal,
cat.StableID(1+ordinal),
name,
typ,
nullable,
false, /* hidden */
*computedExpr,
)
} else {
col.InitNonVirtual(
ordinal,
cat.StableID(1+ordinal),
name,
kind,
typ,
nullable,
false, /* hidden */
defaultExpr,
computedExpr,
)
}
tt.Columns = append(tt.Columns, col)
}

Expand Down Expand Up @@ -820,9 +832,11 @@ func columnForIndexElemExpr(tt *Table, expr tree.Expr) cat.Column {
var col cat.Column
col.InitVirtualComputed(
len(tt.Columns),
cat.StableID(1+len(tt.Columns)),
name,
typ,
true, /* nullable */
true, /* hidden */
exprStr,
)
tt.Columns = append(tt.Columns, col)
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/opt/testutils/testcat/testdata/index
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,24 @@ TABLE xyz
├── y int
├── z string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── idx_expr_1 string as (lower(z)) stored [hidden] [virtual-computed]
├── idx_expr_2 int as (y + 1) stored [hidden] [virtual-computed]
├── idx_expr_3 int as (x + y) stored [hidden] [virtual-computed]
├── idx_expr_1 string as (lower(z)) virtual [hidden]
├── idx_expr_2 int as (y + 1) virtual [hidden]
├── idx_expr_3 int as (x + y) virtual [hidden]
├── INDEX primary
│ └── x int not null
├── INDEX idx1
│ ├── idx_expr_1 string as (lower(z)) stored [hidden] [virtual-computed]
│ ├── idx_expr_1 string as (lower(z)) virtual [hidden]
│ └── x int not null
├── INDEX idx2
│ ├── idx_expr_1 string as (lower(z)) stored [hidden] [virtual-computed]
│ ├── idx_expr_1 string as (lower(z)) virtual [hidden]
│ ├── y int
│ └── x int not null
├── INDEX idx3
│ ├── idx_expr_2 int as (y + 1) stored [hidden] [virtual-computed]
│ ├── idx_expr_1 string as (lower(z)) stored [hidden] [virtual-computed]
│ ├── idx_expr_2 int as (y + 1) virtual [hidden]
│ ├── idx_expr_1 string as (lower(z)) virtual [hidden]
│ └── x int not null
├── INDEX idx4
│ ├── idx_expr_3 int as (x + y) stored [hidden] [virtual-computed]
│ ├── idx_expr_3 int as (x + y) virtual [hidden]
│ ├── y int
│ ├── x int not null
│ ├── z string (storing)
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/opt/testutils/testcat/testdata/table
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ CREATE TABLE abcdef (
c INT DEFAULT (10),
d INT AS (b + c + 1) STORED,
e INT AS (a) STORED,
f INT CHECK (f > 2)
f INT CHECK (f > 2),
g INT AS (a+b) VIRTUAL
)
----

Expand All @@ -37,6 +38,7 @@ TABLE abcdef
├── d int as ((b + c) + 1) stored
├── e int as (a) stored
├── f int
├── g int as (a + b) virtual
├── rowid int not null default (unique_rowid()) [hidden]
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── CHECK (f > 2)
Expand Down

0 comments on commit 2045ba2

Please sign in to comment.