github.com/matrixorigin/matrixone@v1.2.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"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  func currentAccountPrepare(proc *process.Process, arg *Argument) error {
    27  	arg.ctr.state = dataProducing
    28  	if len(arg.Args) > 0 {
    29  		return moerr.NewInvalidInput(proc.Ctx, "current_account: no argument is required")
    30  	}
    31  	return nil
    32  }
    33  
    34  func currentAccountCall(_ int, proc *process.Process, arg *Argument, result *vm.CallResult) (bool, error) {
    35  	var err error
    36  
    37  	switch arg.ctr.state {
    38  	case dataProducing:
    39  		rbat := batch.NewWithSize(len(arg.Attrs))
    40  		rbat.Attrs = arg.Attrs
    41  		for i, attr := range arg.Attrs {
    42  			switch attr {
    43  			case "account_name":
    44  				rbat.Vecs[i], err = vector.NewConstBytes(types.T_varchar.ToType(), []byte(proc.SessionInfo.Account), 1, proc.Mp())
    45  			case "account_id":
    46  				rbat.Vecs[i], err = vector.NewConstFixed(types.T_uint32.ToType(), proc.SessionInfo.AccountId, 1, proc.Mp())
    47  			case "user_name":
    48  				rbat.Vecs[i], err = vector.NewConstBytes(types.T_varchar.ToType(), []byte(proc.SessionInfo.User), 1, proc.Mp())
    49  			case "user_id":
    50  				rbat.Vecs[i], err = vector.NewConstFixed(types.T_uint32.ToType(), proc.SessionInfo.UserId, 1, proc.Mp())
    51  			case "role_name":
    52  				rbat.Vecs[i], err = vector.NewConstBytes(types.T_varchar.ToType(), []byte(proc.SessionInfo.Role), 1, proc.Mp())
    53  			case "role_id":
    54  				rbat.Vecs[i], err = vector.NewConstFixed(types.T_uint32.ToType(), proc.SessionInfo.RoleId, 1, proc.Mp())
    55  			default:
    56  				err = moerr.NewInvalidInput(proc.Ctx, "%v is not supported by current_account()", attr)
    57  			}
    58  			if err != nil {
    59  				return false, err
    60  			}
    61  		}
    62  		rbat.SetRowCount(1)
    63  		result.Batch = rbat
    64  		arg.ctr.state = dataFinished
    65  		return false, nil
    66  
    67  	case dataFinished:
    68  		result.Batch = nil
    69  		return true, nil
    70  	default:
    71  		return false, moerr.NewInternalError(proc.Ctx, "unknown state %v", arg.ctr.state)
    72  	}
    73  }