diff --git a/src/SQLParser.php b/src/SQLParser.php index 82fa2ad..9417dff 100644 --- a/src/SQLParser.php +++ b/src/SQLParser.php @@ -387,6 +387,17 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){ } + # + # VECTOR INDEX|KEY is merged here rather than in the lexer, since + # `vector` can be a plain identifier anywhere else + # + + if (count($tokens) > 1 && StrToUpper($tokens[0]) == 'VECTOR' && ($tokens[1] == 'INDEX' || $tokens[1] == 'KEY')){ + array_shift($tokens); + $tokens[0] = 'VECTOR '.$tokens[0]; + } + + switch ($tokens[0]){ # @@ -466,6 +477,8 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){ # SPATIAL [index_name] (index_col_name,...) [index_option] ... # SPATIAL INDEX [index_name] (index_col_name,...) [index_option] ... # SPATIAL KEY [index_name] (index_col_name,...) [index_option] ... + # VECTOR INDEX [index_name] (index_col_name,...) [index_option] ... + # VECTOR KEY [index_name] (index_col_name,...) [index_option] ... case 'FULLTEXT': case 'FULLTEXT INDEX': @@ -473,6 +486,8 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){ case 'SPATIAL': case 'SPATIAL INDEX': case 'SPATIAL KEY': + case 'VECTOR INDEX': + case 'VECTOR KEY': $index = array( 'type' => 'FULLTEXT', @@ -481,6 +496,8 @@ function parse_field_or_key(&$tokens, &$fields, &$indexes){ if ($tokens[0] == 'SPATIAL' ) $index['type'] = 'SPATIAL'; if ($tokens[0] == 'SPATIAL INDEX') $index['type'] = 'SPATIAL'; if ($tokens[0] == 'SPATIAL KEY' ) $index['type'] = 'SPATIAL'; + if ($tokens[0] == 'VECTOR INDEX') $index['type'] = 'VECTOR'; + if ($tokens[0] == 'VECTOR KEY' ) $index['type'] = 'VECTOR'; array_shift($tokens); @@ -675,8 +692,10 @@ function parse_field($tokens){ # BIT[(length)] # BINARY[(length)] + # VECTOR[(dimensions)] case 'BIT': case 'BINARY': + case 'VECTOR': $this->parse_field_length($tokens, $f); break; diff --git a/tests/CollapseTest.php b/tests/CollapseTest.php index 7e7b674..3038f9d 100644 --- a/tests/CollapseTest.php +++ b/tests/CollapseTest.php @@ -12,5 +12,6 @@ function testCollapsing(){ $this->collapse_test('a b', array('a', 'b')); $this->collapse_test('UNIQUE key', array('UNIQUE KEY')); + $this->collapse_test('vector key', array('vector', 'KEY')); } } diff --git a/tests/FieldTest.php b/tests/FieldTest.php index 037c3ba..70d9c02 100644 --- a/tests/FieldTest.php +++ b/tests/FieldTest.php @@ -280,6 +280,45 @@ function testChars(){ ), $fields); } + function testVectors(){ + + # VECTOR[(dimensions)] + + $fields = $this->get_fields("bar VECTOR"); + $this->assertEquals(array( + array( + 'name' => "bar", + 'type' => "VECTOR", + ) + ), $fields); + + $fields = $this->get_fields("bar vector(1536) NOT NULL"); + $this->assertEquals(array( + array( + 'name' => "bar", + 'type' => "VECTOR", + 'length' => "1536", + 'null' => false, + ) + ), $fields); + + # outside of an index definition, vector is still just a name + $fields = $this->get_fields("vector INT(10), bar INT DEFAULT vector KEY"); + $this->assertEquals(array( + array( + 'name' => "vector", + 'type' => "INT", + 'length' => "10", + ), + array( + 'name' => "bar", + 'type' => "INT", + 'default' => "vector", + 'more' => array("KEY"), + ), + ), $fields); + } + function testTexts(){ # TODO diff --git a/tests/IndexTest.php b/tests/IndexTest.php index b34ef0a..be74430 100644 --- a/tests/IndexTest.php +++ b/tests/IndexTest.php @@ -49,6 +49,21 @@ function testIndexTypes(){ $tbl = $this->get_first_table("CREATE TABLE foo (bar INT, SPATIAL KEY(bar))"); $this->assertEquals($tbl['indexes'][0]['type'], "SPATIAL"); + + $tbl = $this->get_first_table("CREATE TABLE foo (bar VECTOR(3) NOT NULL, VECTOR INDEX(bar))"); + $this->assertEquals($tbl['indexes'][0]['type'], "VECTOR"); + + $tbl = $this->get_first_table("CREATE TABLE foo (bar VECTOR(3) NOT NULL, vector key (bar))"); + $this->assertEquals($tbl['indexes'][0]['type'], "VECTOR"); + + # as dumped by MariaDB + $tbl = $this->get_first_table("CREATE TABLE foo (`bar` vector(3) NOT NULL, VECTOR KEY `baz` (`bar`) `M`='6' `DISTANCE`='cosine')"); + $this->assertEquals($tbl['indexes'][0], array( + 'type' => "VECTOR", + 'name' => "baz", + 'cols' => array(array('name' => "bar")), + 'more' => array("`M`", "=", "'6'", "`DISTANCE`", "=", "'cosine'"), + )); } function testConstraints(){