/*
 *  Copyright (C) 2022 Cojen.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.cojen.tupl.filter;

import java.util.Map;

import org.cojen.tupl.rows.ColumnInfo;
import org.cojen.tupl.rows.OrderBy;

/**
 * Describes a fully parsed query specification.
 *
 * @param projection null if projection is all columns
 * @param orderBy can be null
 * @param filter never null
 * @see Parser#parseQuery
 */
public record Query(Map<String, ColumnInfo> projection, OrderBy orderBy, RowFilter filter) {
    public Query reduce() {
        RowFilter rf = filter.reduce();
        return rf.equals(filter) ? this : new Query(projection, orderBy, rf);
    }

    @Override
    public String toString() {
        if (projection == null) {
            return filter.toString();
        }

        var b = new StringBuilder();

        b.append('{');
        for (String name : projection.keySet()) {
            if (b.length() != 1) {
                b.append(',').append(' ');
            }
            OrderBy.Rule rule;
            if (orderBy != null && (rule = orderBy.get(name)) != null) {
                rule.appendTo(b);
            } else {
                b.append(name);
            }
        }
        b.append('}');

        if (filter != TrueFilter.THE) {
            b.append(':').append(' ');
            filter.appendTo(b);
        }

        return b.toString();
    }
}

@@ -768,16 +768,38 @@
 til.Map;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1370,31 +1370,193 @@
     
-if (projection == null)
+Set%3CString%3E names;%0A%0A        if (projection != null) %7B%0A            names = projection.keySet();%0A        %7D else if (orderBy != null) %7B%0A            names = orderBy.keySet();%0A        %7D else
  %7B%0A 
@@ -1642,20 +1642,8 @@
 er()
-;%0A%0A        b
 .app
@@ -1648,24 +1648,25 @@
 ppend('%7B');%0A
+%0A
         for 
@@ -1680,35 +1680,21 @@
  name : 
-projection.keySet()
+names
 ) %7B%0A    
@@ -1988,32 +1988,107 @@
     %7D%0A        %7D%0A
+%0A        if (projection == null) %7B%0A            b.append(%22, *%22);%0A        %7D%0A%0A
         b.append

Fix test failure.