// check for approximate SELECT COUNT(*) queries
if (fromItems.size() == 1 && selectItems.size() == 1 && groupByItems.isEmpty() && havingItems.isEmpty()) {
final SelectItem selectItem = query.getSelectClause().getItem(0);
if (SelectItem.isCountAllItem(selectItem)) {
final boolean functionApproximationAllowed = selectItem.isFunctionApproximationAllowed();
final FromItem fromItem = query.getFromClause().getItem(0);
final Table table = fromItem.getTable();
if (table != null) {
if (isMainSchemaTable(table)) {
logger.debug("Query is a COUNT query with {} where items. Trying executeCountQuery(...)",
whereItems.size());
final Number count = executeCountQuery(table, whereItems, functionApproximationAllowed);
if (count == null) {
logger.debug("DataContext did not return any count query results. Proceeding with manual counting.");
} else {
List<Row> data = new ArrayList<Row>(1);
final DataSetHeader header = new SimpleDataSetHeader(new SelectItem[] { selectItem });
data.add(new DefaultRow(header, new Object[] { count }));
return new InMemoryDataSet(header, data);
}
}
}
}
}
final int firstRow = (query.getFirstRow() == null ? 1 : query.getFirstRow());
final int maxRows = (query.getMaxRows() == null ? -1 : query.getMaxRows());
// Check for very simple queries with max rows property set (typically
// preview), see Ticket #187
previewTable: if (whereItems.isEmpty() && groupByItems.isEmpty() && havingItems.isEmpty()
&& orderByItems.isEmpty() && fromItems.size() == 1) {
final Table table = fromItems.get(0).getTable();
if (table != null) {
for (SelectItem item : selectItems) {
if (item.getFunction() != null || item.getExpression() != null) {
break previewTable;
}
}
DataSet dataSet = materializeTable(table, selectItems, firstRow, maxRows);
dataSet = MetaModelHelper.getSelection(selectItems, dataSet);
return dataSet;
}
}
// Creates a list for all select items that are needed to execute query
// (some may only be used as part of a filter, but not shown in result)
List<SelectItem> workSelectItems = CollectionUtils.concat(true, selectItems, whereSelectItems,
groupBySelectItems, havingSelectItems, orderBySelectItems);
// Materialize the tables in the from clause
final DataSet[] fromDataSets = new DataSet[fromItems.size()];
for (int i = 0; i < fromDataSets.length; i++) {
FromItem fromItem = fromItems.get(i);
fromDataSets[i] = materializeFromItem(fromItem, workSelectItems);
}
// Execute the query using the raw data
DataSet dataSet = MetaModelHelper.getCarthesianProduct(fromDataSets, whereItems);