Skip to content

Commit

Permalink
fix: Handle commas in permissions array (#1835)
Browse files Browse the repository at this point in the history
#### Summary

Follow up to #1827 (comment)

---
  • Loading branch information
erezrokah committed Jul 30, 2024
1 parent 341c546 commit b633aed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
20 changes: 13 additions & 7 deletions schema/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package schema

import (
"context"
"encoding/json"
"fmt"
"regexp"
"slices"
"strings"

"github.com/apache/arrow/go/v17/arrow"
"github.com/cloudquery/plugin-sdk/v4/glob"
Expand Down Expand Up @@ -182,14 +182,17 @@ func NewTableFromArrowSchema(sc *arrow.Schema) (*Table, error) {
for i, field := range fields {
columns[i] = NewColumnFromArrowField(field)
}

var permissionsNeededArr []string
_ = json.Unmarshal([]byte(permissionsNeeded), &permissionsNeededArr)
table := &Table{
Name: name,
Description: description,
PkConstraintName: constraintName,
Columns: columns,
Title: title,
Parent: parent,
PermissionsNeeded: strings.Split(permissionsNeeded, ","),
PermissionsNeeded: permissionsNeededArr,
}
if isIncremental, found := tableMD.GetValue(MetadataIncremental); found {
table.IsIncremental = isIncremental == MetadataTrue
Expand Down Expand Up @@ -459,11 +462,10 @@ func (t *Table) PrimaryKeysIndexes() []int {
func (t *Table) ToArrowSchema() *arrow.Schema {
fields := make([]arrow.Field, len(t.Columns))
md := map[string]string{
MetadataTableName: t.Name,
MetadataTableDescription: t.Description,
MetadataTableTitle: t.Title,
MetadataConstraintName: t.PkConstraintName,
MetadataTablePermissionsNeeded: strings.Join(t.PermissionsNeeded, ","),
MetadataTableName: t.Name,
MetadataTableDescription: t.Description,
MetadataTableTitle: t.Title,
MetadataConstraintName: t.PkConstraintName,
}
if t.IsIncremental {
md[MetadataIncremental] = MetadataTrue
Expand All @@ -474,10 +476,14 @@ func (t *Table) ToArrowSchema() *arrow.Schema {
if t.IsPaid {
md[MetadataTableIsPaid] = MetadataTrue
}
asJSON, _ := json.Marshal(t.PermissionsNeeded)
md[MetadataTablePermissionsNeeded] = string(asJSON)

schemaMd := arrow.MetadataFrom(md)
for i, c := range t.Columns {
fields[i] = c.ToArrowField()
}

return arrow.NewSchema(fields, &schemaMd)
}

Expand Down
68 changes: 39 additions & 29 deletions schema/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,37 +702,47 @@ func TestTableGetChanges(t *testing.T) {
}
}

func TestTableToAndFromArrow(t *testing.T) {
func TestTablesToAndFromArrow(t *testing.T) {
// The attributes in this table should all be preserved when converting to and from Arrow.
table := &Table{
Name: "test_table",
Description: "Test table description",
Title: "Test Table",
Parent: &Table{
Name: "parent_table",
},
IsIncremental: true,
Columns: []Column{
{Name: "bool", Type: arrow.FixedWidthTypes.Boolean},
{Name: "int", Type: arrow.PrimitiveTypes.Int64},
{Name: "float", Type: arrow.PrimitiveTypes.Float64},
{Name: "string", Type: arrow.BinaryTypes.String},
{Name: "json", Type: types.ExtensionTypes.JSON},
{Name: "unique", Type: arrow.BinaryTypes.String, Unique: true},
{Name: "primary_key", Type: arrow.BinaryTypes.String, PrimaryKey: true},
{Name: "not_null", Type: arrow.BinaryTypes.String, NotNull: true},
{Name: "incremental_key", Type: arrow.BinaryTypes.String, IncrementalKey: true},
{Name: "multiple_attributes", Type: arrow.BinaryTypes.String, PrimaryKey: true, IncrementalKey: true, NotNull: true, Unique: true},
},
PermissionsNeeded: []string{"storage.buckets.list"},
}
arrowSchema := table.ToArrowSchema()
tableFromArrow, err := NewTableFromArrowSchema(arrowSchema)
if err != nil {
t.Fatal(err)
tablesToTest := Tables{
// Test empty table
&Table{
Columns: []Column{},
},
// Test table with attributes
&Table{
Name: "test_table",
Description: "Test table description",
Title: "Test Table",
Parent: &Table{
Name: "parent_table",
},
IsIncremental: true,
Columns: []Column{
{Name: "bool", Type: arrow.FixedWidthTypes.Boolean},
{Name: "int", Type: arrow.PrimitiveTypes.Int64},
{Name: "float", Type: arrow.PrimitiveTypes.Float64},
{Name: "string", Type: arrow.BinaryTypes.String},
{Name: "json", Type: types.ExtensionTypes.JSON},
{Name: "unique", Type: arrow.BinaryTypes.String, Unique: true},
{Name: "primary_key", Type: arrow.BinaryTypes.String, PrimaryKey: true},
{Name: "not_null", Type: arrow.BinaryTypes.String, NotNull: true},
{Name: "incremental_key", Type: arrow.BinaryTypes.String, IncrementalKey: true},
{Name: "multiple_attributes", Type: arrow.BinaryTypes.String, PrimaryKey: true, IncrementalKey: true, NotNull: true, Unique: true},
},
PermissionsNeeded: []string{"storage.buckets.list", "compute.acceleratorTypes.list", "test,test"},
},
}
if diff := cmp.Diff(table, tableFromArrow); diff != "" {
t.Errorf("diff (+got, -want): %v", diff)

for _, table := range tablesToTest {
arrowSchema := table.ToArrowSchema()
tableFromArrow, err := NewTableFromArrowSchema(arrowSchema)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(table, tableFromArrow); diff != "" {
t.Errorf("diff (+got, -want): %v", diff)
}
}
}

Expand Down

1 comment on commit b633aed

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Benchmark results

  • Glob-8 ns/op: 92.6

Please sign in to comment.