code: private boolean areNonFKColumnsUnused(ExtensionalDataNode targetDataNode, IntermediateQuery query,
                                            ForeignKeyConstraint constraint) {
  
          ImmutableList<? extends VariableOrGroundTerm> targetArguments = targetDataNode.getProjectionAtom().getArguments();
  
          ImmutableSet<Integer> fkTargetIndexes = constraint.getComponents().stream()
                  .map(c -> c.getReference().getIndex() - 1)
                  .collect(ImmutableCollectors.toSet());
  
          /**
           * Terms appearing in non-FK positions
           */
          ImmutableList<VariableOrGroundTerm> remainingTerms = IntStream.range(0, targetArguments.size())
                  .filter(i -> !fkTargetIndexes.contains(i))
                  .boxed()
                  .map(targetArguments::get)
                  .collect(ImmutableCollectors.toList());
  
          /**
           * Check usage in the data atom.
           *
           * 1 - They should all variables
           * 2 - They should be no duplicate
           * 3 - They must be distinct from the FK target terms
           */
          if ((!remainingTerms.stream().allMatch(t -> t instanceof Variable))
              || (ImmutableSet.copyOf(remainingTerms).size() < remainingTerms.size())
              || fkTargetIndexes.stream()
                  .map(targetArguments::get)
                  .anyMatch(remainingTerms::contains))
              return false;
  
          /**
           * Check that the remaining variables are not used anywhere else
           */
          // TODO: use a more efficient implementation
          VariableOccurrenceAnalyzer analyzer = new NaiveVariableOccurrenceAnalyzerImpl();
  
          return remainingTerms.stream()
                  .map(v -> (Variable) v)
                  .noneMatch(v -> analyzer.isVariableUsedSomewhereElse(query, targetDataNode, v));
      }
docs: TODO: explain