public LimitSpec withLimit(OptionalInt limit) { return new LimitSpec(columns, limit); } // CHECKSTYLE:ON
/** * Constructs a numeric sort of the given metric and explicitly specifies the order (ascending or descending). * * @param metricName the metric name * @param order sorting order */ public TopNMetric(String metricName, SortDirection order) { this(metricName); if (order == SortDirection.ASC) { this.type = TopNMetricType.INVERTED; this.metricName = Optional.empty(); this.nested = Optional.of(new TopNMetric(metricName)); } }
/** * Strip the columns from the LimitSpec on the query and return it, if present. * * @param query Query to strip the columns from within the LimitSpec * * @return the cleaned LimitSpec if there is one */ private static LimitSpec stripColumnsFromLimitSpec(DruidFactQuery query) { return ((GroupByQuery) query).getLimitSpec() == null ? null : ((GroupByQuery) query).getLimitSpec().withColumns(new LinkedHashSet<>()); } }
druidOrderBy = null; OrderByColumn sortBy = request.getSorts().iterator().next(); druidTopNMetric = new TopNMetric(sortBy.getDimension(), sortBy.getDirection()); } else if (request.getSorts().size() > 0) { druidOrderBy = new LimitSpec(request.getSorts()); druidTopNMetric = null; } else { } else { druidOrderBy = new LimitSpec(request.getSorts(), request.getCount()); druidTopNMetric = null;
LimitSpec limitSpec = groupByQuery.getLimitSpec(); if (limitSpec != null) { limitSpec.getColumns() .stream() .map(orderByColumn -> { String orderByField = apiToFieldMapper.apply(orderByColumn.getDimension()); limitSpecColumns.add(orderByField); if (orderByColumn.getDirection().equals(SortDirection.DESC)) { sort = builder.desc(sort);
@Override public boolean handleRequest( RequestContext context, DataApiRequest request, DruidAggregationQuery<?> druidQuery, ResponseProcessor response ) { MappingResponseProcessor mappingResponse = (MappingResponseProcessor) response; if (request.getDateTimeSort().isPresent()) { mappingResponse.getMappers().add(new DateTimeSortMapper(request.getDateTimeSort().get().getDirection())); } return next.handleRequest(context, request, druidQuery, mappingResponse); } }
/** * Method to generate DateTime sort column from the map of columns and its direction. * * @param sortColumns LinkedHashMap of columns and its direction. Using LinkedHashMap to preserve the order * * @return Instance of OrderByColumn for dateTime */ protected Optional<OrderByColumn> bindDateTimeSortColumn(LinkedHashMap<String, SortDirection> sortColumns) { if (sortColumns != null && sortColumns.containsKey(DATE_TIME_STRING)) { if (!isDateTimeFirstSortField(sortColumns)) { LOG.debug(DATE_TIME_SORT_VALUE_INVALID.logFormat()); throw new BadApiRequestException(DATE_TIME_SORT_VALUE_INVALID.format()); } else { return Optional.of(new OrderByColumn(DATE_TIME_STRING, sortColumns.get(DATE_TIME_STRING))); } } else { return Optional.empty(); } }
/** * Gets the number of rows to limit results to for a Group by Query. Otherwise no limit is applied. * * @param druidQuery The query to get the row limit from. * * @return the number of rows to include in the results. */ protected int getLimit(DruidAggregationQuery<?> druidQuery) { if (druidQuery.getQueryType().equals(GROUP_BY)) { GroupByQuery groupByQuery = (GroupByQuery) druidQuery; LimitSpec limitSpec = groupByQuery.getLimitSpec(); if (limitSpec != null) { return limitSpec.getLimit().orElse(NO_LIMIT); } } return NO_LIMIT; }
/** * Extract valid sort direction. * * @param columnWithDirection Column and its sorting direction * * @return Sorting direction. If no direction provided then the default one will be DESC */ protected SortDirection getSortDirection(List<String> columnWithDirection) { try { return columnWithDirection.size() == 2 ? SortDirection.valueOf(columnWithDirection.get(1).toUpperCase(Locale.ENGLISH)) : SortDirection.DESC; } catch (IllegalArgumentException ignored) { String sortDirectionName = columnWithDirection.get(1); LOG.debug(SORT_DIRECTION_INVALID.logFormat(sortDirectionName)); throw new BadApiRequestException(SORT_DIRECTION_INVALID.format(sortDirectionName)); } }
public LimitSpec withColumns(LinkedHashSet<OrderByColumn> sortColumns) { return new LimitSpec(sortColumns, limit); }
/** * Constructs a sort of the given type with a starting point and explicitly specifies the order (ascending or * descending). * * @param type the type of sort * @param previousStop the starting point of the sort * @param order sorting order */ public TopNMetric(TopNMetricType type, String previousStop, SortDirection order) { this(type, previousStop); if (order == SortDirection.ASC) { this.type = TopNMetricType.INVERTED; this.nested = Optional.of(new TopNMetric(type, previousStop)); this.previousStop = Optional.empty(); } }
continue; metricSortColumns.add(new OrderByColumn(logicalMetric, entry.getValue()));
@Override public void query(Dimension dimension, DataSource dataSource) { SuccessCallback successCallback = buildSuccessCallback(dimension); SqlPhysicalTable sqlTable = (SqlPhysicalTable) dataSource.getPhysicalTable().getSourceTable(); GroupByQuery groupByQuery = new GroupByQuery( dataSource, AllGranularity.INSTANCE, Collections.singletonList(dimension), null, null, Collections.emptyList(), Collections.emptyList(), Collections.singletonList(INTERVAL), new LimitSpec(Utils.asLinkedHashSet(), OptionalInt.of(ROW_LIMIT)) ); sqlBackedClient.executeQuery(groupByQuery, successCallback, failureCallback); }