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

KBC-1240 fix casting and nullability in Synapse CTAS queries #85

Merged
merged 2 commits into from
Dec 1, 2021
Merged
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
195 changes: 116 additions & 79 deletions src/Backend/Synapse/ToFinalTable/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,53 +115,11 @@ public function getCtasDedupCommand(
}
$createTableColumns = $this->getColumnsString($columnsInOrder, ',', 'a');

$columnsSetSql = [];
/** @var SynapseColumn $column */
foreach ($stagingTableDefinition->getColumnsDefinitions() as $column) {
$columnTypeDefinition = $this->getColumnTypeSqlDefinition($column);

if (in_array($column->getColumnName(), $importOptions->getConvertEmptyValuesToNull(), true)) {
$colSql = sprintf(
'NULLIF(%s, \'\')',
SynapseQuote::quoteSingleIdentifier($column->getColumnName())
);
if ($column->getColumnDefinition()->getBasetype() !== BaseType::STRING) {
$colSql = SynapseQuote::quoteSingleIdentifier($column->getColumnName());
}
if ($importOptions->getCastValueTypes()) {
$colSql = sprintf(
'CAST(%s as %s)',
$colSql,
$columnTypeDefinition
);
}
$columnsSetSql[] = sprintf(
'%s AS %s',
$colSql,
SynapseQuote::quoteSingleIdentifier($column->getColumnName())
);
} else {
$colSql = sprintf(
'COALESCE(%s, \'\')',
SynapseQuote::quoteSingleIdentifier($column->getColumnName())
);
if ($column->getColumnDefinition()->getBasetype() !== BaseType::STRING) {
$colSql = SynapseQuote::quoteSingleIdentifier($column->getColumnName());
}
if ($importOptions->getCastValueTypes()) {
$colSql = sprintf(
'CAST(%s as %s)',
$colSql,
$columnTypeDefinition
);
}
$columnsSetSql[] = sprintf(
'%s AS %s',
$colSql,
SynapseQuote::quoteSingleIdentifier($column->getColumnName())
);
}
}
$columnsSetSql = $this->getCTASColumnsSetSql(
$stagingTableDefinition,
$destinationTableDefinition,
$importOptions
);

if ($useTimestamp) {
$columnsSetSql[] = sprintf(
Expand Down Expand Up @@ -376,38 +334,11 @@ public function getCTASInsertAllIntoTargetTableCommand(
true
) && $importOptions->useTimestamp();

$columnsSetSql = [];
/** @var SynapseColumn $columnDefinition */
foreach ($sourceTableDefinition->getColumnsDefinitions() as $columnDefinition) {
if (in_array($columnDefinition->getColumnName(), $importOptions->getConvertEmptyValuesToNull(), true)) {
// use nullif only for string base type
if ($columnDefinition->getColumnDefinition()->getBasetype() === BaseType::STRING) {
$columnsSetSql[] = sprintf(
'NULLIF(%s, \'\') AS %s',
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName()),
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName())
);
} else {
$columnsSetSql[] = SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName());
}
} elseif ($columnDefinition->getColumnDefinition()->getBasetype() === BaseType::STRING) {
$columnsSetSql[] = sprintf(
'CAST(COALESCE(%s, \'\') as %s) AS %s',
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName()),
$this->getColumnTypeSqlDefinition($columnDefinition),
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName())
);
} else {
// on columns other than string dont use COALESCE, use direct cast
// this will fail if the column is not null, but this is expected
$columnsSetSql[] = sprintf(
'CAST(%s as %s) AS %s',
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName()),
$this->getColumnTypeSqlDefinition($columnDefinition),
SynapseQuote::quoteSingleIdentifier($columnDefinition->getColumnName())
);
}
}
$columnsSetSql = $this->getCTASColumnsSetSql(
$sourceTableDefinition,
$destinationTableDefinition,
$importOptions
);

if ($useTimestamp) {
$columnsSetSql[] = sprintf(
Expand Down Expand Up @@ -595,4 +526,110 @@ private function getColumnTypeSqlDefinition(SynapseColumn $column): string
}
return $columnTypeDefinition;
}

/**
* @return string[]
*/
private function getCTASColumnsSetSql(
SynapseTableDefinition $sourceTableDefinition,
SynapseTableDefinition $destinationTableDefinition,
SynapseImportOptions $importOptions
): array {
$columnsSetSql = [];
/** @var SynapseColumn $column */
foreach ($sourceTableDefinition->getColumnsDefinitions() as $column) {
$destinationColumn = null;
/** @var SynapseColumn $col */
foreach ($destinationTableDefinition->getColumnsDefinitions() as $col) {
if ($col->getColumnName() === $column->getColumnName()) {
$destinationColumn = $col;
break;
}
}
assert($destinationColumn !== null);
$columnTypeDefinition = $this->getColumnTypeSqlDefinition($destinationColumn);
if (in_array($column->getColumnName(), $importOptions->getConvertEmptyValuesToNull(), true)) {
$columnsSetSql[] = $this->getCTASColumnSetSQLWithConvertEmptyValues(
$column,
$importOptions,
$columnTypeDefinition,
$destinationColumn
);
} else {
$columnsSetSql[] = $this->getCTASColumnSetSQL(
$column,
$importOptions,
$columnTypeDefinition,
$destinationColumn
);
}
}
return $columnsSetSql;
}

private function getCTASColumnSetSQLWithConvertEmptyValues(
SynapseColumn $source,
SynapseImportOptions $importOptions,
string $columnTypeDefinition,
SynapseColumn $destinationColumn
): string {
if ($source->getColumnDefinition()->getBasetype() === BaseType::STRING) {
// convert nulls only for strings
$colSql = sprintf(
'NULLIF(%s, \'\')',
SynapseQuote::quoteSingleIdentifier($source->getColumnName())
);
} else {
$colSql = SynapseQuote::quoteSingleIdentifier($source->getColumnName());
}
if ($importOptions->getCastValueTypes()) {
$colSql = sprintf(
'CAST(%s as %s)',
$colSql,
$columnTypeDefinition
);
}
return sprintf(
'%s AS %s',
$colSql,
SynapseQuote::quoteSingleIdentifier($destinationColumn->getColumnName())
);
}

private function getCTASColumnSetSQL(
SynapseColumn $sourceColumn,
SynapseImportOptions $importOptions,
string $columnTypeDefinition,
SynapseColumn $destinationColumn
): string {
if ($importOptions->getCastValueTypes()) {
$colSql = sprintf(
'CAST(%s as %s)',
SynapseQuote::quoteSingleIdentifier($sourceColumn->getColumnName()),
$columnTypeDefinition
);
} else {
$colSql = SynapseQuote::quoteSingleIdentifier($sourceColumn->getColumnName());
}
if ($sourceColumn->getColumnDefinition()->getBasetype() === BaseType::STRING) {
if ($destinationColumn->getColumnDefinition()->isNullable()) {
// Columns with coalesce are nullable in CTAS
$colSql = sprintf(
'COALESCE(%s, \'\')',
$colSql
);
} else {
// Columns with isnull are not null in CTAS
$colSql = sprintf(
'ISNULL(%s, \'\')',
Copy link
Contributor

Choose a reason for hiding this comment

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

pokud jsem to rozmotal spravne, tak tady ta vetev (a duvod celeho PR) neni otestovana v testech (vsechny destination column tam jsou nullable), tak to bych doplnil (nebo me vyved z omylu)

$colSql
);
}
}
return sprintf(
'%s AS %s',
$colSql,
SynapseQuote::quoteSingleIdentifier($destinationColumn->getColumnName())
);
}
}
Loading