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

lightning: introduce param to skip CSV header parsing #41128

Merged
merged 7 commits into from
Feb 8, 2023
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
2 changes: 2 additions & 0 deletions br/pkg/lightning/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ type CSVConfig struct {
Terminator string `toml:"terminator" json:"terminator"`
Null string `toml:"null" json:"null"`
Header bool `toml:"header" json:"header"`
SkipHeader bool `toml:"skip-header" json:"skip-header"`
TrimLastSep bool `toml:"trim-last-separator" json:"trim-last-separator"`
NotNull bool `toml:"not-null" json:"not-null"`
BackslashEscape bool `toml:"backslash-escape" json:"backslash-escape"`
Expand Down Expand Up @@ -746,6 +747,7 @@ func NewConfig() *Config {
Separator: ",",
Delimiter: `"`,
Header: true,
SkipHeader: false,
NotNull: false,
Null: `\N`,
BackslashEscape: true,
Expand Down
3 changes: 3 additions & 0 deletions br/pkg/lightning/mydump/csv_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ func (parser *CSVParser) ReadColumns() error {
if err != nil {
return errors.Trace(err)
}
if parser.cfg.SkipHeader {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move it earlier in case the first line contains bad CSV syntax? and use ReadUntilTernimator instead to skip one line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we still need to parse the whole record, rather than stop at the terminator. Suppose the header record covers several lines, if we just stop at the first line and count it as the header to skip. Then the parsing will face syntax error problem. But actually the CSV file is valid.

return nil
}
parser.columns = make([]string, 0, len(columns))
for _, colName := range columns {
colName, _, err = parser.unescapeString(colName)
Expand Down
4 changes: 3 additions & 1 deletion br/pkg/lightning/mydump/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ func SplitLargeFile(
if err = parser.ReadColumns(); err != nil {
return 0, nil, nil, err
}
columns = parser.Columns()
if !cfg.Mydumper.CSV.SkipHeader {
columns = parser.Columns()
}
startOffset, _ = parser.Pos()
endOffset = startOffset + maxRegionSize
if endOffset > dataFile.FileMeta.FileSize {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE testtbl (
id INTEGER PRIMARY KEY,
val1 VARCHAR(40) NOT NULL,
INDEX `idx_val1` (`val1`)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aaa,bbb
1,"aaa01"
2,"aaa01"
3,"aaa02"
4,"aaa02"
5,"aaa05"
9 changes: 9 additions & 0 deletions br/tests/lightning_config_skip_csv_header/err_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[lightning]
check-requirements=true

[mydumper.csv]
header = true
skip-header = false

[tikv-importer]
duplicate-resolution = 'remove'
9 changes: 9 additions & 0 deletions br/tests/lightning_config_skip_csv_header/normal_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[lightning]
check-requirements=true

[mydumper.csv]
header = true
skip-header = true

[tikv-importer]
duplicate-resolution = 'remove'
47 changes: 47 additions & 0 deletions br/tests/lightning_config_skip_csv_header/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
#
# Copyright 2023 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,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eux

mydir=$(dirname "${BASH_SOURCE[0]}")

data_file="${mydir}/data/mytest.testtbl.csv"

total_row_count=$( sed '1d' "${data_file}" | wc -l | xargs echo )

run_sql 'DROP TABLE IF EXISTS mytest.testtbl'

console_output_file="/tmp/${TEST_NAME}.out"

run_lightning --backend tidb --config "${mydir}/err_config.toml" 2>&1 | tee "${console_output_file}"
if [[ ${PIPESTATUS[0]} -eq 0 ]]; then
echo "The lightning import doesn't fail as expected" >&2
exit 1
fi

grep -q "Lightning:Restore:ErrUnknownColumns" "${console_output_file}"

# import a second time

run_sql 'DROP TABLE IF EXISTS mytest.testtbl'

run_lightning --backend tidb --config "${mydir}/normal_config.toml"

run_sql 'SELECT * FROM mytest.testtbl'
run_sql 'SELECT COUNT(*) FROM mytest.testtbl'
check_contains "COUNT(*): ${total_row_count}"
run_sql 'SELECT COUNT(*) FROM mytest.testtbl WHERE id > 0'
check_contains "COUNT(*): ${total_row_count}"