Search in sources :

Example 1 with FieldPath

use of org.ojai.FieldPath in project drill by apache.

the class MaprDBJsonRecordReader method transformColumns.

@Override
protected Collection<SchemaPath> transformColumns(Collection<SchemaPath> columns) {
    Set<SchemaPath> transformed = Sets.newLinkedHashSet();
    Set<SchemaPath> encodedSchemaPathSet = Sets.newLinkedHashSet();
    if (disablePushdown) {
        transformed.add(SchemaPath.STAR_COLUMN);
        includeId = true;
    } else {
        if (isStarQuery()) {
            transformed.add(SchemaPath.STAR_COLUMN);
            includeId = true;
            if (isSkipQuery() && !disableCountOptimization) {
                // `SELECT COUNT(*)` query
                idOnly = true;
                scannedFields = ID_ONLY_PROJECTION;
            }
        } else {
            Set<FieldPath> scannedFieldsSet = Sets.newTreeSet();
            Set<FieldPath> projectedFieldsSet = null;
            for (SchemaPath column : columns) {
                if (EncodedSchemaPathSet.isEncodedSchemaPath(column)) {
                    encodedSchemaPathSet.add(column);
                } else {
                    transformed.add(column);
                    if (!DOCUMENT_SCHEMA_PATH.equals(column)) {
                        FieldPath fp = getFieldPathForProjection(column);
                        scannedFieldsSet.add(fp);
                    } else {
                        projectWholeDocument = true;
                    }
                }
            }
            if (projectWholeDocument) {
                // we do not want to project the fields from the encoded field path list
                // hence make a copy of the scannedFieldsSet here for projection.
                projectedFieldsSet = new ImmutableSet.Builder<FieldPath>().addAll(scannedFieldsSet).build();
            }
            if (encodedSchemaPathSet.size() > 0) {
                Collection<SchemaPath> decodedSchemaPaths = EncodedSchemaPathSet.decode(encodedSchemaPathSet);
                // add them to scanned set or clear the scanned set if all fields were requested.
                for (SchemaPath column : decodedSchemaPaths) {
                    if (column.equals(SchemaPath.STAR_COLUMN)) {
                        includeId = true;
                        scannedFieldsSet.clear();
                        break;
                    }
                    scannedFieldsSet.add(getFieldPathForProjection(column));
                }
            }
            if (scannedFieldsSet.size() > 0) {
                if (includesIdField(scannedFieldsSet)) {
                    includeId = true;
                }
                scannedFields = scannedFieldsSet.toArray(new FieldPath[scannedFieldsSet.size()]);
            }
            if (disableCountOptimization) {
                idOnly = (scannedFields == null);
            }
            if (projectWholeDocument) {
                projector = new FieldProjector(projectedFieldsSet);
            }
        }
    }
    return transformed;
}
Also used : FieldProjector(org.ojai.util.FieldProjector) ImmutableSet(org.apache.drill.shaded.guava.com.google.common.collect.ImmutableSet) SchemaPath(org.apache.drill.common.expression.SchemaPath) FieldPath(org.ojai.FieldPath)

Example 2 with FieldPath

use of org.ojai.FieldPath in project drill by apache.

the class MaprDBJsonRecordReader method getFieldPathForProjection.

/*
   * Extracts contiguous named segments from the SchemaPath, starting from the
   * root segment and build the FieldPath from it for projection.
   *
   * This is due to bug 22726 and 22727, which cause DB's DocumentReaders to
   * behave incorrectly for sparse lists, hence we avoid projecting beyond the
   * first encountered ARRAY field and let Drill handle the projection.
   */
private static FieldPath getFieldPathForProjection(SchemaPath column) {
    Stack<PathSegment.NameSegment> pathSegments = new Stack<>();
    PathSegment seg = column.getRootSegment();
    while (seg != null && seg.isNamed()) {
        pathSegments.push((PathSegment.NameSegment) seg);
        seg = seg.getChild();
    }
    FieldSegment.NameSegment child = null;
    while (!pathSegments.isEmpty()) {
        child = new FieldSegment.NameSegment(pathSegments.pop().getPath(), child, false);
    }
    return new FieldPath(child);
}
Also used : FieldSegment(org.ojai.FieldSegment) FieldPath(org.ojai.FieldPath) PathSegment(org.apache.drill.common.expression.PathSegment) Stack(java.util.Stack)

Example 3 with FieldPath

use of org.ojai.FieldPath in project drill by apache.

the class FieldPathHelper method schemaPathToFieldPath.

/**
 * Returns {@link FieldPath} equivalent of the specified {@link SchemaPath}.
 *
 * @param schemaPath {@link SchemaPath} instance that should be converted
 * @return {@link FieldPath} equivalent of the specified {@link SchemaPath}.
 */
public static FieldPath schemaPathToFieldPath(SchemaPath schemaPath) {
    Deque<PathSegment> pathSegments = Queues.newArrayDeque();
    PathSegment pathSegment = schemaPath.getRootSegment();
    while (pathSegment != null) {
        pathSegments.push(pathSegment);
        pathSegment = pathSegment.getChild();
    }
    FieldSegment child = null;
    while (!pathSegments.isEmpty()) {
        pathSegment = pathSegments.pop();
        if (pathSegment.isNamed()) {
            child = new FieldSegment.NameSegment(((PathSegment.NameSegment) pathSegment).getPath(), child, false);
        } else {
            child = new FieldSegment.IndexSegment(String.valueOf(((PathSegment.ArraySegment) pathSegment).getIndex()), child);
        }
    }
    return new FieldPath((FieldSegment.NameSegment) child);
}
Also used : FieldSegment(org.ojai.FieldSegment) FieldPath(org.ojai.FieldPath) PathSegment(org.apache.drill.common.expression.PathSegment)

Example 4 with FieldPath

use of org.ojai.FieldPath in project drill by apache.

the class FieldPathHelper method schemaPath2FieldPath.

/**
 * Returns {@link FieldPath} equivalent of the specified {@link SchemaPath}.
 */
public static FieldPath schemaPath2FieldPath(SchemaPath column) {
    Stack<PathSegment> pathSegments = new Stack<PathSegment>();
    PathSegment seg = column.getRootSegment();
    while (seg != null) {
        pathSegments.push(seg);
        seg = seg.getChild();
    }
    FieldSegment child = null;
    while (!pathSegments.isEmpty()) {
        seg = pathSegments.pop();
        if (seg.isNamed()) {
            child = new FieldSegment.NameSegment(((PathSegment.NameSegment) seg).getPath(), child, false);
        } else {
            child = new FieldSegment.IndexSegment(((PathSegment.ArraySegment) seg).getIndex(), child);
        }
    }
    return new FieldPath((FieldSegment.NameSegment) child);
}
Also used : FieldSegment(org.ojai.FieldSegment) FieldPath(org.ojai.FieldPath) PathSegment(org.apache.drill.common.expression.PathSegment) Stack(java.util.Stack)

Example 5 with FieldPath

use of org.ojai.FieldPath in project drill by axbaretto.

the class MaprDBJsonRecordReader method transformColumns.

@Override
protected Collection<SchemaPath> transformColumns(Collection<SchemaPath> columns) {
    Set<SchemaPath> transformed = Sets.newLinkedHashSet();
    if (disablePushdown) {
        transformed.add(SchemaPath.STAR_COLUMN);
        includeId = true;
        return transformed;
    }
    if (isStarQuery()) {
        transformed.add(SchemaPath.STAR_COLUMN);
        includeId = true;
        if (isSkipQuery()) {
            // `SELECT COUNT(*)` query
            if (!disableCountOptimization) {
                projectedFields = new FieldPath[1];
                projectedFields[0] = ID_FIELD;
            }
        }
        return transformed;
    }
    Set<FieldPath> projectedFieldsSet = Sets.newTreeSet();
    for (SchemaPath column : columns) {
        if (column.getRootSegment().getPath().equalsIgnoreCase(ID_KEY)) {
            includeId = true;
            if (!disableCountOptimization) {
                projectedFieldsSet.add(ID_FIELD);
            }
        } else {
            projectedFieldsSet.add(getFieldPathForProjection(column));
        }
        transformed.add(column);
    }
    if (projectedFieldsSet.size() > 0) {
        projectedFields = projectedFieldsSet.toArray(new FieldPath[projectedFieldsSet.size()]);
    }
    if (disableCountOptimization) {
        idOnly = (projectedFields == null);
    }
    return transformed;
}
Also used : SchemaPath(org.apache.drill.common.expression.SchemaPath) FieldPath(org.ojai.FieldPath)

Aggregations

FieldPath (org.ojai.FieldPath)8 PathSegment (org.apache.drill.common.expression.PathSegment)5 FieldSegment (org.ojai.FieldSegment)5 Stack (java.util.Stack)3 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 ImmutableSet (org.apache.drill.shaded.guava.com.google.common.collect.ImmutableSet)1 BaseTest (org.apache.drill.test.BaseTest)1 Test (org.junit.Test)1 FieldProjector (org.ojai.util.FieldProjector)1