private static void addSchemaPathToFieldDef(SchemaPath path, FieldDef.Builder builder) {
   for (PathSegment p = path.getRootSegment(); ; p = p.getChild()) {
     NamePart.Builder b = NamePart.newBuilder();
     if (p.isArray()) {
       b.setType(Type.ARRAY);
     } else {
       b.setName(p.getNameSegment().getPath().toString());
       b.setType(Type.NAME);
     }
     builder.addName(b.build());
     if (p.isLastPath()) break;
   }
 }
Beispiel #2
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()));
   }
 }
  public boolean matches(SchemaPath path) {
    Iterator<NamePart> iter = def.getNameList().iterator();

    for (PathSegment p = path.getRootSegment(); ; p = p.getChild()) {
      if (p == null) break;
      if (!iter.hasNext()) return false;
      NamePart n = iter.next();

      if (p.isArray()) {
        if (n.getType() == Type.ARRAY) continue;
        return false;
      } else {
        if (p.getNameSegment().getPath().equals(n.getName())) continue;
        return false;
      }
    }
    // we've reviewed all path segments. confirm that we don't have any extra name parts.
    return !iter.hasNext();
  }
 public void ensureAtLeastOneField(ComplexWriter writer) {
   if (!atLeastOneWrite) {
     // if we had no columns, create one empty one so we can return some data
     // for count purposes.
     SchemaPath sp = columns.get(0);
     PathSegment root = sp.getRootSegment();
     BaseWriter.MapWriter fieldWriter = writer.rootAsMap();
     while (root.getChild() != null && !root.getChild().isArray()) {
       fieldWriter = fieldWriter.map(root.getNameSegment().getPath());
       root = root.getChild();
     }
     fieldWriter.integer(root.getNameSegment().getPath());
   }
 }
  // 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);
      }
    }
  }