Search in sources :

Example 1 with EntityResult

use of com.bakdata.conquery.models.query.results.EntityResult in project conquery by bakdata.

the class TableExportQueryPlan method execute.

@Override
public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity entity) {
    Optional<? extends EntityResult> result = subPlan.execute(ctx, entity);
    if (result.isEmpty() || tables.isEmpty()) {
        return Optional.empty();
    }
    List<Object[]> results = new ArrayList<>();
    final int totalColumns = positions.values().stream().mapToInt(i -> i).max().getAsInt() + 1;
    for (TableExportDescription exportDescription : tables) {
        for (Bucket bucket : ctx.getEntityBucketsForTable(entity, exportDescription.getTable())) {
            int entityId = entity.getId();
            if (!bucket.containsEntity(entityId)) {
                continue;
            }
            int start = bucket.getEntityStart(entityId);
            int end = bucket.getEntityEnd(entityId);
            for (int event = start; event < end; event++) {
                // Export Full-table if it has no validity date.
                if (exportDescription.getValidityDateColumn() != null && !bucket.eventIsContainedIn(event, exportDescription.getValidityDateColumn(), CDateSet.create(dateRange))) {
                    continue;
                }
                Object[] entry = new Object[totalColumns];
                for (Column column : exportDescription.getTable().getColumns()) {
                    if (!bucket.has(event, column)) {
                        continue;
                    }
                    if (column.equals(exportDescription.getValidityDateColumn())) {
                        entry[0] = List.of(bucket.getAsDateRange(event, column));
                    } else {
                        entry[positions.get(column)] = bucket.createScriptValue(event, column);
                    }
                }
                results.add(entry);
            }
        }
    }
    return Optional.of(new MultilineEntityResult(entity.getId(), results));
}
Also used : Bucket(com.bakdata.conquery.models.events.Bucket) Getter(lombok.Getter) Table(com.bakdata.conquery.models.datasets.Table) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) ArrayList(java.util.ArrayList) Entity(com.bakdata.conquery.models.query.entity.Entity) List(java.util.List) QueryExecutionContext(com.bakdata.conquery.models.query.QueryExecutionContext) Map(java.util.Map) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) Optional(java.util.Optional) Nullable(javax.annotation.Nullable) CDateSet(com.bakdata.conquery.models.common.CDateSet) Column(com.bakdata.conquery.models.datasets.Column) Bucket(com.bakdata.conquery.models.events.Bucket) Column(com.bakdata.conquery.models.datasets.Column) ArrayList(java.util.ArrayList) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult)

Example 2 with EntityResult

use of com.bakdata.conquery.models.query.results.EntityResult in project conquery by bakdata.

the class ExcelRenderer method writeBody.

private int writeBody(SXSSFSheet sheet, List<ResultInfo> infos, Stream<EntityResult> resultLines) {
    // Row 0 is the Header the data starts at 1
    final AtomicInteger currentRow = new AtomicInteger(1);
    final int writtenLines = resultLines.mapToInt(l -> this.writeRowsForEntity(infos, l, currentRow, cfg, sheet)).sum();
    // The result was shorter than the number of rows to track, so we auto size here explicitly
    if (writtenLines < config.getLastRowToAutosize()) {
        setColumnWidthsAndUntrack(sheet);
    }
    return writtenLines;
}
Also used : ExcelSheetNameC10n(com.bakdata.conquery.internationalization.ExcelSheetNameC10n) SneakyThrows(lombok.SneakyThrows) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) CellReference(org.apache.poi.ss.util.CellReference) ArrayList(java.util.ArrayList) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) BigDecimal(java.math.BigDecimal) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) CDate(com.bakdata.conquery.models.common.CDate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell) SXSSFSheet(org.apache.poi.xssf.streaming.SXSSFSheet) I18n(com.bakdata.conquery.models.i18n.I18n) CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) UniqueNamer(com.bakdata.conquery.models.query.resultinfo.UniqueNamer) OutputStream(java.io.OutputStream) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) User(com.bakdata.conquery.models.auth.entities.User) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) ExcelConfig(com.bakdata.conquery.models.config.ExcelConfig) List(java.util.List) AreaReference(org.apache.poi.ss.util.AreaReference) Stream(java.util.stream.Stream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) C10N(c10n.C10N) Row(org.apache.poi.ss.usermodel.Row) POIXMLProperties(org.apache.poi.ooxml.POIXMLProperties) CellStyle(org.apache.poi.ss.usermodel.CellStyle) NotNull(org.jetbrains.annotations.NotNull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with EntityResult

use of com.bakdata.conquery.models.query.results.EntityResult 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 4 with EntityResult

use of com.bakdata.conquery.models.query.results.EntityResult in project conquery by bakdata.

the class CsvResultGenerationTest method writeAndRead.

@Test
void writeAndRead() throws IOException {
    // Prepare every input data
    PrintSettings printSettings = new PrintSettings(true, Locale.GERMAN, null, CONFIG, (cer) -> EntityPrintId.from(Integer.toString(cer.getEntityId()), Integer.toString(cer.getEntityId())), (selectInfo) -> selectInfo.getSelect().getLabel());
    // The Shard nodes send Object[] but since Jackson is used for deserialization, nested collections are always a list because they are not further specialized
    List<EntityResult> results = getTestEntityResults();
    ManagedQuery mquery = getTestQuery();
    // First we write to the buffer, than we read from it and parse it as TSV
    StringWriter writer = new StringWriter();
    CsvRenderer renderer = new CsvRenderer(CONFIG.getCsv().createWriter(writer), printSettings);
    renderer.toCSV(ResultTestUtil.ID_FIELDS, mquery.getResultInfos(), mquery.streamResults());
    String computed = writer.toString();
    String expected = generateExpectedCSV(results, mquery.getResultInfos(), printSettings);
    log.info("Wrote and than read this csv data: {}", computed);
    assertThat(computed).isNotEmpty();
    assertThat(computed).isEqualTo(expected);
}
Also used : StringWriter(java.io.StringWriter) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) Test(org.junit.jupiter.api.Test)

Example 5 with EntityResult

use of com.bakdata.conquery.models.query.results.EntityResult in project conquery by bakdata.

the class ArrowRenderer method write.

public static void write(ArrowWriter writer, VectorSchemaRoot root, RowConsumer[] idWriter, RowConsumer[] valueWriter, PrintIdMapper idMapper, Stream<EntityResult> results, int batchSize) throws IOException {
    Preconditions.checkArgument(batchSize > 0, "Batch size needs be larger than 0.");
    // TODO add time metric for writing
    log.trace("Starting result write");
    writer.start();
    int batchCount = 0;
    int batchLineCount = 0;
    Iterator<EntityResult> resultIterator = results.iterator();
    while (resultIterator.hasNext()) {
        EntityResult cer = resultIterator.next();
        for (Object[] line : cer.listResultLines()) {
            if (line.length != valueWriter.length) {
                throw new IllegalStateException("The number of value writers and values in a result line differs. Writers: " + valueWriter.length + " Line: " + line.length);
            }
            for (RowConsumer rowConsumer : idWriter) {
                // Write id information
                rowConsumer.accept(batchLineCount, idMapper.map(cer).getExternalId());
            }
            for (RowConsumer rowConsumer : valueWriter) {
                // Write values
                rowConsumer.accept(batchLineCount, line);
            }
            batchLineCount++;
            if (batchLineCount >= batchSize) {
                root.setRowCount(batchLineCount);
                writer.writeBatch();
                root.clear();
                batchLineCount = 0;
            }
        }
    }
    if (batchLineCount > 0) {
        root.setRowCount(batchLineCount);
        writer.writeBatch();
        root.clear();
        batchCount++;
    }
    log.trace("Wrote {} batches of size {} (last batch might be smaller)", batchCount, batchSize);
    writer.end();
}
Also used : EntityResult(com.bakdata.conquery.models.query.results.EntityResult)

Aggregations

EntityResult (com.bakdata.conquery.models.query.results.EntityResult)9 ArrayList (java.util.ArrayList)6 PrintSettings (com.bakdata.conquery.models.query.PrintSettings)5 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)4 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 CDateSet (com.bakdata.conquery.models.common.CDateSet)3 ResultType (com.bakdata.conquery.models.externalservice.ResultType)3 I18n (com.bakdata.conquery.models.i18n.I18n)3 ResultInfo (com.bakdata.conquery.models.query.resultinfo.ResultInfo)3 MultilineEntityResult (com.bakdata.conquery.models.query.results.MultilineEntityResult)3 IOException (java.io.IOException)3 Stream (java.util.stream.Stream)3 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)2 ResultTestUtil (com.bakdata.conquery.io.result.ResultTestUtil)2 ResultTestUtil.getTestEntityResults (com.bakdata.conquery.io.result.ResultTestUtil.getTestEntityResults)2 CDateRange (com.bakdata.conquery.models.common.daterange.CDateRange)2 ConqueryConfig (com.bakdata.conquery.models.config.ConqueryConfig)2 ExcelConfig (com.bakdata.conquery.models.config.ExcelConfig)2 DateContext (com.bakdata.conquery.models.forms.util.DateContext)2