Skip to content

Commit af8720e

Browse files
Merge pull request #8 from keboola/add-bq-datatypes-more-types
Add bq datatypes more types
2 parents 1d42712 + dcd6848 commit af8720e

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

src/Definition/Bigquery.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Bigquery extends Common
1919
public const NUMERIC_LENGTH_CONST = 29;
2020
public const BIGNUMERIC_LENGTH_CONST = 38;
2121

22-
// public const TYPE_ARRAY = 'ARRAY'; // todo we decided not support array right now
22+
public const TYPE_ARRAY = 'ARRAY';
2323

2424
public const TYPE_BOOL = 'BOOL'; // NULL,TRUE,FALSE
2525

@@ -59,9 +59,10 @@ class Bigquery extends Common
5959

6060
public const TYPE_STRING = 'STRING'; // STRING(L) L is a positive INT64 value
6161

62-
// public const TYPE_STRUCT = 'STRUCT'; // todo we decided not support struct right now
62+
public const TYPE_STRUCT = 'STRUCT';
6363

6464
public const TYPES = [
65+
self::TYPE_ARRAY,
6566
self::TYPE_BOOL,
6667
self::TYPE_BYTES,
6768
self::TYPE_DATE,
@@ -76,6 +77,7 @@ class Bigquery extends Common
7677
self::TYPE_BIGNUMERIC,
7778
self::TYPE_FLOAT64,
7879
self::TYPE_STRING,
80+
self::TYPE_STRUCT,
7981

8082
// aliases
8183
self::TYPE_INT,
@@ -115,16 +117,29 @@ public function __construct(string $type, array $options = [])
115117
public function getTypeOnlySQLDefinition(): string
116118
{
117119
$out = $this->getType();
118-
$length = $this->getLength();
119-
if ($length !== null && $length !== '') {
120-
$out .= sprintf('(%s)', $length);
120+
if (strtoupper($out) === self::TYPE_ARRAY || strtoupper($out) === self::TYPE_STRUCT) {
121+
$length = $this->getLength();
122+
if ($length !== null && $length !== '') {
123+
$out .= sprintf('<%s>', $length);
124+
}
125+
} else {
126+
$length = $this->getLength();
127+
if ($length !== null && $length !== '') {
128+
$out .= sprintf('(%s)', $length);
129+
}
121130
}
122131
return $out;
123132
}
124133

125134
public function getSQLDefinition(): string
126135
{
127136
$definition = $this->getTypeOnlySQLDefinition();
137+
138+
if (strtoupper($this->getType()) === self::TYPE_ARRAY
139+
|| strtoupper($this->getType()) === self::TYPE_STRUCT
140+
) {
141+
return $definition;
142+
}
128143
if ($this->getDefault() !== null) {
129144
$definition .= ' DEFAULT ' . $this->getDefault();
130145
}
@@ -244,6 +259,9 @@ private function validateLength(string $type, $length = null): void
244259
case self::TYPE_BIGDECIMAL:
245260
$valid = $this->validateBigNumericLength($length, 76, 38);
246261
break;
262+
case self::TYPE_ARRAY:
263+
case self::TYPE_STRUCT:
264+
break; // We don't check for this types
247265
default:
248266
if ($length !== null && $length !== '') {
249267
$valid = false;

tests/BigqueryDatatypeTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,54 @@ public function expectedSqlDefinitions(): array
253253
$type . '(1000)',
254254
];
255255
}
256+
// todo mozno resit viac tu validaciu v zanoreni
257+
$tests[] = [
258+
'array',
259+
['length' => 'INT64'],
260+
'array<INT64>',
261+
];
262+
263+
$tests[] = [
264+
'array',
265+
['length' => 'BYTES(5)'],
266+
'array<BYTES(5)>',
267+
];
268+
269+
$tests[] = [
270+
'array',
271+
['length' => 'STRUCT<INT64, INT64>'],
272+
'array<STRUCT<INT64, INT64>>',
273+
];
274+
275+
$tests[] = [
276+
'array',
277+
['length' => 'STRUCT<ARRAY<INT64>>'],
278+
'array<STRUCT<ARRAY<INT64>>>',
279+
];
280+
281+
$tests[] = [
282+
'struct',
283+
['length' => 'INT64'],
284+
'struct<INT64>',
285+
];
286+
287+
$tests[] = [
288+
'struct',
289+
['length' => 'x BYTES(10)'],
290+
'struct<x BYTES(10)>',
291+
];
292+
293+
$tests[] = [
294+
'struct',
295+
['length' => 'x STRUCT<y INT64, z INT64>'],
296+
'struct<x STRUCT<y INT64, z INT64>>',
297+
];
298+
299+
$tests[] = [
300+
'struct',
301+
['length' => 'inner_array ARRAY<INT64>'],
302+
'struct<inner_array ARRAY<INT64>>',
303+
];
256304

257305
return $tests;
258306
}

0 commit comments

Comments
 (0)