github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/udf.go (about)

     1  // Copyright 2023 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 function
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"io"
    21  	"time"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	"github.com/matrixorigin/matrixone/pkg/container/bytejson"
    26  	"github.com/matrixorigin/matrixone/pkg/container/types"
    27  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    28  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    29  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    30  	"github.com/matrixorigin/matrixone/pkg/udf"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    32  	"golang.org/x/sync/errgroup"
    33  )
    34  
    35  type Udf struct {
    36  	// sql string, or json string of NonSqlUdfBody
    37  	Body         string `json:"body"`
    38  	Language     string `json:"language"`
    39  	RetType      string `json:"rettype"`
    40  	Args         []*Arg `json:"args"`
    41  	Db           string `json:"db"`
    42  	ModifiedTime string `json:"modified_time"`
    43  
    44  	ArgsType []types.Type `json:"-"`
    45  }
    46  
    47  type UdfWithContext struct {
    48  	*Udf    `json:"udf"`
    49  	Context map[string]string `json:"context"` // context map
    50  }
    51  
    52  type NonSqlUdfBody struct {
    53  	Handler string `json:"handler"`
    54  	Import  bool   `json:"import"`
    55  	Body    string `json:"body"`
    56  }
    57  
    58  // Arg of Udf
    59  type Arg struct {
    60  	Name string `json:"name"`
    61  	Type string `json:"type"`
    62  }
    63  
    64  func (u *Udf) GetPlanExpr() *plan.Expr {
    65  	uid, _ := uuid.NewV7()
    66  	bytes, _ := json.Marshal(&UdfWithContext{
    67  		Udf: u,
    68  		Context: map[string]string{
    69  			"queryId": uid.String(),
    70  		},
    71  	})
    72  	return &plan.Expr{
    73  		Typ: *type2PlanType(types.T_text.ToType()),
    74  		Expr: &plan.Expr_Lit{
    75  			Lit: &plan.Literal{
    76  				Isnull: false,
    77  				Value: &plan.Literal_Sval{
    78  					Sval: string(bytes),
    79  				},
    80  			},
    81  		},
    82  	}
    83  }
    84  
    85  func (u *Udf) GetArgsPlanType() []*plan.Type {
    86  	typ := u.GetArgsType()
    87  	ptyp := make([]*plan.Type, len(typ))
    88  	for i, t := range typ {
    89  		ptyp[i] = type2PlanType(t)
    90  	}
    91  	return ptyp
    92  }
    93  
    94  func (u *Udf) GetRetPlanType() *plan.Type {
    95  	typ := u.GetRetType()
    96  	return type2PlanType(typ)
    97  }
    98  
    99  func (u *Udf) GetArgsType() []types.Type {
   100  	return u.ArgsType
   101  }
   102  
   103  func (u *Udf) GetRetType() types.Type {
   104  	return types.Types[u.RetType].ToType()
   105  }
   106  
   107  func type2PlanType(typ types.Type) *plan.Type {
   108  	return &plan.Type{
   109  		Id:    int32(typ.Oid),
   110  		Width: typ.Width,
   111  		Scale: typ.Scale,
   112  	}
   113  }
   114  
   115  func UdfArgTypeMatch(from []types.Type, to []types.T) (bool, int) {
   116  	sta, cost := tryToMatch(from, to)
   117  	return sta != matchFailed, cost
   118  }
   119  
   120  func UdfArgTypeCast(from []types.Type, to []types.T) []types.Type {
   121  	castType := make([]types.Type, len(from))
   122  	for i := range castType {
   123  		if to[i] == from[i].Oid {
   124  			castType[i] = from[i]
   125  		} else {
   126  			castType[i] = to[i].ToType()
   127  			setTargetScaleFromSource(&from[i], &castType[i])
   128  		}
   129  	}
   130  	return castType
   131  }
   132  
   133  func getDataFromDataVector(v *udf.DataVector, i int) *udf.Data {
   134  	if v == nil {
   135  		return nil
   136  	}
   137  	if v.Const {
   138  		return v.Data[0]
   139  	}
   140  	return v.Data[i]
   141  }
   142  
   143  func vector2DataVector(v *vector.Vector) (*udf.DataVector, error) {
   144  	if v == nil {
   145  		return nil, nil
   146  	}
   147  	dv := &udf.DataVector{
   148  		Const:  v.IsConst(),
   149  		Length: int64(v.Length()),
   150  		Type:   t2DataType[v.GetType().Oid],
   151  		Scale:  v.GetType().Scale,
   152  	}
   153  	size := v.Length()
   154  	if dv.Const {
   155  		size = 1
   156  	}
   157  	dv.Data = make([]*udf.Data, size)
   158  	switch v.GetType().Oid {
   159  	case types.T_bool:
   160  		p := vector.GenerateFunctionFixedTypeParameter[bool](v)
   161  		for i := 0; i < size; i++ {
   162  			val, isNull := p.GetValue(uint64(i))
   163  			if !isNull {
   164  				dv.Data[i] = &udf.Data{Val: &udf.Data_BoolVal{BoolVal: val}}
   165  			}
   166  		}
   167  	case types.T_int8:
   168  		p := vector.GenerateFunctionFixedTypeParameter[int8](v)
   169  		for i := 0; i < size; i++ {
   170  			val, isNull := p.GetValue(uint64(i))
   171  			if !isNull {
   172  				dv.Data[i] = &udf.Data{Val: &udf.Data_IntVal{IntVal: int32(val)}}
   173  			}
   174  		}
   175  	case types.T_int16:
   176  		p := vector.GenerateFunctionFixedTypeParameter[int16](v)
   177  		for i := 0; i < size; i++ {
   178  			val, isNull := p.GetValue(uint64(i))
   179  			if !isNull {
   180  				dv.Data[i] = &udf.Data{Val: &udf.Data_IntVal{IntVal: int32(val)}}
   181  			}
   182  		}
   183  	case types.T_int32:
   184  		p := vector.GenerateFunctionFixedTypeParameter[int32](v)
   185  		for i := 0; i < size; i++ {
   186  			val, isNull := p.GetValue(uint64(i))
   187  			if !isNull {
   188  				dv.Data[i] = &udf.Data{Val: &udf.Data_IntVal{IntVal: val}}
   189  			}
   190  		}
   191  	case types.T_int64:
   192  		p := vector.GenerateFunctionFixedTypeParameter[int64](v)
   193  		for i := 0; i < size; i++ {
   194  			val, isNull := p.GetValue(uint64(i))
   195  			if !isNull {
   196  				dv.Data[i] = &udf.Data{Val: &udf.Data_Int64Val{Int64Val: val}}
   197  			}
   198  		}
   199  	case types.T_uint8:
   200  		p := vector.GenerateFunctionFixedTypeParameter[uint8](v)
   201  		for i := 0; i < size; i++ {
   202  			val, isNull := p.GetValue(uint64(i))
   203  			if !isNull {
   204  				dv.Data[i] = &udf.Data{Val: &udf.Data_UintVal{UintVal: uint32(val)}}
   205  			}
   206  		}
   207  	case types.T_uint16:
   208  		p := vector.GenerateFunctionFixedTypeParameter[uint16](v)
   209  		for i := 0; i < size; i++ {
   210  			val, isNull := p.GetValue(uint64(i))
   211  			if !isNull {
   212  				dv.Data[i] = &udf.Data{Val: &udf.Data_UintVal{UintVal: uint32(val)}}
   213  			}
   214  		}
   215  	case types.T_uint32:
   216  		p := vector.GenerateFunctionFixedTypeParameter[uint32](v)
   217  		for i := 0; i < size; i++ {
   218  			val, isNull := p.GetValue(uint64(i))
   219  			if !isNull {
   220  				dv.Data[i] = &udf.Data{Val: &udf.Data_UintVal{UintVal: val}}
   221  			}
   222  		}
   223  	case types.T_uint64:
   224  		p := vector.GenerateFunctionFixedTypeParameter[uint64](v)
   225  		for i := 0; i < size; i++ {
   226  			val, isNull := p.GetValue(uint64(i))
   227  			if !isNull {
   228  				dv.Data[i] = &udf.Data{Val: &udf.Data_Uint64Val{Uint64Val: val}}
   229  			}
   230  		}
   231  	case types.T_float32:
   232  		p := vector.GenerateFunctionFixedTypeParameter[float32](v)
   233  		for i := 0; i < size; i++ {
   234  			val, isNull := p.GetValue(uint64(i))
   235  			if !isNull {
   236  				dv.Data[i] = &udf.Data{Val: &udf.Data_FloatVal{FloatVal: val}}
   237  			}
   238  		}
   239  	case types.T_float64:
   240  		p := vector.GenerateFunctionFixedTypeParameter[float64](v)
   241  		for i := 0; i < size; i++ {
   242  			val, isNull := p.GetValue(uint64(i))
   243  			if !isNull {
   244  				dv.Data[i] = &udf.Data{Val: &udf.Data_DoubleVal{DoubleVal: val}}
   245  			}
   246  		}
   247  	case types.T_char, types.T_varchar, types.T_text:
   248  		p := vector.GenerateFunctionStrParameter(v)
   249  		for i := 0; i < size; i++ {
   250  			val, isNull := p.GetStrValue(uint64(i))
   251  			if !isNull {
   252  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: string(val)}}
   253  			}
   254  		}
   255  	case types.T_json:
   256  		p := vector.GenerateFunctionStrParameter(v)
   257  		for i := 0; i < size; i++ {
   258  			val, isNull := p.GetStrValue(uint64(i))
   259  			if !isNull {
   260  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: types.DecodeJson(val).String()}}
   261  			}
   262  		}
   263  	case types.T_uuid:
   264  		p := vector.GenerateFunctionFixedTypeParameter[types.Uuid](v)
   265  		for i := 0; i < size; i++ {
   266  			val, isNull := p.GetValue(uint64(i))
   267  			if !isNull {
   268  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.ToString()}}
   269  			}
   270  		}
   271  	case types.T_time:
   272  		p := vector.GenerateFunctionFixedTypeParameter[types.Time](v)
   273  		for i := 0; i < size; i++ {
   274  			val, isNull := p.GetValue(uint64(i))
   275  			if !isNull {
   276  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.String2(v.GetType().Scale)}}
   277  			}
   278  		}
   279  	case types.T_date:
   280  		p := vector.GenerateFunctionFixedTypeParameter[types.Date](v)
   281  		for i := 0; i < size; i++ {
   282  			val, isNull := p.GetValue(uint64(i))
   283  			if !isNull {
   284  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.String()}}
   285  			}
   286  		}
   287  	case types.T_datetime:
   288  		p := vector.GenerateFunctionFixedTypeParameter[types.Datetime](v)
   289  		for i := 0; i < size; i++ {
   290  			val, isNull := p.GetValue(uint64(i))
   291  			if !isNull {
   292  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.String2(v.GetType().Scale)}}
   293  			}
   294  		}
   295  	case types.T_timestamp:
   296  		p := vector.GenerateFunctionFixedTypeParameter[types.Timestamp](v)
   297  		for i := 0; i < size; i++ {
   298  			val, isNull := p.GetValue(uint64(i))
   299  			if !isNull {
   300  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.String2(time.Local, v.GetType().Scale)}}
   301  			}
   302  		}
   303  	case types.T_decimal64:
   304  		p := vector.GenerateFunctionFixedTypeParameter[types.Decimal64](v)
   305  		for i := 0; i < size; i++ {
   306  			val, isNull := p.GetValue(uint64(i))
   307  			if !isNull {
   308  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.Format(v.GetType().Scale)}}
   309  			}
   310  		}
   311  	case types.T_decimal128:
   312  		p := vector.GenerateFunctionFixedTypeParameter[types.Decimal128](v)
   313  		for i := 0; i < size; i++ {
   314  			val, isNull := p.GetValue(uint64(i))
   315  			if !isNull {
   316  				dv.Data[i] = &udf.Data{Val: &udf.Data_StringVal{StringVal: val.Format(v.GetType().Scale)}}
   317  			}
   318  		}
   319  	case types.T_binary, types.T_varbinary, types.T_blob:
   320  		p := vector.GenerateFunctionStrParameter(v)
   321  		for i := 0; i < size; i++ {
   322  			val, isNull := p.GetStrValue(uint64(i))
   323  			if !isNull {
   324  				dv.Data[i] = &udf.Data{Val: &udf.Data_BytesVal{BytesVal: val}}
   325  			}
   326  		}
   327  	default:
   328  		return nil, moerr.NewInvalidArgNoCtx("python udf arg type", v.GetType().String())
   329  	}
   330  	return dv, nil
   331  }
   332  
   333  func writeResponse(response *udf.Response, result vector.FunctionResultWrapper) error {
   334  	var err error
   335  	retType := dataType2T[response.Vector.Type]
   336  	length := int(response.Vector.Length)
   337  	scale := response.Vector.Scale
   338  
   339  	result.GetResultVector().SetTypeScale(scale)
   340  
   341  	switch retType {
   342  	case types.T_bool:
   343  		res := vector.MustFunctionResult[bool](result)
   344  		for i := 0; i < length; i++ {
   345  			data := getDataFromDataVector(response.Vector, i)
   346  			if data == nil || data.Val == nil {
   347  				err = res.Append(false, true)
   348  			} else {
   349  				err = res.Append(data.GetBoolVal(), false)
   350  			}
   351  			if err != nil {
   352  				return err
   353  			}
   354  		}
   355  	case types.T_int8:
   356  		res := vector.MustFunctionResult[int8](result)
   357  		for i := 0; i < length; i++ {
   358  			data := getDataFromDataVector(response.Vector, i)
   359  			if data == nil || data.Val == nil {
   360  				err = res.Append(0, true)
   361  			} else {
   362  				err = res.Append(int8(data.GetIntVal()), false)
   363  			}
   364  			if err != nil {
   365  				return err
   366  			}
   367  		}
   368  	case types.T_int16:
   369  		res := vector.MustFunctionResult[int16](result)
   370  		for i := 0; i < length; i++ {
   371  			data := getDataFromDataVector(response.Vector, i)
   372  			if data == nil || data.Val == nil {
   373  				err = res.Append(0, true)
   374  			} else {
   375  				err = res.Append(int16(data.GetIntVal()), false)
   376  			}
   377  			if err != nil {
   378  				return err
   379  			}
   380  		}
   381  	case types.T_int32:
   382  		res := vector.MustFunctionResult[int32](result)
   383  		for i := 0; i < length; i++ {
   384  			data := getDataFromDataVector(response.Vector, i)
   385  			if data == nil || data.Val == nil {
   386  				err = res.Append(0, true)
   387  			} else {
   388  				err = res.Append(data.GetIntVal(), false)
   389  			}
   390  			if err != nil {
   391  				return err
   392  			}
   393  		}
   394  	case types.T_int64:
   395  		res := vector.MustFunctionResult[int64](result)
   396  		for i := 0; i < length; i++ {
   397  			data := getDataFromDataVector(response.Vector, i)
   398  			if data == nil || data.Val == nil {
   399  				err = res.Append(0, true)
   400  			} else {
   401  				err = res.Append(data.GetInt64Val(), false)
   402  			}
   403  			if err != nil {
   404  				return err
   405  			}
   406  		}
   407  	case types.T_uint8:
   408  		res := vector.MustFunctionResult[uint8](result)
   409  		for i := 0; i < length; i++ {
   410  			data := getDataFromDataVector(response.Vector, i)
   411  			if data == nil || data.Val == nil {
   412  				err = res.Append(0, true)
   413  			} else {
   414  				err = res.Append(uint8(data.GetUintVal()), false)
   415  			}
   416  			if err != nil {
   417  				return err
   418  			}
   419  		}
   420  	case types.T_uint16:
   421  		res := vector.MustFunctionResult[uint16](result)
   422  		for i := 0; i < length; i++ {
   423  			data := getDataFromDataVector(response.Vector, i)
   424  			if data == nil || data.Val == nil {
   425  				err = res.Append(0, true)
   426  			} else {
   427  				err = res.Append(uint16(data.GetUintVal()), false)
   428  			}
   429  			if err != nil {
   430  				return err
   431  			}
   432  		}
   433  	case types.T_uint32:
   434  		res := vector.MustFunctionResult[uint32](result)
   435  		for i := 0; i < length; i++ {
   436  			data := getDataFromDataVector(response.Vector, i)
   437  			if data == nil || data.Val == nil {
   438  				err = res.Append(0, true)
   439  			} else {
   440  				err = res.Append(data.GetUintVal(), false)
   441  			}
   442  			if err != nil {
   443  				return err
   444  			}
   445  		}
   446  	case types.T_uint64:
   447  		res := vector.MustFunctionResult[uint64](result)
   448  		for i := 0; i < length; i++ {
   449  			data := getDataFromDataVector(response.Vector, i)
   450  			if data == nil || data.Val == nil {
   451  				err = res.Append(0, true)
   452  			} else {
   453  				err = res.Append(data.GetUint64Val(), false)
   454  			}
   455  			if err != nil {
   456  				return err
   457  			}
   458  		}
   459  	case types.T_float32:
   460  		res := vector.MustFunctionResult[float32](result)
   461  		for i := 0; i < length; i++ {
   462  			data := getDataFromDataVector(response.Vector, i)
   463  			if data == nil || data.Val == nil {
   464  				err = res.Append(0, true)
   465  			} else {
   466  				err = res.Append(data.GetFloatVal(), false)
   467  			}
   468  			if err != nil {
   469  				return err
   470  			}
   471  		}
   472  	case types.T_float64:
   473  		res := vector.MustFunctionResult[float64](result)
   474  		for i := 0; i < length; i++ {
   475  			data := getDataFromDataVector(response.Vector, i)
   476  			if data == nil || data.Val == nil {
   477  				err = res.Append(0, true)
   478  			} else {
   479  				err = res.Append(data.GetDoubleVal(), false)
   480  			}
   481  			if err != nil {
   482  				return err
   483  			}
   484  		}
   485  	case types.T_char, types.T_varchar, types.T_text:
   486  		res := vector.MustFunctionResult[types.Varlena](result)
   487  		for i := 0; i < length; i++ {
   488  			data := getDataFromDataVector(response.Vector, i)
   489  			if data == nil || data.Val == nil {
   490  				err = res.AppendBytes(nil, true)
   491  			} else {
   492  				err = res.AppendBytes([]byte(data.GetStringVal()), false)
   493  			}
   494  			if err != nil {
   495  				return err
   496  			}
   497  		}
   498  	case types.T_json:
   499  		res := vector.MustFunctionResult[types.Varlena](result)
   500  		for i := 0; i < length; i++ {
   501  			data := getDataFromDataVector(response.Vector, i)
   502  			if data == nil || data.Val == nil {
   503  				err = res.AppendBytes(nil, true)
   504  			} else {
   505  				bytes, err2 := bytejson.ParseJsonByteFromString(data.GetStringVal())
   506  				if err2 != nil {
   507  					return err2
   508  				}
   509  				err = res.AppendBytes(bytes, false)
   510  			}
   511  			if err != nil {
   512  				return err
   513  			}
   514  		}
   515  	case types.T_uuid:
   516  		res := vector.MustFunctionResult[types.Uuid](result)
   517  		for i := 0; i < length; i++ {
   518  			data := getDataFromDataVector(response.Vector, i)
   519  			if data == nil || data.Val == nil {
   520  				err = res.AppendBytes(nil, true)
   521  			} else {
   522  				uuid, err2 := types.ParseUuid(data.GetStringVal())
   523  				if err2 != nil {
   524  					return err2
   525  				}
   526  				err = res.Append(uuid, false)
   527  			}
   528  			if err != nil {
   529  				return err
   530  			}
   531  		}
   532  	case types.T_time:
   533  		res := vector.MustFunctionResult[types.Time](result)
   534  		for i := 0; i < length; i++ {
   535  			data := getDataFromDataVector(response.Vector, i)
   536  			if data == nil || data.Val == nil {
   537  				err = res.AppendBytes(nil, true)
   538  			} else {
   539  				t, err2 := types.ParseTime(data.GetStringVal(), scale)
   540  				if err2 != nil {
   541  					return err2
   542  				}
   543  				err = res.Append(t, false)
   544  			}
   545  			if err != nil {
   546  				return err
   547  			}
   548  		}
   549  	case types.T_date:
   550  		res := vector.MustFunctionResult[types.Date](result)
   551  		for i := 0; i < length; i++ {
   552  			data := getDataFromDataVector(response.Vector, i)
   553  			if data == nil || data.Val == nil {
   554  				err = res.AppendBytes(nil, true)
   555  			} else {
   556  				d, err2 := types.ParseDateCast(data.GetStringVal())
   557  				if err2 != nil {
   558  					return err2
   559  				}
   560  				err = res.Append(d, false)
   561  			}
   562  			if err != nil {
   563  				return err
   564  			}
   565  		}
   566  	case types.T_datetime:
   567  		res := vector.MustFunctionResult[types.Datetime](result)
   568  		for i := 0; i < length; i++ {
   569  			data := getDataFromDataVector(response.Vector, i)
   570  			if data == nil || data.Val == nil {
   571  				err = res.AppendBytes(nil, true)
   572  			} else {
   573  				dt, err2 := types.ParseDatetime(data.GetStringVal(), scale)
   574  				if err2 != nil {
   575  					return err2
   576  				}
   577  				err = res.Append(dt, false)
   578  			}
   579  			if err != nil {
   580  				return err
   581  			}
   582  		}
   583  	case types.T_timestamp:
   584  		res := vector.MustFunctionResult[types.Timestamp](result)
   585  		for i := 0; i < length; i++ {
   586  			data := getDataFromDataVector(response.Vector, i)
   587  			if data == nil || data.Val == nil {
   588  				err = res.AppendBytes(nil, true)
   589  			} else {
   590  				ts, err2 := types.ParseTimestamp(time.Local, data.GetStringVal(), scale)
   591  				if err2 != nil {
   592  					return err2
   593  				}
   594  				err = res.Append(ts, false)
   595  			}
   596  			if err != nil {
   597  				return err
   598  			}
   599  		}
   600  	case types.T_decimal64, types.T_decimal128:
   601  		res := vector.MustFunctionResult[types.Decimal128](result)
   602  		for i := 0; i < length; i++ {
   603  			data := getDataFromDataVector(response.Vector, i)
   604  			if data == nil || data.Val == nil {
   605  				err = res.AppendBytes(nil, true)
   606  			} else {
   607  				decimal, err2 := types.ParseDecimal128(data.GetStringVal(), 38, scale)
   608  				if err2 != nil {
   609  					return err2
   610  				}
   611  				err = res.Append(decimal, false)
   612  			}
   613  			if err != nil {
   614  				return err
   615  			}
   616  		}
   617  	case types.T_binary, types.T_varbinary, types.T_blob:
   618  		res := vector.MustFunctionResult[types.Varlena](result)
   619  		for i := 0; i < length; i++ {
   620  			data := getDataFromDataVector(response.Vector, i)
   621  			if data == nil || data.Val == nil {
   622  				err = res.AppendBytes(nil, true)
   623  			} else {
   624  				err = res.AppendBytes(data.GetBytesVal(), false)
   625  			}
   626  			if err != nil {
   627  				return err
   628  			}
   629  		}
   630  	default:
   631  		return moerr.NewNotSupportedNoCtx("python udf return type: %v", retType.String())
   632  	}
   633  
   634  	return nil
   635  }
   636  
   637  var (
   638  	t2DataType = map[types.T]udf.DataType{
   639  		types.T_bool:       udf.DataType_BOOL,
   640  		types.T_int8:       udf.DataType_INT8,
   641  		types.T_int16:      udf.DataType_INT16,
   642  		types.T_int32:      udf.DataType_INT32,
   643  		types.T_int64:      udf.DataType_INT64,
   644  		types.T_uint8:      udf.DataType_UINT8,
   645  		types.T_uint16:     udf.DataType_UINT16,
   646  		types.T_uint32:     udf.DataType_UINT32,
   647  		types.T_uint64:     udf.DataType_UINT64,
   648  		types.T_float32:    udf.DataType_FLOAT32,
   649  		types.T_float64:    udf.DataType_FLOAT64,
   650  		types.T_char:       udf.DataType_CHAR,
   651  		types.T_varchar:    udf.DataType_VARCHAR,
   652  		types.T_text:       udf.DataType_TEXT,
   653  		types.T_json:       udf.DataType_JSON,
   654  		types.T_uuid:       udf.DataType_UUID,
   655  		types.T_time:       udf.DataType_TIME,
   656  		types.T_date:       udf.DataType_DATE,
   657  		types.T_datetime:   udf.DataType_DATETIME,
   658  		types.T_timestamp:  udf.DataType_TIMESTAMP,
   659  		types.T_decimal64:  udf.DataType_DECIMAL64,
   660  		types.T_decimal128: udf.DataType_DECIMAL128,
   661  		types.T_binary:     udf.DataType_BINARY,
   662  		types.T_varbinary:  udf.DataType_VARBINARY,
   663  		types.T_blob:       udf.DataType_BLOB,
   664  	}
   665  
   666  	dataType2T = map[udf.DataType]types.T{
   667  		udf.DataType_BOOL:       types.T_bool,
   668  		udf.DataType_INT8:       types.T_int8,
   669  		udf.DataType_INT16:      types.T_int16,
   670  		udf.DataType_INT32:      types.T_int32,
   671  		udf.DataType_INT64:      types.T_int64,
   672  		udf.DataType_UINT8:      types.T_uint8,
   673  		udf.DataType_UINT16:     types.T_uint16,
   674  		udf.DataType_UINT32:     types.T_uint32,
   675  		udf.DataType_UINT64:     types.T_uint64,
   676  		udf.DataType_FLOAT32:    types.T_float32,
   677  		udf.DataType_FLOAT64:    types.T_float64,
   678  		udf.DataType_CHAR:       types.T_char,
   679  		udf.DataType_VARCHAR:    types.T_varchar,
   680  		udf.DataType_TEXT:       types.T_text,
   681  		udf.DataType_JSON:       types.T_json,
   682  		udf.DataType_UUID:       types.T_uuid,
   683  		udf.DataType_TIME:       types.T_time,
   684  		udf.DataType_DATE:       types.T_date,
   685  		udf.DataType_DATETIME:   types.T_datetime,
   686  		udf.DataType_TIMESTAMP:  types.T_timestamp,
   687  		udf.DataType_DECIMAL64:  types.T_decimal64,
   688  		udf.DataType_DECIMAL128: types.T_decimal128,
   689  		udf.DataType_BINARY:     types.T_binary,
   690  		udf.DataType_VARBINARY:  types.T_varbinary,
   691  		udf.DataType_BLOB:       types.T_blob,
   692  	}
   693  )
   694  
   695  type DefaultPkgReader struct {
   696  	Proc *process.Process
   697  }
   698  
   699  func (d *DefaultPkgReader) Get(ctx context.Context, path string) (io.ReadCloser, error) {
   700  	reader, writer := io.Pipe()
   701  	var errGroup *errgroup.Group
   702  	defer func() {
   703  		if errGroup == nil {
   704  			writer.Close()
   705  		}
   706  	}()
   707  
   708  	ioVector := &fileservice.IOVector{
   709  		FilePath: path,
   710  		Entries: []fileservice.IOEntry{
   711  			{
   712  				Offset:        0,
   713  				Size:          -1,
   714  				WriterForRead: writer,
   715  			},
   716  		},
   717  	}
   718  
   719  	errGroup = new(errgroup.Group)
   720  	errGroup.Go(func() error {
   721  		defer writer.Close()
   722  		return d.Proc.FileService.Read(ctx, ioVector)
   723  	})
   724  
   725  	return reader, nil
   726  }