Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

kv: fix handling of column default values #170

Merged
merged 2 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions lightning/kv/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ func (kvcodec *TableKVEncoder) Encode(
}

for i, col := range cols {
if j := columnPermutation[i]; j >= 0 {
if j := columnPermutation[i]; j >= 0 && j < len(row) {
value, err = table.CastValue(kvcodec.se, row[j], col.ToInfo())
if err == nil {
value, err = col.HandleBadNull(value, kvcodec.se.vars.StmtCtx)
}
} else if mysql.HasAutoIncrementFlag(col.Flag) {
// we still need a conversion, e.g. to catch overflow with a TINYINT column.
value, err = table.CastValue(kvcodec.se, types.NewIntDatum(rowID), col.ToInfo())
} else {
value, err = table.GetColOriginDefaultValue(kvcodec.se, col.ToInfo())
value, err = table.GetColDefaultValue(kvcodec.se, col.ToInfo())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not handle some cases like current_timestamp anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CURRENT_TIMESTAMP does work, it currently fetches the actual current time 🤔. (Before this PR it was always NULL.) Gonna change to a fixed timestamp stored in the checkpoint to ensure determinism in a future PR.

}
if err != nil {
return nil, errors.Trace(err)
Expand Down
18 changes: 18 additions & 0 deletions tests/default-columns/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[lightning]
check-requirements = false
file = "/tmp/lightning_test_result/lightning.log"
level = "debug"

[tikv-importer]
addr = "127.0.0.1:8808"

[mydumper]
data-source-dir = "tests/default-columns/data"

[tidb]
host = "127.0.0.1"
port = 4000
user = "root"
status-port = 10080
pd-addr = "127.0.0.1:2379"
log-level = "error"
1 change: 1 addition & 0 deletions tests/default-columns/data/defcol-schema-create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE defcol;
5 changes: 5 additions & 0 deletions tests/default-columns/data/defcol.t-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE t (
pk INT PRIMARY KEY AUTO_INCREMENT,
x INT NULL,
y INT NOT NULL DEFAULT 123
);
1 change: 1 addition & 0 deletions tests/default-columns/data/defcol.t.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO t () VALUES (), (), (), (), (), ();
1 change: 1 addition & 0 deletions tests/default-columns/data/defcol.t.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO t VALUES (), (), ();
4 changes: 4 additions & 0 deletions tests/default-columns/data/defcol.u-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE u (
xx INT UNIQUE AUTO_INCREMENT,
yy INT PRIMARY KEY
);
1 change: 1 addition & 0 deletions tests/default-columns/data/defcol.u.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO u (yy) VALUES (40), (60);
33 changes: 33 additions & 0 deletions tests/default-columns/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

run_sql 'DROP DATABASE IF EXISTS defcol'

run_lightning

run_sql 'SELECT min(pk), count(pk) FROM defcol.t'
check_contains 'min(pk): 1'
check_contains 'count(pk): 9'

run_sql 'SELECT pk FROM defcol.t WHERE x IS NOT NULL OR y <> 123'
check_not_contains 'pk:'

run_sql 'SELECT xx FROM defcol.u WHERE yy = 40'
check_contains 'xx: 1'

run_sql 'SELECT xx FROM defcol.u WHERE yy = 60'
check_contains 'xx: 2'