github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/table_function/current_account.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package table_function
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func currentAccountPrepare(proc *process.Process, arg *Argument) error {
    26  	if len(arg.Args) > 0 {
    27  		return moerr.NewInvalidInput(proc.Ctx, "current_account: no argument is required")
    28  	}
    29  	return nil
    30  }
    31  
    32  func getAccountName(proc *process.Process) *vector.Vector {
    33  	return vector.NewConstString(
    34  		types.Type{Oid: types.T_varchar, Width: types.MaxVarcharLen},
    35  		1,
    36  		proc.SessionInfo.Account,
    37  		proc.Mp(),
    38  	)
    39  }
    40  
    41  func getRoleName(proc *process.Process) *vector.Vector {
    42  	return vector.NewConstString(
    43  		types.Type{Oid: types.T_varchar, Width: types.MaxVarcharLen},
    44  		1,
    45  		proc.SessionInfo.Role,
    46  		proc.Mp(),
    47  	)
    48  }
    49  
    50  func getUserName(proc *process.Process) *vector.Vector {
    51  	return vector.NewConstString(
    52  		types.Type{Oid: types.T_varchar, Width: types.MaxVarcharLen},
    53  		1,
    54  		proc.SessionInfo.User,
    55  		proc.Mp(),
    56  	)
    57  }
    58  
    59  func getAccountId(proc *process.Process) *vector.Vector {
    60  	return vector.NewConstFixed(types.Type{Oid: types.T_uint32}, 1, proc.SessionInfo.AccountId, proc.Mp())
    61  }
    62  
    63  func getRoleId(proc *process.Process) *vector.Vector {
    64  	return vector.NewConstFixed(types.Type{Oid: types.T_uint32}, 1, proc.SessionInfo.RoleId, proc.Mp())
    65  }
    66  
    67  func getUserId(proc *process.Process) *vector.Vector {
    68  	return vector.NewConstFixed(types.Type{Oid: types.T_uint32}, 1, proc.SessionInfo.UserId, proc.Mp())
    69  }
    70  
    71  func currentAccountCall(_ int, proc *process.Process, arg *Argument) (bool, error) {
    72  	rbat := batch.New(false, arg.Attrs)
    73  	for i, attr := range arg.Attrs {
    74  		switch attr {
    75  		case "account_name":
    76  			rbat.Vecs[i] = getAccountName(proc)
    77  		case "account_id":
    78  			rbat.Vecs[i] = getAccountId(proc)
    79  		case "user_name":
    80  			rbat.Vecs[i] = getUserName(proc)
    81  		case "user_id":
    82  			rbat.Vecs[i] = getUserId(proc)
    83  		case "role_name":
    84  			rbat.Vecs[i] = getRoleName(proc)
    85  		case "role_id":
    86  			rbat.Vecs[i] = getRoleId(proc)
    87  		default:
    88  			return false, moerr.NewInvalidInput(proc.Ctx, "%v is not supported by current_account()", attr)
    89  		}
    90  	}
    91  	rbat.InitZsOne(1)
    92  	proc.SetInputBatch(rbat)
    93  	return true, nil
    94  }