matrixorigin / matrixone

Hyperconverged cloud-edge native database
https://docs.matrixorigin.cn/en
Apache License 2.0
1.79k stars 277 forks source link

[Bug]: Func `cosine_similarity` won't work properly with parameterized query #20181

Closed LHQ-MO closed 3 hours ago

LHQ-MO commented 2 days ago

Is there an existing issue for the same bug?

Branch Name

v2.0.0

Commit ID

0bbda66

Other Environment Information

- Hardware parameters:
- OS type: by Docker
- Others: image tag:matrixorigin/matrixone:2.0.0

Actual Behavior

When using vector with text format as a parameter

db.Query(`SELECT id, cosine_similarity(vector, ?) FROM vector_test`, "[1,2,3]")

It returns with error like:

failed to query the database: Error 20203 (HY000): invalid argument function cosine_similarity, bad value [VECF32 TEXT]

Expected Behavior

It's expected to work properly like

SELECT id, cosine_similarity(vector, "[1,2,3]") FROM vector_test

Steps to Reproduce

Create a table and insert data:

CREATE TABLE `vector_test` (
    id INT PRIMARY KEY AUTO_INCREMENT,
    vector vecf32(3)
);
INSERT INTO `vector_test` VALUES(1,"[1,2,4]");

Then do a query with golang:

    dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?&parseTime=true",
        "root",
        "111",
        "localhost",
        6001,
        "demo")
    db, _ := sql.Open("mysql", dsn)

    rows, err := db.Query(`SELECT id, cosine_similarity(vector, ?) FROM vector_test`, "[1,2,3]")
    if err != nil {
        log.Fatalf("failed to query the database: %v", err)
        return
    }

Additional information

A walkaround for this issue is using CAST with the parameter like this:

db.Query(`SELECT id, cosine_similarity(vector, CAST(? AS VECF32)) FROM vector_test`, "[1,2,3]")

BTW variables in SQL also encounter this:

SET @v = "[1,2,4]";
SELECT id, cosine_similarity(vector, @v) from vector_test;
aronchanisme commented 2 days ago

@aunjgr vector related, pls kindly take a look, thx

ouyuanning commented 2 days ago

没有自动把 string("[1,2,3]") cast 成vecf32。应该加一下就好了

zengyan1 commented 1 day ago

fixed

Ariznawlll commented 3 hours ago

测试步骤:

main

mysql> select git_version();
+---------------+
| git_version() |
+---------------+
| e08cd95ce     |
+---------------+
1 row in set (0.00 sec)

mysql -h 127.0.0.1 -P 6001 -udump -p111
create database test;
use test;
CREATE TABLE `vector_test` (
    id INT PRIMARY KEY AUTO_INCREMENT,
    vector vecf32(3)
);
INSERT INTO `vector_test` VALUES(1,"[1,2,4]");
INSERT INTO `vector_test` VALUES(2,"[3,2,4]");

运行脚本

image
 SET @v = "[1,2,4]";
 SELECT id, cosine_similarity(vector, @v) from vector_test;
image

结论:main测试通过

2.0-dev

mysql>  select git_version();
+---------------+
| git_version() |
+---------------+
| 31cdef2f9     |
+---------------+
1 row in set (0.00 sec)

运行脚本:

image image

结论:2.0-dev测试通过