Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/SQLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]){

#
Expand Down Expand Up @@ -466,13 +477,17 @@ 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':
case 'FULLTEXT KEY':
case 'SPATIAL':
case 'SPATIAL INDEX':
case 'SPATIAL KEY':
case 'VECTOR INDEX':
case 'VECTOR KEY':

$index = array(
'type' => 'FULLTEXT',
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/CollapseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
39 changes: 39 additions & 0 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down