-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
67489: clisqlclient: trim down dependencies r=otan a=knz Fixes #67482. Informs #67470. 67669: bazel: upgrade gazelle to latest r=rail a=rickystewart This helps to avoid bazel-contrib/rules_go#2479. Release note: None Co-authored-by: Raphael 'kena' Poss <[email protected]> Co-authored-by: Ricky Stewart <[email protected]>
- Loading branch information
Showing
13 changed files
with
250 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package clisqlclient | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// stringToDuration converts a server-side interval value returned by | ||
// SHOW LAST QUERY STATISTICS. We use a custom parser here to avoid | ||
// depending on package `tree` or `duration`, which makes the SQL | ||
// shell executable significantly larger. | ||
// | ||
// Note: this parser only supports the 'postgres' encoding for | ||
// IntervalStyle. This code breaks if the server-side | ||
// IntervalStyle is set to another value e.g. 'iso_8601'. | ||
// See: https://github.com/cockroachdb/cockroach/issues/67618 | ||
func stringToDuration(s string) (time.Duration, error) { | ||
m := intervalRe.FindStringSubmatch(s) | ||
if m == nil { | ||
return 0, errors.Newf("invalid format: %q", s) | ||
} | ||
th, e1 := strconv.Atoi(m[1]) | ||
tm, e2 := strconv.Atoi(m[2]) | ||
ts, e3 := strconv.Atoi(m[3]) | ||
us := m[4] + "000000"[:6-len(m[4])] | ||
tus, e4 := strconv.Atoi(us) | ||
return (time.Duration(th)*time.Hour + | ||
time.Duration(tm)*time.Minute + | ||
time.Duration(ts)*time.Second + | ||
time.Duration(tus)*time.Microsecond), | ||
errors.CombineErrors(e1, | ||
errors.CombineErrors(e2, | ||
errors.CombineErrors(e3, e4))) | ||
} | ||
|
||
// intervalRe indicates how to parse the interval value. | ||
// The format is HHHH:MM:SS[.ffffff] | ||
// | ||
// Note: we do not need to support a day prefix, because SHOW LAST | ||
// QUERY STATISTICS always reports intervals computed from a number | ||
// of seconds, and these never contain a "days" components. | ||
// | ||
// For example, a query that ran for 3 days will have its interval | ||
// displayed as 72:00:00, not "3 days 00:00:00". | ||
var intervalRe = regexp.MustCompile(`^(\d{2,}):(\d{2}):(\d{2})(?:\.(\d{1,6}))?$`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package clisqlclient | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
func TestStringToDuration(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
testCases := []struct { | ||
input string | ||
output time.Duration | ||
expectedErr string | ||
}{ | ||
{"00:00:00", 0, ""}, | ||
{"01:02:03", time.Hour + 2*time.Minute + 3*time.Second, ""}, | ||
{"11:22:33", 11*time.Hour + 22*time.Minute + 33*time.Second, ""}, | ||
{"1234:22:33", 1234*time.Hour + 22*time.Minute + 33*time.Second, ""}, | ||
{"01:02:03.4", time.Hour + 2*time.Minute + 3*time.Second + 400*time.Millisecond, ""}, | ||
{"01:02:03.004", time.Hour + 2*time.Minute + 3*time.Second + 4*time.Millisecond, ""}, | ||
{"01:02:03.123456", time.Hour + 2*time.Minute + 3*time.Second + 123456*time.Microsecond, ""}, | ||
{"1001:02:03.123456", 1001*time.Hour + 2*time.Minute + 3*time.Second + 123456*time.Microsecond, ""}, | ||
{"00:00", 0, "invalid format"}, | ||
{"00.00.00", 0, "invalid format"}, | ||
{"00:00:00:000000000", 0, "invalid format"}, | ||
{"00:00:00.000000000", 0, "invalid format"}, | ||
{"123 00:00:00.000000000", 0, "invalid format"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
v, err := stringToDuration(tc.input) | ||
if !testutils.IsError(err, tc.expectedErr) { | ||
t.Errorf("%s: expected error %q, got: %v", tc.input, tc.expectedErr, err) | ||
} | ||
if err == nil { | ||
if v != tc.output { | ||
t.Errorf("%s: expected %v, got %v", tc.input, tc.output, v) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.