Skip to content

Commit

Permalink
Merge #68352
Browse files Browse the repository at this point in the history
68352: sql: gate DateStyle behind datestyle_enabled session variable  r=rafiss a=otan

See individual commits for details.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Aug 8, 2021
2 parents cab185f + 6b543ce commit 049ed14
Show file tree
Hide file tree
Showing 20 changed files with 334 additions and 81 deletions.
3 changes: 2 additions & 1 deletion docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ sql.cross_db_sequence_owners.enabled boolean false if true, creating sequences o
sql.cross_db_views.enabled boolean false if true, creating views that refer to other databases is allowed
sql.defaults.copy_partitioning_when_deinterleaving_table.enabled boolean false default value for enable_copying_partitioning_when_deinterleaving_table session variable
sql.defaults.datestyle enumeration iso, mdy default value for DateStyle session setting [iso, mdy = 0, iso, dmy = 1, iso, ymd = 2]
sql.defaults.datestyle.enabled boolean false default value for datestyle_enabled session setting
sql.defaults.default_int_size integer 8 the size, in bytes, of an INT type
sql.defaults.disallow_full_table_scans.enabled boolean false setting to true rejects queries that have planned a full table scan
sql.defaults.distsql enumeration auto default distributed SQL execution mode [off = 0, auto = 1, on = 2]
Expand All @@ -92,7 +93,7 @@ sql.defaults.implicit_select_for_update.enabled boolean true default value for e
sql.defaults.insert_fast_path.enabled boolean true default value for enable_insert_fast_path session setting; enables a specialized insert path
sql.defaults.interleaved_tables.enabled boolean false allows creation of interleaved tables or indexes
sql.defaults.intervalstyle enumeration postgres default value for IntervalStyle session setting [postgres = 0, iso_8601 = 1, sql_standard = 2]
sql.defaults.intervalstyle.enabled boolean false default value for enable_intervalstyle session setting
sql.defaults.intervalstyle.enabled boolean false default value for intervalstyle_enabled session setting
sql.defaults.locality_optimized_partitioned_index_scan.enabled boolean true default value for locality_optimized_partitioned_index_scan session setting; enables searching for rows in the current region before searching remote regions
sql.defaults.optimizer_use_histograms.enabled boolean true default value for optimizer_use_histograms session setting; enables usage of histograms in the optimizer by default
sql.defaults.optimizer_use_multicol_stats.enabled boolean true default value for optimizer_use_multicol_stats session setting; enables usage of multi-column stats in the optimizer by default
Expand Down
3 changes: 2 additions & 1 deletion docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<tr><td><code>sql.cross_db_views.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating views that refer to other databases is allowed</td></tr>
<tr><td><code>sql.defaults.copy_partitioning_when_deinterleaving_table.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for enable_copying_partitioning_when_deinterleaving_table session variable</td></tr>
<tr><td><code>sql.defaults.datestyle</code></td><td>enumeration</td><td><code>iso, mdy</code></td><td>default value for DateStyle session setting [iso, mdy = 0, iso, dmy = 1, iso, ymd = 2]</td></tr>
<tr><td><code>sql.defaults.datestyle.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for datestyle_enabled session setting</td></tr>
<tr><td><code>sql.defaults.default_int_size</code></td><td>integer</td><td><code>8</code></td><td>the size, in bytes, of an INT type</td></tr>
<tr><td><code>sql.defaults.disallow_full_table_scans.enabled</code></td><td>boolean</td><td><code>false</code></td><td>setting to true rejects queries that have planned a full table scan</td></tr>
<tr><td><code>sql.defaults.distsql</code></td><td>enumeration</td><td><code>auto</code></td><td>default distributed SQL execution mode [off = 0, auto = 1, on = 2]</td></tr>
Expand All @@ -96,7 +97,7 @@
<tr><td><code>sql.defaults.insert_fast_path.enabled</code></td><td>boolean</td><td><code>true</code></td><td>default value for enable_insert_fast_path session setting; enables a specialized insert path</td></tr>
<tr><td><code>sql.defaults.interleaved_tables.enabled</code></td><td>boolean</td><td><code>false</code></td><td>allows creation of interleaved tables or indexes</td></tr>
<tr><td><code>sql.defaults.intervalstyle</code></td><td>enumeration</td><td><code>postgres</code></td><td>default value for IntervalStyle session setting [postgres = 0, iso_8601 = 1, sql_standard = 2]</td></tr>
<tr><td><code>sql.defaults.intervalstyle.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for enable_intervalstyle session setting</td></tr>
<tr><td><code>sql.defaults.intervalstyle.enabled</code></td><td>boolean</td><td><code>false</code></td><td>default value for intervalstyle_enabled session setting</td></tr>
<tr><td><code>sql.defaults.locality_optimized_partitioned_index_scan.enabled</code></td><td>boolean</td><td><code>true</code></td><td>default value for locality_optimized_partitioned_index_scan session setting; enables searching for rows in the current region before searching remote regions</td></tr>
<tr><td><code>sql.defaults.optimizer_use_histograms.enabled</code></td><td>boolean</td><td><code>true</code></td><td>default value for optimizer_use_histograms session setting; enables usage of histograms in the optimizer by default</td></tr>
<tr><td><code>sql.defaults.optimizer_use_multicol_stats.enabled</code></td><td>boolean</td><td><code>true</code></td><td>default value for optimizer_use_multicol_stats session setting; enables usage of multi-column stats in the optimizer by default</td></tr>
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,7 @@ func (ex *connExecutor) resetPlanner(
p.semaCtx = tree.MakeSemaContext()
p.semaCtx.SearchPath = ex.sessionData.SearchPath
p.semaCtx.IntervalStyleEnabled = ex.sessionData.IntervalStyleEnabled
p.semaCtx.DateStyleEnabled = ex.sessionData.DateStyleEnabled
p.semaCtx.Annotations = nil
p.semaCtx.TypeResolver = p
p.semaCtx.TableNameResolver = p
Expand Down
18 changes: 16 additions & 2 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,20 @@ var dateStyle = settings.RegisterEnumSetting(
).WithPublic()

// intervalStyleEnabled controls intervals representation.
// TODO(#sql-experience): remove session setting in v21.1 and have this
// TODO(#sql-experience): remove session setting in v22.1 and have this
// always enabled.
var intervalStyleEnabled = settings.RegisterBoolSetting(
"sql.defaults.intervalstyle.enabled",
"default value for enable_intervalstyle session setting",
"default value for intervalstyle_enabled session setting",
false,
).WithPublic()

// dateStyleEnabled controls dates representation.
// TODO(#sql-experience): remove session setting in v22.1 and have this
// always enabled.
var dateStyleEnabled = settings.RegisterBoolSetting(
"sql.defaults.datestyle.enabled",
"default value for datestyle_enabled session setting",
false,
).WithPublic()

Expand Down Expand Up @@ -2629,6 +2638,11 @@ func (m *sessionDataMutator) SetIntervalStyleEnabled(enabled bool) {
m.data.IntervalStyleEnabled = enabled
}

// SetDateStyleEnabled sets the DateStyleEnabled for the given session.
func (m *sessionDataMutator) SetDateStyleEnabled(enabled bool) {
m.data.DateStyleEnabled = enabled
}

// SetStubCatalogTablesEnabled sets default value for stub_catalog_tables.
func (m *sessionDataMutator) SetStubCatalogTablesEnabled(enabled bool) {
m.data.StubCatalogTablesEnabled = enabled
Expand Down
39 changes: 39 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/datetime
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,45 @@ true

subtest datestyle_order

statement error setting DateStyle is not enabled
set datestyle = 'dmy'

statement error setting DateStyle is not enabled
set datestyle = 'ymd'

statement ok
set datestyle_enabled = true

statement error context-dependent operators are not allowed in computed column\nHINT: TIMESTAMP to STRING casts are dependent on DateStyle; consider using to_char\(timestamp\) instead\.
CREATE TABLE invalid_table (
invalid_col string AS ('2020-05-12 10:12:13'::timestamp::string) STORED
)

statement error context-dependent operators are not allowed in computed column\nHINT: DATE to STRING casts are dependent on DateStyle; consider using to_char\(date\) instead\.
CREATE TABLE invalid_table (
invalid_col string AS ('2020-05-12 10:12:13'::date::string) STORED
)

statement error context-dependent operators are not allowed in computed column\nHINT: STRING to TIMESTAMP casts are context-dependent because of relative timestamp strings like 'now' and session settings such as DateStyle; use parse_timestamp\(string\) instead\.
CREATE TABLE invalid_table (
invalid_col timestamp AS ('2020-05-12 10:12:13'::string::timestamp) STORED
)

statement error context-dependent operators are not allowed in computed column\nHINT: STRING to DATE casts depend on session DateStyle; use parse_date\(string\) instead
CREATE TABLE invalid_table (
invalid_col date AS ('2020-05-12 10:12:13'::string::date) STORED
)

statement error context-dependent operators are not allowed in computed column\nHINT: STRING to TIME casts depend on session DateStyle; use parse_time\(string\) instead
CREATE TABLE invalid_table (
invalid_col time AS ('2020-05-12 10:12:13'::string::time) STORED
)

statement error context-dependent operators are not allowed in computed column\nHINT: STRING to TIMETZ casts depend on session DateStyle; use parse_timetz\(string\) instead
CREATE TABLE invalid_table (
invalid_col timetz AS ('2020-05-12 10:12:13'::string::timetz) STORED
)

statement ok
set datestyle = 'dmy'

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -4517,6 +4517,7 @@ client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
datestyle_enabled off
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -3836,6 +3836,7 @@ client_encoding UTF8 NULL
client_min_messages notice NULL NULL NULL string
database test NULL NULL NULL string
datestyle ISO, MDY NULL NULL NULL string
datestyle_enabled off NULL NULL NULL string
default_int_size 8 NULL NULL NULL string
default_tablespace · NULL NULL NULL string
default_transaction_isolation serializable NULL NULL NULL string
Expand Down Expand Up @@ -3922,6 +3923,7 @@ client_encoding UTF8 NULL
client_min_messages notice NULL user NULL notice notice
database test NULL user NULL · test
datestyle ISO, MDY NULL user NULL ISO, MDY ISO, MDY
datestyle_enabled off NULL user NULL off off
default_int_size 8 NULL user NULL 8 8
default_tablespace · NULL user NULL · ·
default_transaction_isolation serializable NULL user NULL default default
Expand Down Expand Up @@ -4004,6 +4006,7 @@ client_min_messages NULL NULL NULL
crdb_version NULL NULL NULL NULL NULL
database NULL NULL NULL NULL NULL
datestyle NULL NULL NULL NULL NULL
datestyle_enabled NULL NULL NULL NULL NULL
default_int_size NULL NULL NULL NULL NULL
default_tablespace NULL NULL NULL NULL NULL
default_transaction_isolation NULL NULL NULL NULL NULL
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/set
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ SHOW datestyle
----
ISO, MDY

statement ok
SET datestyle_enabled = true

statement ok
SET datestyle = 'ymd'

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ client_encoding UTF8
client_min_messages notice
database test
datestyle ISO, MDY
datestyle_enabled off
default_int_size 8
default_tablespace ·
default_transaction_isolation serializable
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type Memo struct {
safeUpdates bool
preferLookupJoinsForFKs bool
saveTablesPrefix string
dateStyleEnabled bool
intervalStyleEnabled bool
dateStyle pgdate.DateStyle
intervalStyle duration.IntervalStyle
Expand Down Expand Up @@ -183,6 +184,7 @@ func (m *Memo) Init(evalCtx *tree.EvalContext) {
preferLookupJoinsForFKs: evalCtx.SessionData.PreferLookupJoinsForFKs,
saveTablesPrefix: evalCtx.SessionData.SaveTablesPrefix,
intervalStyleEnabled: evalCtx.SessionData.IntervalStyleEnabled,
dateStyleEnabled: evalCtx.SessionData.DateStyleEnabled,
dateStyle: evalCtx.SessionData.GetDateStyle(),
intervalStyle: evalCtx.SessionData.GetIntervalStyle(),
}
Expand Down Expand Up @@ -294,6 +296,7 @@ func (m *Memo) IsStale(
m.preferLookupJoinsForFKs != evalCtx.SessionData.PreferLookupJoinsForFKs ||
m.saveTablesPrefix != evalCtx.SessionData.SaveTablesPrefix ||
m.intervalStyleEnabled != evalCtx.SessionData.IntervalStyleEnabled ||
m.dateStyleEnabled != evalCtx.SessionData.DateStyleEnabled ||
m.dateStyle != evalCtx.SessionData.GetDateStyle() ||
m.intervalStyle != evalCtx.SessionData.GetIntervalStyle() {
return true, nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/opt/memo/memo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func TestMemoIsStale(t *testing.T) {
evalCtx.SessionData.IntervalStyleEnabled = false
notStale()

// Stale dateStyleEnabled.
evalCtx.SessionData.DateStyleEnabled = true
stale()
evalCtx.SessionData.DateStyleEnabled = false
notStale()

// Stale DateStyle.
evalCtx.SessionData.DataConversionConfig.DateStyle = pgdate.DateStyle{Order: pgdate.Order_YMD}
stale()
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/pgwire/testdata/pgtest/param_status
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ ReadyForQuery
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SET DateStyle_enabled = true"}
----

until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

until noncrdb_only
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"42704"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SET DateStyle = 'YMD, ISO'"}
----
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func newInternalPlanner(
p.semaCtx = tree.MakeSemaContext()
p.semaCtx.SearchPath = sd.SearchPath
p.semaCtx.IntervalStyleEnabled = sd.IntervalStyleEnabled
p.semaCtx.DateStyleEnabled = sd.DateStyleEnabled
p.semaCtx.TypeResolver = p
p.semaCtx.DateStyle = sd.GetDateStyle()
p.semaCtx.IntervalStyle = sd.GetIntervalStyle()
Expand Down
Loading

0 comments on commit 049ed14

Please sign in to comment.