Skip to content

Commit de1be10

Browse files
authored
Merge pull request #3070 from dolthub/angela/generated_columns
Don't prune VirtualColumnTable tables
2 parents d551d15 + 0cd84e2 commit de1be10

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

enginetest/queries/generated_columns.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,45 @@ var GeneratedColumnTests = []ScriptTest{
14371437
},
14381438
},
14391439
},
1440+
{
1441+
// https://github.com/dolthub/dolt/issues/8968
1442+
Name: "can select all columns from table with generated column",
1443+
SetUpScript: []string{
1444+
"create table t(pk int primary key, j1 json)",
1445+
`insert into t values (1, '{"name": "foo"}')`,
1446+
"alter table t add column g1 varchar(100) generated always as (json_unquote(json_extract(`j1`, '$.name')))",
1447+
},
1448+
Assertions: []ScriptTestAssertion{
1449+
{
1450+
Query: "select * from t",
1451+
Expected: []sql.Row{{1, `{"name":"foo"}`, "foo"}},
1452+
},
1453+
{
1454+
Query: "select pk, j1, g1 from t",
1455+
Expected: []sql.Row{{1, `{"name":"foo"}`, "foo"}},
1456+
},
1457+
{
1458+
Query: "select pk, g1 from t",
1459+
Expected: []sql.Row{{1, "foo"}},
1460+
},
1461+
{
1462+
Query: "select g1 from t",
1463+
Expected: []sql.Row{{"foo"}},
1464+
},
1465+
{
1466+
Query: "select j1, g1 from t",
1467+
Expected: []sql.Row{{`{"name":"foo"}`, "foo"}},
1468+
},
1469+
{
1470+
Query: "select j1 from t",
1471+
Expected: []sql.Row{{`{"name":"foo"}`}},
1472+
},
1473+
{
1474+
Query: "select pk, j1 from t",
1475+
Expected: []sql.Row{{1, `{"name":"foo"}`}},
1476+
},
1477+
},
1478+
},
14401479
}
14411480

14421481
var BrokenGeneratedColumnTests = []ScriptTest{

sql/analyzer/symbol_resolution.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,36 +202,23 @@ func pruneTableCols(
202202
return n, transform.SameTree, nil
203203
}
204204

205+
// columns don't need to be pruned if there's a star
205206
_, selectStar := parentStars[table.Name()]
206-
if unqualifiedStar {
207-
selectStar = true
207+
if selectStar || unqualifiedStar {
208+
return n, transform.SameTree, nil
208209
}
209210

210-
// Don't prune columns if they're needed by a virtual column
211-
virtualColDeps := make(map[string]int)
212-
if !selectStar { // if selectStar, we're adding all columns anyway
213-
if vct, isVCT := n.WrappedTable().(*plan.VirtualColumnTable); isVCT {
214-
for _, projection := range vct.Projections {
215-
transform.InspectExpr(projection, func(e sql.Expression) bool {
216-
if cd, isCD := e.(*sql.ColumnDefaultValue); isCD {
217-
transform.InspectExpr(cd.Expr, func(e sql.Expression) bool {
218-
if gf, ok := e.(*expression.GetField); ok {
219-
virtualColDeps[gf.Name()]++
220-
}
221-
return false
222-
})
223-
}
224-
return false
225-
})
226-
}
227-
}
211+
// pruning VirtualColumnTable underlying tables causes indexing errors when VirtualColumnTable.Projections (which are sql.Expression)
212+
// are evaluated
213+
if _, isVCT := n.WrappedTable().(*plan.VirtualColumnTable); isVCT {
214+
return n, transform.SameTree, nil
228215
}
229216

230217
cols := make([]string, 0)
231218
source := strings.ToLower(table.Name())
232219
for _, col := range table.Schema() {
233220
c := newTableCol(source, col.Name)
234-
if selectStar || parentCols[c] > 0 || virtualColDeps[c.Name()] > 0 {
221+
if parentCols[c] > 0 {
235222
cols = append(cols, c.col)
236223
}
237224
}

0 commit comments

Comments
 (0)