github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/datediff.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/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/datediff"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func DateDiff(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	left := vectors[0]
    27  	right := vectors[1]
    28  	leftValues := vector.MustTCols[types.Date](vectors[0])
    29  	rightValues := vector.MustTCols[types.Date](vectors[1])
    30  
    31  	resultType := types.T_int64.ToType()
    32  	switch {
    33  	case left.IsScalar() && right.IsScalar():
    34  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    35  			return proc.AllocScalarNullVector(resultType), nil
    36  		}
    37  		resultVector := vector.NewConst(resultType, 1)
    38  		resultValues := vector.MustTCols[int64](resultVector)
    39  		datediff.DateDiff(leftValues, rightValues, resultValues)
    40  		return resultVector, nil
    41  	case left.IsScalar() && !right.IsScalar():
    42  		if left.ConstVectorIsNull() {
    43  			return proc.AllocScalarNullVector(resultType), nil
    44  		}
    45  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), right.Nsp)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		resultValues := vector.MustTCols[int64](resultVector)
    50  		datediff.DateDiffLeftConst(leftValues[0], rightValues, resultValues)
    51  		return resultVector, nil
    52  	case !left.IsScalar() && right.IsScalar():
    53  		if right.ConstVectorIsNull() {
    54  			return proc.AllocScalarNullVector(resultType), nil
    55  		}
    56  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), left.Nsp)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		resultValues := vector.MustTCols[int64](resultVector)
    61  		datediff.DateDiffRightConst(leftValues, rightValues[0], resultValues)
    62  		return resultVector, nil
    63  	}
    64  	resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	resultValues := vector.MustTCols[int64](resultVector)
    69  	nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp)
    70  	datediff.DateDiff(leftValues, rightValues, resultValues)
    71  	return resultVector, nil
    72  }