@@ -29,6 +29,7 @@ import (
29
29
"github.com/pingcap/errors"
30
30
"github.com/pingcap/tidb/pkg/errctx"
31
31
"github.com/pingcap/tidb/pkg/expression"
32
+ "github.com/pingcap/tidb/pkg/expression/contextstatic"
32
33
"github.com/pingcap/tidb/pkg/kv"
33
34
"github.com/pingcap/tidb/pkg/parser"
34
35
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -44,7 +45,6 @@ import (
44
45
"github.com/pingcap/tidb/pkg/util/dbterror"
45
46
"github.com/pingcap/tidb/pkg/util/hack"
46
47
"github.com/pingcap/tidb/pkg/util/logutil"
47
- "github.com/pingcap/tidb/pkg/util/mock"
48
48
"github.com/pingcap/tidb/pkg/util/ranger"
49
49
"github.com/pingcap/tidb/pkg/util/stringutil"
50
50
"go.uber.org/zap"
@@ -238,10 +238,30 @@ func initPartition(t *partitionedTable, def model.PartitionDefinition) (*partiti
238
238
return & newPart , nil
239
239
}
240
240
241
+ // NewPartitionExprBuildCtx returns a context to build partition expression.
242
+ func NewPartitionExprBuildCtx () expression.BuildContext {
243
+ return contextstatic .NewStaticExprContext (
244
+ contextstatic .WithEvalCtx (contextstatic .NewStaticEvalContext (
245
+ // Set a non-strict SQL mode and allow all date values if possible to make sure constant fold can work to
246
+ // estimate some undetermined result when locating a row to a partition.
247
+ // See issue: https://github.com/pingcap/tidb/issues/54271 for details.
248
+ contextstatic .WithSQLMode (mysql .ModeAllowInvalidDates ),
249
+ contextstatic .WithTypeFlags (types .StrictFlags .
250
+ WithIgnoreTruncateErr (true ).
251
+ WithIgnoreZeroDateErr (true ).
252
+ WithIgnoreZeroInDate (true ).
253
+ WithIgnoreInvalidDateErr (true ),
254
+ ),
255
+ contextstatic .WithErrLevelMap (errctx.LevelMap {
256
+ errctx .ErrGroupTruncate : errctx .LevelIgnore ,
257
+ }),
258
+ )),
259
+ )
260
+ }
261
+
241
262
func newPartitionExpr (tblInfo * model.TableInfo , tp model.PartitionType , expr string , partCols []model.CIStr , defs []model.PartitionDefinition ) (* PartitionExpr , error ) {
242
- // a partitioned table cannot rely on session context/sql modes, so use a default one!
243
- ctx := mock .NewContext ()
244
- dbName := model .NewCIStr (ctx .GetSessionVars ().CurrentDB )
263
+ ctx := NewPartitionExprBuildCtx ()
264
+ dbName := model .NewCIStr (ctx .GetEvalCtx ().CurrentDB ())
245
265
columns , names , err := expression .ColumnInfos2ColumnsAndNames (ctx , dbName , tblInfo .Name , tblInfo .Cols (), tblInfo )
246
266
if err != nil {
247
267
return nil , err
0 commit comments