Esempio n. 1
0
 private void checkRecord(Type eltType, Record record, IEvaluatorContext ctx) {
   // TODO: not all inconsistencies are detected yet
   // probably because absent values get type void
   // but are nevertheless initialized eventually
   // to 0, false, or "".
   if (record.getType().isSubtypeOf(eltType)) {
     return;
   }
   if (eltType.isTuple()) {
     int expectedArity = eltType.getArity();
     int actualArity = record.getType().getArity();
     if (expectedArity == actualArity) {
       return;
     }
     throw RuntimeExceptionFactory.illegalTypeArgument(
         "Arities of actual type and requested type are different ("
             + actualArity
             + " vs "
             + expectedArity
             + ")",
         ctx.getCurrentAST(),
         ctx.getStackTrace());
   }
   throw RuntimeExceptionFactory.illegalTypeArgument(
       "Invalid tuple " + record + " for requested field " + eltType,
       ctx.getCurrentAST(),
       ctx.getStackTrace());
 }
Esempio n. 2
0
 private IValue read(Type resultType, ISourceLocation loc, IMap options, IEvaluatorContext ctx) {
   setOptions(options);
   Reader reader = null;
   try {
     reader = ctx.getResolverRegistry().getCharacterReader(loc.getURI());
     List<Record> records = loadRecords(reader);
     if (resultType == null) {
       resultType = inferType(records, ctx);
       ctx.getStdOut().println("readCSV inferred the relation type: " + resultType);
       ctx.getStdOut().flush();
     } else if (header) {
       records.remove(0);
     }
     return buildCollection(resultType, records, ctx);
   } catch (IOException e) {
     throw RuntimeExceptionFactory.io(
         values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         throw RuntimeExceptionFactory.io(
             values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace());
       }
     }
   }
 }
Esempio n. 3
0
  public IValue buildCollection(Type type, List<Record> records, IEvaluatorContext ctx) {
    IWriter writer;
    while (type.isAliased()) {
      type = type.getAliased();
    }

    if (type.isList()) {
      writer = values.listWriter(type.getElementType());
    } else if (type.isRelation()) {
      writer = values.relationWriter(type.getElementType());
    } else {
      throw RuntimeExceptionFactory.illegalTypeArgument(
          "Invalid result type for CSV reading: " + type, ctx.getCurrentAST(), ctx.getStackTrace());
    }

    // reverse traversal so that the order in case of ListWriter is correct
    // (IWriter only supports inserts at the front).
    Type eltType = type.getElementType();
    for (int i = records.size() - 1; i >= 0; i--) {
      Record record = records.get(i);
      checkRecord(eltType, record, ctx);
      writer.insert(record.getTuple(eltType));
    }
    return writer.done();
  }
Esempio n. 4
0
 private Type inferType(List<Record> records, IEvaluatorContext ctx) throws IOException {
   String[] labels = null;
   if (header) {
     labels = extractLabels(records.remove(0));
   }
   Type[] fieldTypes = null;
   for (Record ri : records) {
     List<Type> ftypes = ri.getFieldTypes();
     if (fieldTypes == null) {
       fieldTypes = new Type[ftypes.size()];
       Arrays.fill(fieldTypes, types.voidType());
     } else if (ftypes.size() != fieldTypes.length) {
       // We assume all records in the CSV file to have the same arity.
       throw RuntimeExceptionFactory.illegalArgument(
           // "Inconsistent tuple in CSV expected " + fieldTypes.length + " but was " +
           // ftypes.size(),
           ctx.getCurrentAST(), ctx.getStackTrace());
     }
     for (int i = 0; i < ftypes.size(); i++) {
       fieldTypes[i] = fieldTypes[i].lub(ftypes.get(i));
       if (fieldTypes[i].isTop()) {
         fieldTypes[i] = types.stringType();
       }
     }
   }
   if (labels == null) {
     labels = makeUpLabels(fieldTypes.length);
   }
   return types.setType(types.tupleType(fieldTypes, labels));
 }
Esempio n. 5
0
  public void write(IValue rel, ISourceLocation loc, IEvaluatorContext ctx) {

    OutputStream out = null;

    Type paramType = ctx.getCurrentEnvt().getTypeBindings().get(types.parameterType("T"));
    if (!paramType.isRelation() && !paramType.isListRelation()) {
      throw RuntimeExceptionFactory.illegalTypeArgument(
          "A relation type is required instead of " + paramType,
          ctx.getCurrentAST(),
          ctx.getStackTrace());
    }

    try {
      boolean isListRel = rel instanceof IList;
      out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false);
      ISet irel = null;
      IList lrel = null;
      if (isListRel) {
        lrel = (IList) rel;
      } else {
        irel = (ISet) rel;
      }

      int nfields = isListRel ? lrel.asRelation().arity() : irel.asRelation().arity();
      if (header) {
        for (int i = 0; i < nfields; i++) {
          if (i > 0) out.write(separator);
          String label = paramType.getFieldName(i);
          if (label == null || label.isEmpty()) label = "field" + i;
          writeString(out, label);
        }
        out.write('\n');
      }
      String separatorAsString = new String(Character.toChars(separator));
      for (IValue v : (isListRel ? lrel : irel)) {
        ITuple tup = (ITuple) v;
        int sep = 0;
        for (IValue w : tup) {
          if (sep == 0) sep = separator;
          else out.write(sep);
          if (w.getType().isString()) {
            String s = ((IString) w).getValue();

            if (s.contains(separatorAsString)
                || s.contains("\n")
                || s.contains("\r")
                || s.contains("\"")) {
              s = s.replaceAll("\"", "\"\"");
              out.write('"');
              writeString(out, s);
              out.write('"');
            } else writeString(out, s);
          } else {
            writeString(out, w.toString());
          }
        }
        out.write('\n');
      }
      out.flush();
      out.close();
    } catch (IOException e) {
      throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
    } finally {
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException ioex) {
          throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null);
        }
      }
    }
  }