# LogicTest: local local-opt local-parallel-stmts fakedist fakedist-opt fakedist-metadata

# A basic sanity check to demonstrate column type changes.
subtest SanityCheck

statement ok
CREATE TABLE t (s STRING, sl STRING(5), t TIME, ts TIMESTAMP)

statement ok
SET TIME ZONE 'Europe/Amsterdam'

statement ok
INSERT INTO t VALUES ('some string', 'short', TIME '20:16:27', '2018-05-23 20:16:27.658082')

query TTTT
SELECT * FROM t
----
some string  short  0000-01-01 20:16:27 +0000 UTC  2018-05-23 20:16:27.658082 +0000 +0000

# Not using TIMETZ until #26074 and #25224 are resolved.
statement ok
ALTER TABLE t ALTER s TYPE BYTES, ALTER sl TYPE STRING(6), ALTER ts TYPE TIMESTAMPTZ

query TTBTTTB colnames
SHOW COLUMNS FROM t
----
column_name  data_type    is_nullable  column_default  generation_expression  indices    is_hidden
s            BYTES        true         NULL            ·                      {}         false
sl           STRING(6)    true         NULL            ·                      {}         false
t            TIME         true         NULL            ·                      {}         false
ts           TIMESTAMPTZ  true         NULL            ·                      {}         false
rowid        INT          false        unique_rowid()  ·                      {primary}  true

query TTTT
SELECT * FROM t
----
some string  short  0000-01-01 20:16:27 +0000 UTC  2018-05-23 22:16:27.658082 +0200 +0200

statement ok
DROP TABLE t


# Demonstrate manual flow for non-trivial column change
subtest ManualGeneralChange

statement ok
CREATE TABLE t (a INT PRIMARY KEY, b STRING)

statement ok
CREATE INDEX idx ON t (b)

statement ok
INSERT INTO t VALUES (1, '01'), (2, '002'), (3, '0003')

query IT colnames
SELECT * from t ORDER BY b DESC
----
a  b
1  01
2  002
3  0003

statement ok
ALTER TABLE t ADD COLUMN i INT as (b::INT) STORED

statement ok
CREATE INDEX idx2 ON t (i)

statement ok
ALTER TABLE t ALTER COLUMN i DROP STORED, DROP COLUMN b CASCADE

query TT colnames
show create table t
----
table_name  create_statement
t           CREATE TABLE t (
            a INT8 NOT NULL,
            i INT8 NULL,
            CONSTRAINT "primary" PRIMARY KEY (a ASC),
            INDEX idx2 (i ASC),
            FAMILY "primary" (a, i)
)

statement ok
ALTER TABLE t RENAME COLUMN i TO b

statement ok
ALTER INDEX idx2 RENAME TO idx

query II colnames
SELECT * from t ORDER BY b DESC
----
a  b
3  3
2  2
1  1

statement ok
DROP TABLE t CASCADE


# Demonstrate that we can change to an alias of a type
subtest ChangeVisibleColumnType

statement ok
CREATE TABLE t (a INT)

query TTBTTTB colnames
SHOW COLUMNS FROM t
----
column_name  data_type  is_nullable  column_default  generation_expression  indices    is_hidden
a            INT8       true         NULL            ·                      {}         false
rowid        INT        false        unique_rowid()  ·                      {primary}  true

statement ok
ALTER TABLE t ALTER a TYPE INTEGER

query TTBTTTB colnames
SHOW COLUMNS FROM t
----
column_name  data_type  is_nullable  column_default  generation_expression  indices    is_hidden
a            INT8       true         NULL            ·                      {}         false
rowid        INT        false        unique_rowid()  ·                      {primary}  true

statement ok
DROP TABLE t


# Verify error handling when a bad COLLATE is used
subtest BadStringLocale

statement ok
CREATE TABLE t (s STRING)

statement error pq: invalid locale bad_locale
ALTER TABLE t ALTER s TYPE STRING COLLATE bad_locale

statement ok
DROP TABLE t


# Verify error handling when a silly COLLATE is used
subtest BadCollateOnNotString

statement ok
CREATE TABLE t (i INT)

statement error pq: COLLATE can only be used with string types
ALTER TABLE t ALTER i TYPE INT COLLATE nope

statement ok
DROP TABLE t


# Verify that making a no-op change is ok
subtest NoOpColumnChange

statement ok
CREATE TABLE t (s STRING)

statement ok
ALTER TABLE t ALTER s TYPE STRING

statement ok
DROP TABLE t
