MCPcopy Create free account
hub / github.com/apache/cloudberry / get_typavgwidth

Function get_typavgwidth

src/backend/utils/cache/lsyscache.c:3178–3221  ·  view source on GitHub ↗

* get_typavgwidth * * Given a type OID and a typmod value (pass -1 if typmod is unknown), * estimate the average width of values of the type. This is used by * the planner, which doesn't require absolutely correct results; * it's OK (and expected) to guess if we don't know for sure. */

Source from the content-addressed store, hash-verified

3176 * it's OK (and expected) to guess if we don't know for sure.
3177 */
3178int32
3179get_typavgwidth(Oid typid, int32 typmod)
3180{
3181 int typlen = get_typlen(typid);
3182 int32 maxwidth;
3183
3184 /*
3185 * Easy if it's a fixed-width type
3186 */
3187 if (typlen > 0)
3188 return typlen;
3189
3190 /*
3191 * type_maximum_size knows the encoding of typmod for some datatypes;
3192 * don't duplicate that knowledge here.
3193 */
3194 maxwidth = type_maximum_size(typid, typmod);
3195 if (maxwidth > 0)
3196 {
3197 /*
3198 * For BPCHAR, the max width is also the only width. Otherwise we
3199 * need to guess about the typical data width given the max. A sliding
3200 * scale for percentage of max width seems reasonable.
3201 */
3202 if (typid == BPCHAROID)
3203 return maxwidth;
3204 if (maxwidth <= 32)
3205 return maxwidth; /* assume full width */
3206 if (maxwidth < 1000)
3207 return 32 + (maxwidth - 32) / 2; /* assume 50% */
3208
3209 /*
3210 * Beyond 1000, assume we're looking at something like
3211 * "varchar(10000)" where the limit isn't actually reached often, and
3212 * use a fixed estimate.
3213 */
3214 return 32 + (1000 - 32) / 2;
3215 }
3216
3217 /*
3218 * Oops, we have no idea ... wild guess time.
3219 */
3220 return 32;
3221}
3222
3223/*
3224 * get_typtype

Callers 10

create_one_window_pathFunction · 0.85
compute_agg_clause_costsFunction · 0.85
set_rel_widthFunction · 0.85
set_append_rel_sizeFunction · 0.85
get_row_widthFunction · 0.85
add_row_identity_varFunction · 0.85
find_placeholder_infoFunction · 0.85
get_rel_data_widthFunction · 0.85

Calls 2

get_typlenFunction · 0.85
type_maximum_sizeFunction · 0.85

Tested by

no test coverage detected