SourabhBadhya commented on code in PR #4672:
URL: https://github.com/apache/hive/pull/4672#discussion_r1335564831


##########
ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java:
##########
@@ -1527,8 +1527,23 @@ public void truncateTable(String dbDotTableName, 
Map<String, String> partSpec, L
       }
 
       // TODO: APIs with catalog names
-      List<String> partNames = ((null == partSpec)
-        ? null : getPartitionNames(table.getDbName(), table.getTableName(), 
partSpec, (short) -1));
+      List<String> partNames = null;
+      if (table.getStorageHandler() != null && 
table.getStorageHandler().alwaysUnpartitioned() && partSpec != null) {
+        StringBuilder partName = null;
+        for (Map.Entry<String, String> entry : partSpec.entrySet()) {
+          if (partName == null) {
+            partName = new StringBuilder(String.format("%s=%s", 
entry.getKey(), entry.getValue()));

Review Comment:
   This is no more required. `getPartitionsByFilter()` should get the list of 
partitions now.



##########
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/misc/truncate/TruncateTableAnalyzer.java:
##########
@@ -338,4 +370,56 @@ private void addStatTask(ASTNode root, Table table, Path 
oldPartitionLocation, P
       moveTask.addDependentTask(statTask);
     }
   }
+
+  public StringBuilder constructDeleteQuery(Table table, Map<String, String> 
partitionSpec) throws SemanticException {
+    String qualifiedTableName = 
HiveTableName.ofNullable(HiveUtils.unparseIdentifier(table.getTableName(), 
this.conf),
+            HiveUtils.unparseIdentifier(table.getDbName(), this.conf), 
null).getNotEmptyDbTable();
+    StringBuilder sb = new StringBuilder().append("delete from 
").append(qualifiedTableName)
+            .append(" where ");
+    Deserializer deserializer = table.getDeserializer();
+    Map<String, PrimitiveObjectInspector.PrimitiveCategory> stringTypeInfoMap 
= new HashMap<>();
+    try {
+      ObjectInspector objectInspector = deserializer.getObjectInspector();
+      if (objectInspector.getCategory() == ObjectInspector.Category.STRUCT) {
+        StructObjectInspector structObjectInspector = (StructObjectInspector) 
objectInspector;
+        List<? extends StructField> structFields =  
structObjectInspector.getAllStructFieldRefs();
+        for (StructField structField : structFields) {
+          if (structField.getFieldObjectInspector().getCategory() == 
ObjectInspector.Category.PRIMITIVE) {
+            PrimitiveObjectInspector primitiveObjectInspector = 
(PrimitiveObjectInspector) structField.getFieldObjectInspector();
+            stringTypeInfoMap.put(structField.getFieldName(),
+                primitiveObjectInspector.getTypeInfo().getPrimitiveCategory());
+          }
+        }
+      }
+    } catch (SerDeException e) {
+      throw new SemanticException(String.format("Unable to get object 
inspector due to: %s", e));
+    }
+    boolean first = true;
+    for (String key : partitionSpec.keySet()) {
+      PrimitiveObjectInspector.PrimitiveCategory category = 
stringTypeInfoMap.get(key);
+      if (category == null) {
+        throw new SemanticException(String.format(
+            "Only primitive partition column type is supported via TRUNCATE 
operation " +
+            " but column %s is not a primitive category.", key));
+      }
+      String value = partitionSpec.get(key);
+      boolean shouldEncloseQuotes = 
TypeInfoUtils.shouldEncloseQuotes(category);
+      if (value != null) {
+        sb.append(first ? "" : " and ").append(key).append(" = ");
+        if (shouldEncloseQuotes) {
+          // The partition value is containing a single-quote hence we cannot 
use \' as the enclosing quote.
+          // We need to use \" as the enclosing quote.
+          if (value.contains("'")) {
+            sb.append("\"" ).append(value).append("\"");
+          } else {
+            sb.append("'").append(value).append("'");
+          }
+        } else {
+          sb.append(value);
+        }

Review Comment:
   Added the same. Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to