Skip to content

Commit b7e3eda

Browse files
authored
Merge pull request #21 from goccy/fix-delete-spec
Fix deletion of spec
2 parents 265a6ba + 594ef5d commit b7e3eda

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

internal/catalog.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,22 @@ func (c *Catalog) deleteTableSpecByName(name string) error {
218218
return fmt.Errorf("failed to find table spec from map by %s", name)
219219
}
220220
tables := make([]*TableSpec, 0, len(c.tables))
221+
specName := strings.Join(spec.NamePath, "_")
221222
for _, table := range c.tables {
222-
if spec == table {
223+
if specName == strings.Join(table.NamePath, "_") {
223224
continue
224225
}
225226
tables = append(tables, table)
226227
}
227-
c.tables = tables
228-
delete(c.tableMap, name)
229-
if err := c.resetCatalogs(); err != nil {
230-
return fmt.Errorf("failed to reset catalogs: %w", err)
228+
229+
c.defaultCatalog = newSimpleCatalog(defaultCatalogName)
230+
c.pathToCatalogMap = map[string]*types.SimpleCatalog{}
231+
c.tables = []*TableSpec{}
232+
c.tableMap = map[string]*TableSpec{}
233+
for _, spec := range tables {
234+
if err := c.addTableSpec(spec); err != nil {
235+
return err
236+
}
231237
}
232238
return nil
233239
}
@@ -238,28 +244,18 @@ func (c *Catalog) deleteFunctionSpecByName(name string) error {
238244
return fmt.Errorf("failed to find function spec from map by %s", name)
239245
}
240246
functions := make([]*FunctionSpec, 0, len(c.functions))
247+
specName := strings.Join(spec.NamePath, "_")
241248
for _, function := range c.functions {
242-
if spec == function {
249+
if specName == strings.Join(function.NamePath, "_") {
243250
continue
244251
}
245252
functions = append(functions, function)
246253
}
247-
c.functions = functions
248-
delete(c.funcMap, name)
249-
if err := c.resetCatalogs(); err != nil {
250-
return fmt.Errorf("failed to reset catalogs: %w", err)
251-
}
252-
return nil
253-
}
254254

255-
func (c *Catalog) resetCatalogs() error {
256255
c.defaultCatalog = newSimpleCatalog(defaultCatalogName)
257256
c.pathToCatalogMap = map[string]*types.SimpleCatalog{}
258-
for _, spec := range c.tables {
259-
if err := c.addTableSpec(spec); err != nil {
260-
return err
261-
}
262-
}
257+
c.functions = []*FunctionSpec{}
258+
c.funcMap = map[string]*FunctionSpec{}
263259
for _, spec := range c.functions {
264260
if err := c.addFunctionSpec(spec); err != nil {
265261
return err
@@ -353,13 +349,15 @@ func (c *Catalog) addFunctionSpec(spec *FunctionSpec) error {
353349
c.functions = append(c.functions, spec)
354350
c.funcMap[funcName] = spec
355351
catalogMapKey := c.pathToCatalogMapKey(c.trimmedLastPath(spec.NamePath))
356-
cat, exists := c.pathToCatalogMap[catalogMapKey]
357-
if !exists {
358-
cat = newSimpleCatalog(defaultCatalogName)
359-
c.pathToCatalogMap[catalogMapKey] = cat
360-
}
361-
if err := c.addFunctionSpecRecursive(cat, spec); err != nil {
362-
return err
352+
if catalogMapKey != "" {
353+
cat, exists := c.pathToCatalogMap[catalogMapKey]
354+
if !exists {
355+
cat = newSimpleCatalog(defaultCatalogName)
356+
c.pathToCatalogMap[catalogMapKey] = cat
357+
}
358+
if err := c.addFunctionSpecRecursive(cat, spec); err != nil {
359+
return err
360+
}
363361
}
364362
if err := c.addFunctionSpecRecursive(c.defaultCatalog, spec); err != nil {
365363
return err
@@ -376,13 +374,15 @@ func (c *Catalog) addTableSpec(spec *TableSpec) error {
376374
c.tables = append(c.tables, spec)
377375
c.tableMap[tableName] = spec
378376
catalogMapKey := c.pathToCatalogMapKey(c.trimmedLastPath(spec.NamePath))
379-
cat, exists := c.pathToCatalogMap[catalogMapKey]
380-
if !exists {
381-
cat = newSimpleCatalog(defaultCatalogName)
382-
c.pathToCatalogMap[catalogMapKey] = cat
383-
}
384-
if err := c.addTableSpecRecursive(cat, spec); err != nil {
385-
return err
377+
if catalogMapKey != "" {
378+
cat, exists := c.pathToCatalogMap[catalogMapKey]
379+
if !exists {
380+
cat = newSimpleCatalog(defaultCatalogName)
381+
c.pathToCatalogMap[catalogMapKey] = cat
382+
}
383+
if err := c.addTableSpecRecursive(cat, spec); err != nil {
384+
return err
385+
}
386386
}
387387
if err := c.addTableSpecRecursive(c.defaultCatalog, spec); err != nil {
388388
return err

0 commit comments

Comments
 (0)