Skip to content

Commit

Permalink
fix show create table with fk in other database (#1.2-dev) (#16349)
Browse files Browse the repository at this point in the history
fix show create table with fk in other database

Approved by: @ouyuanning, @heni02, @aunjgr, @sukki37
  • Loading branch information
YANGGMM authored May 23, 2024
1 parent d564eeb commit 8ce356a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pkg/sql/plan/build_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func buildShowCreateTable(stmt *tree.ShowCreateTable, ctx CompilerContext) (*Pla
}()
}

_, tableDef := ctx.Resolve(dbName, tblName, *snapshot)
tableObjRef, tableDef := ctx.Resolve(dbName, tblName, *snapshot)
if tableDef == nil {
return nil, moerr.NewNoSuchTable(ctx.GetContext(), dbName, tblName)
}
Expand All @@ -122,7 +122,7 @@ func buildShowCreateTable(stmt *tree.ShowCreateTable, ctx CompilerContext) (*Pla
// sql = fmt.Sprintf(sql, MO_CATALOG_DB_NAME, MO_CATALOG_DB_NAME, dbName, tblName)
// logutil.Info(sql)

ddlStr, err := ConstructCreateTableSQL(tableDef, *snapshot, ctx)
ddlStr, err := ConstructCreateTableSQL(tableObjRef, tableDef, *snapshot, ctx)
if err != nil {
return nil, err
}
Expand Down
22 changes: 15 additions & 7 deletions pkg/sql/plan/build_show_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/util"
"strings"
)

// ConstructCreateTableSQL used to build CREATE Table statement
func ConstructCreateTableSQL(tableDef *plan.TableDef, snapshot Snapshot, ctx CompilerContext) (string, error) {
func ConstructCreateTableSQL(tableObjRef *plan.ObjectRef, tableDef *plan.TableDef, snapshot Snapshot, ctx CompilerContext) (string, error) {
var err error
var createStr string

Expand Down Expand Up @@ -198,15 +199,16 @@ func ConstructCreateTableSQL(tableDef *plan.TableDef, snapshot Snapshot, ctx Com
}

var fkTableDef *TableDef

var fkTableObjRef *ObjectRef
//fk self reference
if fk.ForeignTbl == 0 {
fkTableDef = tableDef
fkTableObjRef = tableObjRef
} else {
if ctx.GetQueryingSubscription() != nil {
_, fkTableDef = ctx.ResolveSubscriptionTableById(fk.ForeignTbl, ctx.GetQueryingSubscription())
fkTableObjRef, fkTableDef = ctx.ResolveSubscriptionTableById(fk.ForeignTbl, ctx.GetQueryingSubscription())
} else {
_, fkTableDef = ctx.ResolveById(fk.ForeignTbl, snapshot)
fkTableObjRef, fkTableDef = ctx.ResolveById(fk.ForeignTbl, snapshot)
}
}

Expand All @@ -222,8 +224,14 @@ func ConstructCreateTableSQL(tableDef *plan.TableDef, snapshot Snapshot, ctx Com
if rowCount != 0 {
createStr += ",\n"
}
createStr += fmt.Sprintf(" CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`) ON DELETE %s ON UPDATE %s",
formatStr(fk.Name), strings.Join(colNames, "`,`"), formatStr(fkTableDef.Name), strings.Join(fkColNames, "`,`"), fk.OnDelete.String(), fk.OnUpdate.String())

if tableObjRef.SchemaName == fkTableObjRef.SchemaName {
createStr += fmt.Sprintf(" CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`) ON DELETE %s ON UPDATE %s",
formatStr(fk.Name), strings.Join(colNames, "`,`"), formatStr(fkTableDef.Name), strings.Join(fkColNames, "`,`"), fk.OnDelete.String(), fk.OnUpdate.String())
} else {
createStr += fmt.Sprintf(" CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s`.`%s` (`%s`) ON DELETE %s ON UPDATE %s",
formatStr(fk.Name), strings.Join(colNames, "`,`"), formatStr(fkTableObjRef.SchemaName), formatStr(fkTableDef.Name), strings.Join(fkColNames, "`,`"), fk.OnDelete.String(), fk.OnUpdate.String())
}
}

if rowCount != 0 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/plan/build_show_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package plan

import (
"testing"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect/mysql"
"testing"
)

func Test_buildTestShowCreateTable(t *testing.T) {
Expand Down Expand Up @@ -188,7 +189,7 @@ func buildTestShowCreateTable(sql string) (string, error) {
}

snapshot := Snapshot{TS: &timestamp.Timestamp{}}
showSQL, err := ConstructCreateTableSQL(tableDef, snapshot, &mock.ctxt)
showSQL, err := ConstructCreateTableSQL(nil, tableDef, snapshot, &mock.ctxt)
if err != nil {
return "", err
}
Expand Down
Binary file modified test/distributed/cases/table/create_table.result
Binary file not shown.
58 changes: 57 additions & 1 deletion test/distributed/cases/table/create_table.test
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,60 @@ create database test_creatsql;
use test_creatsql;
/*comments*/create table /*comments*/ t(a int)/*comments*/;
select rel_createsql from mo_catalog.mo_tables where relname = 't' and reldatabase = 'test_creatsql';
drop database test_creatsql;
drop database test_creatsql;

create database test03;
create database test04;
create database test05;

use test03;
drop table if exists departments;
create table departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);

insert into departments (department_id, department_name)
values (1, 'HR'),
(2, 'Engineering');

use test04;
drop table if exists employees;
create table employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES test03.departments(department_id)
);

insert into employees values
(1, 'John', 'Doe', 1),
(2, 'Jane', 'Smith', 2),
(3, 'Bob', 'Johnson', 1);

use test04;
drop view if exists employee_view;
create view employee_view as select employee_id, first_name, last_name, department_id from test04.employees;
select * from employee_view;

use test03;
drop view if exists department_view;
create view department_view as select department_id, department_name from test03.departments;
select * from department_view;

use test05;
drop view if exists employee_with_department_view;
create view employee_with_department_view as
select e.employee_id, e.first_name, e.last_name, d.department_name
from test04.employee_view e join test03.department_view d on e.department_id = d.department_id;
select * from employee_with_department_view;

-- @ignore:1
show create table test04.employees;
show create table test04.employee_view;
show create table test05.employee_with_department_view;

drop database test04;
drop database test03;
drop database test05;

0 comments on commit 8ce356a

Please sign in to comment.