-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnowflakeImportAdapter.php
More file actions
142 lines (130 loc) · 4.42 KB
/
SnowflakeImportAdapter.php
File metadata and controls
142 lines (130 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Keboola\Db\ImportExport\Storage\ABS;
use Generator;
use Keboola\Csv\CsvOptions;
use Keboola\Db\ImportExport\Backend\ImporterInterface;
use Keboola\Db\ImportExport\Backend\ImportState;
use Keboola\Db\ImportExport\ImportOptions;
use Keboola\Db\ImportExport\Backend\Snowflake\SnowflakeImportAdapterInterface;
use Keboola\Db\ImportExport\Storage\DestinationInterface;
use Keboola\Db\ImportExport\Storage\Snowflake\Table;
use Keboola\Db\ImportExport\Storage\SourceInterface;
use Keboola\SnowflakeDbAdapter\Connection;
use Keboola\SnowflakeDbAdapter\QueryBuilder;
class SnowflakeImportAdapter implements SnowflakeImportAdapterInterface
{
/**
* @var SourceFile
*/
private $source;
/**
* @param SourceFile $source
*/
public function __construct(SourceInterface $source)
{
$this->source = $source;
}
/**
* @inheritDoc
* @param Table $destination
*/
public function executeCopyCommands(
Generator $commands,
Connection $connection,
DestinationInterface $destination,
ImportOptions $importOptions,
ImportState $importState
): int {
$timerName = sprintf('copyToStaging-%s', $this->source->getFilePath());
$importState->startTimer($timerName);
$rowsCount = 0;
foreach ($commands as $command) {
$results = $connection->fetchAll($command);
foreach ($results as $result) {
$rowsCount += (int) $result['rows_loaded'];
}
}
$importState->stopTimer($timerName);
return $rowsCount;
}
/**
* @param Table $destination
*/
public function getCopyCommands(
DestinationInterface $destination,
ImportOptions $importOptions,
string $stagingTableName
): Generator {
$entriesInChunk = [];
foreach ($this->source->getManifestEntries() as $entry) {
$entriesInChunk[] = $entry;
if (count($entriesInChunk) === ImporterInterface::SLICED_FILES_CHUNK_SIZE) {
yield $this->getCopyCommand(
$destination,
$importOptions,
$stagingTableName,
$entriesInChunk
);
$entriesInChunk = [];
}
}
if (!empty($entriesInChunk)) {
yield $this->getCopyCommand(
$destination,
$importOptions,
$stagingTableName,
$entriesInChunk
);
}
}
/**
* @param Table $destination
*/
private function getCopyCommand(
DestinationInterface $destination,
ImportOptions $importOptions,
string $stagingTableName,
array $entriesInChunk
): string {
return sprintf(
'COPY INTO %s.%s
FROM %s
CREDENTIALS=(AZURE_SAS_TOKEN=\'%s\')
FILE_FORMAT = (TYPE=CSV %s)
FILES = (%s)',
QueryBuilder::quoteIdentifier($destination->getSchema()),
QueryBuilder::quoteIdentifier($stagingTableName),
QueryBuilder::quote($this->source->getContainerUrl()),
$this->source->getSasToken(),
implode(' ', $this->getCsvCopyCommandOptions($importOptions, $this->source->getCsvOptions())),
implode(
', ',
array_map(
function ($entry) {
return QueryBuilder::quote(strtr($entry['url'], [$this->source->getContainerUrl() => '']));
},
$entriesInChunk
)
)
);
}
private function getCsvCopyCommandOptions(
ImportOptions $importOptions,
CsvOptions $csvOptions
): array {
$options = [
sprintf('FIELD_DELIMITER = %s', QueryBuilder::quote($csvOptions->getDelimiter())),
];
if ($importOptions->getNumberOfIgnoredLines() > 0) {
$options[] = sprintf('SKIP_HEADER = %d', $importOptions->getNumberOfIgnoredLines());
}
if ($csvOptions->getEnclosure()) {
$options[] = sprintf('FIELD_OPTIONALLY_ENCLOSED_BY = %s', QueryBuilder::quote($csvOptions->getEnclosure()));
$options[] = 'ESCAPE_UNENCLOSED_FIELD = NONE';
} elseif ($csvOptions->getEscapedBy()) {
$options[] = sprintf('ESCAPE_UNENCLOSED_FIELD = %s', QueryBuilder::quote($csvOptions->getEscapedBy()));
}
return $options;
}
}