public function applyJoin()
    {
        if ($this->skipJoin) {
            return $this;
        }
        $attributeCode = $this->attribute->getCode();
        if ($this->joinApplied) {
            throw new \LogicException("Join for attribute query builder {$attributeCode} already applied");
        }
        $qb = $this->eavQueryBuilder->getQueryBuilder();

        // Join based on attributeCode
        $attributeParameter = $this->generateUniqueId('attribute');
        $qb->setParameter($attributeParameter, $attributeCode);
        $joinDql = "{$this->joinAlias}.attributeCode = :{$attributeParameter}";

        if ($this->enforceFamilyCondition) {
            $family = $this->attribute->getFamily();
            if (!$family) {
                throw new MissingFamilyException("Unable to resolve family for attribute {$attributeCode}");
            }
            $familyParameter = $this->generateUniqueId('family');
            $qb->setParameter($familyParameter, $family->getCode());
            $joinDql .= " AND {$this->joinAlias}.familyCode = :{$familyParameter}";
        }

        if ($this->context) {
            /** @var ContextualValueInterface $valueClass */
            $valueClass = $this->attribute->getFamily()->getValueClass();
            if (!is_a($valueClass, ContextualValueInterface::class, true)) {
                throw new ContextException('Unable to filter by context on a non-contextual value class');
            }
            foreach ($this->context as $axis => $axisValues) {
                if (!\in_array($axis, $valueClass::getContextKeys(), true)) {
                    throw new ContextException("Trying to filter on invalid axis '{$axis}'");
                }
                if (!\in_array($axis, $this->attribute->getContextMask(), true)) {
                    continue;
                }
                $axisValueParameter = $this->generateUniqueId($axis.'Axis');
                $joinDql .= " AND {$this->joinAlias}.{$axis}";
                if (is_array($axisValues)) {
                    $joinDql .= " IN (:{$axisValueParameter})";
                } else {
                    $joinDql .= " = :{$axisValueParameter}";
                }
                $qb->setParameter($axisValueParameter, $axisValues);
            }
        }

        $qb->leftJoin(
            $this->eavQueryBuilder->getAlias().'.values',
            $this->joinAlias,
            Join::WITH,
            $joinDql
        );

        if ($this->joinRelation) {
            $qb->leftJoin(
                $this->joinAlias.'.'.$this->attribute->getType()->getDatabaseType(),
                $this->joinRelation
            );
        }

        $this->joinApplied = true;

        return $this;
    }