Skip to content

Commit 2bb1402

Browse files
Address sonar issues
1 parent ccb4922 commit 2bb1402

5 files changed

Lines changed: 103 additions & 107 deletions

File tree

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/DbErrorCodes.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Codes implements DbErrorCodes {
5050
@Override
5151
public boolean isIgnorable(SQLException e) {
5252
String state = e.getSQLState();
53-
String code = String.valueOf(e.getErrorCode());
53+
String code = String.valueOf(e.getErrorCode());
5454
return duplicateCodes.contains(state) || duplicateCodes.contains(code)
5555
|| missingCodes.contains(state) || missingCodes.contains(code);
5656
}
@@ -134,12 +134,12 @@ static DbErrorCodes forDbType(String dbType) {
134134
return NOOP;
135135
}
136136
return switch (dbType.toLowerCase()) {
137-
case "postgres" -> POSTGRES;
138-
case "derby", "derby.clean" -> DERBY;
139-
case "mysql", "mariadb" -> MYSQL;
140-
case "oracle" -> ORACLE;
141-
case "mssql" -> MSSQL;
142-
default -> NOOP;
137+
case "postgres" -> POSTGRES;
138+
case "derby", "derby.clean" -> DERBY;
139+
case "mysql", "mariadb" -> MYSQL;
140+
case "oracle" -> ORACLE;
141+
case "mssql" -> MSSQL;
142+
default -> NOOP;
143143
};
144144
}
145145
}

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/HiveSchemaHelper.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ public boolean needsQuotedIdentifier() {
266266
}
267267

268268
@Override
269-
public List<String> getExecutableCommands(String scriptDir, String scriptFile) throws IllegalFormatException, IOException {
269+
public List<String> getExecutableCommands(String scriptDir, String scriptFile)
270+
throws IllegalFormatException, IOException {
270271
return getExecutableCommands(scriptDir, scriptFile, false);
271272
}
272273

273274
@Override
274-
public List<String> getExecutableCommands(String scriptDir, String scriptFile, boolean fixQuotes) throws IllegalFormatException, IOException {
275+
public List<String> getExecutableCommands(String scriptDir, String scriptFile, boolean fixQuotes)
276+
throws IllegalFormatException, IOException {
275277
List<String> commands = new java.util.ArrayList<>();
276278

277279
try (BufferedReader bfReader =
@@ -280,41 +282,39 @@ public List<String> getExecutableCommands(String scriptDir, String scriptFile, b
280282
String currentCommand = null;
281283

282284
while ((currLine = bfReader.readLine()) != null) {
283-
currLine = currLine.trim();
284-
285-
if (fixQuotes && !getQuoteCharacter().equals(DEFAULT_QUOTE)) {
286-
currLine = currLine.replace("\\\"", getQuoteCharacter());
287-
}
285+
currLine = fixQuotesFromCurrentLine(fixQuotes, currLine.trim());
288286

289287
if (currLine.isEmpty()) {
290288
continue;
291289
}
292290

293-
if (currentCommand == null) {
294-
currentCommand = currLine;
295-
} else {
296-
currentCommand = currentCommand + " " + currLine;
297-
}
298-
299-
if (isPartialCommand(currLine)) {
300-
continue;
301-
}
302-
303-
if (!isNonExecCommand(currentCommand)) {
304-
currentCommand = cleanseCommand(currentCommand);
305-
if (isNestedScript(currentCommand)) {
306-
String currScript = getScriptName(currentCommand);
307-
commands.addAll(getExecutableCommands(scriptDir, currScript, fixQuotes));
308-
} else {
309-
commands.add(currentCommand.trim());
291+
currentCommand = currentCommand == null ? currLine : currentCommand + " " + currLine;
292+
293+
if (!isPartialCommand(currLine)) {
294+
if (!isNonExecCommand(currentCommand)) {
295+
currentCommand = cleanseCommand(currentCommand);
296+
if (isNestedScript(currentCommand)) {
297+
String currScript = getScriptName(currentCommand);
298+
commands.addAll(getExecutableCommands(scriptDir, currScript, fixQuotes));
299+
} else {
300+
commands.add(currentCommand.trim());
301+
}
310302
}
303+
currentCommand = null;
311304
}
312-
currentCommand = null;
305+
313306
}
314307
}
315308
return commands;
316309
}
317310

311+
private String fixQuotesFromCurrentLine(boolean fixQuotes, String currLine) {
312+
if (fixQuotes && !getQuoteCharacter().equals(DEFAULT_QUOTE)) {
313+
currLine = currLine.replace("\\\"", getQuoteCharacter());
314+
}
315+
return currLine;
316+
}
317+
318318
@Override
319319
public String buildCommand(
320320
String scriptDir, String scriptFile) throws IllegalFormatException, IOException {

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/IdempotentDDLExecutor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void executeScript(String scriptFile) throws SQLException, IOException {
6363
private void executeStatement(String sqlStmt) throws SQLException {
6464
if (verbose) {
6565
System.out.println("Executing: " + sqlStmt);
66-
} else if (LOG.isDebugEnabled()) {
66+
}
67+
if (LOG.isDebugEnabled()) {
6768
LOG.debug("Executing: {}", sqlStmt);
6869
}
6970

@@ -76,11 +77,13 @@ private void executeStatement(String sqlStmt) throws SQLException {
7677
if (verbose) {
7778
System.out.println(msg);
7879
}
79-
LOG.info(msg);
80+
if (LOG.isDebugEnabled()) {
81+
LOG.debug(msg);
82+
}
8083
} else {
8184
LOG.error("SQL Error executing: {}. Error Code: {}, SQLState: {}", sqlStmt, e.getErrorCode(), e.getSQLState());
8285
throw e;
8386
}
8487
}
8588
}
86-
}
89+
}

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/MetastoreSchemaTool.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.commons.cli.OptionGroup;
2222
import org.apache.commons.cli.ParseException;
2323
import org.apache.commons.io.FileUtils;
24-
import org.apache.commons.io.output.NullOutputStream;
2524
import org.apache.commons.lang3.StringUtils;
2625
import org.apache.hadoop.conf.Configuration;
2726
import org.apache.hadoop.fs.Path;
@@ -37,16 +36,11 @@
3736
import org.slf4j.Logger;
3837
import org.slf4j.LoggerFactory;
3938

40-
import sqlline.SqlLine;
41-
4239
import java.io.BufferedReader;
43-
import java.io.ByteArrayOutputStream;
4440
import java.io.File;
4541
import java.io.FileReader;
4642
import java.io.IOException;
4743
import java.io.InputStream;
48-
import java.io.OutputStream;
49-
import java.io.PrintStream;
5044
import java.net.URI;
5145
import java.sql.Connection;
5246
import java.sql.SQLException;

standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/tools/schematool/TestSchemaToolForMetastore.java

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ public void testIdempotentTableOperations() throws Exception {
524524
@Test
525525
public void testIdempotentAddColumnOperations() throws Exception {
526526
String addColumnStmt = switch (dbms.getDbType()) {
527-
case "oracle" -> "alter table TEST_C add (NEW_COL integer);";
528-
case "mssql" -> "alter table TEST_C add NEW_COL int;";
529-
default -> "alter table TEST_C add column NEW_COL int;";
527+
case "oracle" -> "alter table TEST_C add (NEW_COL integer);";
528+
case "mssql" -> "alter table TEST_C add NEW_COL int;";
529+
default -> "alter table TEST_C add column NEW_COL int;";
530530
};
531531
String[] addScripts = new String[]{
532532
"create table TEST_C (ID int);",
@@ -546,10 +546,9 @@ public void testIdempotentIndexOperations() throws Exception {
546546
"create index TEST_IDX on TEST_D (ID);"
547547
};
548548
String dropIndexStmt = switch (dbms.getDbType()) {
549-
case "derby" -> "drop index \"APP\".\"TEST_IDX\";";
550-
case "oracle",
551-
"postgres" -> "drop index TEST_IDX;";
552-
default -> "drop index TEST_IDX on TEST_D;";
549+
case "derby" -> "drop index \"APP\".\"TEST_IDX\";";
550+
case "oracle", "postgres" -> "drop index TEST_IDX;";
551+
default -> "drop index TEST_IDX on TEST_D;";
553552
};
554553
String[] dropScripts = new String[]{dropIndexStmt};
555554
executeWithIdempotencyCheck("IndexOperations-create", createScripts);
@@ -561,37 +560,37 @@ public void testIdempotentConstraintOperations() throws Exception {
561560
String[] createScripts;
562561
String[] dropScripts;
563562
switch (dbms.getDbType()) {
564-
case "mysql" -> {
565-
createScripts = new String[]{
566-
"create table TEST_E (ID int primary key, FK_COL int, "
567-
+ "constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID));"
568-
};
569-
dropScripts = new String[]{
570-
"alter table TEST_E drop foreign key TEST_E_FK;",
571-
"alter table TEST_E drop key TEST_E_FK;",
572-
"alter table TEST_E drop column FK_COL;"
573-
};
574-
}
575-
case "mssql" -> {
576-
createScripts = new String[]{
577-
"create table TEST_E (ID int primary key, FK_COL int, VAL int);",
578-
"alter table TEST_E add constraint TEST_E_UQ unique (VAL);",
579-
"alter table TEST_E add constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID);"
580-
};
581-
dropScripts = new String[]{
582-
"alter table TEST_E drop constraint TEST_E_FK;",
583-
"alter table TEST_E drop constraint TEST_E_UQ;"
584-
};
585-
}
586-
default -> {
587-
createScripts = new String[]{
588-
"create table TEST_E (ID int, VAL int);",
589-
"alter table TEST_E add constraint TEST_E_UQ unique (ID);"
590-
};
591-
dropScripts = new String[]{
592-
"alter table TEST_E drop constraint TEST_E_UQ;"
593-
};
594-
}
563+
case "mysql" -> {
564+
createScripts = new String[] {
565+
"create table TEST_E (ID int primary key, FK_COL int, "
566+
+ "constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID));"
567+
};
568+
dropScripts = new String[] {
569+
"alter table TEST_E drop foreign key TEST_E_FK;",
570+
"alter table TEST_E drop key TEST_E_FK;",
571+
"alter table TEST_E drop column FK_COL;"
572+
};
573+
}
574+
case "mssql" -> {
575+
createScripts = new String[] {
576+
"create table TEST_E (ID int primary key, FK_COL int, VAL int);",
577+
"alter table TEST_E add constraint TEST_E_UQ unique (VAL);",
578+
"alter table TEST_E add constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID);"
579+
};
580+
dropScripts = new String[] {
581+
"alter table TEST_E drop constraint TEST_E_FK;",
582+
"alter table TEST_E drop constraint TEST_E_UQ;"
583+
};
584+
}
585+
default -> {
586+
createScripts = new String[] {
587+
"create table TEST_E (ID int, VAL int);",
588+
"alter table TEST_E add constraint TEST_E_UQ unique (ID);"
589+
};
590+
dropScripts = new String[] {
591+
"alter table TEST_E drop constraint TEST_E_UQ;"
592+
};
593+
}
595594
}
596595
executeWithIdempotencyCheck("ConstraintOperations-add", createScripts);
597596
executeWithIdempotencyCheck("ConstraintOperations-drop", dropScripts);
@@ -607,35 +606,35 @@ public void testIdempotentAlterColumnOperations() throws Exception {
607606
String[] alterScripts;
608607
String[] dropScripts = new String[]{"alter table TEST_F drop column COL_RENAMED;"};
609608
switch (dbms.getDbType()) {
610-
case "derby" -> {
611-
alterScripts = new String[]{
612-
"create table \"TEST_F\" (\"ID\" int, \"COL_MOD\" varchar(10), \"COL_RENAME\" varchar(10));",
613-
"alter table \"TEST_F\" alter \"COL_MOD\" set data type varchar(50);",
614-
"rename column \"APP\".\"TEST_F\".\"COL_RENAME\" to \"COL_RENAMED\";"
615-
};
616-
dropScripts = new String[]{"alter table \"TEST_F\" drop column \"COL_RENAMED\";"};
617-
}
618-
case "mssql" -> alterScripts = new String[]{
619-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
620-
"alter table TEST_F alter column COL_MOD varchar(50) not null;",
621-
"exec sp_rename 'TEST_F.COL_RENAME', 'COL_RENAMED', 'COLUMN';"
622-
};
623-
case "oracle" -> alterScripts = new String[]{
624-
"create table TEST_F (ID integer, COL_MOD varchar(10), COL_RENAME varchar(10));",
625-
"alter table TEST_F modify (COL_MOD varchar(50));",
626-
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
627-
};
628-
case "postgres" -> alterScripts = new String[]{
629-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
630-
"alter table TEST_F alter column COL_MOD type varchar(50);",
631-
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
609+
case "derby" -> {
610+
alterScripts = new String[] {
611+
"create table \"TEST_F\" (\"ID\" int, \"COL_MOD\" varchar(10), \"COL_RENAME\" varchar(10));",
612+
"alter table \"TEST_F\" alter \"COL_MOD\" set data type varchar(50);",
613+
"rename column \"APP\".\"TEST_F\".\"COL_RENAME\" to \"COL_RENAMED\";"
632614
};
633-
default -> // mysql: CHANGE COLUMN renames and redefines in one statement
634-
alterScripts = new String[]{
635-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
636-
"alter table TEST_F modify column COL_MOD varchar(50);",
637-
"alter table TEST_F change column COL_RENAME COL_RENAMED varchar(10);"
638-
};
615+
dropScripts = new String[] {"alter table \"TEST_F\" drop column \"COL_RENAMED\";"};
616+
}
617+
case "mssql" -> alterScripts = new String[] {
618+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
619+
"alter table TEST_F alter column COL_MOD varchar(50) not null;",
620+
"exec sp_rename 'TEST_F.COL_RENAME', 'COL_RENAMED', 'COLUMN';"
621+
};
622+
case "oracle" -> alterScripts = new String[] {
623+
"create table TEST_F (ID integer, COL_MOD varchar(10), COL_RENAME varchar(10));",
624+
"alter table TEST_F modify (COL_MOD varchar(50));",
625+
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
626+
};
627+
case "postgres" -> alterScripts = new String[] {
628+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
629+
"alter table TEST_F alter column COL_MOD type varchar(50);",
630+
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
631+
};
632+
default -> // mysql: CHANGE COLUMN renames and redefines in one statement
633+
alterScripts = new String[] {
634+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
635+
"alter table TEST_F modify column COL_MOD varchar(50);",
636+
"alter table TEST_F change column COL_RENAME COL_RENAMED varchar(10);"
637+
};
639638
}
640639
executeWithIdempotencyCheck("AlterColumnOperations-alter", alterScripts);
641640
executeWithIdempotencyCheck("AlterColumnOperations-drop", dropScripts);

0 commit comments

Comments
 (0)