Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Modify Column Type #3701

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ mod tests {
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap_err();
let err = result.output_msg();
assert!(err.contains("expect keyword ADD or DROP or ALERT COLUMN or RENAME after ALTER TABLE"));
assert!(
err.contains("expect keyword ADD or DROP or ALERT COLUMN or RENAME after ALTER TABLE")
);

let sql = "ALTER TABLE test_table RENAME table_t";
let mut result =
Expand Down
32 changes: 21 additions & 11 deletions tests/cases/standalone/common/alter/modify_col.result
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
CREATE TABLE test(i INTEGER, j TIMESTAMP TIME INDEX);
CREATE TABLE test(id INTEGER PRIMARY KEY, i INTEGER, j TIMESTAMP TIME INDEX, k BOOLEAN);

Affected Rows: 0

INSERT INTO test VALUES (1, 1), (2, 2);
INSERT INTO test VALUES (1, 1, 1, false), (2, 2, 2, true);

Affected Rows: 2

ALTER TABLE test ALTER COLUMN "I" TYPE STRING;

Error: 4002(TableColumnNotFound), Column I not exists in table test

ALTER TABLE test ALTER COLUMN I TYPE STRING;
ALTER TABLE test ALTER COLUMN k TYPE DATE;

Affected Rows: 0
Error: 1004(InvalidArguments), Invalid alter table(test) request: column 'k' cannot be cast automatically to type 'Date'

SELECT * FROM test;
ALTER TABLE test ALTER COLUMN id TYPE STRING;

+---+-------------------------+
| i | j |
+---+-------------------------+
| 1 | 1970-01-01T00:00:00.001 |
| 2 | 1970-01-01T00:00:00.002 |
+---+-------------------------+
Error: 1004(InvalidArguments), Not allowed to modify index column id from table test
KKould marked this conversation as resolved.
Show resolved Hide resolved

ALTER TABLE test ALTER COLUMN j TYPE STRING;

Error: 1004(InvalidArguments), Not allowed to modify index column j from table test

ALTER TABLE test ALTER COLUMN I TYPE STRING;

Affected Rows: 0

SELECT * FROM test;

+----+---+-------------------------+-------+
| id | i | j | k |
+----+---+-------------------------+-------+
| 1 | 1 | 1970-01-01T00:00:00.001 | false |
| 2 | 2 | 1970-01-01T00:00:00.002 | true |
+----+---+-------------------------+-------+

DESCRIBE test;

+--------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+--------+----------------------+-----+------+---------+---------------+
| id | Int32 | PRI | YES | | TAG |
| i | String | | YES | | FIELD |
| j | TimestampMillisecond | PRI | NO | | TIMESTAMP |
| k | Boolean | | YES | | FIELD |
+--------+----------------------+-----+------+---------+---------------+

DROP TABLE test;
Expand Down
14 changes: 9 additions & 5 deletions tests/cases/standalone/common/alter/modify_col.sql
KKould marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
CREATE TABLE test(i INTEGER, j TIMESTAMP TIME INDEX);
CREATE TABLE test(id INTEGER PRIMARY KEY, i INTEGER, j TIMESTAMP TIME INDEX, k BOOLEAN);

INSERT INTO test VALUES (1, 1), (2, 2);
INSERT INTO test VALUES (1, 1, 1, false), (2, 2, 2, true);

ALTER TABLE test ALTER COLUMN "I" TYPE STRING;

ALTER TABLE test ALTER COLUMN I TYPE STRING;
ALTER TABLE test ALTER COLUMN k TYPE DATE;

SELECT * FROM test;
ALTER TABLE test ALTER COLUMN id TYPE STRING;

ALTER TABLE test ALTER COLUMN j TYPE STRING;

ALTER TABLE test ALTER COLUMN I TYPE STRING;

SELECT * FROM test;

DESCRIBE test;

DROP TABLE test;
DROP TABLE test;
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ SELECT * FROM test;

DESCRIBE test;

DROP TABLE test;
DROP TABLE test;