Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jan 30, 2022
1 parent 2bacaee commit 6a66f66
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 58 deletions.
13 changes: 5 additions & 8 deletions dsvreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
18 changes: 4 additions & 14 deletions read_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
69 changes: 33 additions & 36 deletions read_nums.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6a66f66

Please sign in to comment.