<commit_before>package pgproto

// GuessOids attemps to guess the Postgres oids for the given data
// values. It assumes that rows is a slice of uniform-length slices
// where each cell corresponds to a column. It returns a slice of oid
// values of the mapped oids, or OID_UNKNOWN where no mapping could
// be determined.
func GuessOids(rows [][]interface{}) (oids []uint32) {
	if len(rows) == 0 {
		// can;t really make much of a guess here
		return []uint32{}
	}
	oids = make([]uint32, len(rows[0]))
	for _, row := range rows {
		gotAll := true
		for i, _ := range oids {
			if o := oids[i]; o == 0 || o == OID_UNKNOWN {
				oids[i] = MappedOid(row[i])
				if oids[i] == OID_UNKNOWN {
					gotAll = false
				}
			}
		}
		if gotAll {
			break
		}
	}
	return oids
}

// MappedOid returns the Postgres oid mapped to the type of the given
// value in femebe, or OID_UNKNOWN if no mapping exists.
func MappedOid(val interface{}) uint32 {
	switch val.(type) {
	case nil:
		// we can't determine a type here
		return OID_UNKNOWN
	case int16:
		return OID_INT2
	case int32:
		return OID_INT4
	case int64:
		return OID_INT8
	case float32:
		return OID_FLOAT4
	case float64:
		return OID_FLOAT8
	case string:
		return OID_TEXT
	case bool:
		return OID_BOOL
	default:
		return OID_UNKNOWN
	}

	panic("Oh snap!")
}

// TypSize returns the size in bytes of the Postgres type specified by
// typOid, where undertood by femebe. For variable-length types or if
// the type is not known, -1 is returned.
func TypSize(typOid uint32) int16 {
	// TODO: right now, we hardcode the length of the various types
	// here; ideally, we should have a mapping for the fixed-length
	// types (although it seems that all the dynamic-length types,
	// even those with explicit length restrictions, is just -1, so
	// perhaps we can provide a mapping for that as well).
	switch typOid {
	case OID_BOOL:
		return 1
	case OID_INT2:
		return 2
	case OID_INT4, OID_FLOAT4:
		return 4
	case OID_INT8, OID_FLOAT8:
		return 8
	case OID_TEXT:
		return -1
	default:
		// unknown, assume variable length
		return -1
	}
}

// The oids of the Postgres built-in types
const (
	// generated via
	// psql -qAt -F $'\t' -p 5434 postgres -c
	//   "select 'OID_' || upper(typname), '=' || oid from pg_type"
	OID_BOOL                                  uint32 = 16
	OID_BYTEA                                        = 17
	OID_CHAR                                         = 18
	OID_NAME                                         = 19
	OID_INT8                                         = 20
	OID_INT2                                         = 21
	OID_INT2VECTOR                                   = 22
	OID_INT4                                         = 23
	OID_REGPROC                                      = 24
	OID_TEXT                                         = 25<commit_msg>Placate Go "gc" compiler family flow analysis<commit_after>package pgproto

// GuessOids attemps to guess the Postgres oids for the given data
// values. It assumes that rows is a slice of uniform-length slices
// where each cell corresponds to a column. It returns a slice of oid
// values of the mapped oids, or OID_UNKNOWN where no mapping could
// be determined.
func GuessOids(rows [][]interface{}) (oids []uint32) {
	if len(rows) == 0 {
		// can;t really make much of a guess here
		return []uint32{}
	}
	oids = make([]uint32, len(rows[0]))
	for _, row := range rows {
		gotAll := true
		for i, _ := range oids {
			if o := oids[i]; o == 0 || o == OID_UNKNOWN {
				oids[i] = MappedOid(row[i])
				if oids[i] == OID_UNKNOWN {
					gotAll = false
				}
			}
		}
		if gotAll {
			break
		}
	}
	return oids
}

// MappedOid returns the Postgres oid mapped to the type of the given
// value in femebe, or OID_UNKNOWN if no mapping exists.
func MappedOid(val interface{}) uint32 {
	switch val.(type) {
	case nil:
		// we can't determine a type here
		return OID_UNKNOWN
	case int16:
		return OID_INT2
	case int32:
		return OID_INT4
	case int64:
		return OID_INT8
	case float32:
		return OID_FLOAT4
	case float64:
		return OID_FLOAT8
	case string:
		return OID_TEXT
	case bool:
		return OID_BOOL
	default:
		return OID_UNKNOWN
	}

	panic("Oh snap!")
}

// TypSize returns the size in bytes of the Postgres type specified by
// typOid, where undertood by femebe. For variable-length types or if
// the type is not known, -1 is returned.
func TypSize(typOid uint32) int16 {
	// TODO: right now, we hardcode the length of the various types
	// here; ideally, we should have a mapping for the fixed-length
	// types (although it seems that all the dynamic-length types,
	// even those with explicit length restrictions, is just -1, so
	// perhaps we can provide a mapping for that as well).
	switch typOid {
	case OID_BOOL:
		return 1
	case OID_INT2:
		return 2
	case OID_INT4, OID_FLOAT4:
		return 4
	case OID_INT8, OID_FLOAT8:
		return 8
	case OID_TEXT:
		return -1
	default:
		// unknown, assume variable length
		return -1
	}

	panic("Oh snap!")
}

// The oids of the Postgres built-in types
const (
	// generated via
	// psql -qAt -F $'\t' -p 5434 postgres -c
	//   "select 'OID_' || upper(typname), '=' || oid from pg_type"
	OID_BOOL                                  uint32 = 16
	OID_BYTEA                                        = 17
	OID_CHAR                                         = 18
	OID_NAME                                         = 19
	OID_INT8                                         = 20
	OID_INT2                                         = 21
	OID_INT2VECTOR                                   = 22
	OID_INT4                                         = 23
	OID_REGPROC                                      = 24
	OID_TEXT                                         = 25
{'fraction_non_alphanumeric': 0.08058286516853932, 'fraction_numerical': 0.025632022471910113, 'mean_word_length': 1.9051504334523202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2525826', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1780, 'n_words': 667}