org.apache.druid.query.groupby.GroupByQueryRunnerTestHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.druid.query.groupby.GroupByQueryRunnerTestHelper.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.druid.query.groupby;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.druid.data.input.MapBasedRow;
import org.apache.druid.data.input.Row;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.guava.Sequence;
import org.apache.druid.query.FinalizeResultsQueryRunner;
import org.apache.druid.query.Query;
import org.apache.druid.query.QueryPlus;
import org.apache.druid.query.QueryRunner;
import org.apache.druid.query.QueryRunnerFactory;
import org.apache.druid.query.QueryToolChest;
import org.apache.druid.segment.column.ColumnHolder;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 */
public class GroupByQueryRunnerTestHelper {
    public static <T> Iterable<T> runQuery(QueryRunnerFactory factory, QueryRunner runner, Query<T> query) {

        QueryToolChest toolChest = factory.getToolchest();
        QueryRunner<T> theRunner = new FinalizeResultsQueryRunner<>(
                toolChest.mergeResults(toolChest.preMergeQueryDecoration(runner)), toolChest);

        Sequence<T> queryResult = theRunner.run(QueryPlus.wrap(query), Maps.newHashMap());
        return queryResult.toList();
    }

    public static Row createExpectedRow(final String timestamp, Object... vals) {
        return createExpectedRow(DateTimes.of(timestamp), vals);
    }

    public static Row createExpectedRow(final DateTime timestamp, Object... vals) {
        Preconditions.checkArgument(vals.length % 2 == 0);

        Map<String, Object> theVals = Maps.newHashMap();
        for (int i = 0; i < vals.length; i += 2) {
            theVals.put(vals[i].toString(), vals[i + 1]);
        }

        return new MapBasedRow(timestamp, theVals);
    }

    public static List<Row> createExpectedRows(String[] columnNames, Object[]... values) {
        int timeIndex = Arrays.asList(columnNames).indexOf(ColumnHolder.TIME_COLUMN_NAME);
        Preconditions.checkArgument(timeIndex >= 0);

        List<Row> expected = Lists.newArrayList();
        for (Object[] value : values) {
            Preconditions.checkArgument(value.length == columnNames.length);
            Map<String, Object> theVals = Maps.newHashMapWithExpectedSize(value.length);
            for (int i = 0; i < columnNames.length; i++) {
                if (i != timeIndex) {
                    theVals.put(columnNames[i], value[i]);
                }
            }
            expected.add(new MapBasedRow(new DateTime(value[timeIndex], ISOChronology.getInstanceUTC()), theVals));
        }
        return expected;
    }

}