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

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


The following commit(s) were added to refs/heads/master by this push:
     new 295ea5b  ORC-568: Have convert tool sort old columns by number.
295ea5b is described below

commit 295ea5be6c2ae643bb3c3ca6db7b018aeb51bebc
Author: Owen O'Malley <[email protected]>
AuthorDate: Fri Nov 1 16:12:15 2019 -0700

    ORC-568: Have convert tool sort old columns by number.
    
    Fixes #449
    
    Signed-off-by: Owen O'Malley <[email protected]>
---
 .../java/org/apache/orc/tools/json/StructType.java | 29 ++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/java/tools/src/java/org/apache/orc/tools/json/StructType.java 
b/java/tools/src/java/org/apache/orc/tools/json/StructType.java
index c79146a..a3b7be0 100644
--- a/java/tools/src/java/org/apache/orc/tools/json/StructType.java
+++ b/java/tools/src/java/org/apache/orc/tools/json/StructType.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -21,6 +21,7 @@ package org.apache.orc.tools.json;
 import org.apache.orc.TypeDescription;
 
 import java.io.PrintStream;
+import java.util.Comparator;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -28,7 +29,31 @@ import java.util.TreeMap;
  * Model structs.
  */
 class StructType extends HiveType {
-  final Map<String, HiveType> fields = new TreeMap<String, HiveType>();
+  private static final String COL_PREFIX = "_col";
+  private final static Comparator<String> FIELD_COMPARATOR = (left, right) -> {
+    if (left == null) {
+      return right == null ? 0 : -1;
+    } else if (right == null) {
+      return 1;
+    } else if (left.startsWith(COL_PREFIX)) {
+      if (right.startsWith(COL_PREFIX)) {
+        try {
+          int leftInt = Integer.parseInt(left.substring(COL_PREFIX.length()));
+          int rightInt = 
Integer.parseInt(right.substring(COL_PREFIX.length()));
+          return Integer.compare(leftInt, rightInt);
+        } catch (Exception e) {
+          // fall back to the normal rules
+        }
+      } else {
+        return 1;
+      }
+    } else if (right.startsWith(COL_PREFIX)) {
+      return 1;
+    }
+    return left.compareTo(right);
+  };
+
+  final Map<String, HiveType> fields = new TreeMap<>(FIELD_COMPARATOR);
 
   StructType() {
     super(Kind.STRUCT);

Reply via email to