Skip to content

Commit

Permalink
[fix](shcema change)fix alter table faild when modify multiple column…
Browse files Browse the repository at this point in the history
…s with column changed positions apache#34244 (apache#34692)
  • Loading branch information
kobe6th authored and weixingyu12 committed May 24, 2024
1 parent 01d6f86 commit 146cbb7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private void processModifyColumn(ModifyColumnClause alterClause, Table externalT
found = true;
}
if (hasColPos) {
if (col.getName().equalsIgnoreCase(columnPos.getLastCol())) {
if (col.getNonShadowName().equalsIgnoreCase(columnPos.getLastCol())) {
lastColIndex = i;
}
} else {
Expand Down Expand Up @@ -604,7 +604,7 @@ private boolean processModifyColumn(ModifyColumnClause alterClause, OlapTable ol
}
}
if (hasColPos) {
if (col.getName().equalsIgnoreCase(columnPos.getLastCol())) {
if (col.getNonShadowName().equalsIgnoreCase(columnPos.getLastCol())) {
lastColIndex = i;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

suite('test_alter_muti_modify_column') {
def tbl = 'test_alter_muti_modify_column_tbl'
sql "DROP TABLE IF EXISTS ${tbl} FORCE"
sql """
CREATE TABLE ${tbl} (
`id` BIGINT NOT NULL,
`v1` BIGINT NULL,
`v2` VARCHAR(128) NULL,
`v3` VARCHAR(128) NULL
) ENGINE=OLAP
UNIQUE KEY(`id`)
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
"""

sql """
INSERT INTO ${tbl} VALUES
(1, 10, 'abc', 'def'),
(2, 20, 'xyz', 'uvw'),
(3, 30, 'pqr', 'lmn');
"""

def expectedResults = [
[1, 10, 'abc', 'def'],
[2, 20, 'xyz', 'uvw'],
[3, 30, 'pqr', 'lmn']
]

// Check data before ALTER TABLE
def resultBefore = sql """ SELECT * FROM ${tbl} ORDER BY id """
assertEquals(expectedResults.size(), resultBefore.size())
for (int i = 0; i < expectedResults.size(); i++) {
for (int j = 0; j < expectedResults[i].size(); j++) {
assertEquals(expectedResults[i][j], resultBefore[i][j])
}
}

sql """
ALTER TABLE ${tbl}
MODIFY COLUMN `v2` VARCHAR(512) NULL AFTER `v1`,
MODIFY COLUMN `v3` VARCHAR(512) NULL AFTER `v2`;
"""

// Wait for ALTER TABLE to take effect
Thread.sleep(5000)

// Check table structure after ALTER TABLE
def resultCreate = sql """ SHOW CREATE TABLE ${tbl} """
def createTbl = resultCreate[0][1].toString().toLowerCase()
assertTrue(createTbl.indexOf("`v2` varchar(512) null") > 0)
assertTrue(createTbl.indexOf("`v3` varchar(512) null") > 0)

// Check data after ALTER TABLE
def resultAfter = sql """ SELECT * FROM ${tbl} ORDER BY id """
assertEquals(expectedResults.size(), resultAfter.size())
for (int i = 0; i < expectedResults.size(); i++) {
for (int j = 0; j < expectedResults[i].size(); j++) {
assertEquals(expectedResults[i][j], resultAfter[i][j])
}
}

sql "DROP TABLE IF EXISTS ${tbl} FORCE"
}

0 comments on commit 146cbb7

Please sign in to comment.