Skip to content

Commit 5a574a5

Browse files
Fix fallback logic for displayWidth=0 and remove trailing whitespace
- Apply scientific notation fallback even when displayWidth=0 using type default widths (24 for FLOAT, 14 for REAL) - Remove trailing whitespace on blank line in connectCommand - Prevents extreme values from producing unbounded output when SQLCMDMAXVARTYPEWIDTH=0 Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
1 parent 38ac3ee commit 5a574a5

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

pkg/sqlcmd/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ func connectCommand(s *Sqlcmd, args []string, line uint) error {
466466
}
467467

468468
commandArgs := strings.Fields(args[0])
469-
469+
470470
// Require at least the server name parameter
471471
if len(commandArgs) == 0 {
472472
return InvalidCommandError("CONNECT", line)

pkg/sqlcmd/format.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,19 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
546546

547547
formatted := strconv.FormatFloat(x, 'f', -1, bitSize)
548548
displayWidth := f.columnDetails[n].displayWidth
549-
if displayWidth > 0 && int64(len(formatted)) > displayWidth {
549+
550+
// Use the type's default display width when displayWidth is 0 (unlimited)
551+
// to avoid extremely long strings for extreme values
552+
widthThreshold := displayWidth
553+
if widthThreshold == 0 {
554+
if typeName == "REAL" || typeName == "SMALLMONEY" {
555+
widthThreshold = 14 // Default for REAL
556+
} else {
557+
widthThreshold = 24 // Default for FLOAT
558+
}
559+
}
560+
561+
if int64(len(formatted)) > widthThreshold {
550562
// Use 'g' format for very large/small values to avoid truncation issues
551563
formatted = strconv.FormatFloat(x, 'g', -1, bitSize)
552564
}
@@ -557,7 +569,14 @@ func (f *sqlCmdFormatterType) scanRow(rows *sql.Rows) ([]string, error) {
557569
// Use bitSize 32 to maintain precision appropriate for the original float32 value
558570
formatted := strconv.FormatFloat(float64(x), 'f', -1, 32)
559571
displayWidth := f.columnDetails[n].displayWidth
560-
if displayWidth > 0 && int64(len(formatted)) > displayWidth {
572+
573+
// Use default REAL display width when displayWidth is 0
574+
widthThreshold := displayWidth
575+
if widthThreshold == 0 {
576+
widthThreshold = 14
577+
}
578+
579+
if int64(len(formatted)) > widthThreshold {
561580
// Use 'g' format for very large/small values to avoid truncation issues
562581
formatted = strconv.FormatFloat(float64(x), 'g', -1, 32)
563582
}

0 commit comments

Comments
 (0)