Skip to content

Commit 984abdb

Browse files
authored
Merge pull request #3067 from dolthub/elianddb/9427-prevent-user-variables-in-defaults
dolthub/dolt#9427 - Prevent user and system variables in column defaults and generated values
2 parents 1af0f6e + 17a77a5 commit 984abdb

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

enginetest/queries/column_default_queries.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,4 +940,120 @@ var ColumnDefaultTests = []ScriptTest{
940940
},
941941
},
942942
},
943+
{
944+
Name: "User variables in column defaults are not allowed",
945+
SetUpScript: []string{
946+
"set @a = 1;",
947+
},
948+
Assertions: []ScriptTestAssertion{
949+
{
950+
Query: "CREATE TABLE t(i int DEFAULT (@a));",
951+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
952+
},
953+
{
954+
Query: "CREATE TABLE t(i int DEFAULT ((@a)));",
955+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
956+
},
957+
{
958+
Query: "CREATE TABLE t(i int DEFAULT (@a + 1));",
959+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
960+
},
961+
},
962+
},
963+
{
964+
Name: "System variables in column defaults are not allowed",
965+
Assertions: []ScriptTestAssertion{
966+
{
967+
Query: "CREATE TABLE t(i int DEFAULT (@@version));",
968+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
969+
},
970+
{
971+
Query: "CREATE TABLE t(i int DEFAULT (@@session.sql_mode));",
972+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
973+
},
974+
{
975+
Query: "CREATE TABLE t(i int DEFAULT (@@global.max_connections));",
976+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
977+
},
978+
},
979+
},
980+
{
981+
Name: "User variables in generated columns are not allowed",
982+
SetUpScript: []string{
983+
"set @a = 1;",
984+
},
985+
Assertions: []ScriptTestAssertion{
986+
{
987+
Query: "CREATE TABLE t(i int GENERATED ALWAYS AS (@a));",
988+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
989+
},
990+
{
991+
Query: "CREATE TABLE t(i int GENERATED ALWAYS AS (@a + 1));",
992+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
993+
},
994+
},
995+
},
996+
{
997+
Name: "System variables in generated columns are not allowed",
998+
Assertions: []ScriptTestAssertion{
999+
{
1000+
Query: "CREATE TABLE t(i int GENERATED ALWAYS AS (@@version));",
1001+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1002+
},
1003+
{
1004+
Query: "CREATE TABLE t(i int GENERATED ALWAYS AS (@@session.sql_mode));",
1005+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1006+
},
1007+
},
1008+
},
1009+
{
1010+
Name: "User variables in ALTER TABLE ADD COLUMN defaults are not allowed",
1011+
SetUpScript: []string{
1012+
"CREATE TABLE t(pk int PRIMARY KEY);",
1013+
"set @a = 1;",
1014+
},
1015+
Assertions: []ScriptTestAssertion{
1016+
{
1017+
Query: "ALTER TABLE t ADD COLUMN i int DEFAULT (@a);",
1018+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1019+
},
1020+
},
1021+
},
1022+
{
1023+
Name: "System variables in ALTER TABLE ADD COLUMN defaults are not allowed",
1024+
SetUpScript: []string{
1025+
"CREATE TABLE t(pk int PRIMARY KEY);",
1026+
},
1027+
Assertions: []ScriptTestAssertion{
1028+
{
1029+
Query: "ALTER TABLE t ADD COLUMN i int DEFAULT (@@version);",
1030+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1031+
},
1032+
},
1033+
},
1034+
{
1035+
Name: "User variables in ALTER TABLE ALTER COLUMN defaults are not allowed",
1036+
SetUpScript: []string{
1037+
"CREATE TABLE t(pk int PRIMARY KEY, i int);",
1038+
"set @a = 1;",
1039+
},
1040+
Assertions: []ScriptTestAssertion{
1041+
{
1042+
Query: "ALTER TABLE t ALTER COLUMN i SET DEFAULT (@a);",
1043+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1044+
},
1045+
},
1046+
},
1047+
{
1048+
Name: "System variables in ALTER TABLE ALTER COLUMN defaults are not allowed",
1049+
SetUpScript: []string{
1050+
"CREATE TABLE t(pk int PRIMARY KEY, i int);",
1051+
},
1052+
Assertions: []ScriptTestAssertion{
1053+
{
1054+
Query: "ALTER TABLE t ALTER COLUMN i SET DEFAULT (@@version);",
1055+
ExpectedErr: sql.ErrColumnDefaultUserVariable,
1056+
},
1057+
},
1058+
},
9431059
}

sql/analyzer/resolve_column_defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ func validateColumnDefault(ctx *sql.Context, col *sql.Column, colDefault *sql.Co
233233
var err error
234234
sql.Inspect(colDefault.Expr, func(e sql.Expression) bool {
235235
switch e.(type) {
236+
case *expression.UserVar, *expression.SystemVar:
237+
err = sql.ErrColumnDefaultUserVariable.New(col.Name)
238+
return false
236239
case sql.FunctionExpression, *expression.UnresolvedFunction:
237240
var funcName string
238241
switch expr := e.(type) {

sql/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ var (
147147
// ErrInvalidColumnDefaultValue is returned when column default function value is not wrapped in parentheses for column types excluding datetime and timestamp
148148
ErrInvalidColumnDefaultValue = errors.NewKind("Invalid default value for '%s'")
149149

150+
// ErrColumnDefaultUserVariable is returned when a column default expression contains user or system variables
151+
ErrColumnDefaultUserVariable = errors.NewKind("Default value expression of column '%s' cannot refer user or system variables.")
152+
150153
// ErrInvalidDefaultValueOrder is returned when a default value references a column that comes after it and contains a default expression.
151154
ErrInvalidDefaultValueOrder = errors.NewKind(`default value of column "%s" cannot refer to a column defined after it if those columns have an expression default value`)
152155

0 commit comments

Comments
 (0)