github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/dump/dump.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 dump
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  )
    25  
    26  type quotedTp interface {
    27  	types.Datetime | types.Time | types.Date | types.Decimal | string
    28  }
    29  
    30  type transTp interface {
    31  	[]byte | quotedTp | types.Datetime | types.Time
    32  }
    33  
    34  func ParseBool(xs []bool, nsp *nulls.Nulls, rs []string) ([]string, error) {
    35  	for i := range xs {
    36  		if nsp.Contains(uint64(i)) {
    37  			rs[i] = "NULL"
    38  			continue
    39  		}
    40  		rs[i] = strconv.FormatBool(xs[i])
    41  	}
    42  	return rs, nil
    43  }
    44  
    45  func ParseSigned[T types.Ints](xs []T, nsp *nulls.Nulls, rs []string) ([]string, error) {
    46  	for i := range xs {
    47  		if nsp.Contains(uint64(i)) {
    48  			rs[i] = "NULL"
    49  			continue
    50  		}
    51  		rs[i] = strconv.FormatInt(int64(xs[i]), 10)
    52  	}
    53  	return rs, nil
    54  }
    55  func ParseUnsigned[T types.UInts](xs []T, nsp *nulls.Nulls, rs []string) ([]string, error) {
    56  	for i := range xs {
    57  		if nsp.Contains(uint64(i)) {
    58  			rs[i] = "NULL"
    59  			continue
    60  		}
    61  		rs[i] = strconv.FormatUint(uint64(xs[i]), 10)
    62  	}
    63  	return rs, nil
    64  }
    65  
    66  func ParseFloats[T types.Floats](xs []T, nsp *nulls.Nulls, rs []string, bitsSize int) ([]string, error) {
    67  	for i := range xs {
    68  		if nsp.Contains(uint64(i)) {
    69  			rs[i] = "NULL"
    70  			continue
    71  		}
    72  		rs[i] = strconv.FormatFloat(float64(xs[i]), 'f', -1, bitsSize) //TODO: precision
    73  	}
    74  	return rs, nil
    75  }
    76  
    77  func ParseQuoted[T transTp](xs []T, nsp *nulls.Nulls, rs []string, fn func(dt T) string) ([]string, error) {
    78  	for i := range xs {
    79  		if nsp.Contains(uint64(i)) {
    80  			rs[i] = "NULL"
    81  			continue
    82  		}
    83  		v := fn(xs[i])
    84  		rs[i] = v
    85  	}
    86  	return rs, nil
    87  }
    88  
    89  func ParseTimeStamp(xs []types.Timestamp, nsp *nulls.Nulls, rs []string, loc *time.Location, precision int32) ([]string, error) {
    90  	for i := range xs {
    91  		if nsp.Contains(uint64(i)) {
    92  			rs[i] = "NULL"
    93  			continue
    94  		}
    95  		v := fmt.Sprintf("'%s'", xs[i].String2(loc, precision))
    96  		rs[i] = v
    97  	}
    98  	return rs, nil
    99  }
   100  func ParseUuid(xs []types.Uuid, nsp *nulls.Nulls, rs []string) ([]string, error) {
   101  	for i := range xs {
   102  		if nsp.Contains(uint64(i)) {
   103  			rs[i] = "NULL"
   104  			continue
   105  		}
   106  		v := fmt.Sprintf("'%s'", xs[i].ToString())
   107  		rs[i] = v
   108  	}
   109  	return rs, nil
   110  }
   111  
   112  func DefaultParser[T quotedTp](t T) string {
   113  	ret := fmt.Sprint("'", t, "'")
   114  	return ret
   115  }
   116  func JsonParser(dt []byte) string {
   117  	json := types.DecodeJson(dt)
   118  	return "'" + json.String() + "'"
   119  }