Tidb FUNCTION Error

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tidb FUNCTION报错

| username: 孤独的狼

【TiDB Usage Environment】Production environment or Test environment or POC
【TiDB Version】tidb 4.0.9
【Encountered Problem】FUNCTION creation failed
【Reproduction Path】What operations were performed that caused the problem
【Problem Phenomenon and Impact】

【Attachment】
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
– max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END

1064 - You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 15 near "FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
– max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETU
Time: 0.049s

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: 孤独的狼 | Original post link

Regarding this issue, I checked the official documentation and found that it is not supported. If I want to implement this feature, are there any other alternative solutions?
Official documentation URL:


!(file:///C:\Users\admin\AppData\Roaming\Tencent\QQTempSys%W@GJ$ACOF(TYDYECOKVDYB.png)与 MySQL 兼容性对比 | PingCAP 文档中心

| username: 孤独的狼 | Original post link

FUNCTION tidb.txt (3.0 KB)

Error screenshot

| username: xfworld | Original post link

It is not supported, :joy:

It is just not supported.

| username: 孤独的狼 | Original post link

The version of tidb is 5.7.25-TiDB-v4.0.9

| username: 孤独的狼 | Original post link

Well, do you know any alternatives since it’s not supported?

| username: xfworld | Original post link

The application layer solves…

| username: 孤独的狼 | Original post link

Sure, implement this feature through SQL.

| username: HACK | Original post link

TiDB does not support functions, this can only be implemented through application code.

| username: 孤独的狼 | Original post link

Well, I guess I’ll have to write the SQL manually. Is there a dedicated WeChat group for technical communication about TiDB?

| username: 啦啦啦啦啦 | Original post link

It is generally recommended to implement stored procedures using application layer code. You can join the WeChat group by adding my cousin on WeChat: billmay

| username: 孤独的狼 | Original post link

Added, but not approved.

| username: xuexiaogang | Original post link

Isn’t it that stored procedures, functions, and triggers are not supported?

| username: Kongdom | Original post link

There’s no need to overthink this kind of situation; just handle it at the application code level. In my real case, during migration, we moved all stored procedures and functions into the program. Now, the database is really just used for storing data. :grin: