for (AbstractColumn column : group.getColumns()) {
if (!column.getAllAggregationColumns().isEmpty()) {
String messageToLogAndUser = MessagesEnum.CANNOT_BE_IN_GROUP_BY.getMessageWithArgs(
localeForUserMessages, column.toQueryString());
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
if (hasPivot()) {
for (AbstractColumn column : pivot.getColumns()) {
if (!column.getAllAggregationColumns().isEmpty()) {
String messageToLogAndUser = MessagesEnum.CANNOT_BE_IN_PIVOT.getMessageWithArgs(
localeForUserMessages, column.toQueryString());
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
if (hasFilter()) {
List<AggregationColumn> filterAggregations = filter.getAggregationColumns();
if (!filterAggregations.isEmpty()) {
String messageToLogAndUser = MessagesEnum.CANNOT_BE_IN_WHERE.getMessageWithArgs(
localeForUserMessages, filterAggregations.get(0).toQueryString());;
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
// A column cannot appear both as an aggregation column and as a regular
// column in the selection.
for (SimpleColumn column1 : selectionSimple) {
String id = column1.getColumnId();
for (AggregationColumn column2 : selectionAggregated) {
if (id.equals(column2.getAggregatedColumn().getId())) {
String messageToLogAndUser = MessagesEnum.SELECT_WITH_AND_WITHOUT_AGG.getMessageWithArgs(
localeForUserMessages, id);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
// When aggregation is used, check that all selected columns are valid
// (a column is valid if it is either grouped-by, or is a
// scalar function column whose arguments are all valid columns).
if (!selectionAggregated.isEmpty()) {
for (AbstractColumn col : selectionColumns) {
checkSelectedColumnWithGrouping(groupColumns, col);
}
}
// Cannot group by a column that appears in an aggregation.
if (hasSelection() && hasGroup()) {
for (AggregationColumn column : selectionAggregated) {
String id = column.getAggregatedColumn().getId();
if (groupColumnIds.contains(id)) {
String messageToLogAndUser = MessagesEnum.COL_AGG_NOT_IN_SELECT.getMessageWithArgs(
localeForUserMessages, id);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
// Cannot use grouping or pivoting when no aggregations are defined in the
// selection.
if (hasGroup() && selectionAggregated.isEmpty()) {
String messageToLogAndUser = MessagesEnum.CANNOT_GROUP_WITNOUT_AGG.getMessage(
localeForUserMessages);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
if (hasPivot() && selectionAggregated.isEmpty()) {
String messageToLogAndUser = MessagesEnum.CANNOT_PIVOT_WITNOUT_AGG.getMessage(
localeForUserMessages);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
// Cannot order by a column that is not in the selection when aggregations
// are defined.
if (hasSort() && !selectionAggregated.isEmpty()) {
for (AbstractColumn column : sort.getColumns()) {
String messageToLogAndUser = MessagesEnum.COL_IN_ORDER_MUST_BE_IN_SELECT.getMessageWithArgs(
localeForUserMessages, column.toQueryString());
checkColumnInList(selection.getColumns(), column,
messageToLogAndUser);
}
}
// Cannot pivot by a column that appears in an aggregation.
if (hasPivot()) {
for (AggregationColumn column : selectionAggregated) {
String id = column.getAggregatedColumn().getId();
if (pivotColumnIds.contains(id)) {
String messageToLogAndUser = MessagesEnum.AGG_IN_SELECT_NO_PIVOT.getMessageWithArgs(
localeForUserMessages, id);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
// Cannot have a column appear in both group by and pivot.
if (hasGroup() && hasPivot()) {
for (String id : groupColumnIds) {
if (pivotColumnIds.contains(id)) {
String messageToLogAndUser = MessagesEnum.NO_COL_IN_GROUP_AND_PIVOT.getMessageWithArgs(
localeForUserMessages, id);
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
// Cannot order by aggregation column when pivoting is used.
if (hasPivot() && !sortAggregated.isEmpty()) {
AggregationColumn column = sortAggregated.get(0);
String messageToLogAndUser = MessagesEnum.NO_AGG_IN_ORDER_WHEN_PIVOT.getMessageWithArgs(
localeForUserMessages, column.getAggregatedColumn().getId());
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
// Cannot order by aggregation columns that weren't defined in the
// selection.
for (AggregationColumn column : sortAggregated) {
String messageToLogAndUser = MessagesEnum.AGG_IN_ORDER_NOT_IN_SELECT.getMessageWithArgs(
localeForUserMessages, column.toQueryString());
checkColumnInList(selectionAggregated, column, messageToLogAndUser);
}
Set<AbstractColumn> labelColumns = (hasLabels()
? labels.getColumns() : Sets.<AbstractColumn>newHashSet());
Set<AbstractColumn> formatColumns = (hasUserFormatOptions()
? userFormatOptions.getColumns() : Sets.<AbstractColumn>newHashSet());
if (hasSelection()) {
for (AbstractColumn col : labelColumns) {
if (!selectionColumns.contains(col)) {
String messageToLogAndUser = MessagesEnum.LABEL_COL_NOT_IN_SELECT.getMessageWithArgs(
localeForUserMessages, col.toQueryString());
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
for (AbstractColumn col : formatColumns) {
if (!selectionColumns.contains(col)) {
String messageToLogAndUser = MessagesEnum.FORMAT_COL_NOT_IN_SELECT.getMessageWithArgs(
localeForUserMessages, col.toQueryString());
log.error(messageToLogAndUser);
throw new InvalidQueryException(messageToLogAndUser);
}
}
}
}