/** Deduces the names and types of a table's columns by reading the first line of a CSV file. */
 static RelDataType deduceRowType(
     JavaTypeFactory typeFactory, File file, List<CsvFieldType> fieldTypes) {
   final List<RelDataType> types = new ArrayList<RelDataType>();
   final List<String> names = new ArrayList<String>();
   CSVReader reader = null;
   try {
     reader = openCsv(file);
     final String[] strings = reader.readNext();
     for (String string : strings) {
       final String name;
       final CsvFieldType fieldType;
       final int colon = string.indexOf(':');
       if (colon >= 0) {
         name = string.substring(0, colon);
         String typeString = string.substring(colon + 1);
         fieldType = CsvFieldType.of(typeString);
         if (fieldType == null) {
           System.out.println(
               "WARNING: Found unknown type: "
                   + typeString
                   + " in file: "
                   + file.getAbsolutePath()
                   + " for column: "
                   + name
                   + ". Will assume the type of column is string");
         }
       } else {
         name = string;
         fieldType = null;
       }
       final RelDataType type;
       if (fieldType == null) {
         type = typeFactory.createJavaType(String.class);
       } else {
         type = fieldType.toType(typeFactory);
       }
       names.add(name);
       types.add(type);
       if (fieldTypes != null) {
         fieldTypes.add(fieldType);
       }
     }
   } catch (IOException e) {
     // ignore
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
   if (names.isEmpty()) {
     names.add("line");
     types.add(typeFactory.createJavaType(String.class));
   }
   return typeFactory.createStructType(Pair.zip(names, types));
 }
示例#2
0
 private static List<Integer> nullableArgs(List<Integer> list0, List<RelDataType> types) {
   final List<Integer> list = new ArrayList<Integer>();
   for (Pair<Integer, RelDataType> pair : Pair.zip(list0, types)) {
     if (pair.right.isNullable()) {
       list.add(pair.left);
     }
   }
   return list;
 }
示例#3
0
 /**
  * Catch-all implementation for {@link BuiltInMetadata.Size#averageRowSize()}, invoked using
  * reflection.
  *
  * @see org.apache.calcite.rel.metadata.RelMetadataQuery#getAverageRowSize
  */
 public Double averageRowSize(RelNode rel, RelMetadataQuery mq) {
   final List<Double> averageColumnSizes = mq.getAverageColumnSizes(rel);
   if (averageColumnSizes == null) {
     return null;
   }
   Double d = 0d;
   final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
   for (Pair<Double, RelDataTypeField> p : Pair.zip(averageColumnSizes, fields)) {
     if (p.left == null) {
       d += averageFieldValueSize(p.right);
     } else {
       d += p.left;
     }
   }
   return d;
 }