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

feat: index creation #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion partitionstorage/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const (
columnScheduledAt = "ScheduledAt"
columnRunningAt = "RunningAt"
columnFinishedAt = "FinishedAt"

IndexWatermarkIndex = "WatermarkIndex"
IndexCreatedAtStartTimestampIndex = "CreatedAtStartTimestampIndex"
)

func (s *SpannerPartitionStorage) CreateTableIfNotExists(ctx context.Context) error {
Expand Down Expand Up @@ -107,9 +110,25 @@ func (s *SpannerPartitionStorage) CreateTableIfNotExists(ctx context.Context) er
columnFinishedAt,
)

// -- For GoogleSQL dialect
// CREATE INDEX WatermarkIndex ON <Metadata Table Name> (Watermark) STORING (State);
// CREATE INDEX CreatedAtStartTimestampIndex ON <Metadata Table Name> (CreatedAt, StartTimestamp);
watermarkIndexStmt := fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %[1]s ON %[2]s(%[3]s) STORING (%[4]s)`,
IndexWatermarkIndex,
s.tableName,
columnWatermark,
columnState,
)
timestampIndexStmt := fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %[1]s ON %[2]s(%[3]s, %[4]s)`,
IndexCreatedAtStartTimestampIndex,
s.tableName,
columnCreatedAt,
columnStartTimestamp,
)

req := &databasepb.UpdateDatabaseDdlRequest{
Database: s.client.DatabaseName(),
Statements: []string{stmt},
Statements: []string{stmt, watermarkIndexStmt, timestampIndexStmt},
}
op, err := databaseAdminClient.UpdateDatabaseDdl(ctx, req)
if err != nil {
Expand Down
39 changes: 38 additions & 1 deletion partitionstorage/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package partitionstorage

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -157,6 +158,15 @@ func TestSpannerPartitionStorage_CreateTableIfNotExists(t *testing.T) {
if !existsTable {
t.Errorf("SpannerPartitionStorage.existsTable() = %v, want %v", existsTable, false)
}

indexNames, err := getTableIndexNames(ctx, client, storage.tableName)
if err != nil {
t.Error(err)
return
}
if len(indexNames) != 3 {
t.Errorf("SpannerPartitionStorage.getTableIndexNames() = %v, want %v", len(indexNames), 3)
}
}

func existsTable(ctx context.Context, client *spanner.Client, tableName string) (bool, error) {
Expand All @@ -178,6 +188,34 @@ func existsTable(ctx context.Context, client *spanner.Client, tableName string)
return true, nil
}

func getTableIndexNames(ctx context.Context, client *spanner.Client, tableName string) ([]string, error) {
iter := client.Single().Query(ctx, spanner.Statement{
SQL: "SELECT index_name FROM information_schema.indexes where table_name = @tableName",
Params: map[string]interface{}{
"tableName": tableName,
},
})
defer iter.Stop()

expectedNames := make([]string, 3)
for {
name := ""
r, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.Done) {
break
}
return nil, err
}
if err := r.ColumnByName("index_name", &name); err != nil {
return nil, err
}
expectedNames = append(expectedNames, name)
}

return expectedNames, nil
}

func setupSpannerPartitionStorage(t *testing.T, ctx context.Context) *SpannerPartitionStorage {
t.Helper()

Expand Down Expand Up @@ -582,5 +620,4 @@ func TestSpannerPartitionStorage_Update(t *testing.T) {
t.Errorf("UpdateWatermark(ctx, %+v, %q): got = %+v, want %+v", partitions[0], timestamp, got, want)
}
})

}
Loading