@SuppressWarnings("unchecked")
    private static <E extends SqlgElement> List<Emit<E>> loadLabeledElements(
            SqlgGraph sqlgGraph,
            final ResultSet resultSet,
            LinkedList<SchemaTableTree> subQueryStack,
            boolean lastQueryStack,
            Map<String, Integer> idColumnCountMap,
            boolean forParent
    ) throws SQLException {

        List<Emit<E>> result = new ArrayList<>();
        int count = 1;
        for (SchemaTableTree schemaTableTree : subQueryStack) {
            if (!schemaTableTree.getLabels().isEmpty()) {
                E sqlgElement = null;
                boolean resultSetWasNull;
                if (schemaTableTree.isHasIDPrimaryKey()) {
                    String idProperty = schemaTableTree.labeledAliasId();
                    Integer columnCount = idColumnCountMap.get(idProperty);
                    Long id = resultSet.getLong(columnCount);
                    resultSetWasNull = resultSet.wasNull();
                    if (!resultSetWasNull) {
                        if (schemaTableTree.getSchemaTable().isVertexTable()) {
                            String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(VERTEX_PREFIX.length());
                            sqlgElement = (E) SqlgVertex.of(sqlgGraph, id, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                            schemaTableTree.loadProperty(resultSet, sqlgElement);
                        } else {
                            String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(EDGE_PREFIX.length());
                            sqlgElement = (E) new SqlgEdge(sqlgGraph, id, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                            schemaTableTree.loadProperty(resultSet, sqlgElement);
                            schemaTableTree.loadEdgeInOutVertices(resultSet, (SqlgEdge) sqlgElement);
                        }
                    }
                } else {
                    List<Comparable> identifierObjects = schemaTableTree.loadIdentifierObjects(idColumnCountMap, resultSet);
                    resultSetWasNull = resultSet.wasNull();
                    if (!resultSetWasNull) {
                        if (schemaTableTree.getSchemaTable().isVertexTable()) {
                            String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(VERTEX_PREFIX.length());
                            sqlgElement = (E) SqlgVertex.of(sqlgGraph, identifierObjects, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                            schemaTableTree.loadProperty(resultSet, sqlgElement);
                        } else {
                            String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(EDGE_PREFIX.length());
                            sqlgElement = (E) new SqlgEdge(sqlgGraph, identifierObjects, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                            schemaTableTree.loadProperty(resultSet, sqlgElement);
                            schemaTableTree.loadEdgeInOutVertices(resultSet, (SqlgEdge) sqlgElement);
                        }
                    }
                }
                if (!resultSetWasNull) {
                    //The following if statement is for for "repeat(traversal()).emit().as('label')"
                    //i.e. for emit queries with labels
                    //Only the last node in the subQueryStacks' subQueryStack must get the labels as the label only apply to the exiting element that gets emitted.
                    //Elements that come before the last element in the path must not get the labels.
                    Emit<E> emit;
                    if (schemaTableTree.isEmit() && !lastQueryStack) {
                        if (forParent) {
                            //1 is the parentIndex. This is the id of the incoming parent.
                            emit = new Emit<>(resultSet.getLong(1), sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        } else {
                            emit = new Emit<>(sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        }
                    } else if (schemaTableTree.isEmit() && lastQueryStack && (count != subQueryStack.size())) {
                        if (forParent) {
                            emit = new Emit<>(resultSet.getLong(1), sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        } else {
                            emit = new Emit<>(sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        }
                    } else {
                        if (forParent) {
                            emit = new Emit<>(resultSet.getLong(1), sqlgElement, schemaTableTree.getRealLabels(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        } else {
                            emit = new Emit<>(sqlgElement, schemaTableTree.getRealLabels(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder());
                        }
                    }
                    SchemaTableTree lastSchemaTableTree = subQueryStack.getLast();
                    if (lastSchemaTableTree.isLocalStep() && lastSchemaTableTree.isOptionalLeftJoin()) {
                        emit.setIncomingOnlyLocalOptionalStep(true);
                    }
                    result.add(emit);
                }
            }
            count++;
        }
        return result;
    }
@ SuppressWarnings ( "unchecked" ) private static < E extends SqlgElement > List < Emit < E > > loadLabeledElements ( SqlgGraph sqlgGraph , final ResultSet resultSet , LinkedList < SchemaTableTree > subQueryStack , boolean lastQueryStack , Map < String , Integer > idColumnCountMap , boolean forParent ) throws SQLException { List < Emit < E > > result = new ArrayList < > ( ) ; int count = 1 ; for ( SchemaTableTree schemaTableTree : subQueryStack ) { if ( ! schemaTableTree . getLabels ( ) . isEmpty ( ) ) { E sqlgElement = null ; boolean resultSetWasNull ; if ( schemaTableTree . isHasIDPrimaryKey ( ) ) { String idProperty = schemaTableTree . labeledAliasId ( ) ; Integer columnCount = idColumnCountMap . get ( idProperty ) ; Long id = resultSet . getLong ( columnCount ) ; resultSetWasNull = resultSet . wasNull ( ) ; if ( ! resultSetWasNull ) { if ( schemaTableTree . getSchemaTable ( ) . isVertexTable ( ) ) { String rawLabel = schemaTableTree . getSchemaTable ( ) . getTable ( ) . substring ( VERTEX_PREFIX . length ( ) ) ; sqlgElement = ( E ) SqlgVertex . of ( sqlgGraph , id , schemaTableTree . getSchemaTable ( ) . getSchema ( ) , rawLabel ) ; schemaTableTree . loadProperty ( resultSet , sqlgElement ) ; } else { String rawLabel = schemaTableTree . getSchemaTable ( ) . getTable ( ) . substring ( EDGE_PREFIX . length ( ) ) ; sqlgElement = ( E ) new SqlgEdge ( sqlgGraph , id , schemaTableTree . getSchemaTable ( ) . getSchema ( ) , rawLabel ) ; schemaTableTree . loadProperty ( resultSet , sqlgElement ) ; schemaTableTree . loadEdgeInOutVertices ( resultSet , ( SqlgEdge ) sqlgElement ) ; } } } else { List < Comparable > identifierObjects = schemaTableTree . loadIdentifierObjects ( idColumnCountMap , resultSet ) ; resultSetWasNull = resultSet . wasNull ( ) ; if ( ! resultSetWasNull ) { if ( schemaTableTree . getSchemaTable ( ) . isVertexTable ( ) ) { String rawLabel = schemaTableTree . getSchemaTable ( ) . getTable ( ) . substring ( VERTEX_PREFIX . length ( ) ) ; sqlgElement = ( E ) SqlgVertex . of ( sqlgGraph , identifierObjects , schemaTableTree . getSchemaTable ( ) . getSchema ( ) , rawLabel ) ; schemaTableTree . loadProperty ( resultSet , sqlgElement ) ; } else { String rawLabel = schemaTableTree . getSchemaTable ( ) . getTable ( ) . substring ( EDGE_PREFIX . length ( ) ) ; sqlgElement = ( E ) new SqlgEdge ( sqlgGraph , identifierObjects , schemaTableTree . getSchemaTable ( ) . getSchema ( ) , rawLabel ) ; schemaTableTree . loadProperty ( resultSet , sqlgElement ) ; schemaTableTree . loadEdgeInOutVertices ( resultSet , ( SqlgEdge ) sqlgElement ) ; } } } if ( ! resultSetWasNull ) { Emit < E > emit ; if ( schemaTableTree . isEmit ( ) && ! lastQueryStack ) { if ( forParent ) { emit = new Emit < > ( resultSet . getLong ( 1 ) , sqlgElement , Collections . emptySet ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } else { emit = new Emit < > ( sqlgElement , Collections . emptySet ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } } else if ( schemaTableTree . isEmit ( ) && lastQueryStack && ( count != subQueryStack . size ( ) ) ) { if ( forParent ) { emit = new Emit < > ( resultSet . getLong ( 1 ) , sqlgElement , Collections . emptySet ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } else { emit = new Emit < > ( sqlgElement , Collections . emptySet ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } } else { if ( forParent ) { emit = new Emit < > ( resultSet . getLong ( 1 ) , sqlgElement , schemaTableTree . getRealLabels ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } else { emit = new Emit < > ( sqlgElement , schemaTableTree . getRealLabels ( ) , schemaTableTree . getStepDepth ( ) , schemaTableTree . getSqlgComparatorHolder ( ) ) ; } } SchemaTableTree lastSchemaTableTree = subQueryStack . getLast ( ) ; if ( lastSchemaTableTree . isLocalStep ( ) && lastSchemaTableTree . isOptionalLeftJoin ( ) ) { emit . setIncomingOnlyLocalOptionalStep ( true ) ; } result . add ( emit ) ; } } count ++ ; } return result ; }
Loads all labeled or emitted elements.

@param sqlgGraph
@param resultSet
@param subQueryStack
@param lastQueryStack
@param idColumnCountMap
@return
@throws SQLException