<commit_before>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.sql.optimizer.rule.cost;

import com.foundationdb.ais.model.*;
import com.foundationdb.qp.rowtype.IndexRowType;
import com.foundationdb.qp.rowtype.RowType;
import com.foundationdb.qp.rowtype.TableRowType;

public abstract class TreeStatistics
{
    public abstract long rowCount();

    public abstract int rowWidth();

    public abstract RowType rowType();

    public static TreeStatistics forTable(TableRowType rowType, TableRowCounts tableRowCounts)
    {
        return new TableStatistics(rowType, tableRowCounts);
    }

    public static TreeStatistics forIndex(IndexRowType rowType, TableRowCounts tableRowCounts)
    {
        return 
            rowType.index().isGroupIndex()
            ? new GroupIndexStatistics(rowType, tableRowCounts)
            : new TableIndexStatistics(rowType, tableRowCounts);
    }

    int fieldWidth(Column column)
    {
        int fieldWidth = 0;
        Type type = column.getType();
        switch (type.akType()) {
            case DATE:
                fieldWidth = 4;
                break;
            case DATETIME:
                fieldWidth = 8;
                break;
            case DECIMAL:
                fieldWidth = (int)column.getAverageStorageSize();
                break;
            case DOUBLE:
                fieldWidth = 8;
                break;
            case FLOAT:
                fieldWidth = 8;
                break;
            case INT:
                fieldWidth = 8;
                break;
            case LONG:
                fieldWidth = 8;
                break;
            case VARCHAR:
                fieldWidth = (int) (column.getAverageStorageSize() * PLAUSIBLE_AVERAGE_VAR_USAGE);
                break;
            case TEXT:
                fieldWidth = PLAUSIBLE_AVERAGE_BLOB_SIZE;
                break;
            case TIME:
                fieldWidth = 4;
                break;
            case TIMESTAMP:
                fieldWidth = 8;
                break;
            case U_BIGINT:
                fieldWidth = (int)column.getAverageStorageSize();
                break;
            case U_DOUBLE:
                fieldWidth = 8;
                break;
            case U_FLOAT:
                fieldWidth = 8;
                break;
            case U_INT:
                fieldWidth = 8;
                break;
            case VARBINARY:
                fieldWidth = (int) (column.getAverageStorageSize() * PLAUSIBLE_AVERAGE_VAR_USAGE);
                break;
            case YEAR:
                fieldWidth = 8;
                break;
            case BOOL:
                fieldWidth = 8;
                break;
            case INTERVAL_MILLIS:
                fieldWidth = 4;
                break;
            case INTERVAL_MONTH:
                fieldWidth = 4;
                break;
            default:
                assert false;
        }
        return fieldWidth;
    }

    private static final int PLAUSIBLE_AVERAGE_BLOB_SIZE = 100000;
    private static final double PLAUSIBLE_AVERAGE_VAR_USAGE = 0.3;

    protected TreeStatistics(TableRowCounts tableRowCounts) {
        this.tableRowCounts = tableRowCounts;
    }

    protected final TableRowCounts tableRowCounts;<commit_msg>Use TClass to estimate field width, not AkType.<commit_after>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.sql.optimizer.rule.cost;

import com.foundationdb.ais.model.*;
import com.foundationdb.qp.rowtype.IndexRowType;
import com.foundationdb.qp.rowtype.RowType;
import com.foundationdb.qp.rowtype.TableRowType;
import com.foundationdb.server.types.TClass;
import com.foundationdb.server.types.common.types.TBinary;
import com.foundationdb.server.types.common.types.TString;

public abstract class TreeStatistics
{
    public abstract long rowCount();

    public abstract int rowWidth();

    public abstract RowType rowType();

    public static TreeStatistics forTable(TableRowType rowType, TableRowCounts tableRowCounts)
    {
        return new TableStatistics(rowType, tableRowCounts);
    }

    public static TreeStatistics forIndex(IndexRowType rowType, TableRowCounts tableRowCounts)
    {
        return 
            rowType.index().isGroupIndex()
            ? new GroupIndexStatistics(rowType, tableRowCounts)
            : new TableIndexStatistics(rowType, tableRowCounts);
    }

    int fieldWidth(Column column)
    {
        TClass tclass = column.tInstance().typeClass();
        if (tclass.hasFixedSerializationSize()) {
            return tclass.fixedSerializationSize();
        }
        if (tclass instanceof TString) {
            int length = ((TString)tclass).getFixedLength();
            if (length < 0) {
                return (int)(column.getAverageStorageSize() * PLAUSIBLE_AVERAGE_VAR_USAGE);
            }
            else {
                return PLAUSIBLE_AVERAGE_BLOB_SIZE;
            }
        }
        if (tclass instanceof TBinary) {
            int length = ((TBinary)tclass).getDefaultLength();
            if (length < 0) {
                return (int)(column.getAverageStorageSize() * PLAUSIBLE_AVERAGE_VAR_USAGE);
            }
            else {
                return PLAUSIBLE_AVERAGE_BLOB_SIZE;
            }
        }
        return (int)column.getAverageStorageSize();
    }

    private static final int PLAUSIBLE_AVERAGE_BLOB_SIZE = 100000;
    private static final double PLAUSIBLE_AVERAGE_VAR_USAGE = 0.3;

    protected TreeStatistics(TableRowCounts tableRowCounts) {
        this.tableRowCounts = tableRowCounts;
    }

    protected final TableRowCounts tableRowCounts;
{'fraction_non_alphanumeric': 0.07031807160176864, 'fraction_numerical': 0.007416916274425902, 'mean_word_length': 2.079490557751427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6963159', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1817, 'n_words': 538}