You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pv...@apache.org on 2020/05/14 09:41:46 UTC

[nifi] branch master updated: NIFI-7448: Fix quoting of DDL table name in PutORC

This is an automated email from the ASF dual-hosted git repository.

pvillard pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/master by this push:
     new 53a1612  NIFI-7448: Fix quoting of DDL table name in PutORC
53a1612 is described below

commit 53a161234e651c9b5259723d5ffc986cc6eab7dd
Author: Matthew Burgess <ma...@apache.org>
AuthorDate: Wed May 13 12:35:19 2020 -0400

    NIFI-7448: Fix quoting of DDL table name in PutORC
    
    Signed-off-by: Pierre Villard <pi...@gmail.com>
    
    This closes #4269.
---
 .../apache/hadoop/hive/ql/io/orc/NiFiOrcUtils.java |  8 +++++---
 .../org/apache/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 1418b1b..f726010 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<String> hiveColumns = new ArrayList<>();
         List<RecordField> 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 2df3467..a6d0214 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<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);
     }