Search in sources :

Example 6 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ArrowResultGenerationTest method generateFieldsValue.

@Test
void generateFieldsValue() {
    List<ResultInfo> resultInfos = getResultTypes().stream().map(ResultTestUtil.TypedSelectDummy::new).map(select -> new SelectResultInfo(select, new CQConcept())).collect(Collectors.toList());
    List<Field> fields = generateFields(resultInfos, // Custom column namer so we don't require a dataset registry
    UNIQUE_NAMER);
    assertThat(fields).containsExactlyElementsOf(List.of(new Field("BOOLEAN", FieldType.nullable(ArrowType.Bool.INSTANCE), null), new Field("INTEGER", FieldType.nullable(new ArrowType.Int(32, true)), null), new Field("NUMERIC", FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), null), new Field("CATEGORICAL", FieldType.nullable(new ArrowType.Utf8()), null), new Field("RESOLUTION", FieldType.nullable(new ArrowType.Utf8()), null), new Field("DATE", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null), new Field("DATE_RANGE", FieldType.nullable(ArrowType.Struct.INSTANCE), List.of(new Field("min", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null), new Field("max", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null))), new Field("STRING", FieldType.nullable(new ArrowType.Utf8()), null), new Field("MONEY", FieldType.nullable(new ArrowType.Int(32, true)), null), new Field("LIST[BOOLEAN]", FieldType.nullable(ArrowType.List.INSTANCE), List.of(new Field("LIST[BOOLEAN]", FieldType.nullable(ArrowType.Bool.INSTANCE), null))), new Field("LIST[DATE_RANGE]", FieldType.nullable(ArrowType.List.INSTANCE), List.of(new Field("LIST[DATE_RANGE]", FieldType.nullable(ArrowType.Struct.INSTANCE), List.of(new Field("min", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null), new Field("max", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null))))), new Field("LIST[STRING]", FieldType.nullable(ArrowType.List.INSTANCE), List.of(new Field("LIST[STRING]", FieldType.nullable(new ArrowType.Utf8()), null)))));
}
Also used : ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrowType(org.apache.arrow.vector.types.pojo.ArrowType) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) EntityPrintId(com.bakdata.conquery.models.identifiable.mapping.EntityPrintId) ByteArrayInputStream(java.io.ByteArrayInputStream) FloatingPointPrecision(org.apache.arrow.vector.types.FloatingPointPrecision) ArrowRenderer(com.bakdata.conquery.io.result.arrow.ArrowRenderer) CDate(com.bakdata.conquery.models.common.CDate) Locale(java.util.Locale) I18n(com.bakdata.conquery.models.i18n.I18n) ArrowStreamReader(org.apache.arrow.vector.ipc.ArrowStreamReader) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) DictionaryProvider(org.apache.arrow.vector.dictionary.DictionaryProvider) UniqueNamer(com.bakdata.conquery.models.query.resultinfo.UniqueNamer) FieldVector(org.apache.arrow.vector.FieldVector) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) NonNull(lombok.NonNull) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) FieldType(org.apache.arrow.vector.types.pojo.FieldType) Collection(java.util.Collection) IOException(java.io.IOException) Field(org.apache.arrow.vector.types.pojo.Field) Collectors(java.util.stream.Collectors) ArrowStreamWriter(org.apache.arrow.vector.ipc.ArrowStreamWriter) JsonStringArrayList(org.apache.arrow.vector.util.JsonStringArrayList) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ResultTestUtil(com.bakdata.conquery.io.result.ResultTestUtil) StringJoiner(java.util.StringJoiner) ROOT_ALLOCATOR(com.bakdata.conquery.io.result.arrow.ArrowUtil.ROOT_ALLOCATOR) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) DateUnit(org.apache.arrow.vector.types.DateUnit) InputStream(java.io.InputStream) Field(org.apache.arrow.vector.types.pojo.Field) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) CDate(com.bakdata.conquery.models.common.CDate) Test(org.junit.jupiter.api.Test)

Example 7 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class CsvResultGenerationTest method generateExpectedCSV.

private String generateExpectedCSV(List<EntityResult> results, List<ResultInfo> resultInfos, PrintSettings settings) {
    List<String> expected = new ArrayList<>();
    expected.add(ResultTestUtil.ID_FIELDS.stream().map(info -> info.defaultColumnName(settings)).collect(Collectors.joining(",")) + "," + getResultTypes().stream().map(ResultType::typeInfo).collect(Collectors.joining(",")) + "\n");
    results.stream().map(EntityResult.class::cast).forEach(res -> {
        for (Object[] line : res.listResultLines()) {
            StringJoiner valueJoiner = new StringJoiner(",");
            valueJoiner.add(String.valueOf(res.getEntityId()));
            valueJoiner.add(String.valueOf(res.getEntityId()));
            for (int lIdx = 0; lIdx < line.length; lIdx++) {
                Object val = line[lIdx];
                if (val == null) {
                    valueJoiner.add("");
                    continue;
                }
                ResultInfo info = resultInfos.get(lIdx);
                final String printVal = info.getType().printNullable(settings, val);
                valueJoiner.add(printVal.contains(String.valueOf(CONFIG.getCsv().getDelimeter())) ? "\"" + printVal + "\"" : printVal);
            }
            expected.add(valueJoiner + "\n");
        }
    });
    return expected.stream().collect(Collectors.joining());
}
Also used : ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) EntityPrintId(com.bakdata.conquery.models.identifiable.mapping.EntityPrintId) Locale(java.util.Locale) I18n(com.bakdata.conquery.models.i18n.I18n) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) ResultTestUtil.getTestEntityResults(com.bakdata.conquery.io.result.ResultTestUtil.getTestEntityResults) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ResultTestUtil(com.bakdata.conquery.io.result.ResultTestUtil) StringJoiner(java.util.StringJoiner) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) NotNull(org.jetbrains.annotations.NotNull) NonPersistentStoreFactory(com.bakdata.conquery.util.NonPersistentStoreFactory) ArrayList(java.util.ArrayList) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) StringJoiner(java.util.StringJoiner)

Example 8 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ManagedQuery method generateColumnDescriptions.

/**
 * Generates a description of each column that will appear in the resulting csv.
 */
public List<ColumnDescriptor> generateColumnDescriptions(DatasetRegistry datasetRegistry) {
    Preconditions.checkArgument(isInitialized(), "The execution must have been initialized first");
    List<ColumnDescriptor> columnDescriptions = new ArrayList<>();
    final Locale locale = I18n.LOCALE.get();
    PrintSettings settings = new PrintSettings(true, locale, datasetRegistry, config, null);
    UniqueNamer uniqNamer = new UniqueNamer(settings);
    // First add the id columns to the descriptor list. The are the first columns
    for (ResultInfo header : config.getFrontend().getQueryUpload().getIdResultInfos()) {
        columnDescriptions.add(ColumnDescriptor.builder().label(uniqNamer.getUniqueName(header)).type(ResultType.IdT.INSTANCE.typeInfo()).build());
    }
    final UniqueNamer collector = new UniqueNamer(settings);
    getResultInfos().forEach(info -> columnDescriptions.add(info.asColumnDescriptor(settings, collector)));
    return columnDescriptions;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) UniqueNamer(com.bakdata.conquery.models.query.resultinfo.UniqueNamer) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo)

Example 9 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class CQExternal method getResultInfos.

@Override
public List<ResultInfo> getResultInfos() {
    if (extra == null) {
        return Collections.emptyList();
    }
    List<ResultInfo> resultInfos = new ArrayList<>();
    for (int col = 0; col < format.size(); col++) {
        if (!format.get(col).equals(FORMAT_EXTRA)) {
            continue;
        }
        String column = values[0][col];
        resultInfos.add(new SimpleResultInfo(column, new ResultType.ListT(ResultType.StringT.INSTANCE)));
    }
    return resultInfos;
}
Also used : SimpleResultInfo(com.bakdata.conquery.models.query.resultinfo.SimpleResultInfo) ArrayList(java.util.ArrayList) SimpleResultInfo(com.bakdata.conquery.models.query.resultinfo.SimpleResultInfo) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo)

Example 10 with ResultInfo

use of com.bakdata.conquery.models.query.resultinfo.ResultInfo in project conquery by bakdata.

the class ResultArrowProcessor method getArrowResult.

public static <E extends ManagedExecution<?> & SingleTableResult> Response getArrowResult(Function<OutputStream, Function<VectorSchemaRoot, ArrowWriter>> writerProducer, Subject subject, E exec, Dataset dataset, DatasetRegistry datasetRegistry, boolean pretty, String fileExtension, MediaType mediaType, ConqueryConfig config) {
    final Namespace namespace = datasetRegistry.get(dataset.getId());
    ConqueryMDC.setLocation(subject.getName());
    log.info("Downloading results for {} on dataset {}", exec, dataset);
    subject.authorize(dataset, Ability.READ);
    subject.authorize(dataset, Ability.DOWNLOAD);
    subject.authorize(exec, Ability.READ);
    // Check if subject is permitted to download on all datasets that were referenced by the query
    authorizeDownloadDatasets(subject, exec);
    if (!(exec instanceof ManagedQuery || (exec instanceof ManagedForm && ((ManagedForm) exec).getSubQueries().size() == 1))) {
        return Response.status(HttpStatus.SC_UNPROCESSABLE_ENTITY, "Execution result is not a single Table").build();
    }
    // Get the locale extracted by the LocaleFilter
    IdPrinter idPrinter = config.getFrontend().getQueryUpload().getIdPrinter(subject, exec, namespace);
    final Locale locale = I18n.LOCALE.get();
    PrintSettings settings = new PrintSettings(pretty, locale, datasetRegistry, config, idPrinter::createId);
    // Collect ResultInfos for id columns and result columns
    final List<ResultInfo> resultInfosId = config.getFrontend().getQueryUpload().getIdResultInfos();
    final List<ResultInfo> resultInfosExec = exec.getResultInfos();
    StreamingOutput out = output -> renderToStream(writerProducer.apply(output), settings, config.getArrow().getBatchSize(), resultInfosId, resultInfosExec, exec.streamResults());
    return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), fileExtension, mediaType, ResultUtil.ContentDispositionOption.ATTACHMENT);
}
Also used : IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) Locale(java.util.Locale) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) Subject(com.bakdata.conquery.models.auth.entities.Subject) AuthorizationHelper.authorizeDownloadDatasets(com.bakdata.conquery.models.auth.AuthorizationHelper.authorizeDownloadDatasets) HttpStatus(org.apache.http.HttpStatus) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) UtilityClass(lombok.experimental.UtilityClass) MediaType(javax.ws.rs.core.MediaType) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) ArrowWriter(org.apache.arrow.vector.ipc.ArrowWriter) Locale(java.util.Locale) ArrowRenderer.renderToStream(com.bakdata.conquery.io.result.arrow.ArrowRenderer.renderToStream) I18n(com.bakdata.conquery.models.i18n.I18n) IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) OutputStream(java.io.OutputStream) ResultUtil(com.bakdata.conquery.io.result.ResultUtil) ConqueryMDC(com.bakdata.conquery.util.io.ConqueryMDC) ResultUtil.makeResponseWithFileName(com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) StreamingOutput(javax.ws.rs.core.StreamingOutput) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) Dataset(com.bakdata.conquery.models.datasets.Dataset) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) Ability(com.bakdata.conquery.models.auth.permissions.Ability) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) Namespace(com.bakdata.conquery.models.worker.Namespace) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) StreamingOutput(javax.ws.rs.core.StreamingOutput) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) Namespace(com.bakdata.conquery.models.worker.Namespace)

Aggregations

ResultInfo (com.bakdata.conquery.models.query.resultinfo.ResultInfo)17 ArrayList (java.util.ArrayList)10 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)6 PrintSettings (com.bakdata.conquery.models.query.PrintSettings)6 List (java.util.List)6 ConqueryConfig (com.bakdata.conquery.models.config.ConqueryConfig)5 ResultType (com.bakdata.conquery.models.externalservice.ResultType)5 I18n (com.bakdata.conquery.models.i18n.I18n)5 Locale (java.util.Locale)5 SelectResultInfo (com.bakdata.conquery.models.query.resultinfo.SelectResultInfo)4 UniqueNamer (com.bakdata.conquery.models.query.resultinfo.UniqueNamer)4 EntityResult (com.bakdata.conquery.models.query.results.EntityResult)4 StringJoiner (java.util.StringJoiner)4 Slf4j (lombok.extern.slf4j.Slf4j)4 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)3 ResultTestUtil (com.bakdata.conquery.io.result.ResultTestUtil)3 EntityPrintId (com.bakdata.conquery.models.identifiable.mapping.EntityPrintId)3 IOException (java.io.IOException)3 Stream (java.util.stream.Stream)3 Cell (org.apache.poi.ss.usermodel.Cell)3