Repository: pingcap/tidb (Go)

Issue:
@@global.default_week_format was not immediately loaded on new session, causing week() to use the wrong mode
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

First, change the global default_week_format

```sh
mysql -u root -h 127.0.0.1 -P 4000 -e "set global default_week_format = 4"
```

Then execute week() in a new connection.

```sh
mysql -u root -h 127.0.0.1 -P 4000 -E -e "select week('2020-02-02'), @@default_week_format, week('2020-02-02')"
```

### 2. What did you expect to see? (Required)

```
*************************** 1. row ***************************
   week('2020-02-02'): 6
@@default_week_format: 4
   week('2020-02-02'): 6
```

### 3. What did you see instead (Required)

```
*************************** 1. row ***************************
   week('2020-02-02'): 5
@@default_week_format: 4
   week('2020-02-02'): 6
```

The first execution of `week('2020-02-02')` used mode 0 instead of 4.

### 4. What is your TiDB version? (Required)

<!-- Paste the output of SELECT tidb_version() -->

Repro with both v4.0.8 and nightly 62fd2b735934e67580fefade0584e9eeb63f32d3

```
Release Version: v4.0.8
Edition: Community
Git Commit Hash: 66ac9fc31f1733e5eb8d11891ec1b38f9c422817
Git Branch: heads/refs/tags/v4.0.8
UTC Build Time: 2020-10-30 08:21:16
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
```

```
Release Version: v4.0.0-beta.2-1734-g62fd2b735
Edition: Community
Git Commit Hash: 62fd2b735934e67580fefade0584e9eeb63f32d3
Git Branch: master
UTC Build Time: 2020-12-04 12:32:58
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
```


Discussion:
/label  component/session,sig/DDL
## Please edit this comment or add a new comment to complete the following information

### Not a bug

1. Remove the 'type/bug' label
2. Add notes to indicate why it is not a bug

### Duplicate bug

1. Add the 'type/duplicate' label
2. Add the link to the original bug

### Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: https://github.com/pingcap/tidb/issues/20100

#### 1. Root Cause Analysis (RCA) (optional) 
<!-- Write down the reason why this bug occurs -->

#### 2. Symptom (optional)

<!-- What will the user see when this bug occurs. The error message may be in the terminal, log or monitoring -->

#### 3. All Trigger Conditions (optional)

<!-- All the user scenarios that may trigger this bug -->

#### 4. Workaround (optional)

#### 5. Affected versions

<!--
In the format of [start_version:end_version], multiple version ranges are
accepted. If the bug only affects the unreleased version, please input:
"unreleased". For example:

Notes:
  1. Do not use any white spaces in '[]'.
  2. The range in '[]' is a closed interval
  3. The version format is `v$Major.$Minor.$Patch`, the $Majoy and $Minor
     number in a version range should be the same. [v3.0.1:v3.1.2] is
     invalid because the $Minor number of the version range is different.

Example 1: [v3.0.1:v3.0.5], [v4.0.1:v4.0.5]
Example 2: unreleased
-->

#### 6. Fixed versions

<!--
The first released version that contains this fix in each minor version. If the bug's affected version has been released, the fixed version should be a detailed version number; If the bug doesn't affect any released version, the fixed version can be "master";  

Example 1: v3.0.13, v4.0.5
Example 2: master
-->



Patch:
diff --git a/session/session.go b/session/session.go
index a7c1b20a0df8f..82c872759f6cb 100644
--- a/session/session.go
+++ b/session/session.go
@@ -2209,6 +2209,7 @@ var builtinGlobalVariable = []string{
 	variable.InnodbLockWaitTimeout,
 	variable.WindowingUseHighPrecision,
 	variable.SQLSelectLimit,
+	variable.DefaultWeekFormat,
 
 	/* TiDB specific global variables: */
 	variable.TiDBSkipASCIICheck,


Test patch:
diff --git a/session/session_test.go b/session/session_test.go
index 496cf4aec27f5..3133f76457249 100644
--- a/session/session_test.go
+++ b/session/session_test.go
@@ -3816,3 +3816,13 @@ func (s *testSessionSerialSuite) TestCoprocessorOOMAction(c *C) {
 		se.Close()
 	}
 }
+
+// TestDefaultWeekFormat checks for issue #21510.
+func (s *testSessionSerialSuite) TestDefaultWeekFormat(c *C) {
+	tk1 := testkit.NewTestKitWithInit(c, s.store)
+	tk1.MustExec("set @@global.default_week_format = 4;")
+	defer tk1.MustExec("set @@global.default_week_format = default;")
+
+	tk2 := testkit.NewTestKitWithInit(c, s.store)
+	tk2.MustQuery("select week('2020-02-02'), @@default_week_format, week('2020-02-02');").Check(testkit.Rows("6 4 6"))
+}