private static function join(AggregatorInterface $aggregator, QueryInterface $query) : QueryInterface
    {
        $config = $aggregator->getConfig();

        if (! $config->joinMode()) {
            return $query;
        }

        Assert::isInstanceOf($query, Query::class);
        $association = static::findAssociation($aggregator);

        // limit to record's associated data
        $query->innerJoinWith($association->getName(), function ($q) use ($association, $config) {
            $table = $config->getJoinTable();
            $primaryKey = $table->getPrimaryKey();
            if (! is_string($primaryKey)) {
                Log::error('Failed to apply inner join for aggregated field value: primary key must be a string', [
                    'source_table' => $config->getTable()->getAlias(),
                    'target_table' => $table->getAlias(),
                    'primar_key' => $table->getPrimaryKey(),
                    'association' => $association->getName()
                ]);

                return $q;
            }

            $entity = $config->getEntity();

            return $q->where([
                $association->getTarget()->aliasField($primaryKey) => $entity->get($primaryKey)
            ]);
        });

        return $query;
    }