Skip to content

Commit 28a7f97

Browse files
Address sonar issues
1 parent 8ed889f commit 28a7f97

File tree

5 files changed

+99
-106
lines changed

5 files changed

+99
-106
lines changed

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: 24 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,36 +282,26 @@ 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;
313305
}
314306

315307
if (currentCommand != null && !isNonExecCommand(currentCommand)) {
@@ -319,6 +311,13 @@ public List<String> getExecutableCommands(String scriptDir, String scriptFile, b
319311
return commands;
320312
}
321313

314+
private String fixQuotesFromCurrentLine(boolean fixQuotes, String currLine) {
315+
if (fixQuotes && !getQuoteCharacter().equals(DEFAULT_QUOTE)) {
316+
currLine = currLine.replace("\\\"", getQuoteCharacter());
317+
}
318+
return currLine;
319+
}
320+
322321
@Override
323322
public String buildCommand(
324323
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public void executeScript(String scriptFile) throws SQLException, IOException {
6767
private void executeStatement(String sqlStmt) throws SQLException {
6868
if (verbose) {
6969
LOG.info("Executing: {}", sqlStmt);
70-
} else if (LOG.isDebugEnabled()) {
70+
}
71+
if (LOG.isDebugEnabled()) {
7172
LOG.debug("Executing: {}", sqlStmt);
7273
}
7374

@@ -126,4 +127,4 @@ private String normalizeToken(String token) {
126127
private boolean isDropHelperProcedure(String token) {
127128
return token != null && token.toUpperCase(Locale.ROOT).startsWith("#DROP_");
128129
}
129-
}
130+
}

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
@@ -525,9 +525,9 @@ public void testIdempotentTableOperations() throws Exception {
525525
@Test
526526
public void testIdempotentAddColumnOperations() throws Exception {
527527
String addColumnStmt = switch (dbms.getDbType()) {
528-
case "oracle" -> "alter table TEST_C add (NEW_COL integer);";
529-
case "mssql" -> "alter table TEST_C add NEW_COL int;";
530-
default -> "alter table TEST_C add column NEW_COL int;";
528+
case "oracle" -> "alter table TEST_C add (NEW_COL integer);";
529+
case "mssql" -> "alter table TEST_C add NEW_COL int;";
530+
default -> "alter table TEST_C add column NEW_COL int;";
531531
};
532532
String[] addScripts = new String[]{
533533
"create table TEST_C (ID int);",
@@ -547,10 +547,9 @@ public void testIdempotentIndexOperations() throws Exception {
547547
"create index TEST_IDX on TEST_D (ID);"
548548
};
549549
String dropIndexStmt = switch (dbms.getDbType()) {
550-
case "derby" -> "drop index \"APP\".\"TEST_IDX\";";
551-
case "oracle",
552-
"postgres" -> "drop index TEST_IDX;";
553-
default -> "drop index TEST_IDX on TEST_D;";
550+
case "derby" -> "drop index \"APP\".\"TEST_IDX\";";
551+
case "oracle", "postgres" -> "drop index TEST_IDX;";
552+
default -> "drop index TEST_IDX on TEST_D;";
554553
};
555554
String[] dropScripts = new String[]{dropIndexStmt};
556555
executeWithIdempotencyCheck("IndexOperations-create", createScripts);
@@ -562,38 +561,38 @@ public void testIdempotentConstraintOperations() throws Exception {
562561
String[] createScripts;
563562
String[] dropScripts;
564563
switch (dbms.getDbType()) {
565-
case "mysql" -> {
566-
createScripts = new String[]{
567-
"create table TEST_E (ID int primary key, FK_COL int, "
568-
+ "constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID));"
569-
};
570-
dropScripts = new String[]{
571-
"alter table TEST_E drop foreign key TEST_E_FK;",
572-
"alter table TEST_E drop key TEST_E_FK;",
573-
"alter table TEST_E drop column FK_COL;"
574-
};
575-
}
576-
case "mssql" -> {
577-
createScripts = new String[]{
578-
"create table TEST_E (ID int primary key, FK_COL int, VAL int);",
579-
"alter table TEST_E add constraint TEST_E_UQ unique (VAL);",
580-
"alter table TEST_E add constraint TEST_E_FK foreign key (FK_COL) references TEST_E(ID);"
581-
};
582-
dropScripts = new String[]{
583-
"create procedure #DROP_FK_HELPER as begin alter table TEST_E drop constraint TEST_E_FK end;",
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+
"create procedure #DROP_FK_HELPER as begin alter table TEST_E drop constraint TEST_E_FK end;",
584583
"exec #DROP_FK_HELPER;",
585-
"alter table TEST_E drop constraint TEST_E_UQ;"
586-
};
587-
}
588-
default -> {
589-
createScripts = new String[]{
590-
"create table TEST_E (ID int, VAL int);",
591-
"alter table TEST_E add constraint TEST_E_UQ unique (ID);"
592-
};
593-
dropScripts = new String[]{
594-
"alter table TEST_E drop constraint TEST_E_UQ;"
595-
};
596-
}
584+
"alter table TEST_E drop constraint TEST_E_UQ;"
585+
};
586+
}
587+
default -> {
588+
createScripts = new String[] {
589+
"create table TEST_E (ID int, VAL int);",
590+
"alter table TEST_E add constraint TEST_E_UQ unique (ID);"
591+
};
592+
dropScripts = new String[] {
593+
"alter table TEST_E drop constraint TEST_E_UQ;"
594+
};
595+
}
597596
}
598597
executeWithIdempotencyCheck("ConstraintOperations-add", createScripts);
599598
executeWithIdempotencyCheck("ConstraintOperations-drop", dropScripts);
@@ -609,35 +608,35 @@ public void testIdempotentAlterColumnOperations() throws Exception {
609608
String[] alterScripts;
610609
String[] dropScripts = new String[]{"alter table TEST_F drop column COL_RENAMED;"};
611610
switch (dbms.getDbType()) {
612-
case "derby" -> {
613-
alterScripts = new String[]{
614-
"create table \"TEST_F\" (\"ID\" int, \"COL_MOD\" varchar(10), \"COL_RENAME\" varchar(10));",
615-
"alter table \"TEST_F\" alter \"COL_MOD\" set data type varchar(50);",
616-
"rename column \"APP\".\"TEST_F\".\"COL_RENAME\" to \"COL_RENAMED\";"
617-
};
618-
dropScripts = new String[]{"alter table \"TEST_F\" drop column \"COL_RENAMED\";"};
619-
}
620-
case "mssql" -> alterScripts = new String[]{
621-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
622-
"alter table TEST_F alter column COL_MOD varchar(50) not null;",
623-
"exec sp_rename 'TEST_F.COL_RENAME', 'COL_RENAMED', 'COLUMN';"
624-
};
625-
case "oracle" -> alterScripts = new String[]{
626-
"create table TEST_F (ID integer, COL_MOD varchar(10), COL_RENAME varchar(10));",
627-
"alter table TEST_F modify (COL_MOD varchar(50));",
628-
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
629-
};
630-
case "postgres" -> alterScripts = new String[]{
631-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
632-
"alter table TEST_F alter column COL_MOD type varchar(50);",
633-
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
611+
case "derby" -> {
612+
alterScripts = new String[] {
613+
"create table \"TEST_F\" (\"ID\" int, \"COL_MOD\" varchar(10), \"COL_RENAME\" varchar(10));",
614+
"alter table \"TEST_F\" alter \"COL_MOD\" set data type varchar(50);",
615+
"rename column \"APP\".\"TEST_F\".\"COL_RENAME\" to \"COL_RENAMED\";"
634616
};
635-
default -> // mysql: CHANGE COLUMN renames and redefines in one statement
636-
alterScripts = new String[]{
637-
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
638-
"alter table TEST_F modify column COL_MOD varchar(50);",
639-
"alter table TEST_F change column COL_RENAME COL_RENAMED varchar(10);"
640-
};
617+
dropScripts = new String[] {"alter table \"TEST_F\" drop column \"COL_RENAMED\";"};
618+
}
619+
case "mssql" -> alterScripts = new String[] {
620+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
621+
"alter table TEST_F alter column COL_MOD varchar(50) not null;",
622+
"exec sp_rename 'TEST_F.COL_RENAME', 'COL_RENAMED', 'COLUMN';"
623+
};
624+
case "oracle" -> alterScripts = new String[] {
625+
"create table TEST_F (ID integer, COL_MOD varchar(10), COL_RENAME varchar(10));",
626+
"alter table TEST_F modify (COL_MOD varchar(50));",
627+
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
628+
};
629+
case "postgres" -> alterScripts = new String[] {
630+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
631+
"alter table TEST_F alter column COL_MOD type varchar(50);",
632+
"alter table TEST_F rename column COL_RENAME to COL_RENAMED;"
633+
};
634+
default -> // mysql: CHANGE COLUMN renames and redefines in one statement
635+
alterScripts = new String[] {
636+
"create table TEST_F (ID int, COL_MOD varchar(10), COL_RENAME varchar(10));",
637+
"alter table TEST_F modify column COL_MOD varchar(50);",
638+
"alter table TEST_F change column COL_RENAME COL_RENAMED varchar(10);"
639+
};
641640
}
642641
executeWithIdempotencyCheck("AlterColumnOperations-alter", alterScripts);
643642
executeWithIdempotencyCheck("AlterColumnOperations-drop", dropScripts);

0 commit comments

Comments
 (0)