github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/fromOldFunction.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 plan
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"unicode"
    20  )
    21  
    22  // From old function code, and they were only used by plan.
    23  
    24  const (
    25  	// MaxFsp is the maximum digit of fractional seconds part.
    26  	MaxFsp = 6
    27  )
    28  
    29  func ExtractToDateReturnType(format string) (tp types.T, fsp int) {
    30  	isTime, isDate := getTimeFormatType(format)
    31  	if isTime && !isDate {
    32  		tp = types.T_time
    33  	} else if !isTime && isDate {
    34  		tp = types.T_date
    35  	} else {
    36  		tp = types.T_datetime
    37  	}
    38  	//if strings.Contains(format, "%f") {
    39  	//	fsp = MaxFsp
    40  	//}
    41  	return tp, MaxFsp
    42  }
    43  
    44  // getTimeFormatType checks the type(Time, Date or Datetime) of a format string.
    45  func getTimeFormatType(format string) (isTime, isDate bool) {
    46  	format = trimWhiteSpace(format)
    47  	var token string
    48  	var succ bool
    49  	for {
    50  		token, format, succ = nextFormatToken(format)
    51  		if len(token) == 0 {
    52  			break
    53  		}
    54  		if !succ {
    55  			isTime, isDate = false, false
    56  			break
    57  		}
    58  		if len(token) >= 2 && token[0] == '%' {
    59  			switch token[1] {
    60  			case 'h', 'H', 'i', 'I', 's', 'S', 'k', 'l', 'f', 'r', 'T':
    61  				isTime = true
    62  			case 'y', 'Y', 'm', 'M', 'c', 'b', 'D', 'd', 'e':
    63  				isDate = true
    64  			}
    65  		}
    66  		if isTime && isDate {
    67  			break
    68  		}
    69  	}
    70  	return
    71  }
    72  
    73  func trimWhiteSpace(input string) string {
    74  	for i, c := range input {
    75  		if !unicode.IsSpace(c) {
    76  			return input[i:]
    77  		}
    78  	}
    79  	return ""
    80  }
    81  
    82  func nextFormatToken(format string) (token string, remain string, success bool) {
    83  	if len(format) == 0 {
    84  		return "", "", true
    85  	}
    86  
    87  	// Just one character.
    88  	if len(format) == 1 {
    89  		if format[0] == '%' {
    90  			return "", "", false
    91  		}
    92  		return format, "", true
    93  	}
    94  
    95  	// More than one character.
    96  	if format[0] == '%' {
    97  		return format[:2], format[2:], true
    98  	}
    99  
   100  	return format[:1], format[1:], true
   101  }