Skip to content

Commit 6ae27ca

Browse files
alter table custom to custom (#57)
1 parent 66afb45 commit 6ae27ca

9 files changed

Lines changed: 291 additions & 39 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
INSTALL EXTENSION vsql_tvector;
2+
CREATE TABLE t1 (
3+
id INT PRIMARY KEY,
4+
vec3 TVECTOR(3)
5+
);
6+
INSERT INTO t1 VALUES (1, '[1.0,2.0,3.0]');
7+
INSERT INTO t1 VALUES (2, '[4.5,5.5,6.5]');
8+
INSERT INTO t1 VALUES (3, NULL);
9+
SHOW CREATE TABLE t1;
10+
Table Create Table
11+
t1 CREATE TABLE `t1` (
12+
`id` int NOT NULL,
13+
`vec3` vsql_tvector.TVECTOR DEFAULT NULL,
14+
PRIMARY KEY (`id`)
15+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
16+
SELECT * FROM t1 ORDER BY id;
17+
id vec3
18+
1 [1,2,3]
19+
2 [4.5,5.5,6.5]
20+
3 NULL
21+
ALTER TABLE t1 CHANGE vec3 vec4 TVECTOR(4);
22+
ERROR HY000: Cannot convert between incompatible custom types 'vsql_tvector.TVECTOR(3)' and 'vsql_tvector.TVECTOR(4)'
23+
ALTER TABLE t1 CHANGE vec3 vec3_2 TVECTOR(3);
24+
SHOW CREATE TABLE t1;
25+
Table Create Table
26+
t1 CREATE TABLE `t1` (
27+
`id` int NOT NULL,
28+
`vec3_2` vsql_tvector.TVECTOR DEFAULT NULL,
29+
PRIMARY KEY (`id`)
30+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
31+
SELECT * FROM t1 ORDER BY id;
32+
id vec3_2
33+
1 [1,2,3]
34+
2 [4.5,5.5,6.5]
35+
3 NULL
36+
ALTER TABLE t1 CHANGE vec3_2 vec3_str VARCHAR(100);
37+
SHOW CREATE TABLE t1;
38+
Table Create Table
39+
t1 CREATE TABLE `t1` (
40+
`id` int NOT NULL,
41+
`vec3_str` varchar(100) DEFAULT NULL,
42+
PRIMARY KEY (`id`)
43+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
44+
SELECT * FROM t1 ORDER BY id;
45+
id vec3_str
46+
1 [1,2,3]
47+
2 [4.5,5.5,6.5]
48+
3 NULL
49+
DROP TABLE t1;
50+
CREATE TABLE t2 (
51+
id INT PRIMARY KEY,
52+
vec2 TVECTOR(2),
53+
vec3 TVECTOR(3)
54+
);
55+
INSERT INTO t2 VALUES (1, '[-1.0,2.5]', '[1.0,2.0,3.0]');
56+
INSERT INTO t2 VALUES (2, '[4.0,-5.0]', '[4.5,5.5,6.5]');
57+
INSERT INTO t2 VALUES (3, NULL, NULL);
58+
SELECT * FROM t2 WHERE vec2 = vec3;
59+
ERROR HY000: Cannot compare types vsql_tvector.TVECTOR(2) and vsql_tvector.TVECTOR(3) in =
60+
DROP TABLE t2;
61+
include/rpl/deprecated/show_binlog_events.inc
62+
Log_name Pos Event_type Server_id End_log_pos Info
63+
# # Query # # use `test`; CREATE TABLE `t1` (
64+
`id` int NOT NULL,
65+
`vec3` vsql_tvector.TVECTOR DEFAULT NULL,
66+
PRIMARY KEY (`id`)
67+
)
68+
# # Query # # BEGIN
69+
# # Table_map # # table_id: # (test.t1)
70+
# # Write_rows # # table_id: # flags: STMT_END_F
71+
# # Xid # # COMMIT /* XID */
72+
# # Query # # BEGIN
73+
# # Table_map # # table_id: # (test.t1)
74+
# # Write_rows # # table_id: # flags: STMT_END_F
75+
# # Xid # # COMMIT /* XID */
76+
# # Query # # BEGIN
77+
# # Table_map # # table_id: # (test.t1)
78+
# # Write_rows # # table_id: # flags: STMT_END_F
79+
# # Xid # # COMMIT /* XID */
80+
# # Query # # use `test`; ALTER TABLE t1 CHANGE vec3 vec3_2 TVECTOR(3)
81+
# # Query # # use `test`; ALTER TABLE t1 CHANGE vec3_2 vec3_str VARCHAR(100)
82+
# # Query # # use `test`; DROP TABLE `t1` /* generated by server */
83+
# # Query # # use `test`; CREATE TABLE `t2` (
84+
`id` int NOT NULL,
85+
`vec2` vsql_tvector.TVECTOR DEFAULT NULL,
86+
`vec3` vsql_tvector.TVECTOR DEFAULT NULL,
87+
PRIMARY KEY (`id`)
88+
)
89+
# # Query # # BEGIN
90+
# # Table_map # # table_id: # (test.t2)
91+
# # Write_rows # # table_id: # flags: STMT_END_F
92+
# # Xid # # COMMIT /* XID */
93+
# # Query # # BEGIN
94+
# # Table_map # # table_id: # (test.t2)
95+
# # Write_rows # # table_id: # flags: STMT_END_F
96+
# # Xid # # COMMIT /* XID */
97+
# # Query # # BEGIN
98+
# # Table_map # # table_id: # (test.t2)
99+
# # Write_rows # # table_id: # flags: STMT_END_F
100+
# # Xid # # COMMIT /* XID */
101+
# # Query # # use `test`; DROP TABLE `t2` /* generated by server */
102+
UNINSTALL EXTENSION vsql_tvector;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
CREATE TABLE t1 (
2+
id INT PRIMARY KEY,
3+
value COMPLEX
4+
);
5+
INSERT INTO t1 VALUES (1, '(1.0,2.0)');
6+
INSERT INTO t1 VALUES (2, NULL);
7+
SELECT * FROM t1 ORDER BY id;
8+
id value
9+
1 (1,2)
10+
2 NULL
11+
ALTER TABLE t1 MODIFY COLUMN value COMPLEX NOT NULL;
12+
ERROR 22004: Invalid use of NULL value
13+
SELECT * FROM t1 ORDER BY id;
14+
id value
15+
1 (1,2)
16+
2 NULL
17+
UPDATE t1 SET value='(-2.0,-3.0)' WHERE id=2;
18+
ALTER TABLE t1 MODIFY COLUMN value COMPLEX NOT NULL;
19+
SELECT * FROM t1 ORDER BY id;
20+
id value
21+
1 (1,2)
22+
2 (-2,-3)
23+
DROP TABLE t1;
24+
include/rpl/deprecated/show_binlog_events.inc
25+
Log_name Pos Event_type Server_id End_log_pos Info
26+
# # Query # # use `test`; CREATE TABLE `t1` (
27+
`id` int NOT NULL,
28+
`value` vsql_complex.COMPLEX DEFAULT NULL,
29+
PRIMARY KEY (`id`)
30+
)
31+
# # Query # # BEGIN
32+
# # Table_map # # table_id: # (test.t1)
33+
# # Write_rows # # table_id: # flags: STMT_END_F
34+
# # Xid # # COMMIT /* XID */
35+
# # Query # # BEGIN
36+
# # Table_map # # table_id: # (test.t1)
37+
# # Write_rows # # table_id: # flags: STMT_END_F
38+
# # Xid # # COMMIT /* XID */
39+
# # Query # # BEGIN
40+
# # Table_map # # table_id: # (test.t1)
41+
# # Update_rows # # table_id: # flags: STMT_END_F
42+
# # Xid # # COMMIT /* XID */
43+
# # Query # # use `test`; ALTER TABLE t1 MODIFY COLUMN value COMPLEX NOT NULL
44+
# # Query # # use `test`; DROP TABLE `t1` /* generated by server */
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Test ALTER TABLE CHANGE on existing TVECTOR column
2+
# Tests copy between TVECTOR(N) columns with same and different prefix sizes,
3+
# and conversion to string.
4+
5+
INSTALL EXTENSION vsql_tvector;
6+
--source include/villagesql/binlog_check_begin.inc
7+
8+
CREATE TABLE t1 (
9+
id INT PRIMARY KEY,
10+
vec3 TVECTOR(3)
11+
);
12+
13+
INSERT INTO t1 VALUES (1, '[1.0,2.0,3.0]');
14+
INSERT INTO t1 VALUES (2, '[4.5,5.5,6.5]');
15+
INSERT INTO t1 VALUES (3, NULL);
16+
17+
SHOW CREATE TABLE t1;
18+
SELECT * FROM t1 ORDER BY id;
19+
20+
--error ER_VILLAGESQL_GENERIC_ERROR
21+
ALTER TABLE t1 CHANGE vec3 vec4 TVECTOR(4);
22+
23+
ALTER TABLE t1 CHANGE vec3 vec3_2 TVECTOR(3);
24+
25+
SHOW CREATE TABLE t1;
26+
SELECT * FROM t1 ORDER BY id;
27+
28+
ALTER TABLE t1 CHANGE vec3_2 vec3_str VARCHAR(100);
29+
30+
SHOW CREATE TABLE t1;
31+
SELECT * FROM t1 ORDER BY id;
32+
33+
DROP TABLE t1;
34+
35+
CREATE TABLE t2 (
36+
id INT PRIMARY KEY,
37+
vec2 TVECTOR(2),
38+
vec3 TVECTOR(3)
39+
);
40+
41+
INSERT INTO t2 VALUES (1, '[-1.0,2.5]', '[1.0,2.0,3.0]');
42+
INSERT INTO t2 VALUES (2, '[4.0,-5.0]', '[4.5,5.5,6.5]');
43+
INSERT INTO t2 VALUES (3, NULL, NULL);
44+
45+
--error ER_VILLAGESQL_GENERIC_ERROR
46+
SELECT * FROM t2 WHERE vec2 = vec3;
47+
48+
DROP TABLE t2;
49+
50+
--source include/villagesql/binlog_check_end.inc
51+
UNINSTALL EXTENSION vsql_tvector;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Test ALTER TABLE MODIFY COLUMN on a COMPLEX column with NULL data.
2+
# nullable COMPLEX -> NOT NULL COMPLEX with NULL data: should error.
3+
4+
--source include/villagesql/install_complex_extension.inc
5+
--source include/villagesql/binlog_check_begin.inc
6+
7+
CREATE TABLE t1 (
8+
id INT PRIMARY KEY,
9+
value COMPLEX
10+
);
11+
12+
INSERT INTO t1 VALUES (1, '(1.0,2.0)');
13+
INSERT INTO t1 VALUES (2, NULL);
14+
15+
SELECT * FROM t1 ORDER BY id;
16+
17+
--error ER_INVALID_USE_OF_NULL
18+
ALTER TABLE t1 MODIFY COLUMN value COMPLEX NOT NULL;
19+
20+
SELECT * FROM t1 ORDER BY id;
21+
22+
UPDATE t1 SET value='(-2.0,-3.0)' WHERE id=2;
23+
24+
ALTER TABLE t1 MODIFY COLUMN value COMPLEX NOT NULL;
25+
26+
SELECT * FROM t1 ORDER BY id;
27+
28+
DROP TABLE t1;
29+
30+
--source include/villagesql/binlog_check_end.inc
31+
--source include/villagesql/uninstall_complex_extension.inc

scripts/villint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ ALLOWED_TODO_TAGS=(
163163
"villagesql-performance"
164164
"villagesql-rebase"
165165
"villagesql-windows"
166+
"villagesql-blob"
166167
)
167168

168169
INVALID_TODO_TAGS_FOUND=0

sql/field_conv.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,6 @@ static void do_field_string(Copy_field *, const Field *from_field,
324324
to_field->store(res.ptr(), res.length(), res.charset());
325325
}
326326

327-
// VillageSQL: Copy from a custom type field to string field.
328-
static void do_field_custom_to_string(Copy_field *, const Field *from_field,
329-
Field *to_field) {
330-
villagesql::CopyCustomToStringField(from_field, to_field);
331-
}
332327

333328
static void do_field_enum(Copy_field *copy, const Field *from_field,
334329
Field *to_field) {
@@ -572,11 +567,7 @@ Copy_field::Copy_func *Copy_field::get_copy_func() {
572567

573568
// VillageSQL: Route to the appropriate custom type copy function.
574569
if (m_from_field->has_type_context()) {
575-
// TODO(villagesql) - should there be a do_field_custom_to_custom
576-
// instead of relying on varbinary field copy
577-
if (!m_to_field->has_type_context()) {
578-
return do_field_custom_to_string;
579-
}
570+
return villagesql::GetCopyFunc(m_from_field, m_to_field);
580571
}
581572

582573
const bool compatible_db_low_byte_first =

villagesql/sql/metadata_modifier.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool Metadata_modifier::alter_columns(THD *thd [[maybe_unused]],
300300
ColumnEntry new_entry(
301301
ColumnKey(db_name, table_name, alter->m_new_name),
302302
old_entry_ptr->extension_name, old_entry_ptr->extension_version,
303-
old_entry_ptr->type_name);
303+
old_entry_ptr->type_name, old_entry_ptr->type_parameters);
304304

305305
to_rename_.emplace_back(new_entry, old_entry_ptr->key());
306306
}
@@ -361,7 +361,8 @@ bool Metadata_modifier::alter_columns(THD *thd [[maybe_unused]],
361361
to_add_.emplace_back(ColumnKey(db_name, table_name, field.field_name),
362362
field.custom_type_context->extension_name(),
363363
field.custom_type_context->extension_version(),
364-
field.custom_type_context->type_name());
364+
field.custom_type_context->type_name(),
365+
field.custom_type_context->parameters().to_json());
365366
} else if (was_custom_type && is_custom_type) {
366367
// Changing FROM custom TO custom - use delete-then-insert pattern
367368
// Note: Apply removals before additions
@@ -371,14 +372,16 @@ bool Metadata_modifier::alter_columns(THD *thd [[maybe_unused]],
371372
to_add_.emplace_back(ColumnKey(db_name, table_name, field.field_name),
372373
field.custom_type_context->extension_name(),
373374
field.custom_type_context->extension_version(),
374-
field.custom_type_context->type_name());
375+
field.custom_type_context->type_name(),
376+
field.custom_type_context->parameters().to_json());
375377
}
376378
} else if (is_custom_type) {
377379
// This is ADD COLUMN with custom type - insert entry
378380
to_add_.emplace_back(ColumnKey(db_name, table_name, field.field_name),
379381
field.custom_type_context->extension_name(),
380382
field.custom_type_context->extension_version(),
381-
field.custom_type_context->type_name());
383+
field.custom_type_context->type_name(),
384+
field.custom_type_context->parameters().to_json());
382385
}
383386
}
384387
return false;

0 commit comments

Comments
 (0)