Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Fix checksum for multi underlying tables. #613

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion sql/plan/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ func getIndexableTable(t sql.Table) (sql.IndexableTable, error) {
}
}

func getChecksumable(t sql.Table) sql.Checksumable {
switch t := t.(type) {
case sql.Checksumable:
return t
case sql.TableWrapper:
return getChecksumable(t.Underlying())
default:
return nil
}
}

// RowIter implements the Node interface.
func (c *CreateIndex) RowIter(ctx *sql.Context) (sql.RowIter, error) {
table, ok := c.Table.(*ResolvedTable)
Expand Down Expand Up @@ -119,7 +130,7 @@ func (c *CreateIndex) RowIter(ctx *sql.Context) (sql.RowIter, error) {
}
}

if ch, ok := table.Table.(sql.Checksumable); ok {
if ch := getChecksumable(table.Table); ch != nil {
c.Config[sql.ChecksumKey], err = ch.Checksum()
if err != nil {
return nil, err
Expand Down
48 changes: 48 additions & 0 deletions sql/plan/create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,48 @@ func TestCreateIndexChecksum(t *testing.T) {
require.Equal("1", driver.config["idx"][sql.ChecksumKey])
}

func TestCreateIndexChecksumWithUnderlying(t *testing.T) {
require := require.New(t)

table :=
&underlyingTable{
&underlyingTable{
&underlyingTable{
&checksumTable{
mem.NewTable("foo", sql.Schema{
{Name: "a", Source: "foo"},
{Name: "b", Source: "foo"},
{Name: "c", Source: "foo"},
}),
"1",
},
},
},
}

driver := new(mockDriver)
catalog := sql.NewCatalog()
catalog.RegisterIndexDriver(driver)

exprs := []sql.Expression{
expression.NewGetFieldWithTable(2, sql.Int64, "foo", "c", true),
expression.NewGetFieldWithTable(0, sql.Int64, "foo", "a", true),
}

ci := NewCreateIndex(
"idx", NewResolvedTable(table), exprs, "mock",
map[string]string{"async": "false"},
)
ci.Catalog = catalog
ci.CurrentDatabase = "foo"

_, err := ci.RowIter(sql.NewEmptyContext())
require.NoError(err)

require.Equal([]string{"idx"}, driver.saved)
require.Equal("1", driver.config["idx"][sql.ChecksumKey])
}

func TestCreateIndexWithIter(t *testing.T) {
require := require.New(t)
foo := mem.NewPartitionedTable("foo", sql.Schema{
Expand Down Expand Up @@ -360,3 +402,9 @@ func (t *checksumTable) Checksum() (string, error) {
}

func (t *checksumTable) Underlying() sql.Table { return t.Table }

type underlyingTable struct {
sql.Table
}

func (t *underlyingTable) Underlying() sql.Table { return t.Table }