Skip to content

Commit ec58afe

Browse files
committed
use generator to optimize memory
1 parent 3bb9ac2 commit ec58afe

12 files changed

+41
-26
lines changed

src/Backend/BackendImportAdapterInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\Db\ImportExport\Backend;
66

7+
use Generator;
78
use Keboola\Db\ImportExport\ImportOptions;
89
use Keboola\Db\ImportExport\Storage\DestinationInterface;
910
use Keboola\Db\ImportExport\Storage\SourceInterface;
@@ -12,12 +13,9 @@ interface BackendImportAdapterInterface
1213
{
1314
public function __construct(SourceInterface $source);
1415

15-
/**
16-
* @return string[]
17-
*/
1816
public function getCopyCommands(
1917
DestinationInterface $destination,
2018
ImportOptions $importOptions,
2119
string $stagingTableName
22-
): array;
20+
): Generator;
2321
}

src/Backend/Snowflake/SnowflakeImportAdapterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\Db\ImportExport\Backend\Snowflake;
66

7+
use Generator;
78
use Keboola\Db\ImportExport\Backend\ImportState;
89
use Keboola\Db\ImportExport\ImportOptions;
910
use Keboola\Db\ImportExport\Backend\BackendImportAdapterInterface;
@@ -15,11 +16,10 @@ interface SnowflakeImportAdapterInterface extends BackendImportAdapterInterface
1516
/**
1617
* Snowflake import is handled differently for copy table2table and file2table
1718
*
18-
* @param string[] $commands - sql commands array
1919
* @return int - number of imported rows
2020
*/
2121
public function executeCopyCommands(
22-
array $commands,
22+
Generator $commands,
2323
Connection $connection,
2424
DestinationInterface $destination,
2525
ImportOptions $importOptions,

src/Storage/ABS/SnowflakeImportAdapter.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\Db\ImportExport\Storage\ABS;
66

7+
use Generator;
78
use Keboola\Csv\CsvOptions;
89
use Keboola\Db\ImportExport\Backend\ImporterInterface;
910
use Keboola\Db\ImportExport\Backend\ImportState;
@@ -35,7 +36,7 @@ public function __construct(SourceInterface $source)
3536
* @param Table $destination
3637
*/
3738
public function executeCopyCommands(
38-
array $commands,
39+
Generator $commands,
3940
Connection $connection,
4041
DestinationInterface $destination,
4142
ImportOptions $importOptions,
@@ -62,11 +63,10 @@ public function getCopyCommands(
6263
DestinationInterface $destination,
6364
ImportOptions $importOptions,
6465
string $stagingTableName
65-
): array {
66+
): Generator {
6667
$filesToImport = $this->source->getManifestEntries();
67-
$commands = [];
6868
foreach (array_chunk($filesToImport, ImporterInterface::SLICED_FILES_CHUNK_SIZE) as $entries) {
69-
$commands[] = sprintf(
69+
yield sprintf(
7070
'COPY INTO %s.%s
7171
FROM %s
7272
CREDENTIALS=(AZURE_SAS_TOKEN=\'%s\')
@@ -88,7 +88,6 @@ function ($entry) {
8888
)
8989
);
9090
}
91-
return $commands;
9291
}
9392

9493
private function getCsvCopyCommandOptions(

src/Storage/Snowflake/SnowflakeImportAdapter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Keboola\Db\ImportExport\Storage\Snowflake;
66

7+
use Generator;
78
use Keboola\Db\ImportExport\Backend\ImportState;
89
use Keboola\Db\ImportExport\ImportOptions;
9-
use Keboola\Db\ImportExport\Backend\Snowflake\Helper\QuoteHelper;
1010
use Keboola\Db\ImportExport\Backend\Snowflake\SnowflakeImportAdapterInterface;
1111
use Keboola\Db\ImportExport\Storage\DestinationInterface;
1212
use Keboola\Db\ImportExport\Storage\SourceInterface;
@@ -32,14 +32,14 @@ public function __construct(SourceInterface $source)
3232
* @param Table $destination
3333
*/
3434
public function executeCopyCommands(
35-
array $commands,
35+
Generator $commands,
3636
Connection $connection,
3737
DestinationInterface $destination,
3838
ImportOptions $importOptions,
3939
ImportState $importState
4040
): int {
4141
$importState->startTimer('copyToStaging');
42-
$connection->query($commands[0]);
42+
$connection->query($commands->current());
4343
$rows = $connection->fetchAll(sprintf(
4444
'SELECT COUNT(*) AS "count" FROM %s.%s',
4545
QueryBuilder::quoteIdentifier($destination->getSchema()),
@@ -56,7 +56,7 @@ public function getCopyCommands(
5656
DestinationInterface $destination,
5757
ImportOptions $importOptions,
5858
string $stagingTableName
59-
): array {
59+
): Generator {
6060
$quotedColumns = array_map(function ($column) {
6161
return QueryBuilder::quoteIdentifier($column);
6262
}, $importOptions->getColumns());
@@ -75,6 +75,6 @@ public function getCopyCommands(
7575
QueryBuilder::quoteIdentifier($this->source->getTableName()),
7676
);
7777

78-
return [$sql];
78+
yield $sql;
7979
}
8080
}

src/Storage/Snowflake/Table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Keboola\Db\ImportExport\Backend\BackendImportAdapterInterface;
99
use Keboola\Db\ImportExport\Backend\ExporterInterface;
1010
use Keboola\Db\ImportExport\Backend\ImporterInterface;
11-
use Keboola\Db\ImportExport\Backend\Snowflake\Helper\QuoteHelper;
1211
use Keboola\Db\ImportExport\Backend\Snowflake\Importer as SnowflakeImporter;
1312
use Keboola\Db\ImportExport\Storage\DestinationInterface;
1413
use Keboola\Db\ImportExport\Storage\NoBackendAdapterException;
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/unit/SourceStorage/ABS/SnowflakeImportAdapterTest.php renamed to tests/unit/Storage/ABS/SnowflakeImportAdapterTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Keboola\Db\ImportExportUnit\Storage\ABS;
66

7+
use Generator;
78
use Keboola\Csv\CsvOptions;
89
use Keboola\Db\ImportExport\Backend\ImportState;
910
use Keboola\Db\ImportExport\ImportOptions;
@@ -30,9 +31,14 @@ public function testExecuteCopyCommands(): void
3031
/** @var ImportOptions|MockObject $options */
3132
$options = self::createMock(ImportOptions::class);
3233

34+
$generator = function (): Generator {
35+
yield 'cmd1';
36+
yield 'cmd2';
37+
};
38+
3339
$adapter = new SnowflakeImportAdapter($source);
3440
$rows = $adapter->executeCopyCommands(
35-
['cmd1', 'cmd2'],
41+
$generator(),
3642
$connection,
3743
new Storage\Snowflake\Table('', ''),
3844
$options,
@@ -60,16 +66,16 @@ public function testGetCopyCommands(): void
6066
'stagingTable'
6167
);
6268

63-
self::assertSame([
69+
self::assertSame(
6470
<<<EOT
6571
COPY INTO "schema"."stagingTable"
6672
FROM 'containerUrl'
6773
CREDENTIALS=(AZURE_SAS_TOKEN='sasToken')
6874
FILE_FORMAT = (TYPE=CSV FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY = '\"' ESCAPE_UNENCLOSED_FIELD = NONE)
6975
FILES = ('azure://url')
7076
EOT,
71-
72-
], $commands);
77+
$commands->current()
78+
);
7379
}
7480

7581
public function testGetCopyCommandsChunk(): void
@@ -105,21 +111,28 @@ public function testGetCopyCommandsChunk(): void
105111
return sprintf("'%s'", $file);
106112
}, $cmd2Files));
107113

108-
self::assertSame([
114+
self::assertSame(
109115
<<<EOT
110116
COPY INTO "schema"."stagingTable"
111117
FROM 'containerUrl'
112118
CREDENTIALS=(AZURE_SAS_TOKEN='sasToken')
113119
FILE_FORMAT = (TYPE=CSV FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY = '\"' ESCAPE_UNENCLOSED_FIELD = NONE)
114120
FILES = ($cmd1Files)
115121
EOT,
122+
$commands->current()
123+
);
124+
125+
$commands->next();
126+
127+
self::assertSame(
116128
<<<EOT
117129
COPY INTO "schema"."stagingTable"
118130
FROM 'containerUrl'
119131
CREDENTIALS=(AZURE_SAS_TOKEN='sasToken')
120132
FILE_FORMAT = (TYPE=CSV FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY = '\"' ESCAPE_UNENCLOSED_FIELD = NONE)
121133
FILES = ($cmd2Files)
122134
EOT,
123-
], $commands);
135+
$commands->current()
136+
);
124137
}
125138
}
File renamed without changes.

0 commit comments

Comments
 (0)