Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: errors with CREATE TABLE LIKE when the original table has indexes #48

Merged
merged 11 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions meta/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ func DecodeIndexName(encodedName string) (string, string) {
}
return parts[0], parts[1]
}

// DecodeColumnName extracts column names from a SQL string, Only consider single-column indexes or multi-column indexes
func DecodeColumnName(sql_string string) []string {
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
leftParen := strings.Index(sql_string, "(")
rightParen := strings.Index(sql_string, ")")
if leftParen != -1 && rightParen != -1 {
content := sql_string[leftParen+1 : rightParen]
columns := strings.Split(content, ",")
for i, col := range columns {
columns[i] = strings.TrimSpace(col)
}
return columns
}
return []string{}
}
3 changes: 2 additions & 1 deletion meta/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ var _ sql.Index = (*Index)(nil)

// TODO: DuckDB doesn't have a convenient way to get the expressions from an index
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
// so we need to implement our own. Storing it in the index comment is a good idea.
func NewIndex(dbName, tableName, name string, unique bool, comment *Comment) *Index {
func NewIndex(dbName, tableName, name string, unique bool, comment *Comment, exprs []sql.Expression) *Index {
return &Index{
DbName: dbName,
TableName: tableName,
Name: name,
Unique: unique,
CommentObj: comment,
Exprs: exprs,
}
}

Expand Down
33 changes: 30 additions & 3 deletions meta/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/marcboeker/go-duckdb"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -358,7 +359,7 @@ func (t *Table) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
defer t.mu.RUnlock()

// Query to get the indexes for the table
rows, err := t.db.storage.Query(`SELECT index_name, is_unique, comment FROM duckdb_indexes() WHERE database_name = ? AND schema_name = ? AND table_name = ?`,
rows, err := t.db.storage.Query(`SELECT index_name, is_unique, comment, sql FROM duckdb_indexes() WHERE database_name = ? AND schema_name = ? AND table_name = ?`,
t.db.catalogName, t.db.name, t.name)
if err != nil {
return nil, ErrDuckDB.New(err)
Expand All @@ -370,14 +371,40 @@ func (t *Table) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
var encodedIndexName string
var comment stdsql.NullString
var isUnique bool
var sql_string string
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
var exprs []sql.Expression

if err := rows.Scan(&encodedIndexName, &isUnique, &comment); err != nil {
if err := rows.Scan(&encodedIndexName, &isUnique, &comment, &sql_string); err != nil {
return nil, ErrDuckDB.New(err)
}

_, indexName := DecodeIndexName(encodedIndexName)
columnNames := DecodeColumnName(sql_string)

indexes = append(indexes, NewIndex(t.db.name, t.name, indexName, isUnique, DecodeComment(comment.String)))
decodedComment := DecodeComment(comment.String)

for _, columnName := range columnNames {
// Query to get the column information for the index
column_rows, err := t.db.storage.Query(`SELECT column_index, table_oid, data_type, is_nullable, numeric_precision, numeric_scale FROM duckdb_columns() WHERE database_name = ? AND schema_name = ? AND table_name = ? AND column_name = ?`,
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
t.db.catalogName, t.db.name, t.name, columnName)
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, ErrDuckDB.New(err)
}
defer column_rows.Close()
if column_rows.Next() {
var columnIndex int
var isNullable bool
var dataType string
var tableId int
var numericPrecision, numericScale stdsql.NullInt32
if err := column_rows.Scan(&columnIndex, &tableId, &dataType, &isNullable, &numericPrecision, &numericScale); err != nil {
return nil, ErrDuckDB.New(err)
}
data_type := mysqlDataType(newDuckType(dataType, decodedComment.Meta), uint8(numericPrecision.Int32), uint8(numericScale.Int32))
exprs = append(exprs, expression.NewGetFieldWithTable(columnIndex, tableId, data_type, t.db.name, t.name, columnName, isNullable))
GaoYusong marked this conversation as resolved.
Show resolved Hide resolved
}
}
indexes = append(indexes, NewIndex(t.db.name, t.name, indexName, isUnique, DecodeComment(comment.String), exprs))
}

if err := rows.Err(); err != nil {
Expand Down