<commit_before>/**
 * Copyright (C) 2012 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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.akiban.sql.optimizer.rule.join_enum;

import com.akiban.sql.optimizer.rule.CostEstimator;
import com.akiban.sql.optimizer.rule.join_enum.DPhyp.JoinOperator;
import com.akiban.sql.optimizer.rule.range.ColumnRanges;
import com.akiban.sql.optimizer.rule.range.RangeSegment;

import com.akiban.sql.optimizer.plan.*;
import com.akiban.sql.optimizer.plan.IndexScan.OrderEffectiveness;
import com.akiban.sql.optimizer.plan.Sort.OrderByExpression;
import com.akiban.sql.optimizer.plan.TableGroupJoinTree.TableGroupJoinNode;

import com.akiban.ais.model.Column;
import com.akiban.ais.model.GroupIndex;
import com.akiban.ais.model.Index.JoinType;
import com.akiban.ais.model.IndexColumn;
import com.akiban.ais.model.TableIndex;
import com.akiban.ais.model.UserTable;
import com.akiban.server.expression.std.Comparison;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/** The goal of a indexes within a group. */
public class GroupIndexGoal implements Comparator<IndexScan>
{
    private static final Logger logger = LoggerFactory.getLogger(GroupIndexGoal.class);

    // The overall goal.
    private QueryIndexGoal queryGoal;
    // The grouped tables.
    private TableGroupJoinTree tables;
    
    // All the conditions that might be indexable.
    private List<ConditionExpression> conditions;
    // Where they came from.
    private List<ConditionList> conditionSources;

    // All the columns besides those in conditions that will be needed.
    private RequiredColumns requiredColumns;

    // Tables already bound outside.
    private Set<ColumnSource> boundTables;

    // Can an index be used to take care of sorting.
    // TODO: Make this a subset of queryGoal's sorting, based on what
    // tables are in what join, rather than an all or nothing.
    private boolean sortAllowed;

    // Mapping of Range-expressible conditions, by their column. lazy loaded.
    private Map<ColumnExpression,ColumnRanges> columnsToRanges;

    public GroupIndexGoal(QueryIndexGoal queryGoal, TableGroupJoinTree tables) {
        this.queryGoal = queryGoal;
        this.tables = tables;

        if (queryGoal.getWhereConditions() != null) {
            conditionSources = Collections.singletonList(queryGoal.getWhereConditions());
            conditions = queryGoal.getWhereConditions();
        }
        else {
            conditionSources = Collections.emptyList();
            conditions = Collections.emptyList();
        }

        requiredColumns = new RequiredColumns(tables);

        boundTables = queryGoal.getQuery().getOuterTables();
        sortAllowed = true;
    }

    public QueryIndexGoal getQueryGoal() {
        return queryGoal;
    }

    public TableGroupJoinTree getTables() {
        return tables;
    }

    public List<ConditionList> updateContext(Set<ColumnSource> boundTables,
                                             Collection<JoinOperator> joins,
                                             boolean sortAllowed) {
        setBoundTables(boundTables);
        this.sortAllowed = sortAllowed;
        setJoinConditions(joins);
        updateRequiredColumns();
        return conditionSources;
    }

    public void setBoundTables(Set<ColumnSource> boundTables) {
        this.boundTables = boundTables;
    }
    
    public void setJoinConditions(Collection<JoinOperator> joins) {
        conditionSources = new ArrayList<ConditionList>();
        if (queryGoal.getWhereConditions() != null)
            conditionSources.add(queryGoal.getWhereConditions());
        for (JoinOperator join : joins) {
            ConditionList joinConditions = join.getJoinConditions();
            if (joinConditions != null)
                conditionSources.add(joinConditions);
        }
        switch (conditionSources.size()) {
        case 0:
            conditions = Collections.emptyList();
            break;
        case 1:
            conditions = conditionSources.get(0);
            break;
        default:
            conditions = new ArrayList<ConditionExpression>();
            for (ConditionList conditionSource : conditionSources) {
                conditions.addAll(conditionSource);
            }
        }
    }

    public void updateRequiredColumns() {
        requiredColumns.clear();
        Collection<PlanNode> orderings = (queryGoal.getOrdering() == null) ? 
            Collections.<PlanNode>emptyList() : 
            Collections.<PlanNode>singletonList(queryGoal.getOrdering());
        queryGoal.getQuery().accept(new RequiredColumnsFiller(requiredColumns, 
                                                              orderings, conditions));
    }

    /** Populate given index usage according to goal.
     * @return <code>false</code> if the index is useless.
     */
    public boolean usable(IndexScan index) {
        List<IndexColumn> indexColumns = index.getIndex().getKeyColumns();
        int ncols = indexColumns.size();
        List<ExpressionNode> indexExpressions = new ArrayList<ExpressionNode>(ncols);
        List<OrderByExpression> orderBy = new ArrayList<OrderByExpression>(ncols);
        for (IndexColumn indexColumn : indexColumns) {
            ExpressionNode indexExpression = getIndexExpression(index, indexColumn);
            indexExpressions.add(indexExpression);
            orderBy.add(new OrderByExpression(indexExpression,
                                              indexColumn.isAscending()));
        }
        index.setColumns(indexExpressions);
        index.setOrdering(orderBy);
        int nequals = 0;
        while (nequals < ncols) {
            ExpressionNode indexExpression = indexExpressions.get(nequals);
            if (indexExpression == null) break;
            ConditionExpression equalityCondition = null;
            ExpressionNode otherComparand = null;
            for (ConditionExpression condition : conditions) {
                if (condition instanceof ComparisonCondition) {
                    ComparisonCondition ccond = (ComparisonCondition)condition;
                    ExpressionNode comparand = null;
                    if (ccond.getOperation() == Comparison.EQ) {
                        if (indexExpressionMatches(indexExpression, ccond.getLeft())) {
                            comparand = ccond.getRight();<commit_msg>Include all index columns, including hkey columns that aren't declared.<commit_after>/**
 * Copyright (C) 2012 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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.akiban.sql.optimizer.rule.join_enum;

import com.akiban.sql.optimizer.rule.CostEstimator;
import com.akiban.sql.optimizer.rule.join_enum.DPhyp.JoinOperator;
import com.akiban.sql.optimizer.rule.range.ColumnRanges;
import com.akiban.sql.optimizer.rule.range.RangeSegment;

import com.akiban.sql.optimizer.plan.*;
import com.akiban.sql.optimizer.plan.IndexScan.OrderEffectiveness;
import com.akiban.sql.optimizer.plan.Sort.OrderByExpression;
import com.akiban.sql.optimizer.plan.TableGroupJoinTree.TableGroupJoinNode;

import com.akiban.ais.model.Column;
import com.akiban.ais.model.GroupIndex;
import com.akiban.ais.model.Index.JoinType;
import com.akiban.ais.model.IndexColumn;
import com.akiban.ais.model.TableIndex;
import com.akiban.ais.model.UserTable;
import com.akiban.server.expression.std.Comparison;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/** The goal of a indexes within a group. */
public class GroupIndexGoal implements Comparator<IndexScan>
{
    private static final Logger logger = LoggerFactory.getLogger(GroupIndexGoal.class);

    // The overall goal.
    private QueryIndexGoal queryGoal;
    // The grouped tables.
    private TableGroupJoinTree tables;
    
    // All the conditions that might be indexable.
    private List<ConditionExpression> conditions;
    // Where they came from.
    private List<ConditionList> conditionSources;

    // All the columns besides those in conditions that will be needed.
    private RequiredColumns requiredColumns;

    // Tables already bound outside.
    private Set<ColumnSource> boundTables;

    // Can an index be used to take care of sorting.
    // TODO: Make this a subset of queryGoal's sorting, based on what
    // tables are in what join, rather than an all or nothing.
    private boolean sortAllowed;

    // Mapping of Range-expressible conditions, by their column. lazy loaded.
    private Map<ColumnExpression,ColumnRanges> columnsToRanges;

    public GroupIndexGoal(QueryIndexGoal queryGoal, TableGroupJoinTree tables) {
        this.queryGoal = queryGoal;
        this.tables = tables;

        if (queryGoal.getWhereConditions() != null) {
            conditionSources = Collections.singletonList(queryGoal.getWhereConditions());
            conditions = queryGoal.getWhereConditions();
        }
        else {
            conditionSources = Collections.emptyList();
            conditions = Collections.emptyList();
        }

        requiredColumns = new RequiredColumns(tables);

        boundTables = queryGoal.getQuery().getOuterTables();
        sortAllowed = true;
    }

    public QueryIndexGoal getQueryGoal() {
        return queryGoal;
    }

    public TableGroupJoinTree getTables() {
        return tables;
    }

    public List<ConditionList> updateContext(Set<ColumnSource> boundTables,
                                             Collection<JoinOperator> joins,
                                             boolean sortAllowed) {
        setBoundTables(boundTables);
        this.sortAllowed = sortAllowed;
        setJoinConditions(joins);
        updateRequiredColumns();
        return conditionSources;
    }

    public void setBoundTables(Set<ColumnSource> boundTables) {
        this.boundTables = boundTables;
    }
    
    public void setJoinConditions(Collection<JoinOperator> joins) {
        conditionSources = new ArrayList<ConditionList>();
        if (queryGoal.getWhereConditions() != null)
            conditionSources.add(queryGoal.getWhereConditions());
        for (JoinOperator join : joins) {
            ConditionList joinConditions = join.getJoinConditions();
            if (joinConditions != null)
                conditionSources.add(joinConditions);
        }
        switch (conditionSources.size()) {
        case 0:
            conditions = Collections.emptyList();
            break;
        case 1:
            conditions = conditionSources.get(0);
            break;
        default:
            conditions = new ArrayList<ConditionExpression>();
            for (ConditionList conditionSource : conditionSources) {
                conditions.addAll(conditionSource);
            }
        }
    }

    public void updateRequiredColumns() {
        requiredColumns.clear();
        Collection<PlanNode> orderings = (queryGoal.getOrdering() == null) ? 
            Collections.<PlanNode>emptyList() : 
            Collections.<PlanNode>singletonList(queryGoal.getOrdering());
        queryGoal.getQuery().accept(new RequiredColumnsFiller(requiredColumns, 
                                                              orderings, conditions));
    }

    /** Populate given index usage according to goal.
     * @return <code>false</code> if the index is useless.
     */
    public boolean usable(IndexScan index) {
        List<IndexColumn> keyColumns = index.getIndex().getKeyColumns();
        List<IndexColumn> valueColumns = index.getIndex().getValueColumns();
        int ncols = keyColumns.size() + valueColumns.size();
        List<ExpressionNode> indexExpressions = new ArrayList<ExpressionNode>(ncols);
        List<OrderByExpression> orderBy = new ArrayList<OrderByExpression>(ncols);
        for (IndexColumn indexColumn : keyColumns) {
            ExpressionNode indexExpression = getIndexExpression(index, indexColumn);
            indexExpressions.add(indexExpression);
            orderBy.add(new OrderByExpression(indexExpression,
                                              indexColumn.isAscending()));
        }
        for (IndexColumn indexColumn : valueColumns) {
            ExpressionNode indexExpression = getIndexExpression(index, indexColumn);
            indexExpressions.add(indexExpression);
            orderBy.add(new OrderByExpression(indexExpression,
                                              indexColumn.isAscending()));
        }
        index.setColumns(indexExpressions);
        index.setOrdering(orderBy);
        int nequals = 0;
        while (nequals < ncols) {
            ExpressionNode indexExpression = indexExpressions.get(nequals);
            if (indexExpression == null) break;
            ConditionExpression equalityCondition = null;
            ExpressionNode otherComparand = null;
            for (ConditionExpression condition : conditions) {
                if (condition instanceof ComparisonCondition) {
                    ComparisonCondition ccond = (ComparisonCondition)condition;
                    ExpressionNode comparand = null;
                    if (ccond.getOperation() == Comparison.EQ) {
                        if (indexExpressionMatches(indexExpression, ccond.getLeft())) {
                            comparand = ccond.getRight();
{'fraction_non_alphanumeric': 0.083784534221852, 'fraction_numerical': 0.0015271414688324309, 'mean_word_length': 2.6519645120405575, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4266214', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3534, 'n_words': 934}