Skip to content

Commit

Permalink
add parser support for udf (#6968)
Browse files Browse the repository at this point in the history
add parser support for user-defined function grammar /  query logic handling

Approved by: @daviszhen, @fengttt, @heni02, @iamlinjunhong
  • Loading branch information
chrisxu333 committed Dec 20, 2022
1 parent cd15bd4 commit c7400a0
Show file tree
Hide file tree
Showing 11 changed files with 7,382 additions and 6,437 deletions.
16 changes: 16 additions & 0 deletions pkg/frontend/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,10 @@ handleFailed:
return err
}

func doDropFunction(ctx context.Context, ses *Session, df *tree.DropFunction) error {
return nil
}

// doRevokePrivilege accomplishes the RevokePrivilege statement
func doRevokePrivilege(ctx context.Context, ses *Session, rp *tree.RevokePrivilege) error {
var err error
Expand Down Expand Up @@ -3331,6 +3335,10 @@ func determinePrivilegeSetOfStatement(stmt tree.Statement) *privilege {
if st.Name != nil {
dbName = string(st.Name.SchemaName)
}
case *tree.CreateFunction:
objType = objectTypeDatabase
typs = append(typs, PrivilegeTypeCreateView, PrivilegeTypeDatabaseAll, PrivilegeTypeDatabaseOwnership)
writeDatabaseAndTableDirectly = true
case *tree.DropTable:
objType = objectTypeDatabase
typs = append(typs, PrivilegeTypeDropTable, PrivilegeTypeDropObject, PrivilegeTypeDatabaseAll, PrivilegeTypeDatabaseOwnership)
Expand All @@ -3345,6 +3353,10 @@ func determinePrivilegeSetOfStatement(stmt tree.Statement) *privilege {
if len(st.Names) != 0 {
dbName = string(st.Names[0].SchemaName)
}
case *tree.DropFunction:
objType = objectTypeDatabase
typs = append(typs, PrivilegeTypeCreateView, PrivilegeTypeDatabaseAll, PrivilegeTypeDatabaseOwnership)
writeDatabaseAndTableDirectly = true
case *tree.Select, *tree.Do:
objType = objectTypeTable
typs = append(typs, PrivilegeTypeSelect, PrivilegeTypeTableAll, PrivilegeTypeTableOwnership)
Expand Down Expand Up @@ -5549,3 +5561,7 @@ handleFailed:
}
return err
}

func InitFunction(ctx context.Context, ses *Session, tenant *TenantInfo, cf *tree.CreateFunction) error {
return nil
}
54 changes: 54 additions & 0 deletions pkg/frontend/authenticate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,39 @@ func Test_checkUserExistsOrNot(t *testing.T) {
})
}

func Test_initFunction(t *testing.T) {
convey.Convey("init function", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pu := config.NewParameterUnit(&config.FrontendParameters{}, nil, nil, nil, nil)
pu.SV.SetDefaultValues()

ctx := context.WithValue(context.TODO(), config.ParameterUnitKey, pu)

cu := &tree.CreateFunction{
Name: tree.NewFuncName("testFunc"),
Args: nil,
ReturnType: tree.NewReturnType("int"),
Body: "",
Language: "sql",
}

tenant := &TenantInfo{
Tenant: sysAccountName,
User: rootName,
DefaultRole: moAdminRoleName,
TenantID: sysAccountID,
UserID: rootID,
DefaultRoleID: moAdminRoleID,
}

ses := &Session{}
err := InitFunction(ctx, ses, tenant, cu)
convey.So(err, convey.ShouldBeNil)
})
}

func Test_initUser(t *testing.T) {
convey.Convey("init user", t, func() {
ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -5452,6 +5485,27 @@ func Test_doRevokePrivilege(t *testing.T) {
})
}

func Test_doDropFunction(t *testing.T) {
convey.Convey("drop function", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pu := config.NewParameterUnit(&config.FrontendParameters{}, nil, nil, nil, nil)
pu.SV.SetDefaultValues()

ctx := context.WithValue(context.TODO(), config.ParameterUnitKey, pu)

cu := &tree.DropFunction{
Name: tree.NewFuncName("testFunc"),
Args: nil,
}

ses := &Session{}
err := doDropFunction(ctx, ses, cu)
convey.So(err, convey.ShouldBeNil)
})
}

func Test_doDropRole(t *testing.T) {
convey.Convey("drop role succ", t, func() {
ctrl := gomock.NewController(t)
Expand Down
22 changes: 22 additions & 0 deletions pkg/frontend/mysql_cmd_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,17 @@ func (mce *MysqlCmdExecutor) handleDropRole(ctx context.Context, dr *tree.DropRo
return doDropRole(ctx, mce.GetSession(), dr)
}

func (mce *MysqlCmdExecutor) handleCreateFunction(ctx context.Context, cf *tree.CreateFunction) error {
ses := mce.GetSession()
tenant := ses.GetTenantInfo()

return InitFunction(ctx, ses, tenant, cf)
}

func (mce *MysqlCmdExecutor) handleDropFunction(ctx context.Context, df *tree.DropFunction) error {
return doDropFunction(ctx, mce.GetSession(), df)
}

// handleGrantRole grants the role
func (mce *MysqlCmdExecutor) handleGrantRole(ctx context.Context, gr *tree.GrantRole) error {
return doGrantRole(ctx, mce.GetSession(), gr)
Expand Down Expand Up @@ -3383,6 +3394,16 @@ func (mce *MysqlCmdExecutor) doComQuery(requestCtx context.Context, sql string)
if err = mce.handleDropRole(requestCtx, st); err != nil {
goto handleFailed
}
case *tree.CreateFunction:
selfHandle = true
if err = mce.handleCreateFunction(requestCtx, st); err != nil {
goto handleFailed
}
case *tree.DropFunction:
selfHandle = true
if err = mce.handleDropFunction(requestCtx, st); err != nil {
goto handleFailed
}
case *tree.Grant:
selfHandle = true
ses.InvalidatePrivilegeCache()
Expand Down Expand Up @@ -3704,6 +3725,7 @@ func (mce *MysqlCmdExecutor) doComQuery(requestCtx context.Context, sql string)
*tree.CreateIndex, *tree.DropIndex, *tree.Insert, *tree.Update,
*tree.CreateView, *tree.DropView, *tree.Load, *tree.MoDump,
*tree.CreateAccount, *tree.DropAccount, *tree.AlterAccount,
*tree.CreateFunction, *tree.DropFunction,
*tree.CreateUser, *tree.DropUser, *tree.AlterUser,
*tree.CreateRole, *tree.DropRole, *tree.Revoke, *tree.Grant,
*tree.SetDefaultRole, *tree.SetRole, *tree.SetPassword, *tree.Delete, *tree.TruncateTable, *tree.Use,
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/parsers/dialect/mysql/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,5 +526,7 @@ func init() {
"preceding": PRECEDING,
"following": FOLLOWING,
"groups": GROUPS,
"returns": RETURNS,
"extension": EXTENSION,
}
}
Loading

0 comments on commit c7400a0

Please sign in to comment.