Skip to content

Commit 3dbd7f3

Browse files
authored
feat: Add more metadata to tables needed for docs generation (#1129)
This adds metadata fields for table titles and parent relationships, so that this information can be sent over the wire in Arrow format. We will use this in the CLI to generate markdown and json docs for source plugins.
1 parent 6567043 commit 3dbd7f3

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

schema/arrow.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
MetadataFalse = "false"
1515
MetadataTableName = "cq:table_name"
1616
MetadataTableDescription = "cq:table_description"
17+
MetadataTableTitle = "cq:table_title"
18+
MetadataTableDependsOn = "cq:table_depends_on"
1719
)
1820

1921
type Schemas []*arrow.Schema

schema/table.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Transform func(table *Table) error
2626

2727
type Tables []*Table
2828

29-
// This is deprecated
29+
// Deprecated: SyncSummary is deprecated.
3030
type SyncSummary struct {
3131
Resources uint64
3232
Errors uint64
@@ -94,7 +94,7 @@ var (
9494
reValidColumnName = regexp.MustCompile(`^[a-z_][a-z\d_]*$`)
9595
)
9696

97-
// AddCqIds adds the cq_id and cq_parent_id columns to the table and all its relations
97+
// AddCqIDs adds the cq_id and cq_parent_id columns to the table and all its relations
9898
// set cq_id as primary key if no other primary keys
9999
func AddCqIDs(table *Table) {
100100
havePks := len(table.PrimaryKeys()) > 0
@@ -126,9 +126,9 @@ func NewTablesFromArrowSchemas(schemas []*arrow.Schema) (Tables, error) {
126126
return tables, nil
127127
}
128128

129-
// Create a CloudQuery Table abstraction from an arrow schema
130-
// arrow schema is a low level representation of a table that can be sent
131-
// over the wire in a cross-language way
129+
// NewTableFromArrowSchema creates a CloudQuery Table abstraction from an Arrow schema.
130+
// The Arrow schema is a low level representation of a table that can be sent
131+
// over the wire in a cross-language way.
132132
func NewTableFromArrowSchema(sc *arrow.Schema) (*Table, error) {
133133
tableMD := sc.Metadata()
134134
name, found := tableMD.GetValue(MetadataTableName)
@@ -137,6 +137,12 @@ func NewTableFromArrowSchema(sc *arrow.Schema) (*Table, error) {
137137
}
138138
description, _ := tableMD.GetValue(MetadataTableDescription)
139139
constraintName, _ := tableMD.GetValue(MetadataConstraintName)
140+
title, _ := tableMD.GetValue(MetadataTableTitle)
141+
dependsOn, _ := tableMD.GetValue(MetadataTableDependsOn)
142+
var parent *Table
143+
if dependsOn != "" {
144+
parent = &Table{Name: dependsOn}
145+
}
140146
fields := sc.Fields()
141147
columns := make(ColumnList, len(fields))
142148
for i, field := range fields {
@@ -147,6 +153,8 @@ func NewTableFromArrowSchema(sc *arrow.Schema) (*Table, error) {
147153
Description: description,
148154
PkConstraintName: constraintName,
149155
Columns: columns,
156+
Title: title,
157+
Parent: parent,
150158
}
151159
if isIncremental, found := tableMD.GetValue(MetadataIncremental); found {
152160
table.IsIncremental = isIncremental == MetadataTrue
@@ -391,20 +399,23 @@ func (t *Table) ToArrowSchema() *arrow.Schema {
391399
md := map[string]string{
392400
MetadataTableName: t.Name,
393401
MetadataTableDescription: t.Description,
402+
MetadataTableTitle: t.Title,
394403
MetadataConstraintName: t.PkConstraintName,
395-
MetadataIncremental: MetadataFalse,
396404
}
397405
if t.IsIncremental {
398406
md[MetadataIncremental] = MetadataTrue
399407
}
408+
if t.Parent != nil {
409+
md[MetadataTableDependsOn] = t.Parent.Name
410+
}
400411
schemaMd := arrow.MetadataFrom(md)
401412
for i, c := range t.Columns {
402413
fields[i] = c.ToArrowField()
403414
}
404415
return arrow.NewSchema(fields, &schemaMd)
405416
}
406417

407-
// Get Changes returns changes between two tables when t is the new one and old is the old one.
418+
// GetChanges returns changes between two tables when t is the new one and old is the old one.
408419
func (t *Table) GetChanges(old *Table) []TableColumnChange {
409420
var changes []TableColumnChange
410421
for _, c := range t.Columns {
@@ -475,6 +486,7 @@ func (t *Table) Column(name string) *Column {
475486
return nil
476487
}
477488

489+
// OverwriteOrAddColumn overwrites or adds columns.
478490
// If the column with the same name exists, overwrites it.
479491
// Otherwise, adds the column to the beginning of the table.
480492
func (t *Table) OverwriteOrAddColumn(column *Column) {

schema/table_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/apache/arrow/go/v13/arrow"
7+
"github.com/cloudquery/plugin-sdk/v4/types"
78
"github.com/google/go-cmp/cmp"
89
"github.com/stretchr/testify/require"
910
)
@@ -346,3 +347,36 @@ func TestTableGetChanges(t *testing.T) {
346347
})
347348
}
348349
}
350+
351+
func TestTableToAndFromArrow(t *testing.T) {
352+
// The attributes in this table should all be preserved when converting to and from Arrow.
353+
table := &Table{
354+
Name: "test_table",
355+
Description: "Test table description",
356+
Title: "Test Table",
357+
Parent: &Table{
358+
Name: "parent_table",
359+
},
360+
IsIncremental: true,
361+
Columns: []Column{
362+
{Name: "bool", Type: arrow.FixedWidthTypes.Boolean},
363+
{Name: "int", Type: arrow.PrimitiveTypes.Int64},
364+
{Name: "float", Type: arrow.PrimitiveTypes.Float64},
365+
{Name: "string", Type: arrow.BinaryTypes.String},
366+
{Name: "json", Type: types.ExtensionTypes.JSON},
367+
{Name: "unique", Type: arrow.BinaryTypes.String, Unique: true},
368+
{Name: "primary_key", Type: arrow.BinaryTypes.String, PrimaryKey: true},
369+
{Name: "not_null", Type: arrow.BinaryTypes.String, NotNull: true},
370+
{Name: "incremental_key", Type: arrow.BinaryTypes.String, IncrementalKey: true},
371+
{Name: "multiple_attributes", Type: arrow.BinaryTypes.String, PrimaryKey: true, IncrementalKey: true, NotNull: true, Unique: true},
372+
},
373+
}
374+
arrowSchema := table.ToArrowSchema()
375+
tableFromArrow, err := NewTableFromArrowSchema(arrowSchema)
376+
if err != nil {
377+
t.Fatal(err)
378+
}
379+
if diff := cmp.Diff(table, tableFromArrow); diff != "" {
380+
t.Errorf("diff (+got, -want): %v", diff)
381+
}
382+
}

0 commit comments

Comments
 (0)