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

Add RelationExists interface to txnDatabase 2.0 #20403

Merged
merged 4 commits into from
Nov 28, 2024
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
15 changes: 15 additions & 0 deletions pkg/frontend/test/engine_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/testutil/testengine/testengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/runtime"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/stretchr/testify/require"
)

func TestTestEngine(t *testing.T) {
Expand All @@ -40,3 +42,20 @@ func BenchmarkNew(b *testing.B) {
New(context.Background())
}
}

func TestRelationExists(t *testing.T) {
catalog.SetupDefines("")
ctx := context.Background()
ctx = defines.AttachAccountId(ctx, 0)
eng, _, _ := New(ctx)
db, err := eng.Database(ctx, "test", nil)
require.NoError(t, err)

exist, err := db.RelationExists(ctx, "NotExist", nil)
require.NoError(t, err)
require.False(t, exist)

exist, err = db.RelationExists(ctx, "r", nil)
require.NoError(t, err)
require.True(t, exist)
}
40 changes: 32 additions & 8 deletions pkg/vm/engine/disttae/txn_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (db *txnDatabase) Relations(ctx context.Context) ([]string, error) {
return rels, nil
}

func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
func (db *txnDatabase) relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
common.DoIfDebugEnabled(func() {
logutil.Debug(
"Transaction.Relation",
Expand Down Expand Up @@ -117,7 +117,8 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng

// check the table is deleted or not
if !openSys && txn.tableOps.existAndDeleted(key) {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
logutil.Info("[relation] deleted in txn", zap.String("table", name))
return nil, nil
}

// get relation from the txn created tables cache: created by this txn
Expand All @@ -143,10 +144,13 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng
if err != nil {
return nil, err
}
if item == nil {
return nil, nil
}

tbl, err := newTxnTable(
db,
item,
*item,
p,
shardservice.GetService(p.GetService()),
txn.engine,
Expand All @@ -159,6 +163,25 @@ func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (eng
return tbl, nil
}

func (db *txnDatabase) Relation(ctx context.Context, name string, proc any) (engine.Relation, error) {
rel, err := db.relation(ctx, name, proc)
if err != nil {
return nil, err
}
if rel == nil {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
}
return rel, nil
}

func (db *txnDatabase) RelationExists(ctx context.Context, name string, proc any) (bool, error) {
rel, err := db.relation(ctx, name, proc)
if err != nil {
return false, err
}
return rel != nil, nil
}

func (db *txnDatabase) Delete(ctx context.Context, name string) error {
_, err := db.deleteTable(ctx, name, false, false)
return err
Expand Down Expand Up @@ -584,7 +607,7 @@ func (db *txnDatabase) getTableItem(
accountID uint32,
name string,
engine *Engine,
) (cache.TableItem, error) {
) (*cache.TableItem, error) {
item := cache.TableItem{
Name: name,
DatabaseId: db.databaseId,
Expand All @@ -596,14 +619,15 @@ func (db *txnDatabase) getTableItem(
if ok := c.GetTable(&item); !ok {
var tableitem *cache.TableItem
if !c.CanServe(types.TimestampToTS(db.op.SnapshotTS())) {
logutil.Info("FIND_TABLE getTableItem cache cannot serve", zap.String("table", name), zap.Uint32("accountID", accountID), zap.String("timestamp", db.op.SnapshotTS().DebugString()))
if tableitem, err = db.loadTableFromStorage(ctx, accountID, name); err != nil {
return cache.TableItem{}, err
return nil, err
}
}
if tableitem == nil {
return cache.TableItem{}, moerr.NewParseErrorf(ctx, "table %q does not exist", name)
return nil, nil
}
return *tableitem, nil
return tableitem, nil
}
return item, nil
return &item, nil
}
5 changes: 4 additions & 1 deletion pkg/vm/engine/disttae/txn_table_sharding_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,13 @@ func getTxnTable(
if err != nil {
return nil, err
}
if item == nil {
return nil, moerr.NewParseErrorf(ctx, "table %q does not exist", param.TxnTable.TableName)
}

tbl := newTxnTableWithItem(
db,
item,
*item,
proc,
engine.(*Engine),
)
Expand Down
11 changes: 11 additions & 0 deletions pkg/vm/engine/memoryengine/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ func (d *Database) Relation(ctx context.Context, relName string, _ any) (engine.
}

}
func (d *Database) RelationExists(ctx context.Context, relName string, _ any) (bool, error) {
rel, err := d.Relation(ctx, relName, nil)
if err != nil {
if moerr.IsMoErrCode(err, moerr.ErrNoSuchTable) {
return false, nil
} else {
return false, err
}
}
return rel != nil, nil
}

func (d *Database) Relations(ctx context.Context) ([]string, error) {

Expand Down
31 changes: 31 additions & 0 deletions pkg/vm/engine/test/disttae_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,3 +1207,34 @@ func TestCache3Tables(t *testing.T) {
require.Equal(t, catalog.MO_COLUMNS, tname)
require.NotNil(t, rel)
}

func TestRelationExists(t *testing.T) {
opts := config.WithLongScanAndCKPOpts(nil)
p := testutil.InitEnginePack(testutil.TestOptions{TaeEngineOptions: opts}, t)
defer p.Close()
txnop := p.StartCNTxn()
schema := catalog2.MockSchemaAll(2, 0)
schema.Name = "test"
p.CreateDBAndTable(txnop, "db", schema)

db, err := p.D.Engine.Database(p.Ctx, "db", txnop)
require.NoError(t, err)

exist, err := db.RelationExists(p.Ctx, "test", nil)
require.NoError(t, err)
require.True(t, exist)

exist, err = db.RelationExists(p.Ctx, "testxx", nil)
require.NoError(t, err)
require.False(t, exist)

// craft no-account-id error
ctx := context.WithValue(p.Ctx, defines.TenantIDKey{}, nil)
exist, err = db.RelationExists(ctx, "testxx", nil)
require.Error(t, err)
require.False(t, exist)

rel, err := db.Relation(ctx, "test", nil)
require.Error(t, err)
require.Nil(t, rel)
}
1 change: 1 addition & 0 deletions pkg/vm/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ type Reader interface {
type Database interface {
Relations(context.Context) ([]string, error)
Relation(context.Context, string, any) (Relation, error)
RelationExists(context.Context, string, any) (bool, error)

Delete(context.Context, string) error
Create(context.Context, string, []TableDef) error // Create Table - (name, table define)
Expand Down
Loading