From 8ce356ade0efabbd895138d6520b3d0162536f75 Mon Sep 17 00:00:00 2001 From: YANGGMM Date: Thu, 23 May 2024 18:44:06 +0800 Subject: [PATCH] fix show create table with fk in other database (#1.2-dev) (#16349) fix show create table with fk in other database Approved by: @ouyuanning, @heni02, @aunjgr, @sukki37 --- pkg/sql/plan/build_show.go | 4 +- pkg/sql/plan/build_show_util.go | 22 ++++--- pkg/sql/plan/build_show_util_test.go | 5 +- .../cases/table/create_table.result | Bin 7821 -> 10658 bytes .../distributed/cases/table/create_table.test | 58 +++++++++++++++++- 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/pkg/sql/plan/build_show.go b/pkg/sql/plan/build_show.go index 44fca4abacb8..e13c36e373b6 100644 --- a/pkg/sql/plan/build_show.go +++ b/pkg/sql/plan/build_show.go @@ -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) } @@ -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 } diff --git a/pkg/sql/plan/build_show_util.go b/pkg/sql/plan/build_show_util.go index 2a6e4939c27a..7abc258d6554 100644 --- a/pkg/sql/plan/build_show_util.go +++ b/pkg/sql/plan/build_show_util.go @@ -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 @@ -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) } } @@ -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 { diff --git a/pkg/sql/plan/build_show_util_test.go b/pkg/sql/plan/build_show_util_test.go index ee908361af49..81e35030e235 100644 --- a/pkg/sql/plan/build_show_util_test.go +++ b/pkg/sql/plan/build_show_util_test.go @@ -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) { @@ -188,7 +189,7 @@ func buildTestShowCreateTable(sql string) (string, error) { } snapshot := Snapshot{TS: ×tamp.Timestamp{}} - showSQL, err := ConstructCreateTableSQL(tableDef, snapshot, &mock.ctxt) + showSQL, err := ConstructCreateTableSQL(nil, tableDef, snapshot, &mock.ctxt) if err != nil { return "", err } diff --git a/test/distributed/cases/table/create_table.result b/test/distributed/cases/table/create_table.result index 3c0de914a249c167ac5299fdc7ae94352778a260..61c233b1a7d88ea727441d7152d7a1c92956dd88 100644 GIT binary patch literal 10658 zcmds7&2rnw5zZM;F$YzVbip#gKU-1hAWPI*yIg8RQCp=dSCIo?Bq1RIE&;BkXUI7Z zu(!SClvhgaW8~`@3@`ve(lV8l9UKTu&tLb~Jv}`=^p=7rf+gIYrN2$5&e`~KczZb< zv&Ax)^W_u!n|QLB%dUSs9Sn$mefiz6e{M66QR!xwmVNgA@;jo0QNqI2Y{qOR*u~)L zbZpxkjaTbIL2_VP7#6~AW@*d5EmSwT^ve9oGHy-=4~paaU6z*lD%>RRo}ue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|Cz^ zfdE)7ZQ*ja%W_VwF0MWF8OyCTDc_?aBzVBRqaz)X$ytgqS<`KTAfIBQ%1LustWM!W zFgDh zq<;Y-O@>u3WU>=CZt1O?Z7t-MsdJWH1lxHXSiOw zGxb@fgRg;gvGC(y2_zgDP#TBodwt%}o)FPPk&+(n(2;2Iu`V#43Yc zr+&1py`iie4M{{+oVz^6O;R4F;{c$pYT{pJnI^M_lv6RPSLj|U$oUkUxTSdpN&!&q8gQEdjlP(q!fq3aLw+qrQxH#hL&<|_+r7A3FQUKtIDXs z!pch%SSp=tj4;+Vt0h$0IGtuwMzLl}Hyy^ma?Q@1#xo=z34zXsxyv6S`H6QXBEC>s^3 z7?1V+MMfuW=b+jw1F~QQ058A!>m2IdP-Ahc)fFixSy6e_d>(Ro*-TW(afaGe_M9-F zDnLiCG>=ZmQBfjCAL)%FfTL0h9DO>uIqFqc!p)d;9l27aft^uPu~rPA3g%OW${0mW zZV(h)27wG-q4Hs*ZoQ0^m&*xHBbQxC&P)w-13fW@U6*=mQ(&O=EixOB{z4q)y2T%C z4l~`tii|ZR7p_%hNz!2O&z!$TbO%~qd#ct)aE`GJ{(;j;ae}}vogvp!#>8pilrhb!bV^JUpt;hi zLuSl!2=cT)1vfg}Pp?#vLd)RU)zE-W&$%|5v5(=bVe+U_OJ`TZ>+$H6w!CT6d3Seo z*YjU@yk6(^+atI0+V#BVYtQQ*Iq!Pip5x-qqTuPLMIH*ieyR*^dT2C?DXmrRb2S7C zgFf<3qdu6qJR38RZhn%fC{3bl;7&!R4)SMm|GFxhrp+($C4g2*r{kxC=Hx z(ajx5wO7(uya+T^QxNNwP*pbO@kji5M*a(C58wAO{4YDd-TEk=2c+feXssBw>P@pq H#TN4)_!iH> delta 7 OcmZ1!+-tj`R}KITWCI%j diff --git a/test/distributed/cases/table/create_table.test b/test/distributed/cases/table/create_table.test index 713a6014adf9..56f1587124ae 100755 --- a/test/distributed/cases/table/create_table.test +++ b/test/distributed/cases/table/create_table.test @@ -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; \ No newline at end of file +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;