Skip to content

Commit 848e505

Browse files
authored
feat: Add Unflatten tables method (#1138)
This adds an `UnflattenTables` method to the `Tables` type, the opposite operation of `FlattenTables`. ### Why? Until now, destination plugins were the only part of the code that needed to decode Arrow schemas to tables, and destinations don't need relational information. Now that docs generation is moving to the CLI, however, we need to be able to reconstruct the original relational structure in order to accurately reflect it in the docs. Rather than modifying `FlattenTables`, which would not be backwards-compatible, I have opted to add a new method that the CLI can use. With this change in place, I am able to fully generate the docs for the AWS plugin from the CLI, with zero changes.
1 parent 3c6c959 commit 848e505

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

schema/table.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,31 @@ func (tt Tables) FlattenTables() Tables {
275275
return slices.Clip(deduped)
276276
}
277277

278+
// UnflattenTables returns a new Tables copy with the relations unflattened. This is the
279+
// opposite operation of FlattenTables.
280+
func (tt Tables) UnflattenTables() (Tables, error) {
281+
tables := make(Tables, 0, len(tt))
282+
for _, t := range tt {
283+
table := *t
284+
tables = append(tables, &table)
285+
}
286+
topLevel := make([]*Table, 0, len(tt))
287+
// build relations
288+
for _, table := range tables {
289+
if table.Parent == nil {
290+
topLevel = append(topLevel, table)
291+
continue
292+
}
293+
parent := tables.Get(table.Parent.Name)
294+
if parent == nil {
295+
return nil, fmt.Errorf("parent table %s not found", table.Parent.Name)
296+
}
297+
table.Parent = parent
298+
parent.Relations = append(parent.Relations, table)
299+
}
300+
return slices.Clip(topLevel), nil
301+
}
302+
278303
func (tt Tables) TableNames() []string {
279304
ret := []string{}
280305
for _, t := range tt {

schema/table_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var testTable = &Table{
1616
{
1717
Name: "test2",
1818
Columns: []Column{},
19+
Parent: &Table{Name: "test"},
1920
},
2021
},
2122
}
@@ -45,6 +46,15 @@ func TestTablesFlatten(t *testing.T) {
4546
}
4647
}
4748

49+
func TestTablesUnflatten(t *testing.T) {
50+
srcTables := Tables{testTable}
51+
tables, err := srcTables.FlattenTables().UnflattenTables()
52+
require.NoError(t, err)
53+
require.Equal(t, 1, len(srcTables)) // verify that the source Tables were left untouched
54+
require.Equal(t, 1, len(tables)) // verify that the tables are equal to what we started with
55+
require.Equal(t, 1, len(tables[0].Relations))
56+
}
57+
4858
func TestTablesFilterDFS(t *testing.T) {
4959
tests := []struct {
5060
name string

0 commit comments

Comments
 (0)