Search in sources :

Example 1 with Column

use of com.bakdata.conquery.models.datasets.Column in project conquery by bakdata.

the class CBlock method calculateEntityDateIndices.

/**
 * For every included entity, calculate min and max and store them as statistics in the CBlock.
 *
 * @implNote This is an unrolled implementation of {@link CDateRange#spanClosed(CDateRange)}.
 */
private static CDateRange[] calculateEntityDateIndices(Bucket bucket, int bucketSize) {
    CDateRange[] spans = new CDateRange[bucketSize];
    Arrays.fill(spans, CDateRange.all());
    // First initialize to an illegal state that's easy on our comparisons
    Table table = bucket.getTable();
    for (Column column : table.getColumns()) {
        if (!column.getType().isDateCompatible()) {
            continue;
        }
        for (int entity : bucket.getEntities()) {
            final int index = bucket.getEntityIndex(entity);
            final int end = bucket.getEntityEnd(entity);
            // We unroll spanClosed for the whole bucket/entity, this avoids costly
            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
            for (int event = bucket.getEntityStart(entity); event < end; event++) {
                if (!bucket.has(event, column)) {
                    continue;
                }
                CDateRange range = bucket.getAsDateRange(event, column);
                if (range.hasLowerBound()) {
                    final int minValue = range.getMinValue();
                    max = Math.max(max, minValue);
                    min = Math.min(min, minValue);
                }
                if (range.hasUpperBound()) {
                    final int maxValue = range.getMaxValue();
                    max = Math.max(max, maxValue);
                    min = Math.min(min, maxValue);
                }
            }
            spans[index] = createClosed(max, min, spans[index]);
        }
    }
    return spans;
}
Also used : Table(com.bakdata.conquery.models.datasets.Table) Column(com.bakdata.conquery.models.datasets.Column) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange)

Example 2 with Column

use of com.bakdata.conquery.models.datasets.Column in project conquery by bakdata.

the class RequiredColumn method toColumn.

public Column toColumn(Table table, CentralRegistry storage) {
    Column col = new Column();
    col.setName(name);
    col.setType(type);
    col.setSharedDictionary(sharedDictionary);
    col.setTable(table);
    if (!Strings.isNullOrEmpty(secondaryId)) {
        final SecondaryIdDescription description = storage.resolve(new SecondaryIdDescriptionId(table.getDataset().getId(), secondaryId));
        col.setSecondaryId(description);
    }
    return col;
}
Also used : SecondaryIdDescriptionId(com.bakdata.conquery.models.identifiable.ids.specific.SecondaryIdDescriptionId) Column(com.bakdata.conquery.models.datasets.Column) SecondaryIdDescription(com.bakdata.conquery.models.datasets.SecondaryIdDescription)

Example 3 with Column

use of com.bakdata.conquery.models.datasets.Column 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 4 with Column

use of com.bakdata.conquery.models.datasets.Column in project conquery by bakdata.

the class TableExportQuery method resolve.

@Override
public void resolve(QueryResolveContext context) {
    query.resolve(context);
    // First is dates
    AtomicInteger currentPosition = new AtomicInteger(1);
    positions = new HashMap<>();
    Map<SecondaryIdDescription, Integer> secondaryIdPositions = new HashMap<>();
    // SecondaryIds are pulled to the front and grouped over all tables
    tables.stream().map(cqUnfilteredTable -> cqUnfilteredTable.getTable().getTable().getColumns()).flatMap(Arrays::stream).map(Column::getSecondaryId).filter(Objects::nonNull).distinct().sorted(Comparator.comparing(SecondaryIdDescription::getLabel)).forEach(secondaryId -> secondaryIdPositions.put(secondaryId, currentPosition.getAndIncrement()));
    for (CQUnfilteredTable table : tables) {
        Connector connector = table.getTable();
        final Column validityDateColumn = findValidityDateColumn(connector, table.getDateColumn());
        if (validityDateColumn != null) {
            positions.putIfAbsent(validityDateColumn, 0);
        }
        // Set column positions, set SecondaryId positions to precomputed ones.
        for (Column column : connector.getTable().getColumns()) {
            positions.computeIfAbsent(column, col -> col.getSecondaryId() != null ? secondaryIdPositions.get(col.getSecondaryId()) : currentPosition.getAndIncrement());
        }
    }
    resultInfos = createResultInfos(currentPosition.get(), secondaryIdPositions, positions);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connector(com.bakdata.conquery.models.datasets.concepts.Connector) CQUnfilteredTable(com.bakdata.conquery.apiv1.query.concept.filter.CQUnfilteredTable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Column(com.bakdata.conquery.models.datasets.Column) SecondaryIdDescription(com.bakdata.conquery.models.datasets.SecondaryIdDescription)

Example 5 with Column

use of com.bakdata.conquery.models.datasets.Column in project conquery by bakdata.

the class CQConcept method createQueryPlan.

@Override
public QPNode createQueryPlan(QueryPlanContext context, ConceptQueryPlan plan) {
    final List<Aggregator<?>> conceptAggregators = createAggregators(plan, selects);
    List<QPNode> tableNodes = new ArrayList<>();
    for (CQTable table : tables) {
        List<FilterNode<?>> filters = table.getFilters().stream().map(FilterValue::createNode).collect(Collectors.toList());
        // add filter to children
        List<Aggregator<?>> aggregators = new ArrayList<>();
        aggregators.addAll(conceptAggregators);
        final List<Aggregator<?>> connectorAggregators = createAggregators(plan, table.getSelects());
        // Exists aggregators hold a reference to their parent FiltersNode so they need to be treated separately.
        // They also don't need aggregation as they simply imitate their reference.
        List<ExistsAggregator> existsAggregators = connectorAggregators.stream().filter(ExistsAggregator.class::isInstance).map(ExistsAggregator.class::cast).collect(Collectors.toList());
        aggregators.addAll(connectorAggregators);
        aggregators.removeIf(ExistsAggregator.class::isInstance);
        List<Aggregator<CDateSet>> eventDateUnionAggregators = aggregateEventDates ? List.of(new EventDateUnionAggregator(Set.of(table.getConnector().getTable()))) : Collections.emptyList();
        aggregators.addAll(eventDateUnionAggregators);
        final QPNode filtersNode = getConcept().createConceptQuery(context, filters, aggregators, eventDateUnionAggregators);
        // Link up the ExistsAggregators to the node
        existsAggregators.forEach(agg -> agg.setReference(filtersNode));
        // Select if matching secondaryId available
        final boolean hasSelectedSecondaryId = Arrays.stream(table.getConnector().getTable().getColumns()).map(Column::getSecondaryId).filter(Objects::nonNull).anyMatch(o -> Objects.equals(context.getSelectedSecondaryId(), o));
        final Column validityDateColumn = selectValidityDateColumn(table);
        final ConceptNode node = new ConceptNode(// TODO Don't set validity node, when no validity column exists. See workaround for this and remove it: https://github.com/bakdata/conquery/pull/1362
        new ValidityDateNode(validityDateColumn, filtersNode), elements, table, // if the node is excluded, don't pass it into the Node.
        !excludeFromSecondaryId && hasSelectedSecondaryId ? context.getSelectedSecondaryId() : null);
        tableNodes.add(node);
    }
    // We always merge on concept level
    final QPNode outNode = OrNode.of(tableNodes, aggregateEventDates ? DateAggregationAction.MERGE : DateAggregationAction.BLOCK);
    // Link concept-level Exists-select to outer node.
    conceptAggregators.stream().filter(aggregator -> aggregator instanceof ExistsAggregator).forEach(aggregator -> ((ExistsAggregator) aggregator).setReference(outNode));
    return outNode;
}
Also used : ConceptNode(com.bakdata.conquery.models.query.queryplan.specific.ConceptNode) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) ConceptElement(com.bakdata.conquery.models.datasets.concepts.ConceptElement) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) NotEmpty(javax.validation.constraints.NotEmpty) Valid(javax.validation.Valid) JsonAlias(com.fasterxml.jackson.annotation.JsonAlias) Locale(java.util.Locale) QPNode(com.bakdata.conquery.models.query.queryplan.QPNode) ToString(lombok.ToString) CQTable(com.bakdata.conquery.apiv1.query.concept.filter.CQTable) InternalOnly(com.bakdata.conquery.io.jackson.InternalOnly) ConceptQueryPlan(com.bakdata.conquery.models.query.queryplan.ConceptQueryPlan) NamespacedIdentifiableHolding(com.bakdata.conquery.models.query.NamespacedIdentifiableHolding) CPSType(com.bakdata.conquery.io.cps.CPSType) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) NsIdRefCollection(com.bakdata.conquery.io.jackson.serializer.NsIdRefCollection) Concept(com.bakdata.conquery.models.datasets.concepts.Concept) Set(java.util.Set) ConceptNode(com.bakdata.conquery.models.query.queryplan.specific.ConceptNode) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) FilterValue(com.bakdata.conquery.apiv1.query.concept.filter.FilterValue) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) JsonManagedReference(com.fasterxml.jackson.annotation.JsonManagedReference) Column(com.bakdata.conquery.models.datasets.Column) Setter(lombok.Setter) Getter(lombok.Getter) NamespacedIdentifiable(com.bakdata.conquery.models.identifiable.ids.NamespacedIdentifiable) ArrayList(java.util.ArrayList) Select(com.bakdata.conquery.models.datasets.concepts.select.Select) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) DateAggregationMode(com.bakdata.conquery.models.query.DateAggregationMode) FilterNode(com.bakdata.conquery.models.query.queryplan.filter.FilterNode) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) DateAggregationAction(com.bakdata.conquery.models.query.queryplan.DateAggregationAction) ValidityDateNode(com.bakdata.conquery.models.query.queryplan.specific.ValidityDateNode) ExportForm(com.bakdata.conquery.apiv1.forms.export_form.ExportForm) QueryPlanContext(com.bakdata.conquery.models.query.QueryPlanContext) ValidationMethod(io.dropwizard.validation.ValidationMethod) QueryResolveContext(com.bakdata.conquery.models.query.QueryResolveContext) OrNode(com.bakdata.conquery.models.query.queryplan.specific.OrNode) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) CQElement(com.bakdata.conquery.apiv1.query.CQElement) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator) Collections(java.util.Collections) CDateSet(com.bakdata.conquery.models.common.CDateSet) QPNode(com.bakdata.conquery.models.query.queryplan.QPNode) CQTable(com.bakdata.conquery.apiv1.query.concept.filter.CQTable) FilterNode(com.bakdata.conquery.models.query.queryplan.filter.FilterNode) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) ArrayList(java.util.ArrayList) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator) Column(com.bakdata.conquery.models.datasets.Column) ValidityDateNode(com.bakdata.conquery.models.query.queryplan.specific.ValidityDateNode) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator)

Aggregations

Column (com.bakdata.conquery.models.datasets.Column)18 Table (com.bakdata.conquery.models.datasets.Table)10 Dataset (com.bakdata.conquery.models.datasets.Dataset)6 CQTable (com.bakdata.conquery.apiv1.query.concept.filter.CQTable)5 Validator (javax.validation.Validator)5 Injectable (com.bakdata.conquery.io.jackson.Injectable)4 MutableInjectableValues (com.bakdata.conquery.io.jackson.MutableInjectableValues)4 SecondaryIdDescription (com.bakdata.conquery.models.datasets.SecondaryIdDescription)4 CentralRegistry (com.bakdata.conquery.models.identifiable.CentralRegistry)4 Test (org.junit.jupiter.api.Test)4 Connector (com.bakdata.conquery.models.datasets.concepts.Connector)3 TreeConcept (com.bakdata.conquery.models.datasets.concepts.tree.TreeConcept)3 IdMapSerialisationTest (com.bakdata.conquery.models.identifiable.IdMapSerialisationTest)3 CQUnfilteredTable (com.bakdata.conquery.apiv1.query.concept.filter.CQUnfilteredTable)2 InternalOnly (com.bakdata.conquery.io.jackson.InternalOnly)2 CDateSet (com.bakdata.conquery.models.common.CDateSet)2 CDateRange (com.bakdata.conquery.models.common.daterange.CDateRange)2 Import (com.bakdata.conquery.models.datasets.Import)2 Concept (com.bakdata.conquery.models.datasets.concepts.Concept)2 ValidityDate (com.bakdata.conquery.models.datasets.concepts.ValidityDate)2