Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pg_catalog: populate atttypmod for timestamp/interval types #111400

Merged
merged 1 commit into from Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/pg_catalog
Expand Up @@ -4006,6 +4006,46 @@ weird_array text[] '{a,"","b,c",a''::string,''::string,"a''
int_array bigint[] '{1,2}'::INT8[] false 1016 -1
varchar_array character varying(32)[] '{cat,dog}'::STRING[] false 1015 36

# Regression test for atttypmod being populated for timestamp types (#110787)
statement ok
CREATE TABLE timestamp_with_typmod(
a time(1) NULL,
b timetz(2) NULL,
c timestamp(3) NULL,
d timestamptz(4) NULL,
e interval(5) NULL,
f time NULL,
g timetz NULL,
h timestamp NULL,
i timestamptz NULL,
j interval NULL
);

query TTTBOI
SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = 'timestamp_with_typmod'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
----
a time(1) without time zone NULL false 1083 1
b time(2) with time zone NULL false 1266 2
c timestamp(3) without time zone NULL false 1114 3
d timestamp(4) with time zone NULL false 1184 4
e interval(5) NULL false 1186 5
f time without time zone NULL false 1083 -1
g time with time zone NULL false 1266 -1
h timestamp without time zone NULL false 1114 -1
i timestamp with time zone NULL false 1184 -1
j interval NULL false 1186 -1
rowid bigint unique_rowid() true 20 -1



# Regression test for limits on virtual index scans. (#53522)

let $testid
Expand Down Expand Up @@ -4467,7 +4507,7 @@ JOIN pg_class ON pg_statistic_ext.stxrelid = pg_class.oid
----
relname stxname stxnamespace stxowner stxstattarget stxkeys stxkind
stxtbl stxobj 105 NULL -1 {2,3} {d}
stxtbl2 stxobj2 193 NULL -1 {1,3} {d}
stxtbl2 stxobj2 194 NULL -1 {1,3} {d}
stx NULL 105 NULL -1 {2} {d}
stx NULL 105 NULL -1 {1} {d}

Expand Down
14 changes: 10 additions & 4 deletions pkg/sql/types/types.go
Expand Up @@ -1313,6 +1313,12 @@ func (t *T) TypeModifier() int32 {
if width := t.Width(); width != 0 {
return width
}
case TimestampFamily, TimestampTZFamily, TimeFamily, TimeTZFamily, IntervalFamily:
// For timestamp the precision is the type modifier value.
if !t.InternalType.TimePrecisionIsSet {
return -1
}
return t.Precision()
case DecimalFamily:
// attTypMod is calculated by putting the precision in the upper
// bits and the scale in the lower bits of a 32-bit int, and adding
Expand Down Expand Up @@ -1714,10 +1720,10 @@ func (t *T) SQLStandardNameWithTypmod(haveTypmod bool, typmod int) string {
panic(errors.AssertionFailedf("programming error: unknown int width: %d", t.Width()))
}
case IntervalFamily:
// TODO(jordan): intervals can have typmods, but we don't support them in the same way.
// Masking is used to extract the precision (src/include/utils/timestamp.h), whereas
// we store it as `IntervalDurationField`.
return "interval"
if !haveTypmod || typmod < 0 {
return "interval"
}
return fmt.Sprintf("interval(%d)", typmod)
case JsonFamily:
// Only binary JSON is currently supported.
return "jsonb"
Expand Down