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

Add ability to set character set for load data infile. #1942

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions classes/ETL/Ingestor/IngestorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function __construct(array $options = null)
// INFILE...REPLACE INTO instead.
"force_load_data_infile_replace_into" => false,

// Character set override to use when loading data via a file.
"load_data_infile_character_set" => null,

// Hide all SQL warnings returned by the database.
"hide_sql_warnings" => false,

Expand Down
11 changes: 11 additions & 0 deletions classes/ETL/Ingestor/pdoIngestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ private function multiDatabaseIngest()
// Keys are table columns (destination) and values are query result columns (source)
$destColumnList = array_keys($destFieldToSourceFieldMap);

// The mysql documentation claims that file contents are interpreted using the character set
// in the character_set_database system variable. However, I was not able to get this to work
// Explicitly setting the CHARACTER SET does appear to work though.

$characterSetOverride = '';
if ( $this->options->load_data_infile_character_set ) {
$characterSetOverride = "CHARACTER SET '" . $this->options->load_data_infile_character_set . "' ";
}

// The default method for ingestion is INSERT INTO ON DUPLICATE KEY UPDATE because tests
// have shown an approx 40% performance improvement when updating existing data over
// REPLACE INTO. REPLACE INTO also may cause issues with auto increment keys because
Expand All @@ -615,6 +624,7 @@ private function multiDatabaseIngest()

if ( $this->options->force_load_data_infile_replace_into ) {
$loadStatement = "LOAD DATA LOCAL INFILE '$infileName' replace into table $qualifiedDestTableName "
. $characterSetOverride
. "FIELDS TERMINATED BY " . sprintf("0x%02x", ord($this->fieldSeparator))
. " OPTIONALLY ENCLOSED BY " . sprintf("0x%02x", ord($this->stringEnclosure))
. " ESCAPED BY " . sprintf("0x%02x", ord($this->escapeChar))
Expand All @@ -639,6 +649,7 @@ function ($s) {
$loadStatement = "CREATE TABLE $tmpTable LIKE $qualifiedDestTableName; "
. "ALTER TABLE $tmpTable DISABLE KEYS; "
. "LOAD DATA LOCAL INFILE '$infileName' INTO TABLE $tmpTable "
. $characterSetOverride
. "FIELDS TERMINATED BY " . sprintf("0x%02x", ord($this->fieldSeparator))
. " OPTIONALLY ENCLOSED BY " . sprintf("0x%02x", ord($this->stringEnclosure))
. " ESCAPED BY " . sprintf("0x%02x", ord($this->escapeChar))
Expand Down