packages/sql-semantics-mysql/src/Statement/Dml/Load/BulkOptions.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Dml\Load;
6
7use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Statement\Node;
10use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
11use SqlSemantics\Platform\MySql\Statement\Literal\ByteSize;
12use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
13
14/**
15 * The trailing options of a bulk LOAD DATA: `PARALLEL = n`, `MEMORY = size` and `ALGORITHM = BULK`.
16 *
17 * They belong to the bulk load of the HeatWave grammars: ALGORITHM = BULK
18 * since 8.0.30, PARALLEL and MEMORY since 8.2.
19 * Source: https://dev.mysql.com/doc/heatwave-aws/en/mys-hw-aws-bulk-ingest.html.
20 *
21 * @visibility public
22 * @example Reading the options of a bulk load
23 * $load = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("LOAD DATA INFILE 'f' INTO TABLE t PARALLEL = 4 ALGORITHM = BULK");
24 * [$load->statement->bulk->parallel->text, $load->statement->bulk->bulk] // => ['4', true]
25 */
26final class BulkOptions implements Node
27{
28 use Snapshot;
29
30 /**
31 * @param Numeral|null $parallel The number of threads
32 * @param ByteSize|null $memory The memory the load may use
33 * @param bool $bulk Whether ALGORITHM = BULK is written
34 */
35 public function __construct(public readonly ?Numeral $parallel = null, public readonly ?ByteSize $memory = null, public readonly bool $bulk = false)
36 {
37 Check::input($parallel !== null || $memory !== null || $bulk, 'Bulk options hold at least one option.');
38 }
39
40 /**
41 * Writes the options.
42 */
43 public function render(Output $out): void
44 {
45 if ($this->parallel !== null) {
46 $out->keyword('PARALLEL')->symbol('=')->node($this->parallel);
47 }
48 if ($this->memory !== null) {
49 $out->keyword('MEMORY')->symbol('=')->node($this->memory);
50 }
51 if ($this->bulk) {
52 $out->keyword('ALGORITHM')->symbol('=')->keyword('BULK');
53 }
54 }
55}
56