protected void applyGroupBy()

in sql-gremlin/src/main/java/org/twilmes/sql/gremlin/adapter/converter/ast/nodes/select/GremlinSqlSelectMulti.java [239:274]


    protected void applyGroupBy(final GraphTraversal<?, ?> graphTraversal, final String edgeLabel,
                                final String inVRename, final String outVRename) throws SQLException {
        if ((sqlSelect.getGroup() == null) || (sqlSelect.getGroup().getList().isEmpty())) {
            // If we group bys but we have aggregates, we need to shove things into groups by ourselves.-
            graphTraversal.group().unfold();
        } else {
            final List<GremlinSqlIdentifier> gremlinSqlIdentifiers = new ArrayList<>();
            for (final SqlNode sqlNode : sqlSelect.getGroup().getList()) {
                gremlinSqlIdentifiers.add(GremlinSqlFactory.createNodeCheckType(sqlNode, GremlinSqlIdentifier.class));
            }
            graphTraversal.group();
            final List<GraphTraversal> byUnion = new ArrayList<>();
            for (final GremlinSqlIdentifier gremlinSqlIdentifier : gremlinSqlIdentifiers) {
                final String table = sqlMetadata.getRenamedTable(gremlinSqlIdentifier.getName(0));
                final String column = sqlMetadata
                        .getActualColumnName(sqlMetadata.getGremlinTable(table), gremlinSqlIdentifier.getName(1));
                if (column.replace(GremlinTableBase.ID, "").equalsIgnoreCase(edgeLabel)) {
                    byUnion.add(__.id());
                } else if (column.endsWith(GremlinTableBase.ID)) {
                    // TODO: Grouping edges that are not the edge that the vertex are connected - needs to be implemented.
                    throw SqlGremlinError.create(SqlGremlinError.CANNOT_GROUP_EDGES);
                } else {
                    if (inVRename.equals(table)) {
                        byUnion.add(__.inV().hasLabel(table)
                                .values(sqlMetadata.getActualColumnName(sqlMetadata.getGremlinTable(table), column)));
                    } else if (outVRename.equals(table)) {
                        byUnion.add(__.outV().hasLabel(table)
                                .values(sqlMetadata.getActualColumnName(sqlMetadata.getGremlinTable(table), column)));
                    } else {
                        throw SqlGremlinError.create(SqlGremlinError.CANNOT_GROUP_TABLE, table);
                    }
                }
            }
            graphTraversal.by(__.union(byUnion.toArray(new GraphTraversal[0])).fold()).unfold();
        }
    }