<commit_before>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 <NAME>
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/

package mondrian.rolap.agg;

import mondrian.olap.Util;
import mondrian.olap.MondrianDef;
import mondrian.rolap.RolapStar;
import mondrian.rolap.sql.SqlQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

/**
 * Provides the information necessary to generate SQL for a drill-through
 * request.
 * 
 * @author jhyde
 * @author <NAME>
 * @version $Id$
 */
class DrillThroughQuerySpec extends AbstractQuerySpec {
    private final CellRequest request;
    private final String[] columnNames;

    public DrillThroughQuerySpec(final CellRequest request) {
        super(request.getMeasure().getStar());
        this.request = request;
        this.columnNames = computeDistinctColumnNames();
    }

    private String[] computeDistinctColumnNames() {
        final List columnNames = new ArrayList();
        final Set columnNameSet = new HashSet();

        final RolapStar.Column[] columns = getColumns();
        for (int i = 0; i < columns.length; i++) {
            RolapStar.Column column = columns[i];
            addColumnName(column, columnNames, columnNameSet);
        }

        addColumnName(request.getMeasure(), columnNames, columnNameSet);

        return (String[]) columnNames.toArray(new String[columnNames.size()]);
    }

    private void addColumnName(final RolapStar.Column column, 
                               final List columnNames, 
                               final Set columnNameSet) {
        String columnName = column.getName();
        if (columnName != null) {
            // nothing
        } else if (column.getExpression() instanceof MondrianDef.Column) {
            columnName = ((MondrianDef.Column) column.getExpression()).name;
        } else {
            columnName = "c" + Integer.toString(columnNames.size());
        }
        // Register the column name, and if it's not unique,
        // generate names until it is.
        for (int j = 0; !columnNameSet.add(columnName); j++) {
            columnName = "x" + Integer.toString(j);
        }
        columnNames.add(columnName);
    }

    public int getMeasureCount() {
        return 1;
    }

    public RolapStar.Measure getMeasure(final int i) {
        Util.assertTrue(i == 0);
        return request.getMeasure();
    }

    public String getMeasureAlias(final int i) {
        Util.assertTrue(i == 0);
        return columnNames[columnNames.length - 1];<commit_msg>MONDRIAN        For JPivot users, rather than having mysterious column name like         x0, x1, etc., rename columns as ColumnName_0, ColumnName_1 etc.<commit_after>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 <NAME>
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/

package mondrian.rolap.agg;

import mondrian.olap.Util;
import mondrian.olap.MondrianDef;
import mondrian.rolap.RolapStar;
import mondrian.rolap.sql.SqlQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

/**
 * Provides the information necessary to generate SQL for a drill-through
 * request.
 * 
 * @author jhyde
 * @author <NAME>
 * @version $Id$
 */
class DrillThroughQuerySpec extends AbstractQuerySpec {
    private final CellRequest request;
    private final String[] columnNames;

    public DrillThroughQuerySpec(final CellRequest request) {
        super(request.getMeasure().getStar());
        this.request = request;
        this.columnNames = computeDistinctColumnNames();
    }

    private String[] computeDistinctColumnNames() {
        final List columnNames = new ArrayList();
        final Set columnNameSet = new HashSet();

        final RolapStar.Column[] columns = getColumns();
        for (int i = 0; i < columns.length; i++) {
            RolapStar.Column column = columns[i];
            addColumnName(column, columnNames, columnNameSet);
        }

        addColumnName(request.getMeasure(), columnNames, columnNameSet);

        return (String[]) columnNames.toArray(new String[columnNames.size()]);
    }

    private void addColumnName(final RolapStar.Column column, 
                               final List columnNames, 
                               final Set columnNameSet) {
        String columnName = column.getName();
        if (columnName != null) {
            // nothing
        } else if (column.getExpression() instanceof MondrianDef.Column) {
            columnName = ((MondrianDef.Column) column.getExpression()).name;
        } else {
            columnName = "c" + Integer.toString(columnNames.size());
        }
        // Register the column name, and if it's not unique,
        // generate names until it is.
        // Instead of having mysterious column name like x0, x1 ..
        // rename columns as ColumnName_0, ColumnName_1 ..
        for (int j = 0; !columnNameSet.add(columnName); j++) {
            columnName = columnName + "_" + Integer.toString(j);
        }
        columnNames.add(columnName);
    }

    public int getMeasureCount() {
        return 1;
    }

    public RolapStar.Measure getMeasure(final int i) {
        Util.assertTrue(i == 0);
        return request.getMeasure();
    }

    public String getMeasureAlias(final int i) {
        Util.assertTrue(i == 0);
        return columnNames[columnNames.length - 1];
{'fraction_non_alphanumeric': 0.10595567867036011, 'fraction_numerical': 0.006232686980609419, 'mean_word_length': 2.5948973242065962, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5008180', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1566, 'n_words': 454}