forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_exprs.go
112 lines (103 loc) · 3.27 KB
/
default_exprs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sqlbase
import (
"github.com/cockroachdb/cockroach/pkg/sql/parser"
)
// MakeDefaultExprs returns a slice of the default expressions for the slice
// of input column descriptors, or nil if none of the input column descriptors
// have default expressions.
func MakeDefaultExprs(
cols []ColumnDescriptor, parse *parser.Parser, evalCtx *parser.EvalContext,
) ([]parser.TypedExpr, error) {
// Check to see if any of the columns have DEFAULT expressions. If there
// are no DEFAULT expressions, we don't bother with constructing the
// defaults map as the defaults are all NULL.
haveDefaults := false
for _, col := range cols {
if col.DefaultExpr != nil {
haveDefaults = true
break
}
}
if !haveDefaults {
return nil, nil
}
// Build the default expressions map from the parsed SELECT statement.
defaultExprs := make([]parser.TypedExpr, 0, len(cols))
exprStrings := make([]string, 0, len(cols))
for _, col := range cols {
if col.DefaultExpr != nil {
exprStrings = append(exprStrings, *col.DefaultExpr)
}
}
exprs, err := parser.ParseExprs(exprStrings)
if err != nil {
return nil, err
}
defExprIdx := 0
for _, col := range cols {
if col.DefaultExpr == nil {
defaultExprs = append(defaultExprs, parser.DNull)
continue
}
expr := exprs[defExprIdx]
typedExpr, err := parser.TypeCheck(expr, nil, col.Type.ToDatumType())
if err != nil {
return nil, err
}
if typedExpr, err = parse.NormalizeExpr(evalCtx, typedExpr); err != nil {
return nil, err
}
defaultExprs = append(defaultExprs, typedExpr)
defExprIdx++
}
return defaultExprs, nil
}
// ProcessDefaultColumns adds columns with DEFAULT to cols if not present
// and returns the defaultExprs for cols.
func ProcessDefaultColumns(
cols []ColumnDescriptor,
tableDesc *TableDescriptor,
parse *parser.Parser,
evalCtx *parser.EvalContext,
) ([]ColumnDescriptor, []parser.TypedExpr, error) {
colIDSet := make(map[ColumnID]struct{}, len(cols))
for _, col := range cols {
colIDSet[col.ID] = struct{}{}
}
// Add the column if it has a DEFAULT expression.
addIfDefault := func(col ColumnDescriptor) {
if col.DefaultExpr != nil {
if _, ok := colIDSet[col.ID]; !ok {
colIDSet[col.ID] = struct{}{}
cols = append(cols, col)
}
}
}
// Add any column that has a DEFAULT expression.
for _, col := range tableDesc.Columns {
addIfDefault(col)
}
// Also add any column in a mutation that is DELETE_AND_WRITE_ONLY and has
// a DEFAULT expression.
for _, m := range tableDesc.Mutations {
if col := m.GetColumn(); col != nil &&
m.State == DescriptorMutation_DELETE_AND_WRITE_ONLY {
addIfDefault(*col)
}
}
defaultExprs, err := MakeDefaultExprs(cols, parse, evalCtx)
return cols, defaultExprs, err
}