Repository: dolthub/dolt
File: go/libraries/doltcore/sqle/database.go (Go)

Code before:
	}

	return db.getAllTableNames(ctx, root, showSystemTables, true, doReadNonlocalTables)
}

func (db Database) getAllTableNames(ctx *sql.Context, root doltdb.RootValue, includeGeneratedSystemTables bool, includeRootObjects bool, includeNonlocalTables readNonlocalTablesFlag) ([]string, error) {
	var err error
	var result []string
	localNameSet := map[string]any{}
	// If we are in a schema-enabled session and the schema name is not set, we need to union all table names in all
	// schemas in the search_path
	if resolve.UseSearchPath && db.schemaName == "" {
		names, err := resolve.TablesOnSearchPath(ctx, root)
		if err != nil {
			return nil, err
		}
		// TODO: this method should probably return TableNames, but need to iron out the effective schema for system
		//  tables first
		result = doltdb.FlattenTableNames(names)
	} else {
		result, err = root.GetTableNames(ctx, db.schemaName, includeRootObjects)
		if err != nil {
			return nil, err
		}
		for _, nameStr := range result {
			localNameSet[nameStr] = nil
		}
	}

	if includeGeneratedSystemTables {
		// TODO: this should work on the current schema only, if there is one
		// TODO: this is getting called with showSystemTables = true, which seems wrong in most cases
		systemTables, err := resolve.GetGeneratedSystemTables(ctx, root)
		if err != nil {
			return nil, err
		}
		result = append(result, doltdb.FlattenTableNames(systemTables)...)
	}

	if includeNonlocalTables {
		nonlocalTableNames, err := db.getNonlocalTableNames(ctx, root)
		if err != nil {
			return nil, err
		}
		for _, nonLocalTableName := range nonlocalTableNames {
			_, ok := localNameSet[nonLocalTableName]
			// The semantics here are a little backwards, in reality the non-local table is prioritized. Since the names
			// happen to match, we avoid the extra modification of shifting the name position. This does result in some
			// non-local table names to not be grouped together, however.
			if !ok {
				result = append(result, nonLocalTableName)

Review comment (suggestion):
```suggestion
			localNameSet[nameStr] = struct{}{}
```

Code after:
	}

	return db.getAllTableNames(ctx, root, showSystemTables, true, doReadNonlocalTables)
}

func (db Database) getAllTableNames(ctx *sql.Context, root doltdb.RootValue, includeGeneratedSystemTables bool, includeRootObjects bool, includeNonlocalTables readNonlocalTablesFlag) ([]string, error) {
	var err error
	var result []string
	localNameSet := map[string]struct{}{}
	// If we are in a schema-enabled session and the schema name is not set, we need to union all table names in all
	// schemas in the search_path
	if resolve.UseSearchPath && db.schemaName == "" {
		names, err := resolve.TablesOnSearchPath(ctx, root)
		if err != nil {
			return nil, err
		}
		// TODO: this method should probably return TableNames, but need to iron out the effective schema for system
		//  tables first
		result = doltdb.FlattenTableNames(names)
	} else {
		result, err = root.GetTableNames(ctx, db.schemaName, includeRootObjects)
		if err != nil {
			return nil, err
		}
		for _, nameStr := range result {
			localNameSet[nameStr] = struct{}{}
		}
	}

	if includeGeneratedSystemTables {
		// TODO: this should work on the current schema only, if there is one
		// TODO: this is getting called with showSystemTables = true, which seems wrong in most cases
		systemTables, err := resolve.GetGeneratedSystemTables(ctx, root)
		if err != nil {
			return nil, err
		}
		result = append(result, doltdb.FlattenTableNames(systemTables)...)
	}

	if includeNonlocalTables {
		nonlocalTableNames, err := db.getNonlocalTableNames(ctx, root)
		if err != nil {
			return nil, err
		}
		for _, nonLocalTableName := range nonlocalTableNames {
			_, ok := localNameSet[nonLocalTableName]
			// The semantics here are a little backwards, in reality the non-local table is prioritized. Since the names
			// happen to match, we avoid the extra modification of shifting the name position. This does result in some
			// non-local table names to not be grouped together, however.
			if !ok {
				result = append(result, nonLocalTableName)