From 6a66f66c5f8b12a03b129b212862a109f4e4cfbd Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sun, 30 Jan 2022 21:00:19 +0100 Subject: [PATCH] Cleanups --- dsvreader.go | 13 ++++------ read_date.go | 18 +++----------- read_nums.go | 69 +++++++++++++++++++++++++--------------------------- 3 files changed, 42 insertions(+), 58 deletions(-) diff --git a/dsvreader.go b/dsvreader.go index c84829a..52dfdae 100644 --- a/dsvreader.go +++ b/dsvreader.go @@ -135,7 +135,7 @@ func (tr *Reader) Next() bool { } n, err := tr.r.Read(tr.rBuf[:]) tr.rb = tr.rBuf[:n] - tr.needUnescape = (bytes.IndexByte(tr.rb, '\\') >= 0) + tr.needUnescape = bytes.IndexByte(tr.rb, '\\') >= 0 tr.rErr = err } @@ -167,8 +167,7 @@ func (tr *Reader) SkipCol() { if tr.err != nil { return } - _, err := tr.nextCol() - if err != nil { + if _, err := tr.nextCol(); err != nil { tr.setColError("cannot skip column", err) } } @@ -187,15 +186,13 @@ func (tr *Reader) Bytes() []byte { } if !tr.needUnescape { - // Fast path - nothing to unescape. - return b + return b // Fast path - nothing to unescape. } // Unescape b n := bytes.IndexByte(b, '\\') if n < 0 { - // Nothing to unescape in the current column. - return b + return b // Nothing to unescape in the current column. } // Slow path - in-place unescaping compatible with ClickHouse. @@ -268,5 +265,5 @@ func (tr *Reader) nextCol() ([]byte, error) { } func (tr *Reader) setColError(msg string, err error) { - tr.err = fmt.Errorf("%s at row #%d, col #%d %q: %s", msg, tr.row, tr.col, tr.rowBuf, err) + tr.err = fmt.Errorf("%s at row #%d, col #%d %q: %w", msg, tr.row, tr.col, tr.rowBuf, err) } diff --git a/read_date.go b/read_date.go index 8514ebd..9795266 100644 --- a/read_date.go +++ b/read_date.go @@ -21,15 +21,12 @@ func (tr *Reader) Date() time.Time { tr.setColError("cannot read `date`", err) return zeroTime } - s := b2s(b) - - y, m, d, err := parseDate(s) + y, m, d, err := parseDate(b2s(b)) if err != nil { tr.setColError("cannot parse `date`", err) return zeroTime } if y == 0 && m == 0 && d == 0 { - // special case for ClickHouse return zeroTime } return time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC) @@ -47,9 +44,7 @@ func (tr *Reader) DateTime() time.Time { tr.setColError("cannot read `datetime`", err) return zeroTime } - s := b2s(b) - - dt, err := parseDateTime(s) + dt, err := parseDateTime(b2s(b)) if err != nil { tr.setColError("cannot parse `datetime`", err) return zeroTime @@ -69,9 +64,7 @@ func parseDateTime(s string) (time.Time, error) { if s[0] != ' ' || s[3] != ':' || s[6] != ':' { return zeroTime, errors.New("invalid time format. Must be hh:mm:ss") } - hS := s[1:3] - minS := s[4:6] - secS := s[7:] + hS, minS, secS := s[1:3], s[4:6], s[7:] h, err := strconv.Atoi(hS) if err != nil { return zeroTime, fmt.Errorf("invalid hour: %w", err) @@ -85,7 +78,6 @@ func parseDateTime(s string) (time.Time, error) { return zeroTime, fmt.Errorf("invalid second: %w", err) } if y == 0 && m == 0 && d == 0 { - // Special case for ClickHouse return zeroTime, nil } return time.Date(y, time.Month(m), d, h, min, sec, 0, time.UTC), nil @@ -99,9 +91,7 @@ func parseDate(s string) (y, m, d int, err error) { if s[4] != '-' && s[7] != '-' { return 0, 0, 0, errors.New("invalid date format. Must be YYYY-MM-DD") } - yS := s[:4] - mS := s[5:7] - dS := s[8:] + yS, mS, dS := s[:4], s[5:7], s[8:] y, err = strconv.Atoi(yS) if err != nil { return 0, 0, 0, fmt.Errorf("invalid year: %w", err) diff --git a/read_nums.go b/read_nums.go index d1c2bbe..c2035d3 100644 --- a/read_nums.go +++ b/read_nums.go @@ -40,6 +40,7 @@ func (tr *Reader) Uint() uint { tr.setColError("cannot read `uint`", err) return 0 } + s := b2s(b) // Fast path - attempt to use Atoi @@ -67,6 +68,7 @@ func (tr *Reader) Int32() int32 { tr.setColError("cannot read `int32`", err) return 0 } + s := b2s(b) // Fast path - attempt to use Atoi @@ -94,6 +96,7 @@ func (tr *Reader) Uint32() uint32 { tr.setColError("cannot read `uint32`", err) return 0 } + s := b2s(b) // Fast path - attempt to use Atoi @@ -122,15 +125,15 @@ func (tr *Reader) Int16() int16 { return 0 } n, err := strconv.Atoi(b2s(b)) - if err != nil { + switch { + case err != nil: tr.setColError("cannot parse `int16`", err) - return 0 - } - if n < math.MinInt16 || n > math.MaxInt16 { + case n < math.MinInt16 || n > math.MaxInt16: tr.setColError("cannot parse `int16`", errOutOfRange) - return 0 + default: + return int16(n) } - return int16(n) + return 0 } // Uint16 returns the next uint16 column value from the current row. @@ -144,19 +147,17 @@ func (tr *Reader) Uint16() uint16 { return 0 } n, err := strconv.Atoi(b2s(b)) - if err != nil { + switch { + case err != nil: tr.setColError("cannot parse `uint16`", err) - return 0 - } - if n < 0 { + case n < 0: tr.setColError("cannot parse `uint16`", errInvalidSyntex) - return 0 - } - if n > math.MaxUint16 { + case n > math.MaxUint16: tr.setColError("cannot parse `uint16`", errOutOfRange) - return 0 + default: + return uint16(n) } - return uint16(n) + return 0 } // Int8 returns the next int8 column value from the current row. @@ -170,15 +171,15 @@ func (tr *Reader) Int8() int8 { return 0 } n, err := strconv.Atoi(b2s(b)) - if err != nil { + switch { + case err != nil: tr.setColError("cannot parse `int8`", err) - return 0 - } - if n < math.MinInt8 || n > math.MaxInt8 { + case n < math.MinInt8 || n > math.MaxInt8: tr.setColError("cannot parse `int8`", errOutOfRange) - return 0 + default: + return int8(n) } - return int8(n) + return 0 } // Uint8 returns the next uint8 column value from the current row. @@ -192,19 +193,17 @@ func (tr *Reader) Uint8() uint8 { return 0 } n, err := strconv.Atoi(b2s(b)) - if err != nil { + switch { + case err != nil: tr.setColError("cannot parse `uint8`", err) - return 0 - } - if n < 0 { + case n < 0: tr.setColError("cannot parse `uint8`", errInvalidSyntex) - return 0 - } - if n > math.MaxUint8 { + case n > math.MaxUint8: tr.setColError("cannot parse `uint8`", errOutOfRange) - return 0 + default: + return uint8(n) } - return uint8(n) + return 0 } // Int64 returns the next int64 column value from the current row. @@ -217,6 +216,7 @@ func (tr *Reader) Int64() int64 { tr.setColError("cannot read `int64`", err) return 0 } + s := b2s(b) // Fast path - attempt to use Atoi @@ -244,6 +244,7 @@ func (tr *Reader) Uint64() uint64 { tr.setColError("cannot read `uint64`", err) return 0 } + s := b2s(b) // Fast path - attempt to use Atoi @@ -271,9 +272,7 @@ func (tr *Reader) Float32() float32 { tr.setColError("cannot read `float32`", err) return 0 } - s := b2s(b) - - f32, err := strconv.ParseFloat(s, 32) + f32, err := strconv.ParseFloat(b2s(b), 32) if err != nil { tr.setColError("cannot parse `float32`", err) return 0 @@ -291,9 +290,7 @@ func (tr *Reader) Float64() float64 { tr.setColError("cannot read `float64`", err) return 0 } - s := b2s(b) - - f64, err := strconv.ParseFloat(s, 64) + f64, err := strconv.ParseFloat(b2s(b), 64) if err != nil { tr.setColError("cannot parse `float64`", err) return 0