Przeglądaj źródła

HUE-1746 [metastore] Auto detect header if all cols are strings

Better float type detection.
Romain Rigaux 12 lat temu
rodzic
commit
98c9b54

+ 6 - 1
apps/beeswax/src/beeswax/create_table.py

@@ -38,6 +38,7 @@ from beeswax.forms import CreateTableForm, ColumnTypeFormSet,\
   TERMINATOR_CHOICES
   TERMINATOR_CHOICES
 from beeswax.server import dbms
 from beeswax.server import dbms
 from beeswax.views import execute_directly
 from beeswax.views import execute_directly
+import re
 
 
 
 
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
@@ -201,13 +202,17 @@ def import_wizard(request, database='default'):
             })
             })
           s3_col_formset = ColumnTypeFormSet(prefix='cols', initial=columns)
           s3_col_formset = ColumnTypeFormSet(prefix='cols', initial=columns)
         try:
         try:
+          fields_list_for_json = list(fields_list)
+          if fields_list_for_json:
+            fields_list_for_json[0] = map(lambda a: re.sub('[^\w]', '', a), fields_list_for_json[0]) # Cleaning headers
+
           return render('define_columns.mako', request, {
           return render('define_columns.mako', request, {
             'action': reverse(app_name + ':import_wizard', kwargs={'database': database}),
             'action': reverse(app_name + ':import_wizard', kwargs={'database': database}),
             'file_form': s1_file_form,
             'file_form': s1_file_form,
             'delim_form': s2_delim_form,
             'delim_form': s2_delim_form,
             'column_formset': s3_col_formset,
             'column_formset': s3_col_formset,
             'fields_list': fields_list,
             'fields_list': fields_list,
-            'fields_list_json': json.dumps(fields_list),
+            'fields_list_json': json.dumps(fields_list_for_json),
             'n_cols': n_cols,
             'n_cols': n_cols,
             'database': database,
             'database': database,
             'databases': databases
             'databases': databases

+ 5 - 4
apps/beeswax/src/beeswax/templates/define_columns.mako

@@ -219,6 +219,7 @@ ${ layout.metastore_menubar() }
           $(item).text($(item).data('previous'));
           $(item).text($(item).data('previous'));
         }
         }
       });
       });
+
       guessColumnTypes();
       guessColumnTypes();
     });
     });
 
 
@@ -227,12 +228,12 @@ ${ layout.metastore_menubar() }
     // Really basic heuristic to detect if first row is a header.
     // Really basic heuristic to detect if first row is a header.
     var isString = 0;
     var isString = 0;
     $(".cols-1").each(function (cnt, item) {
     $(".cols-1").each(function (cnt, item) {
-      if ($(".cols-1").data("possibleType") == 'string') {
+      if ($(item).data("possibleType") == 'string') {
         isString += 1;
         isString += 1;
       }
       }
     });
     });
-
-    if (isString > $(".cols-1").length - 1) {
+    // First row is just strings
+    if (isString == $(".cols-1").length) {
       $("#useHeader").click();
       $("#useHeader").click();
     }
     }
 
 
@@ -356,7 +357,7 @@ ${ layout.metastore_menubar() }
     }
     }
 
 
     function isInt(n) {
     function isInt(n) {
-      return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
+      return Math.floor(n) == n && n.toString().indexOf(".") == -1;
     }
     }
   });
   });
 </script>
 </script>