/*
* Copyright 2014-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wplatform.ddal.command.dml;
import java.util.ArrayList;
import com.wplatform.ddal.command.expression.Expression;
import com.wplatform.ddal.command.expression.ExpressionColumn;
import com.wplatform.ddal.dbobject.table.Column;
import com.wplatform.ddal.dbobject.table.ColumnResolver;
import com.wplatform.ddal.dbobject.table.TableFilter;
import com.wplatform.ddal.value.Value;
/**
* This class represents a column resolver for the column list of a SELECT
* statement. It is used to resolve select column aliases in the HAVING clause.
* Example:
* <p>
* SELECT X/3 AS A, COUNT(*) FROM SYSTEM_RANGE(1, 10) GROUP BY A HAVING A>2;
* </p>
*
* @author Thomas Mueller
*/
public class SelectListColumnResolver implements ColumnResolver {
private final Select select;
private final Expression[] expressions;
private final Column[] columns;
SelectListColumnResolver(Select select) {
this.select = select;
int columnCount = select.getColumnCount();
columns = new Column[columnCount];
expressions = new Expression[columnCount];
ArrayList<Expression> columnList = select.getExpressions();
for (int i = 0; i < columnCount; i++) {
Expression expr = columnList.get(i);
Column column = new Column(expr.getAlias(), Value.NULL);
column.setTable(null, i);
columns[i] = column;
expressions[i] = expr.getNonAliasExpression();
}
}
@Override
public Column[] getColumns() {
return columns;
}
@Override
public String getSchemaName() {
return null;
}
@Override
public Select getSelect() {
return select;
}
@Override
public Column[] getSystemColumns() {
return null;
}
@Override
public Column getRowIdColumn() {
return null;
}
@Override
public String getTableAlias() {
return null;
}
@Override
public TableFilter getTableFilter() {
return null;
}
@Override
public Value getValue(Column column) {
return null;
}
@Override
public Expression optimize(ExpressionColumn expressionColumn, Column column) {
return expressions[column.getColumnId()];
}
}