<commit_before>package mysql

import (
	"database/sql"
	"fmt"
	"sort"
	"strings"
)

type (
	IndexColumn struct {
		Table        string
		NonUniue     int8
		KeyName      string
		SeqInIndex   int32
		ColumnName   string
		Collation    string
		Cardinality  JsonNullInt64
		SubPart      JsonNullString
		Packed       JsonNullString
		Null         string
		IndexType    string
		Comment      string
		IndexComment string
	}
	Index   []IndexColumn
	Indices []Index
)

func (m Index) IsPrimaryKey() bool {
	return m[0].KeyName == "PRIMARY"
}

func (m Index) IsUniqueKey() bool {
	return m[0].NonUniue != 1
}

func (m Index) GetKeyName() string {
	return m[0].KeyName
}

func (m Index) ColumnNames() []string {
	names := make([]string, 0, len(m))
	for _, info := range m {
		names = append(names, Quote(info.ColumnName))
	}
	return names
}

func (m Index) KeyNamesWithSubPart() []string {
	names := make([]string, 0, len(m))
	for _, info := range m {
		if info.SubPart.Valid {
			names = append(names, fmt.Sprintf("%s(%s)", Quote(info.ColumnName), info.SubPart.String))
		} else {
			names = append(names, Quote(info.ColumnName))
		}
	}
	return names
}

func (m *Indices) ResetCardinality() {
	for i := range *m {
		for j := range (*m)[i] {
			(*m)[i][j].Cardinality = JsonNullInt64{}
		}
	}
}

func (m Indices) ToSQL() []string {
	indices := m.getSortedIndices(m.GetSortedKeys())
	indexSQLs := make([]string, 0, len(m))
	for _, index := range indices {
		sql := ""
		comment := index[0].Comment
		switch {
		case index.IsPrimaryKey():
			sql = fmt.Sprintf("primary key (%s)", strings.Join(index.ColumnNames(), ","))
		case !index.IsPrimaryKey() && index.IsUniqueKey():
			sql = fmt.Sprintf("unique key %s (%s)", Quote(index.GetKeyName()), strings.Join(index.ColumnNames(), ","))
		default:
			sql = fmt.Sprintf("key %s (%s)", Quote(index.GetKeyName()), strings.Join(index.KeyNamesWithSubPart(), ","))
		}
		if comment != "" {
			indexSQLs = append(indexSQLs, fmt.Sprintf("%s comment %s", sql, QuoteString(comment)))
		}
	}
	return indexSQLs
}

func (m Indices) ToAddSQL() []string {
	sqls := m.ToSQL()
	for i, sql := range sqls {
		sqls[i] = fmt.Sprintf("add %s", sql)
	}
	return sqls
}

func (m Indices) ToDropSQL() []string {
	idxMap := map[string]struct{}{}
	for _, index := range m {
		idxMap[index.GetKeyName()] = struct{}{}
	}
	sqls := make([]string, 0, len(idxMap))
	for keyName := range idxMap {
		sqls = append(sqls, fmt.Sprintf("drop key %s", Quote(keyName)))
	}
	return sqls
}

func (m Indices) GroupByKeyName() map[string]Indices {<commit_msg>Bug fix (#12)<commit_after>package mysql

import (
	"database/sql"
	"fmt"
	"sort"
	"strings"
)

type (
	IndexColumn struct {
		Table        string
		NonUniue     int8
		KeyName      string
		SeqInIndex   int32
		ColumnName   string
		Collation    string
		Cardinality  JsonNullInt64
		SubPart      JsonNullString
		Packed       JsonNullString
		Null         string
		IndexType    string
		Comment      string
		IndexComment string
	}
	Index   []IndexColumn
	Indices []Index
)

func (m Index) IsPrimaryKey() bool {
	return m[0].KeyName == "PRIMARY"
}

func (m Index) IsUniqueKey() bool {
	return m[0].NonUniue != 1
}

func (m Index) GetKeyName() string {
	return m[0].KeyName
}

func (m Index) ColumnNames() []string {
	names := make([]string, 0, len(m))
	for _, info := range m {
		names = append(names, Quote(info.ColumnName))
	}
	return names
}

func (m Index) KeyNamesWithSubPart() []string {
	names := make([]string, 0, len(m))
	for _, info := range m {
		if info.SubPart.Valid {
			names = append(names, fmt.Sprintf("%s(%s)", Quote(info.ColumnName), info.SubPart.String))
		} else {
			names = append(names, Quote(info.ColumnName))
		}
	}
	return names
}

func (m *Indices) ResetCardinality() {
	for i := range *m {
		for j := range (*m)[i] {
			(*m)[i][j].Cardinality = JsonNullInt64{}
		}
	}
}

func (m Indices) ToSQL() []string {
	indices := m.getSortedIndices(m.GetSortedKeys())
	indexSQLs := make([]string, 0, len(m))
	for _, index := range indices {
		sql := ""
		comment := index[0].Comment
		switch {
		case index.IsPrimaryKey():
			sql = fmt.Sprintf("primary key (%s)", strings.Join(index.ColumnNames(), ","))
		case !index.IsPrimaryKey() && index.IsUniqueKey():
			sql = fmt.Sprintf("unique key %s (%s)", Quote(index.GetKeyName()), strings.Join(index.ColumnNames(), ","))
		default:
			sql = fmt.Sprintf("key %s (%s)", Quote(index.GetKeyName()), strings.Join(index.KeyNamesWithSubPart(), ","))
		}
		if comment != "" {
			sql = fmt.Sprintf("%s comment %s", sql, QuoteString(comment))
		}
		indexSQLs = append(indexSQLs, sql)
	}
	return indexSQLs
}

func (m Indices) ToAddSQL() []string {
	sqls := m.ToSQL()
	for i, sql := range sqls {
		sqls[i] = fmt.Sprintf("add %s", sql)
	}
	return sqls
}

func (m Indices) ToDropSQL() []string {
	idxMap := map[string]struct{}{}
	for _, index := range m {
		idxMap[index.GetKeyName()] = struct{}{}
	}
	sqls := make([]string, 0, len(idxMap))
	for keyName := range idxMap {
		sqls = append(sqls, fmt.Sprintf("drop key %s", Quote(keyName)))
	}
	return sqls
}

func (m Indices) GroupByKeyName() map[string]Indices {
{'fraction_non_alphanumeric': 0.16425498631208446, 'fraction_numerical': 0.00664841611263199, 'mean_word_length': 3.736111111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7520046', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1859, 'n_words': 458}