Ejemplo n.º 1
0
 @JsonCreator
 public Project(@JsonProperty("projections") NamedExpression[] selections) {
   this.selections = selections;
   if (selections == null || selections.length == 0)
     throw new ExpressionParsingException(
         "Project did not provide any projection selections.  At least one projection must be provided.");
   for (int i = 0; i < selections.length; i++) {
     PathSegment segment = selections[i].getRef().getRootSegment();
     CharSequence path = segment.getNameSegment().getPath();
     if (!segment.isNamed() || !path.equals("output"))
       throw new ExpressionParsingException(
           String.format(
               "Outputs for projections always have to start with named path of output. First segment was named '%s' or was named [%s]",
               path, segment.isNamed()));
   }
 }
  // This function assumes that the fields in the schema parameter are in the same order as the
  // fields in the columns parameter. The
  // columns parameter may have fields that are not present in the schema, though.
  public DrillParquetGroupConverter(
      OutputMutator mutator,
      MapWriter mapWriter,
      GroupType schema,
      Collection<SchemaPath> columns,
      OptionManager options) {
    this.mapWriter = mapWriter;
    this.mutator = mutator;
    converters = Lists.newArrayList();
    this.options = options;

    Iterator<SchemaPath> colIterator = columns.iterator();

    for (Type type : schema.getFields()) {
      Repetition rep = type.getRepetition();
      boolean isPrimitive = type.isPrimitive();

      // Match the name of the field in the schema definition to the name of the field in the query.
      String name = null;
      SchemaPath col = null;
      PathSegment colPath = null;
      PathSegment colNextChild = null;
      while (colIterator.hasNext()) {
        col = colIterator.next();
        colPath = col.getRootSegment();
        colNextChild = colPath.getChild();

        if (colPath != null
            && colPath.isNamed()
            && (!colPath.getNameSegment().getPath().equals("*"))) {
          name = colPath.getNameSegment().getPath();
          // We may have a field that does not exist in the schema
          if (!name.equalsIgnoreCase(type.getName())) {
            continue;
          }
        }
        break;
      }
      if (name == null) {
        name = type.getName();
      }

      if (!isPrimitive) {
        Collection<SchemaPath> c = new ArrayList<SchemaPath>();

        while (colNextChild != null) {
          if (colNextChild.isNamed()) {
            break;
          }
          colNextChild = colNextChild.getChild();
        }

        if (colNextChild != null) {
          SchemaPath s = new SchemaPath(colNextChild.getNameSegment());
          c.add(s);
        }
        if (rep != Repetition.REPEATED) {
          DrillParquetGroupConverter converter =
              new DrillParquetGroupConverter(
                  mutator, mapWriter.map(name), type.asGroupType(), c, options);
          converters.add(converter);
        } else {
          DrillParquetGroupConverter converter =
              new DrillParquetGroupConverter(
                  mutator, mapWriter.list(name).map(), type.asGroupType(), c, options);
          converters.add(converter);
        }
      } else {
        PrimitiveConverter converter = getConverterForType(name, type.asPrimitiveType());
        converters.add(converter);
      }
    }
  }