github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/extract.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 binary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/extract"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  /*
    26  // when implicit cast from varchar to date is ready, get rid of this
    27  func ExtractFromString(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    28  	left, right := vectors[0], vectors[1]
    29  	resultType := types.Type{Oid: types.T_uint32, Size: 4}
    30  	resultElementSize := int(resultType.Size)
    31  	switch {
    32  	case left.IsScalar() && right.IsScalar():
    33  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    34  			return proc.AllocScalarNullVector(resultType), nil
    35  		}
    36  		leftValues, rightValues := left.Col.(*types.Bytes), right.Col.(*types.Bytes)
    37  		resultVector := vector.NewConst(resultType)
    38  		resultValues := make([]uint32, 1)
    39  		unit := string(leftValues.Data)
    40  		inputDate, err := types.ParseDateCast(string(rightValues.Get(0)))
    41  		if err != nil {
    42  			return nil, moerr.NewInternalError("invalid input")
    43  		}
    44  		resultValues, err = extract.ExtractFromDate(unit, []types.Date{inputDate}, resultValues)
    45  		if err != nil {
    46  			return nil, moerr.NewInternalError("invalid input")
    47  		}
    48  		vector.SetCol(resultVector, resultValues)
    49  		return resultVector, nil
    50  	case left.IsScalar() && !right.IsScalar():
    51  		if left.ConstVectorIsNull() {
    52  			return proc.AllocScalarNullVector(resultType), nil
    53  		}
    54  		leftValues, rightValues := left.Col.(*types.Bytes), right.Col.(*types.Bytes)
    55  		unit := string(leftValues.Data)
    56  		resultValues, err := proc.AllocVector(resultType, int64(resultElementSize) * int64(len(rightValues.Lengths)))
    57  		if
    58  
    59  		result, resultNsp, err := extract.ExtractFromInputBytes(unit, rightValues, right.Nsp, )
    60  
    61  	default:
    62  		return nil, moerr.NewInternalError("invalid input")
    63  	}
    64  }
    65  */
    66  
    67  func ExtractFromDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    68  	left, right := vectors[0], vectors[1]
    69  	resultType := types.Type{Oid: types.T_uint32, Size: 4}
    70  	resultElementSize := int(resultType.Size)
    71  	leftValues, rightValues := vector.MustStrCols(left), vector.MustTCols[types.Date](right)
    72  	switch {
    73  	case left.IsScalar() && right.IsScalar():
    74  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    75  			return proc.AllocScalarNullVector(resultType), nil
    76  		}
    77  		resultVector := vector.NewConst(resultType, 1)
    78  		resultValues := vector.MustTCols[uint32](resultVector)
    79  		unit := leftValues[0]
    80  		_, err := extract.ExtractFromDate(unit, rightValues, resultValues)
    81  		if err != nil {
    82  			return nil, moerr.NewInternalError(proc.Ctx, "invalid input")
    83  		}
    84  		return resultVector, nil
    85  	case left.IsScalar() && !right.IsScalar():
    86  		if left.ConstVectorIsNull() {
    87  			return proc.AllocScalarNullVector(resultType), nil
    88  		}
    89  		resultVector, err := proc.AllocVector(resultType, int64(resultElementSize*len(rightValues)))
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  		resultValues := vector.MustTCols[uint32](resultVector)
    94  		unit := leftValues[0]
    95  		_, err = extract.ExtractFromDate(unit, rightValues, resultValues)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		return resultVector, nil
   100  	default:
   101  		return nil, moerr.NewInternalError(proc.Ctx, "invalid input")
   102  	}
   103  }
   104  
   105  func ExtractFromDatetime(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   106  	left, right := vectors[0], vectors[1]
   107  	resultType := types.Type{Oid: types.T_varchar, Size: 24, Width: types.MaxVarcharLen}
   108  	leftValues, rightValues := vector.MustStrCols(left), vector.MustTCols[types.Datetime](right)
   109  	switch {
   110  	case left.IsScalar() && right.IsScalar():
   111  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
   112  			return proc.AllocScalarNullVector(resultType), nil
   113  		}
   114  		resultValues := make([]string, 1)
   115  		unit := leftValues[0]
   116  		resultValues, err := extract.ExtractFromDatetime(unit, rightValues, resultValues)
   117  		if err != nil {
   118  			return nil, moerr.NewInternalError(proc.Ctx, "invalid input")
   119  		}
   120  		return vector.NewConstString(resultType, 1, resultValues[0], proc.Mp()), nil
   121  	case left.IsScalar() && !right.IsScalar():
   122  		if left.ConstVectorIsNull() {
   123  			return proc.AllocScalarNullVector(resultType), nil
   124  		}
   125  		resultValues := make([]string, len(rightValues))
   126  		unit := leftValues[0]
   127  		resultValues, err := extract.ExtractFromDatetime(unit, rightValues, resultValues)
   128  		if err != nil {
   129  			return nil, err
   130  		}
   131  		return vector.NewWithStrings(resultType, resultValues, right.Nsp, proc.Mp()), nil
   132  	default:
   133  		return nil, moerr.NewInternalError(proc.Ctx, "invalid input")
   134  	}
   135  }