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

ddl: use the correct timezone to encode record for adding index #46055

Merged
merged 7 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ddl/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func FetchChunk4Test(copCtx *copContext, tbl table.PhysicalTable, startKey, endK
}

func ConvertRowToHandleAndIndexDatum(row chunk.Row, copCtx *copContext) (kv.Handle, []types.Datum, error) {
idxData := extractDatumByOffsets(row, copCtx.idxColOutputOffsets, copCtx.expColInfos, nil)
handleData := extractDatumByOffsets(row, copCtx.handleOutputOffsets, copCtx.expColInfos, nil)
sVars := copCtx.sessCtx.GetSessionVars()
idxData := extractDatumByOffsets(row, copCtx.idxColOutputOffsets, copCtx.expColInfos, sVars, nil)
handleData := extractDatumByOffsets(row, copCtx.handleOutputOffsets, copCtx.expColInfos, sVars, nil)
handle, err := buildHandle(handleData, copCtx.tblInfo, copCtx.pkInfo, &stmtctx.StatementContext{TimeZone: time.Local})
return handle, idxData, err
}
4 changes: 2 additions & 2 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,8 +1679,8 @@ func writeChunkToLocal(writer ingest.Writer,
defer unlock()
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
idxDataBuf, handleDataBuf = idxDataBuf[:0], handleDataBuf[:0]
idxDataBuf = extractDatumByOffsets(row, copCtx.idxColOutputOffsets, copCtx.expColInfos, idxDataBuf)
handleDataBuf := extractDatumByOffsets(row, copCtx.handleOutputOffsets, copCtx.expColInfos, handleDataBuf)
idxDataBuf = extractDatumByOffsets(row, copCtx.idxColOutputOffsets, copCtx.expColInfos, vars, idxDataBuf)
handleDataBuf := extractDatumByOffsets(row, copCtx.handleOutputOffsets, copCtx.expColInfos, vars, handleDataBuf)
handle, err := buildHandle(handleDataBuf, copCtx.tblInfo, copCtx.pkInfo, sCtx)
if err != nil {
return 0, nil, errors.Trace(err)
Expand Down
22 changes: 20 additions & 2 deletions ddl/index_cop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"context"
"encoding/hex"
"sync"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
Expand All @@ -28,6 +29,7 @@
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
Expand Down Expand Up @@ -506,10 +508,26 @@
return &tipb.Executor{Tp: tipb.ExecType_TypeTableScan, TblScan: tblScan}, err
}

func extractDatumByOffsets(row chunk.Row, offsets []int, expCols []*expression.Column, buf []types.Datum) []types.Datum {
func extractDatumByOffsets(
row chunk.Row,
offsets []int,
expCols []*expression.Column,
vars *variable.SessionVars,
buf []types.Datum,
) []types.Datum {
for _, offset := range offsets {
c := expCols[offset]
rowDt := row.GetDatum(offset, c.GetType())
ft := c.GetType()
rowDt := row.GetDatum(offset, ft)
if ft.GetType() == mysql.TypeTimestamp && vars.TimeZone != time.UTC {
// Convert from utc to current timezone.
t := rowDt.GetMysqlTime()
err := t.ConvertTimeZone(time.UTC, vars.TimeZone)
if err != nil {
logutil.BgLogger().Warn("convert timestamp timezone failed", zap.Error(err))
}

Check warning on line 528 in ddl/index_cop.go

View check run for this annotation

Codecov / codecov/patch

ddl/index_cop.go#L527-L528

Added lines #L527 - L528 were not covered by tests
rowDt.SetMysqlTime(t)
}
buf = append(buf, rowDt)
}
return buf
Expand Down
2 changes: 1 addition & 1 deletion ddl/ingest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ go_test(
embed = [":ingest"],
flaky = True,
race = "on",
shard_count = 14,
shard_count = 15,
deps = [
"//config",
"//ddl",
Expand Down
13 changes: 13 additions & 0 deletions ddl/ingest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,16 @@ func TestAddIndexIngestRecoverPartition(t *testing.T) {
tk.MustExec("alter table t add index idx(b);")
tk.MustExec("admin check table t;")
}

func TestAddIndexIngestTimezone(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
defer injectMockBackendMgr(t, store)()

tk.MustExec("SET time_zone = '-06:00';")
tk.MustExec("create table t (`src` varchar(48),`t` timestamp,`timezone` varchar(100));")
tk.MustExec("insert into t values('2000-07-29 23:15:30 -0600','2000-07-29 23:15:30 -0600','-6:00');")
tk.MustExec("alter table t add index idx(t);")
tk.MustExec("admin check table t;")
}