NIFI-7448: Fix quoting of DDL table name in PutORC

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #4269.
This commit is contained in:
Matthew Burgess 2020-05-13 12:35:19 -04:00 committed by Pierre Villard
parent 9608fe33fa
commit 53a161234e
No known key found for this signature in database
GPG Key ID: BEE1599F0726E9CD
2 changed files with 28 additions and 3 deletions

View File

@ -238,9 +238,11 @@ public class NiFiOrcUtils {
}
public static String generateHiveDDL(RecordSchema recordSchema, String tableName, boolean hiveFieldNames) {
StringBuilder sb = new StringBuilder("CREATE EXTERNAL TABLE IF NOT EXISTS `");
sb.append(tableName);
sb.append("` (");
StringBuilder sb = new StringBuilder("CREATE EXTERNAL TABLE IF NOT EXISTS ");
String[] tableSections = tableName.split("\\.");
String quotedTableName = Arrays.stream(tableSections).map((section) -> "`" + section + "`").collect(Collectors.joining("."));
sb.append(quotedTableName);
sb.append(" (");
List<String> hiveColumns = new ArrayList<>();
List<RecordField> fields = recordSchema.getFields();
if (fields != null) {

View File

@ -451,6 +451,29 @@ public class PutORCTest {
testRunner.assertAllFlowFilesTransferred(PutORC.REL_SUCCESS, 1);
}
@Test
public void testDDLQuoteTableNameSections() throws IOException, InitializationException {
configure(proc, 100);
final String filename = "testORCWithDefaults-" + System.currentTimeMillis();
final Map<String, String> flowFileAttributes = new HashMap<>();
flowFileAttributes.put(CoreAttributes.FILENAME.key(), filename);
testRunner.setProperty(PutORC.HIVE_TABLE_NAME, "mydb.myTable");
testRunner.enqueue("trigger", flowFileAttributes);
testRunner.run();
testRunner.assertAllFlowFilesTransferred(PutORC.REL_SUCCESS, 1);
final Path orcFile = new Path(DIRECTORY + "/" + filename);
// verify the successful flow file has the expected attributes
final MockFlowFile mockFlowFile = testRunner.getFlowFilesForRelationship(PutORC.REL_SUCCESS).get(0);
mockFlowFile.assertAttributeEquals(PutORC.HIVE_DDL_ATTRIBUTE,
"CREATE EXTERNAL TABLE IF NOT EXISTS `mydb`.`myTable` (`name` STRING, `favorite_number` INT, `favorite_color` STRING, `scale` DOUBLE) STORED AS ORC");
}
private void verifyORCUsers(final Path orcUsers, final int numExpectedUsers) throws IOException {
verifyORCUsers(orcUsers, numExpectedUsers, null);
}