Skip to content

Commit

Permalink
Fix: Table.BatchLoadTables interface cannot reload schema information…
Browse files Browse the repository at this point in the history
… correctly (#37)

* fix(table): refactor batch load table

* fix: change to newTableWithModel

* fix: remove function BatchLoadTablesInSpecificSchema
  • Loading branch information
dingxin-tech authored Oct 30, 2024
1 parent a02590d commit 281e261
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
30 changes: 25 additions & 5 deletions odps/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ const (

// Table represent the table in odps projects
type Table struct {
model tableModel
tableSchema tableschema.TableSchema
odpsIns *Odps
beLoaded bool
model tableModel
tableSchema tableschema.TableSchema
odpsIns *Odps
beLoaded bool
beLoadedExtended bool
}

// TableOrErr is used for the return value of Tables.List
Expand Down Expand Up @@ -76,10 +77,29 @@ func NewTable(odpsIns *Odps, projectName string, schemaName string, tableName st
}
}

func newTableWithModel(odpsIns *Odps, model *tableModel) (*Table, error) {
table := Table{
model: *model,
odpsIns: odpsIns,
beLoaded: true,
beLoadedExtended: false,
}

err := json.Unmarshal([]byte(model.Schema), &table.tableSchema)
if err != nil {
return nil, errors.WithStack(err)
}
return &table, nil
}

func (t *Table) IsLoaded() bool {
return t.beLoaded
}

func (t *Table) IsLoadedExtended() bool {
return t.beLoaded
}

func (t *Table) Load() error {
client := t.odpsIns.restClient
resource := t.ResourceUrl()
Expand Down Expand Up @@ -131,7 +151,7 @@ func (t *Table) LoadExtendedInfo() error {
if err != nil {
return errors.WithStack(err)
}

t.beLoadedExtended = true
return nil
}

Expand Down
13 changes: 9 additions & 4 deletions odps/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (ts *Tables) BatchLoadTables(tableNames []string) ([]*Table, error) {
Tables []struct {
Project string
Name string
Schema string
} `xml:"Table"`
}

Expand All @@ -113,7 +114,8 @@ func (ts *Tables) BatchLoadTables(tableNames []string) ([]*Table, error) {
postBodyModel.Tables = append(postBodyModel.Tables, struct {
Project string
Name string
}{Project: ts.projectName, Name: tableName})
Schema string
}{Project: ts.projectName, Name: tableName, Schema: ts.schemaName})
}

type ResModel struct {
Expand All @@ -139,9 +141,12 @@ func (ts *Tables) BatchLoadTables(tableNames []string) ([]*Table, error) {

ret := make([]*Table, len(resModel.Table))

for i, tableModel := range resModel.Table {
table := NewTable(ts.odpsIns, ts.projectName, ts.schemaName, tableModel.Name)
table.model = tableModel
for i, _ := range resModel.Table {
tableModel := &resModel.Table[i]
table, err := newTableWithModel(ts.odpsIns, tableModel)
if err != nil {
return nil, errors.WithStack(err)
}
ret[i] = table
}

Expand Down

0 comments on commit 281e261

Please sign in to comment.