github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/ctl/mo_log_date.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 ctl
    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/sql/plan/function/builtin/binary"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function/builtin/multi"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  const ZeroDate = "0001-01-01"
    26  const formatMask = "%Y/%m/%d"
    27  const regexpMask = `\d{1,4}/\d{1,2}/\d{1,2}`
    28  
    29  // MOLogDate parse 'YYYY/MM/DD' date from input string.
    30  // return '0001-01-01' if input string not container 'YYYY/MM/DD' substr, until DateParse Function support return NULL for invalid date string.
    31  func MOLogDate(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    32  
    33  	regexpVec := vector.NewWithStrings(types.T_varchar.ToType(), []string{regexpMask}, nil, proc.Mp())
    34  	regexpInput := append(vectors, regexpVec)
    35  	parsedInput, err := multi.RegularSubstr(regexpInput, proc)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	formatVec := vector.NewConstString(types.T_char.ToType(), len(formatMask), formatMask, proc.Mp())
    41  	strToDateInput := []*vector.Vector{parsedInput, formatVec}
    42  	resultVector, err := binary.StrToDate(strToDateInput, proc)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return resultVector, err
    47  }