use of com.linkedin.pinot.common.response.broker.SelectionResults in project pinot by linkedin.
the class BrokerReduceService method setSelectionResults.
/**
* Reduce selection results from multiple servers and set them into BrokerResponseNative passed in.
*
* @param brokerResponseNative broker response.
* @param selection selection information.
* @param dataTableMap map from server to data table.
* @param dataSchema data schema.
*/
private void setSelectionResults(@Nonnull BrokerResponseNative brokerResponseNative, @Nonnull Selection selection, @Nonnull Map<ServerInstance, DataTable> dataTableMap, @Nonnull DataSchema dataSchema) {
// Reduce the selection results.
SelectionResults selectionResults;
int selectionSize = selection.getSize();
if (selection.isSetSelectionSortSequence() && selectionSize != 0) {
// Selection order-by.
SelectionOperatorService selectionService = new SelectionOperatorService(selection, dataSchema);
selectionService.reduceWithOrdering(dataTableMap);
selectionResults = selectionService.renderSelectionResultsWithOrdering();
} else {
// Selection only.
selectionResults = SelectionOperatorUtils.renderSelectionResultsWithoutOrdering(SelectionOperatorUtils.reduceWithoutOrdering(dataTableMap, selectionSize), dataSchema, SelectionOperatorUtils.getSelectionColumns(selection.getSelectionColumns(), dataSchema));
}
brokerResponseNative.setSelectionResults(selectionResults);
}
use of com.linkedin.pinot.common.response.broker.SelectionResults in project pinot by linkedin.
the class SelectionOperatorServiceTest method testCompatibleRowsRenderSelectionResultsWithoutOrdering.
@Test
public void testCompatibleRowsRenderSelectionResultsWithoutOrdering() {
List<Serializable[]> rows = new ArrayList<>(2);
rows.add(_row1.clone());
rows.add(_compatibleRow1.clone());
SelectionResults selectionResults = SelectionOperatorUtils.renderSelectionResultsWithoutOrdering(rows, _upgradedDataSchema, Arrays.asList(_columnNames));
List<Serializable[]> formattedRows = selectionResults.getRows();
Serializable[] expectedFormattedRow1 = { "0", "1.0", "2.0", "3.0", "4", new String[] { "5" }, new String[] { "6.0" }, new String[] { "7.0" }, new String[] { "8.0" }, new String[] { "9" } };
Serializable[] expectedFormattedRow2 = { "1", "2.0", "3.0", "4.0", "5", new String[] { "6" }, new String[] { "7.0" }, new String[] { "8.0" }, new String[] { "9.0" }, new String[] { "10" } };
Assert.assertEquals(formattedRows.get(0), expectedFormattedRow1);
Assert.assertEquals(formattedRows.get(1), expectedFormattedRow2);
}
use of com.linkedin.pinot.common.response.broker.SelectionResults in project pinot by linkedin.
the class SelectionOperatorServiceTest method testCompatibleRowsRenderSelectionResultsWithOrdering.
@Test
public void testCompatibleRowsRenderSelectionResultsWithOrdering() {
SelectionOperatorService selectionOperatorService = new SelectionOperatorService(_selectionOrderBy, _upgradedDataSchema);
PriorityQueue<Serializable[]> rows = selectionOperatorService.getRows();
rows.offer(_row1.clone());
rows.offer(_compatibleRow1.clone());
rows.offer(_compatibleRow2.clone());
SelectionResults selectionResults = selectionOperatorService.renderSelectionResultsWithOrdering();
List<Serializable[]> formattedRows = selectionResults.getRows();
Serializable[] expectedFormattedRow1 = { "1", "2.0", "3.0", "4.0", "5", new String[] { "6" }, new String[] { "7.0" }, new String[] { "8.0" }, new String[] { "9.0" }, new String[] { "10" } };
Serializable[] expectedFormattedRow2 = { "0", "1.0", "2.0", "3.0", "4", new String[] { "5" }, new String[] { "6.0" }, new String[] { "7.0" }, new String[] { "8.0" }, new String[] { "9" } };
Assert.assertEquals(formattedRows.get(0), expectedFormattedRow1);
Assert.assertEquals(formattedRows.get(1), expectedFormattedRow2);
}
use of com.linkedin.pinot.common.response.broker.SelectionResults in project pinot by linkedin.
the class BrokerReduceService method reduceOnDataTable.
@Nonnull
@Override
public BrokerResponseNative reduceOnDataTable(@Nonnull BrokerRequest brokerRequest, @Nonnull Map<ServerInstance, DataTable> dataTableMap, @Nullable BrokerMetrics brokerMetrics) {
if (dataTableMap.size() == 0) {
// Empty response.
return BrokerResponseNative.empty();
}
BrokerResponseNative brokerResponseNative = new BrokerResponseNative();
List<QueryProcessingException> processingExceptions = brokerResponseNative.getProcessingExceptions();
long numDocsScanned = 0L;
long numEntriesScannedInFilter = 0L;
long numEntriesScannedPostFilter = 0L;
long numTotalRawDocs = 0L;
// Cache a data schema from data tables (try to cache one with data rows associated with it).
DataSchema cachedDataSchema = null;
// Process server response metadata.
Iterator<Map.Entry<ServerInstance, DataTable>> iterator = dataTableMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<ServerInstance, DataTable> entry = iterator.next();
ServerInstance serverInstance = entry.getKey();
DataTable dataTable = entry.getValue();
Map<String, String> metadata = dataTable.getMetadata();
// Reduce on trace info.
if (brokerRequest.isEnableTrace()) {
brokerResponseNative.getTraceInfo().put(serverInstance.getHostname(), metadata.get(DataTable.TRACE_INFO_METADATA_KEY));
}
// Reduce on exceptions.
for (String key : metadata.keySet()) {
if (key.startsWith(DataTable.EXCEPTION_METADATA_KEY)) {
processingExceptions.add(new QueryProcessingException(Integer.parseInt(key.substring(9)), metadata.get(key)));
}
}
// Reduce on execution statistics.
String numDocsScannedString = metadata.get(DataTable.NUM_DOCS_SCANNED_METADATA_KEY);
if (numDocsScannedString != null) {
numDocsScanned += Long.parseLong(numDocsScannedString);
}
String numEntriesScannedInFilterString = metadata.get(DataTable.NUM_ENTRIES_SCANNED_IN_FILTER_METADATA_KEY);
if (numEntriesScannedInFilterString != null) {
numEntriesScannedInFilter += Long.parseLong(numEntriesScannedInFilterString);
}
String numEntriesScannedPostFilterString = metadata.get(DataTable.NUM_ENTRIES_SCANNED_POST_FILTER_METADATA_KEY);
if (numEntriesScannedPostFilterString != null) {
numEntriesScannedPostFilter += Long.parseLong(numEntriesScannedPostFilterString);
}
String numTotalRawDocsString = metadata.get(DataTable.TOTAL_DOCS_METADATA_KEY);
if (numTotalRawDocsString != null) {
numTotalRawDocs += Long.parseLong(numTotalRawDocsString);
}
// After processing the metadata, remove data tables without data rows inside.
DataSchema dataSchema = dataTable.getDataSchema();
if (dataSchema == null) {
iterator.remove();
} else {
// Try to cache a data table with data rows inside, or cache one with data schema inside.
if (dataTable.getNumberOfRows() == 0) {
if (cachedDataSchema == null) {
cachedDataSchema = dataSchema;
}
iterator.remove();
} else {
cachedDataSchema = dataSchema;
}
}
}
// Set execution statistics.
brokerResponseNative.setNumDocsScanned(numDocsScanned);
brokerResponseNative.setNumEntriesScannedInFilter(numEntriesScannedInFilter);
brokerResponseNative.setNumEntriesScannedPostFilter(numEntriesScannedPostFilter);
brokerResponseNative.setTotalDocs(numTotalRawDocs);
// Update broker metrics.
String tableName = brokerRequest.getQuerySource().getTableName();
if (brokerMetrics != null) {
brokerMetrics.addMeteredTableValue(tableName, BrokerMeter.DOCUMENTS_SCANNED, numDocsScanned);
brokerMetrics.addMeteredTableValue(tableName, BrokerMeter.ENTRIES_SCANNED_IN_FILTER, numEntriesScannedInFilter);
brokerMetrics.addMeteredTableValue(tableName, BrokerMeter.ENTRIES_SCANNED_POST_FILTER, numEntriesScannedPostFilter);
}
if (dataTableMap.isEmpty()) {
// This will only happen to selection query.
if (cachedDataSchema != null) {
List<String> selectionColumns = SelectionOperatorUtils.getSelectionColumns(brokerRequest.getSelections().getSelectionColumns(), cachedDataSchema);
brokerResponseNative.setSelectionResults(new SelectionResults(selectionColumns, new ArrayList<Serializable[]>(0)));
}
} else {
// Reduce server responses data and set query results into the broker response.
assert cachedDataSchema != null;
if (brokerRequest.isSetSelections()) {
// Selection query.
// For data table map with more than one data tables, remove conflicting data tables.
DataSchema masterDataSchema = cachedDataSchema.clone();
if (dataTableMap.size() > 1) {
List<String> droppedServers = removeConflictingResponses(masterDataSchema, dataTableMap);
if (!droppedServers.isEmpty()) {
String errorMessage = QueryException.MERGE_RESPONSE_ERROR.getMessage() + ": responses for table: " + tableName + " from servers: " + droppedServers + " got dropped due to data schema inconsistency.";
LOGGER.error(errorMessage);
if (brokerMetrics != null) {
brokerMetrics.addMeteredTableValue(tableName, BrokerMeter.RESPONSE_MERGE_EXCEPTIONS, 1);
}
brokerResponseNative.addToExceptions(new QueryProcessingException(QueryException.MERGE_RESPONSE_ERROR_CODE, errorMessage));
}
}
setSelectionResults(brokerResponseNative, brokerRequest.getSelections(), dataTableMap, masterDataSchema);
} else {
// Aggregation query.
AggregationFunction[] aggregationFunctions = AggregationFunctionUtils.getAggregationFunctions(brokerRequest.getAggregationsInfo());
if (!brokerRequest.isSetGroupBy()) {
// Aggregation only query.
setAggregationResults(brokerResponseNative, aggregationFunctions, dataTableMap, cachedDataSchema);
} else {
// Aggregation group-by query.
setGroupByResults(brokerResponseNative, aggregationFunctions, brokerRequest.getGroupBy(), dataTableMap);
}
}
}
return brokerResponseNative;
}
Aggregations