Skip to content

Commit

Permalink
fix create view with hint(#2.0-dev) (#19978)
Browse files Browse the repository at this point in the history
fix create view with hint

Approved by: @heni02, @ouyuanning, @sukki37, @aunjgr
  • Loading branch information
YANGGMM authored Nov 12, 2024
1 parent 109d4ca commit 1e62f05
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/sql/plan/build_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func genViewTableDef(ctx CompilerContext, stmt *tree.Select) (*plan.TableDef, er

// Check alter and change the viewsql.
viewSql := ctx.GetRootSql()
// remove sql hint
viewSql = cleanHint(viewSql)
if len(viewSql) != 0 {
if viewSql[0] == 'A' {
viewSql = strings.Replace(viewSql, "ALTER", "CREATE", 1)
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/plan/build_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package plan
import (
"context"
"fmt"
"regexp"
"strings"

"github.com/matrixorigin/matrixone/pkg/catalog"
Expand Down Expand Up @@ -745,3 +746,9 @@ func getPartColsFromExpr(expr *Expr, colNameMap map[string]bool) {
}
}
}

func cleanHint(originSql string) string {
re := regexp.MustCompile(`/\*[^!].*?\*/`)
cleanSQL := re.ReplaceAllString(originSql, "")
return cleanSQL
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Table Create Table
test_table CREATE TABLE `test_table` (\n `col1` int DEFAULT NULL,\n `col2` varchar(65535) DEFAULT NULL\n)
/* cloud_user */ show create view test_view;
View Create View character_set_client collation_connection
test_view /* cloud_user */ create view test_view as select * from test_table; utf8mb4 utf8mb4_general_ci
test_view create view test_view as select * from test_table; utf8mb4 utf8mb4_general_ci
/* cloud_user */ show triggers;
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
/* cloud_user */ show procedure status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Table Create Table
test_table CREATE TABLE `test_table` (\n `col1` int DEFAULT NULL,\n `col2` varchar(65535) DEFAULT NULL\n)
/* cloud_nonuser */ show create view test_view;
View Create View character_set_client collation_connection
test_view /* cloud_nonuser */ create view test_view as select * from test_table; utf8mb4 utf8mb4_general_ci
test_view create view test_view as select * from test_table; utf8mb4 utf8mb4_general_ci
/* cloud_nonuser */ show triggers;
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
/* cloud_nonuser */ show procedure status;
Expand Down
23 changes: 23 additions & 0 deletions test/distributed/cases/view/view.result
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,26 @@ drop table t1;
drop table t2;
drop view v1;
drop database test;
drop database if exists test;
create database test;
use test;
drop table if exists table01;
/* cloud_user */create table table01 (col1 int, col2 decimal(6), col3 varchar(30));
insert into table01 values (1, null, 'database');
insert into table01 values (2, 38291.32132, 'database');
insert into table01 values (3, null, 'database management system');
insert into table01 values (4, 10, null);
insert into table01 values (1, -321.321, null);
insert into table01 values (2, -1, null);
select count(*) from table01;
count(*)
6
show create table table01;
Table Create Table
table01 CREATE TABLE `table01` (\n `col1` int DEFAULT NULL,\n `col2` decimal(6,0) DEFAULT NULL,\n `col3` varchar(30) DEFAULT NULL\n)
drop view if exists v01;
/* cloud_user */create view v01 as select * from table01;
show create view v01;
View Create View character_set_client collation_connection
v01 create view v01 as select * from table01; utf8mb4 utf8mb4_general_ci
drop database if exists test;
21 changes: 20 additions & 1 deletion test/distributed/cases/view/view.test
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ desc v1;
drop table t1;
drop table t2;
drop view v1;
drop database test;
drop database test;

drop database if exists test;
create database test;
use test;
drop table if exists table01;
/* cloud_user */create table table01 (col1 int, col2 decimal(6), col3 varchar(30));
insert into table01 values (1, null, 'database');
insert into table01 values (2, 38291.32132, 'database');
insert into table01 values (3, null, 'database management system');
insert into table01 values (4, 10, null);
insert into table01 values (1, -321.321, null);
insert into table01 values (2, -1, null);
select count(*) from table01;
show create table table01;

drop view if exists v01;
/* cloud_user */create view v01 as select * from table01;
show create view v01;
drop database if exists test;

0 comments on commit 1e62f05

Please sign in to comment.