Skip to content

Commit cb3b828

Browse files
authored
Merge pull request #166 from keboola/vb-CT-1526-add-snowflake-vector-datatype
CT-1526 add snowflake vector datatype
2 parents c869241 + 124dbcf commit cb3b828

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/Definition/Exasol.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private function validateLength(string $type, $length = null): void
328328
}
329329

330330
if (preg_match('/(?<val>[1-9]+\d*)\s*(?<unit>BYTE|BIT)/i', (string) $length, $matched)) {
331-
$val = $matched['val'];
331+
$val = (int) $matched['val'];
332332
$unit = strtoupper($matched['unit']);
333333

334334
$limits = [

src/Definition/Snowflake.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Snowflake extends Common
6060
public const TYPE_ARRAY = 'ARRAY';
6161
public const TYPE_GEOGRAPHY = 'GEOGRAPHY';
6262
public const TYPE_GEOMETRY = 'GEOMETRY';
63+
public const TYPE_VECTOR = 'VECTOR';
6364
public const TYPES = [
6465
self::TYPE_NUMBER,
6566
self::TYPE_DEC,
@@ -103,6 +104,7 @@ class Snowflake extends Common
103104
self::TYPE_ARRAY,
104105
self::TYPE_GEOGRAPHY,
105106
self::TYPE_GEOMETRY,
107+
self::TYPE_VECTOR,
106108
];
107109
public const MAX_VARCHAR_LENGTH = 16777216;
108110
public const MAX_VARBINARY_LENGTH = 8388608;
@@ -350,6 +352,24 @@ private function validateLength(string $type, $length = null): void
350352
break;
351353
}
352354
break;
355+
case self::TYPE_VECTOR:
356+
$valid = false;
357+
if ($length === null || !is_string($length)) {
358+
break;
359+
}
360+
/** matches:
361+
* TYPE - INT|FLOAT - case insensitive
362+
* ,
363+
* any white space zero or infinite times
364+
* any digit with 0 to 4 places
365+
*/
366+
if (preg_match('/^(?<TYPE>INT|FLOAT),[^\S\r\n]*(?<DIM>[\d]{1,4})$/i', $length, $matches)) {
367+
$dimension = (int) $matches['DIM'];
368+
if ($dimension > 0 && $dimension <= 4096) {
369+
$valid = true;
370+
}
371+
}
372+
break;
353373
default:
354374
if (!is_null($length) && $length !== '') {
355375
$valid = false;

tests/SnowflakeDatatypeTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public function testInvalidBinaryLengths($length): void
268268
public function testBasetypes(): void
269269
{
270270
foreach (Snowflake::TYPES as $type) {
271-
$basetype = (new Snowflake($type))->getBasetype();
271+
$basetype = (new Snowflake($type, $this->getTypeDefaultOptions($type)))->getBasetype();
272272
switch ($type) {
273273
case 'INT':
274274
case 'INTEGER':
@@ -484,7 +484,8 @@ public function testArrayFromLength(string $type, ?string $length, array $expect
484484
*/
485485
public function testBackendBasetypeFromAlias(string $type, string $expectedType): void
486486
{
487-
$definition = new Snowflake($type);
487+
488+
$definition = new Snowflake($type, $this->getTypeDefaultOptions($type));
488489
$this->assertSame($expectedType, $definition->getBackendBasetype());
489490
}
490491

@@ -569,4 +570,24 @@ public function provideTestGetTypeFromAlias(): Generator
569570
];
570571
}
571572
}
573+
574+
/**
575+
* @return array{
576+
* length?:string|null|array,
577+
* nullable?:bool,
578+
* default?:string|null
579+
* }
580+
*/
581+
private function getTypeDefaultOptions(string $type): array
582+
{
583+
$options = [];
584+
if ($type === Snowflake::TYPE_VECTOR) {
585+
// VECTOR don't have any meaningfully default option
586+
$options = [
587+
'length' => 'INT,3',
588+
];
589+
}
590+
591+
return $options;
592+
}
572593
}

0 commit comments

Comments
 (0)