<commit_before>package io.crnk.data.jpa.internal.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec;
import io.crnk.core.queryspec.pagingspec.PagingSpec;
import io.crnk.data.jpa.query.JpaQueryExecutor;
import io.crnk.meta.model.MetaAttributePath;
import io.crnk.meta.model.MetaDataObject;

public abstract class AbstractQueryExecutorImpl<T> implements JpaQueryExecutor<T> {

	private static final String ENTITY_GRAPH_BUILDER_IMPL = "io.crnk.data.jpa.internal.query.EntityGraphBuilderImpl";

	protected int offset = 0;

	protected int limit = -1;

	protected boolean cached = false;

	protected EntityManager em;

	protected int numAutoSelections;

	protected Set<MetaAttributePath> fetchPaths = new HashSet<>();

	protected MetaDataObject meta;

	protected Map<String, Integer> selectionBindings;

	public AbstractQueryExecutorImpl(EntityManager em, MetaDataObject meta, int numAutoSelections,
			Map<String, Integer> selectionBindings) {
		this.em = em;
		this.meta = meta;
		this.numAutoSelections = numAutoSelections;
		this.selectionBindings = selectionBindings;
	}

	protected static List<Object[]> enforceDistinct(List<?> list) {
		HashSet<TupleElement> distinctSet = new HashSet<>();
		ArrayList<Object[]> distinctResult = new ArrayList<>();
		for (Object obj : list) {
			Object[] values = (Object[]) obj;
			TupleElement tuple = new TupleElement(values);
			if (!distinctSet.contains(tuple)) {
				distinctSet.add(tuple);
				distinctResult.add(values);
			}
		}

		return distinctResult;
	}

	@Override
	public JpaQueryExecutor<T> fetch(List<String> attrPath) {
		// include path an all prefix paths
		MetaAttributePath path = meta.resolvePath(attrPath);
		for (int i = 1; i <= path.length(); i++) {
			fetchPaths.add(path.subPath(0, i));
		}
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setCached(boolean cached) {
		this.cached = cached;
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setOffset(int offset) {
		this.offset = offset;
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setLimit(int limit) {
		this.limit = limit;
		return this;
	}

	@Override
	public int getLimit() {
		return limit;
	}

	@Override
	public void setPaging(PagingSpec pagingSpec) {
		OffsetLimitPagingSpec offsetLimit = pagingSpec.convert(OffsetLimitPagingSpec.class);
		setOffset((int) offsetLimit.getOffset());
		if (offsetLimit.getOffset() > Integer.MAX_VALUE) {<commit_msg>simplified JPA fetching behavior #650<commit_after>package io.crnk.data.jpa.internal.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec;
import io.crnk.core.queryspec.pagingspec.PagingSpec;
import io.crnk.data.jpa.query.JpaQueryExecutor;
import io.crnk.meta.model.MetaAttributePath;
import io.crnk.meta.model.MetaDataObject;

public abstract class AbstractQueryExecutorImpl<T> implements JpaQueryExecutor<T> {

	private static final String ENTITY_GRAPH_BUILDER_IMPL = "io.crnk.data.jpa.internal.query.EntityGraphBuilderImpl";

	protected int offset = 0;

	protected int limit = -1;

	protected boolean cached = false;

	protected EntityManager em;

	protected int numAutoSelections;

	protected Set<MetaAttributePath> fetchPaths = new HashSet<>();

	protected MetaDataObject meta;

	protected Map<String, Integer> selectionBindings;

	public AbstractQueryExecutorImpl(EntityManager em, MetaDataObject meta, int numAutoSelections,
			Map<String, Integer> selectionBindings) {
		this.em = em;
		this.meta = meta;
		this.numAutoSelections = numAutoSelections;
		this.selectionBindings = selectionBindings;
	}

	protected static List<Object[]> enforceDistinct(List<?> list) {
		HashSet<TupleElement> distinctSet = new HashSet<>();
		ArrayList<Object[]> distinctResult = new ArrayList<>();
		for (Object obj : list) {
			Object[] values = (Object[]) obj;
			TupleElement tuple = new TupleElement(values);
			if (!distinctSet.contains(tuple)) {
				distinctSet.add(tuple);
				distinctResult.add(values);
			}
		}

		return distinctResult;
	}

	@Override
	public JpaQueryExecutor<T> fetch(List<String> attrPath) {
		// include path an all prefix paths
		MetaAttributePath path = meta.resolvePath(attrPath);
		fetchPaths.add(path);
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setCached(boolean cached) {
		this.cached = cached;
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setOffset(int offset) {
		this.offset = offset;
		return this;
	}

	@Override
	public JpaQueryExecutor<T> setLimit(int limit) {
		this.limit = limit;
		return this;
	}

	@Override
	public int getLimit() {
		return limit;
	}

	@Override
	public void setPaging(PagingSpec pagingSpec) {
		OffsetLimitPagingSpec offsetLimit = pagingSpec.convert(OffsetLimitPagingSpec.class);
		setOffset((int) offsetLimit.getOffset());
		if (offsetLimit.getOffset() > Integer.MAX_VALUE) {
{'fraction_non_alphanumeric': 0.10680882903105125, 'fraction_numerical': 0.0016835016835016834, 'mean_word_length': 5.174364896073903, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2567405', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1688, 'n_words': 380}