Skip to content

Commit

Permalink
feature: support show database rule from (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lvnszn authored Jun 10, 2023
1 parent 3143c33 commit 9c7357c
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/appleboy/gin-jwt/v2 v2.9.1
github.com/arana-db/parser v0.2.15
github.com/arana-db/parser v0.2.16
github.com/blang/semver v3.5.1+incompatible
github.com/bwmarrin/snowflake v0.3.0
github.com/cespare/xxhash/v2 v2.1.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ github.com/appleboy/gin-jwt/v2 v2.9.1 h1:l29et8iLW6omcHltsOP6LLk4s3v4g2FbFs0koxG
github.com/appleboy/gin-jwt/v2 v2.9.1/go.mod h1:jwcPZJ92uoC9nOUTOKWoN/f6JZOgMSKlFSHw5/FrRUk=
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
github.com/arana-db/parser v0.2.15 h1:F8eDqISje/6BDWckdKJQnDDYXttJh97i4If63NyFsvg=
github.com/arana-db/parser v0.2.15/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/arana-db/parser v0.2.16 h1:tBUny7dWSwvINTzdxPdYX7BGZ9TWmJWn9juJL80TSJs=
github.com/arana-db/parser v0.2.16/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down
1 change: 1 addition & 0 deletions pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func buildNamespace(ctx context.Context, tenant string, provider Discovery, clus
}
ru.SetVTable(table, vt)
}

initCmds = append(initCmds, namespace.UpdateRule(&ru))

return namespace.New(clusterName, initCmds...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/boot/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ func (d *watcher) onClusterAdd(ctx context.Context, cluster *config.DataSourceCl
if err != nil {
return errors.WithStack(err)
}

clonedNode := *node
nodeParams := make(config.ParametersMap)
nodeParams.Merge(node.Parameters)
Expand Down Expand Up @@ -542,7 +543,6 @@ func (d *watcher) onTableAdd(_ context.Context, table *config.Table) error {
}

ns.Rule().SetVTable(tbl, vtab)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (executor *RedirectExecutor) doExecutorComQuery(ctx *proto.Context, act ast
switch stmt.Tp {
case ast.ShowDatabases, ast.ShowVariables, ast.ShowTopology, ast.ShowStatus, ast.ShowTableStatus,
ast.ShowWarnings, ast.ShowCharset, ast.ShowMasterStatus, ast.ShowProcessList, ast.ShowReplicas, ast.ShowShardingTable,
ast.ShowReplicaStatus, ast.ShowNodes, ast.ShowUsers, ast.ShowCreateSequence:
ast.ShowReplicaStatus, ast.ShowNodes, ast.ShowUsers, ast.ShowCreateSequence, ast.ShowDatabaseRules:
return true
default:
return false
Expand Down
8 changes: 8 additions & 0 deletions pkg/mysql/thead/thead.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ var (
Col{Name: "tbl_rules", FieldType: consts.FieldTypeVarString},
Col{Name: "attributes", FieldType: consts.FieldTypeVarString},
}

DBRule = Thead{
Col{Name: "table_name", FieldType: consts.FieldTypeVarString},
Col{Name: "column", FieldType: consts.FieldTypeVarString},
Col{Name: "type", FieldType: consts.FieldTypeVarString},
Col{Name: "expr", FieldType: consts.FieldTypeVarString},
Col{Name: "step", FieldType: consts.FieldTypeInt24},
}
)

type Col struct {
Expand Down
11 changes: 11 additions & 0 deletions pkg/proto/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ func (ru *Rule) VTables() map[string]*VTable {
return ru.vtabs
}

// DBRule returns all the database rule
func (ru *Rule) DBRule() map[string][]*RawShardRule {
ru.mu.RLock()
defer ru.mu.RUnlock()
allDBRule := make(map[string][]*RawShardRule, 10)
for _, tb := range ru.vtabs {
allDBRule[tb.name] = tb.rawShards.DbRules
}
return allDBRule
}

// MustVTable returns the VTable with given table name, panic if not exist.
func (ru *Rule) MustVTable(name string) *VTable {
v, ok := ru.VTable(name)
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ func (cc *convCtx) convShowStmt(node *ast.ShowStmt) Statement {
pattern.String = like
}
return &ShowDatabases{BaseShowWithSingleColumn: &BaseShowWithSingleColumn{toBaseShow(), pattern}}
case ast.ShowDatabaseRules:
return &ShowDatabaseRule{BaseShow: toBaseShow(), Database: node.DBName, TableName: node.Table.Name.String()}
case ast.ShowCollation:
return &ShowCollation{BaseShow: toBaseShow()}
case ast.ShowCreateTable:
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/ast/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
SQLTypeRenameTable // RENAME TABLE
SQLTypeRepairTable // REPAIR TABLE
SQLTypeCreateTable // CREATE TABLE
SQLTypeShowDatabaseRules // SHOW DATABASE RULES
)

var _sqlTypeNames = [...]string{
Expand Down Expand Up @@ -104,6 +105,7 @@ var _sqlTypeNames = [...]string{
SQLTypeRenameTable: "RENAME TABLE",
SQLTypeRepairTable: "REPAIR TABLE",
SQLTypeCreateTable: "CREATE TABLE",
SQLTypeShowDatabaseRules: "SHOW DATABASE RULES",
}

// SQLType represents the type of SQL.
Expand Down
29 changes: 29 additions & 0 deletions pkg/runtime/ast/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
_ Statement = (*ShowWarnings)(nil)
_ Statement = (*ShowMasterStatus)(nil)
_ Statement = (*ShowReplicaStatus)(nil)
_ Statement = (*ShowDatabaseRule)(nil)
)

var TruePredicate = func(row proto.Row) bool { return true }
Expand All @@ -53,6 +54,12 @@ func (f FromTable) String() string {
return string(f)
}

type FromDatabase string

func (f FromDatabase) String() string {
return string(f)
}

type BaseShow struct {
filter interface{} // ExpressionNode or string
}
Expand All @@ -66,6 +73,10 @@ func (bs *BaseShow) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int)
case FromTable:
sb.WriteString(val.String())
return nil
case FromDatabase:
sb.WriteString(" FROM ")
sb.WriteString(val.String())
return nil
case PredicateNode:
return val.Restore(flag, sb, nil)
case ExpressionNode:
Expand Down Expand Up @@ -651,3 +662,21 @@ func (s *ShowCreateSequence) Restore(flag RestoreFlag, sb *strings.Builder, args
}
return nil
}

type ShowDatabaseRule struct {
*BaseShow
Database string
TableName string
}

func (s ShowDatabaseRule) Mode() SQLType {
return SQLTypeShowDatabaseRules
}

func (s ShowDatabaseRule) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
sb.WriteString("SHOW DATABASE RULES ")
if err := s.BaseShow.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
return nil
}
42 changes: 42 additions & 0 deletions pkg/runtime/optimize/dal/show_database_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dal

import (
"context"
)

import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
"github.com/arana-db/arana/pkg/runtime/optimize"
"github.com/arana-db/arana/pkg/runtime/plan/dal"
)

func init() {
optimize.Register(ast.SQLTypeShowDatabaseRules, optimizeShowDatabaseRules)
}

func optimizeShowDatabaseRules(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
rule := o.Rule
stmt := o.Stmt.(*ast.ShowDatabaseRule)
ret := dal.NewShowDatabaseRulesPlan(stmt)
ret.BindArgs(o.Args)
ret.SetRule(rule)
return ret, nil
}
79 changes: 79 additions & 0 deletions pkg/runtime/plan/dal/show_database_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dal

import (
"context"
"github.com/arana-db/arana/pkg/mysql/rows"
"github.com/arana-db/arana/pkg/proto/rule"
)

import (
"github.com/arana-db/arana/pkg/dataset"
"github.com/arana-db/arana/pkg/mysql/thead"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/resultx"
"github.com/arana-db/arana/pkg/runtime/ast"
"github.com/arana-db/arana/pkg/runtime/plan"
)

var _ proto.Plan = (*ShowDatabaseRulesPlan)(nil)

type ShowDatabaseRulesPlan struct {
plan.BasePlan
Stmt *ast.ShowDatabaseRule
rule *rule.Rule
}

func (s *ShowDatabaseRulesPlan) Type() proto.PlanType {
return proto.PlanTypeQuery
}

func (s *ShowDatabaseRulesPlan) ExecIn(ctx context.Context, _ proto.VConn) (proto.Result, error) {
_, span := plan.Tracer.Start(ctx, "ShowDatabaseRulesPlan.ExecIn")
defer span.End()

fields := thead.DBRule.ToFields()
ds := &dataset.VirtualDataset{
Columns: fields,
}

dbRules := s.rule.DBRule()
if rules, ok := dbRules[s.Stmt.TableName]; ok {
for _, ruleItem := range rules {
ds.Rows = append(ds.Rows, rows.NewTextVirtualRow(fields, []proto.Value{
proto.NewValueString(s.Stmt.TableName),
proto.NewValueString(ruleItem.Column), proto.NewValueString(ruleItem.Type),
proto.NewValueString(ruleItem.Expr), proto.NewValueInt64(int64(ruleItem.Step)),
}))
}
}

return resultx.New(resultx.WithDataset(ds)), nil
}

func (s *ShowDatabaseRulesPlan) SetRule(rule *rule.Rule) {
s.rule = rule
}

// NewShowDatabaseRulesPlan create ShowDatabaseRules Plan
func NewShowDatabaseRulesPlan(stmt *ast.ShowDatabaseRule) *ShowDatabaseRulesPlan {
return &ShowDatabaseRulesPlan{
Stmt: stmt,
}
}
37 changes: 37 additions & 0 deletions pkg/runtime/plan/dal/show_database_rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dal

import (
"testing"
)

import (
"github.com/arana-db/parser"
"github.com/stretchr/testify/assert"
)

func TestShowDatabaseRulesSQL(t *testing.T) {
sql := "SHOW DATABASE RULES FROM employees"

p := parser.New()

stmtNodes, _, err := p.Parse(sql, "", "")
assert.Nil(t, err)
assert.NotNil(t, stmtNodes)
}
33 changes: 33 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,39 @@ func (s *IntegrationSuite) TestShowShardingTable() {
assert.NoErrorf(t, err, "show sharding table from employees error: %v", err)
}

func (s *IntegrationSuite) TestShowDatabaseRules() {
var (
db = s.DB()
t = s.T()
)

tests := []struct {
name string
sql string
expectNum int
}{
{
name: "show database rules from employees",
sql: "show database rules from employees",
expectNum: 0,
},
{
name: "show database rules from student",
sql: "show database rules from student",
expectNum: 1,
},
}

for _, v := range tests {
rows, err := db.Query(v.sql)
defer rows.Close()
assert.NoErrorf(t, err, "show database rules error: %v", err)
results, err := utils.PrintTable(rows)
assert.NoErrorf(t, err, "show database rules error: %v", err)
assert.Equal(t, len(results), v.expectNum)
}
}

func (s *IntegrationSuite) TestDropTrigger() {
var (
db = s.DB()
Expand Down

0 comments on commit 9c7357c

Please sign in to comment.