github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/date_add.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 multi
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/date_add"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func DateAdd(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    25  	firstVector := vectors[0]
    26  	secondVector := vectors[1]
    27  	firstValues := vector.MustTCols[types.Date](vectors[0])
    28  	secondValues := vector.MustTCols[int64](vectors[1])
    29  	thirdValues := vector.MustTCols[int64](vectors[2])
    30  
    31  	resultType := types.Type{Oid: types.T_date, Size: 4}
    32  	if firstVector.IsScalar() && secondVector.IsScalar() {
    33  		if firstVector.IsScalarNull() || secondVector.IsScalarNull() {
    34  			return proc.AllocScalarNullVector(resultType), nil
    35  		}
    36  		resultVector := proc.AllocScalarVector(resultType)
    37  		rs := make([]types.Date, 1)
    38  		res, err := date_add.DateAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs)
    39  		vector.SetCol(resultVector, res)
    40  		return resultVector, err
    41  	} else {
    42  		var maxLen int
    43  		if len(firstValues) > len(secondValues) {
    44  			maxLen = len(firstValues)
    45  		} else {
    46  			maxLen = len(secondValues)
    47  		}
    48  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		resultValues := vector.MustTCols[types.Date](resultVector)
    53  		_, err = date_add.DateAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues)
    54  		return resultVector, err
    55  	}
    56  }
    57  
    58  func TimeAdd(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    59  	firstVector := vectors[0]
    60  	secondVector := vectors[1]
    61  	firstValues := vector.MustTCols[types.Time](vectors[0])
    62  	secondValues := vector.MustTCols[int64](vectors[1])
    63  	thirdValues := vector.MustTCols[int64](vectors[2])
    64  
    65  	precision := firstVector.Typ.Precision
    66  	switch types.IntervalType(thirdValues[0]) {
    67  	case types.MicroSecond:
    68  		precision = 6
    69  	}
    70  
    71  	resultType := types.Type{Oid: types.T_time, Precision: precision, Size: 8}
    72  
    73  	if firstVector.IsScalar() && secondVector.IsScalar() {
    74  		if firstVector.IsScalarNull() || secondVector.IsScalarNull() {
    75  			return proc.AllocScalarNullVector(resultType), nil
    76  		}
    77  		resultVector := proc.AllocScalarVector(resultType)
    78  		rs := make([]types.Time, 1)
    79  		res, err := date_add.TimeAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs)
    80  		vector.SetCol(resultVector, res)
    81  		return resultVector, err
    82  	} else {
    83  		var maxLen int
    84  		if len(firstValues) > len(secondValues) {
    85  			maxLen = len(firstValues)
    86  		} else {
    87  			maxLen = len(secondValues)
    88  		}
    89  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil)
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  		resultValues := vector.MustTCols[types.Time](resultVector)
    94  		resultValues = resultValues[:maxLen]
    95  		_, err = date_add.TimeAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues)
    96  		return resultVector, err
    97  	}
    98  }
    99  
   100  func DatetimeAdd(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   101  	firstVector := vectors[0]
   102  	secondVector := vectors[1]
   103  	firstValues := vector.MustTCols[types.Datetime](vectors[0])
   104  	secondValues := vector.MustTCols[int64](vectors[1])
   105  	thirdValues := vector.MustTCols[int64](vectors[2])
   106  
   107  	precision := firstVector.Typ.Precision
   108  	switch types.IntervalType(thirdValues[0]) {
   109  	case types.MicroSecond:
   110  		precision = 6
   111  	}
   112  
   113  	resultType := types.Type{Oid: types.T_datetime, Precision: precision, Size: 8}
   114  
   115  	if firstVector.IsScalar() && secondVector.IsScalar() {
   116  		if firstVector.IsScalarNull() || secondVector.IsScalarNull() {
   117  			return proc.AllocScalarNullVector(resultType), nil
   118  		}
   119  		resultVector := proc.AllocScalarVector(resultType)
   120  		rs := make([]types.Datetime, 1)
   121  		res, err := date_add.DatetimeAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs)
   122  		vector.SetCol(resultVector, res)
   123  		return resultVector, err
   124  	} else {
   125  		var maxLen int
   126  		if len(firstValues) > len(secondValues) {
   127  			maxLen = len(firstValues)
   128  		} else {
   129  			maxLen = len(secondValues)
   130  		}
   131  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil)
   132  		if err != nil {
   133  			return nil, err
   134  		}
   135  		resultValues := vector.MustTCols[types.Datetime](resultVector)
   136  		resultValues = resultValues[:maxLen]
   137  		_, err = date_add.DatetimeAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues)
   138  		return resultVector, err
   139  	}
   140  }
   141  
   142  func DateStringAdd(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   143  	firstVector := vectors[0]
   144  	secondVector := vectors[1]
   145  	firstValues := vector.MustStrCols(vectors[0])
   146  	secondValues := vector.MustTCols[int64](vectors[1])
   147  	thirdValues := vector.MustTCols[int64](vectors[2])
   148  	resultType := types.Type{Oid: types.T_datetime, Precision: 6, Size: 8}
   149  
   150  	if firstVector.IsScalar() && secondVector.IsScalar() {
   151  		if firstVector.IsScalarNull() || secondVector.IsScalarNull() {
   152  			return proc.AllocScalarNullVector(resultType), nil
   153  		}
   154  		resultVector := proc.AllocScalarVector(resultType)
   155  		rs := make([]types.Datetime, 1)
   156  		res, err := date_add.DateStringAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs)
   157  		vector.SetCol(resultVector, res)
   158  		return resultVector, err
   159  	} else {
   160  		var maxLen int
   161  		if len(firstValues) > len(secondValues) {
   162  			maxLen = len(firstValues)
   163  		} else {
   164  			maxLen = len(secondValues)
   165  		}
   166  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), nil)
   167  		if err != nil {
   168  			return nil, err
   169  		}
   170  		resultValues := vector.MustTCols[types.Datetime](resultVector)
   171  		resultValues = resultValues[:maxLen]
   172  		_, err = date_add.DateStringAdd(firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues)
   173  		return resultVector, err
   174  	}
   175  }
   176  
   177  func TimeStampAdd(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   178  	firstVector := vectors[0]
   179  	secondVector := vectors[1]
   180  	firstValues := vector.MustTCols[types.Timestamp](vectors[0])
   181  	secondValues := vector.MustTCols[int64](vectors[1])
   182  	thirdValues := vector.MustTCols[int64](vectors[2])
   183  
   184  	precision := firstVector.Typ.Precision
   185  	switch types.IntervalType(thirdValues[0]) {
   186  	case types.MicroSecond:
   187  		precision = 6
   188  	}
   189  
   190  	resultType := types.Type{Oid: types.T_timestamp, Precision: precision, Size: 8}
   191  
   192  	if firstVector.IsScalar() && secondVector.IsScalar() {
   193  		if firstVector.IsScalarNull() || secondVector.IsScalarNull() {
   194  			return proc.AllocScalarNullVector(resultType), nil
   195  		}
   196  		resultVector := proc.AllocScalarVector(resultType)
   197  		rs := make([]types.Timestamp, 1)
   198  		res, err := date_add.TimestampAdd(proc.SessionInfo.TimeZone, firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, rs)
   199  		vector.SetCol(resultVector, res)
   200  		return resultVector, err
   201  	} else {
   202  		var maxLen int
   203  		if len(firstValues) > len(secondValues) {
   204  			maxLen = len(firstValues)
   205  		} else {
   206  			maxLen = len(secondValues)
   207  		}
   208  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(maxLen), firstVector.Nsp)
   209  		if err != nil {
   210  			return nil, err
   211  		}
   212  		resultValues := vector.GetFixedVectorValues[types.Timestamp](resultVector)
   213  		_, err = date_add.TimestampAdd(proc.SessionInfo.TimeZone, firstValues, secondValues, thirdValues, firstVector.Nsp, secondVector.Nsp, resultVector.Nsp, resultValues)
   214  		return resultVector, err
   215  	}
   216  }