From 281e2618f05b9df9a33b5e8a7c945d4b3fb179b8 Mon Sep 17 00:00:00 2001 From: Jason Zhang <50667759+dingxin-tech@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:14:06 +0800 Subject: [PATCH] Fix: Table.BatchLoadTables interface cannot reload schema information correctly (#37) * fix(table): refactor batch load table * fix: change to newTableWithModel * fix: remove function BatchLoadTablesInSpecificSchema --- odps/table.go | 30 +++++++++++++++++++++++++----- odps/tables.go | 13 +++++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/odps/table.go b/odps/table.go index 7a88626..314f027 100644 --- a/odps/table.go +++ b/odps/table.go @@ -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 @@ -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() @@ -131,7 +151,7 @@ func (t *Table) LoadExtendedInfo() error { if err != nil { return errors.WithStack(err) } - + t.beLoadedExtended = true return nil } diff --git a/odps/tables.go b/odps/tables.go index 21afec8..e39d46b 100644 --- a/odps/tables.go +++ b/odps/tables.go @@ -105,6 +105,7 @@ func (ts *Tables) BatchLoadTables(tableNames []string) ([]*Table, error) { Tables []struct { Project string Name string + Schema string } `xml:"Table"` } @@ -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 { @@ -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 }