From 53a161234e651c9b5259723d5ffc986cc6eab7dd Mon Sep 17 00:00:00 2001 From: Matthew Burgess Date: Wed, 13 May 2020 12:35:19 -0400 Subject: [PATCH] NIFI-7448: Fix quoting of DDL table name in PutORC Signed-off-by: Pierre Villard This closes #4269. --- .../hadoop/hive/ql/io/orc/NiFiOrcUtils.java | 8 ++++--- .../nifi/processors/orc/PutORCTest.java | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/main/java/org/apache/hadoop/hive/ql/io/orc/NiFiOrcUtils.java b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/main/java/org/apache/hadoop/hive/ql/io/orc/NiFiOrcUtils.java index 1418b1bc49..f726010695 100644 --- a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/main/java/org/apache/hadoop/hive/ql/io/orc/NiFiOrcUtils.java +++ b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/main/java/org/apache/hadoop/hive/ql/io/orc/NiFiOrcUtils.java @@ -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 hiveColumns = new ArrayList<>(); List fields = recordSchema.getFields(); if (fields != null) { diff --git a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/test/java/org/apache/nifi/processors/orc/PutORCTest.java b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/test/java/org/apache/nifi/processors/orc/PutORCTest.java index 2df3467aa3..a6d02148b1 100644 --- a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/test/java/org/apache/nifi/processors/orc/PutORCTest.java +++ b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/test/java/org/apache/nifi/processors/orc/PutORCTest.java @@ -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 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); }