Skip to content

Commit 3124490

Browse files
committed
sys var fix
# Conflicts: # enginetest/queries/script_queries.go
1 parent 3418751 commit 3124490

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

enginetest/queries/script_queries.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,66 @@ type ScriptTestAssertion struct {
122122
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
123123
// the tests.
124124
var ScriptTests = []ScriptTest{
125+
{
126+
// https://github.com/dolthub/go-mysql-server/issues/3259
127+
Name: "Missing column with same name as system variable",
128+
SetUpScript: []string{
129+
"CREATE DATABASE IF NOT EXISTS test_db",
130+
"USE test_db",
131+
"CREATE TABLE A (id INT)",
132+
"CREATE TABLE B (id INT)",
133+
"INSERT INTO A VALUES (1)",
134+
"INSERT INTO B VALUES (2)",
135+
},
136+
Assertions: []ScriptTestAssertion{
137+
{
138+
Query: "SELECT UNIX_TIMESTAMP(A.timestamp) FROM A LIMIT 1",
139+
ExpectedErr: sql.ErrTableColumnNotFound,
140+
},
141+
{
142+
Query: "SELECT A.timestamp FROM A",
143+
ExpectedErr: sql.ErrTableColumnNotFound,
144+
},
145+
{
146+
Query: "SELECT A.version FROM A",
147+
ExpectedErr: sql.ErrTableColumnNotFound,
148+
},
149+
{
150+
Query: "SELECT A.max_connections FROM A",
151+
ExpectedErr: sql.ErrTableColumnNotFound,
152+
},
153+
{
154+
Query: "SELECT UPPER(A.sql_mode) FROM A",
155+
ExpectedErr: sql.ErrTableColumnNotFound,
156+
},
157+
{
158+
Query: "SELECT @@timestamp",
159+
Expected: []sql.Row{{float64(0)}},
160+
SkipResultsCheck: true, // dynamic var
161+
},
162+
{
163+
Query: "SELECT @@version",
164+
Expected: []sql.Row{{""}},
165+
SkipResultsCheck: true, // dynamic var
166+
},
167+
{
168+
Query: "SELECT test_db.A.timestamp FROM A",
169+
ExpectedErr: sql.ErrTableColumnNotFound,
170+
},
171+
{
172+
Query: "SELECT test_db.A.version FROM test_db.A",
173+
ExpectedErr: sql.ErrTableColumnNotFound,
174+
},
175+
{
176+
Query: "SELECT a1.timestamp FROM A a1 JOIN B b1 ON a1.id = b1.id",
177+
ExpectedErr: sql.ErrTableColumnNotFound,
178+
},
179+
{
180+
Query: "SELECT b1.max_connections FROM A a1 JOIN B b1 ON a1.id = b1.id",
181+
ExpectedErr: sql.ErrTableColumnNotFound,
182+
},
183+
},
184+
},
125185
{
126186
// https://github.com/dolthub/dolt/issues/9927
127187
// https://github.com/dolthub/dolt/issues/9053

sql/planbuilder/scalar.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,15 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
126126
if aliasedExpr, ok := inScope.selectAliases[colName]; ok {
127127
return aliasedExpr
128128
}
129-
sysVar, scope, ok := b.buildSysVar(v, ast.SetScope_None)
130-
if ok {
131-
return sysVar
129+
// Only try system variable lookup if there's no table qualifier.
130+
// Qualified names like "A.timestamp" are always column references, never system variables.
131+
var scope ast.SetScope
132+
if tblName == "" && dbName == "" {
133+
var sysVar sql.Expression
134+
sysVar, scope, ok = b.buildSysVar(v, ast.SetScope_None)
135+
if ok {
136+
return sysVar
137+
}
132138
}
133139
var err error
134140
if scope == ast.SetScope_User || scope == ast.SetScope_Persist || scope == ast.SetScope_PersistOnly {

0 commit comments

Comments
 (0)