github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sessiondatapb/session_data.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sessiondatapb
    12  
    13  import (
    14  	"fmt"
    15  	"strings"
    16  
    17  	"github.com/cockroachdb/cockroachdb-parser/pkg/security/username"
    18  	"github.com/cockroachdb/cockroachdb-parser/pkg/sql/types"
    19  )
    20  
    21  // GetFloatPrec computes a precision suitable for a call to
    22  // strconv.FormatFloat() or for use with '%.*g' in a printf-like
    23  // function.
    24  func (c DataConversionConfig) GetFloatPrec(typ *types.T) int {
    25  	// The user-settable parameter ExtraFloatDigits indicates the number of digits
    26  	// to be used to format the float value. PostgreSQL combines this with %g.
    27  	// If ExtraFloatDigits is less than or equal to zero, the formula is
    28  	// <type>_DIG + extra_float_digits, where <type> is either FLT (float4) or DBL
    29  	// (float8).
    30  	// If ExtraFloatDigits is greater than zero, then the "shortest precise
    31  	// format" is used. The Go formatter uses the special value -1 for this and
    32  	// activates a separate path in the formatter.
    33  	// NB: In previous versions, only the value "3" would result in the shortest
    34  	// precise format. This change in behavior was made in PostgreSQL 12.
    35  	// See https://github.com/postgres/postgres/commit/02ddd499322ab6f2f0d58692955dc9633c2150fc.
    36  	if c.ExtraFloatDigits > 0 {
    37  		return -1
    38  	}
    39  
    40  	// Go does not expose FLT_DIG or DBL_DIG, so we use the standard literal
    41  	// constants for 32-bit and 64-bit floats.
    42  	const StdFloatDigits = 6
    43  	const StdDoubleDigits = 15
    44  
    45  	nDigits := StdDoubleDigits + c.ExtraFloatDigits
    46  	if typ.Width() == 32 {
    47  		nDigits = StdFloatDigits + c.ExtraFloatDigits
    48  	}
    49  	if nDigits < 1 {
    50  		// Ensure the value is clamped at 1: printf %g does not allow
    51  		// values lower than 1. PostgreSQL does this too.
    52  		nDigits = 1
    53  	}
    54  	return int(nDigits)
    55  }
    56  
    57  func (m VectorizeExecMode) String() string {
    58  	if m == VectorizeUnset {
    59  		m = VectorizeOn
    60  	}
    61  	name, ok := VectorizeExecMode_name[int32(m)]
    62  	if !ok {
    63  		return fmt.Sprintf("invalid (%d)", m)
    64  	}
    65  	return name
    66  }
    67  
    68  // VectorizeExecModeFromString converts a string into a VectorizeExecMode.
    69  // False is returned if the conversion was unsuccessful.
    70  func VectorizeExecModeFromString(val string) (VectorizeExecMode, bool) {
    71  	lowerVal := strings.ToLower(val)
    72  	mInt, ok := VectorizeExecMode_value[lowerVal]
    73  	if !ok {
    74  		return 0, false
    75  	}
    76  	m := VectorizeExecMode(mInt)
    77  	if m == VectorizeUnset {
    78  		return 0, false
    79  	}
    80  	return m, true
    81  }
    82  
    83  // User retrieves the current user.
    84  func (s *SessionData) User() username.SQLUsername {
    85  	return s.UserProto.Decode()
    86  }
    87  
    88  // SystemIdentity retrieves the session's system identity.
    89  // (Identity presented by the client prior to identity mapping.)
    90  func (s *LocalOnlySessionData) SystemIdentity() username.SQLUsername {
    91  	return s.SystemIdentityProto.Decode()
    92  }