@Override public Expression optimize(Session session) { if (columnResolver == null) { Schema schema = session .getDatabase() .findSchema(tableAlias == null ? session.getCurrentSchemaName() : tableAlias); if (schema != null) { Constant constant = schema.findConstant(columnName); if (constant != null) { return (Expression) constant.getValue(); } } // 处理在where和having中出现别名的情况,如: // SELECT id AS A FROM mytable where A>=0 // SELECT id/3 AS A, COUNT(*) FROM mytable GROUP BY A HAVING A>=0 if (select != null) { for (Expression e : select.getExpressions()) { if (database.equalsIdentifiers(columnName, e.getAlias())) return e.getNonAliasExpression().optimize(session); } } String name = columnName; if (tableAlias != null) { name = tableAlias + "." + name; if (schemaName != null) { name = schemaName + "." + name; } } throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, name); } return (Expression) columnResolver.optimize(this, column); }
@Override public void mapColumns(ColumnResolver resolver, int level) { if (select == null) select = (Select) resolver.getSelect(); if (resolver instanceof TableFilter && resolver.getTableFilter().getTable().supportsColumnFamily()) { Table t = resolver.getTableFilter().getTable(); // if (!t.isStatic() && t.getRowKeyName().equalsIgnoreCase(columnName)) { // if (columnFamilyName != null) { // schemaName = tableAlias; // tableAlias = columnFamilyName; // columnFamilyName = null; // } // if (tableAlias != null && !database.equalsIdentifiers(tableAlias, // resolver.getTableAlias())) { // return; // } // if (schemaName != null && !database.equalsIdentifiers(schemaName, // resolver.getSchemaName())) { // return; // } // mapColumn(resolver, t.getRowKeyColumn(), level); // return; // } if (database.equalsIdentifiers(Column.ROWKEY, columnName)) { Column col = t.getRowKeyColumn(); if (col != null) { mapColumn(resolver, col, level); return; } } if (resolver.getSelect() == null) { Column c = t.getColumn(columnName); mapColumn(resolver, c, level); return; } String tableAlias = this.tableAlias; boolean useAlias = false; // 当columnFamilyName不存在时,有可能是想使用简化的tableAlias.columnName语法 if (columnFamilyName != null && !t.doesColumnFamilyExist(columnFamilyName)) { // 不替换原有的tableAlias,因为有可能在另一个table中存在这样的columnFamilyName tableAlias = columnFamilyName; if (!t.doesColumnExist(columnName)) return; useAlias = true; } if (tableAlias != null && !database.equalsIdentifiers(tableAlias, resolver.getTableAlias())) { return; } if (schemaName != null && !database.equalsIdentifiers(schemaName, resolver.getSchemaName())) { return; } String fullColumnName; if (useAlias || columnFamilyName == null) fullColumnName = columnName; else fullColumnName = t.getFullColumnName(columnFamilyName, columnName); if (t.doesColumnExist(fullColumnName)) { Column c = t.getColumn(fullColumnName); mapColumn(resolver, c, level); return; } } else { if (columnFamilyName != null) { schemaName = tableAlias; tableAlias = columnFamilyName; columnFamilyName = null; } } if (tableAlias != null && !database.equalsIdentifiers(tableAlias, resolver.getTableAlias())) { return; } if (schemaName != null && !database.equalsIdentifiers(schemaName, resolver.getSchemaName())) { return; } for (Column col : resolver.getColumns()) { String n = col.getName(); if (database.equalsIdentifiers(columnName, n)) { mapColumn(resolver, col, level); return; } } if (database.equalsIdentifiers(Column.ROWID, columnName)) { Column col = resolver.getRowIdColumn(); if (col != null) { mapColumn(resolver, col, level); return; } } Column[] columns = resolver.getSystemColumns(); for (int i = 0; columns != null && i < columns.length; i++) { Column col = columns[i]; if (database.equalsIdentifiers(columnName, col.getName())) { mapColumn(resolver, col, level); return; } } }