/**
* Return a new select with expressions initialized.
*/
private Select newSelect(ExpContext ctx, Select parent,
String alias, QueryExpressions exps, QueryExpressionsState state) {
Select sel = ctx.store.getSQLFactory().newSelect();
sel.setAutoDistinct((exps.distinct & exps.DISTINCT_AUTO) != 0);
sel.setJoinSyntax(ctx.fetch.getJoinSyntax());
sel.setParent(parent, alias);
initialize(sel, ctx, exps, state);
if (!sel.getAutoDistinct()) {
if ((exps.distinct & exps.DISTINCT_TRUE) != 0)
sel.setDistinct(true);
else if ((exps.distinct & exps.DISTINCT_FALSE) != 0)
sel.setDistinct(false);
} else if (exps.projections.length > 0) {
if (!sel.isDistinct() && (exps.distinct & exps.DISTINCT_TRUE) != 0){
// if the select is not distinct but the query is, force
// the select to be distinct
sel.setDistinct(true);
} else if (sel.isDistinct()) {
// when aggregating data or making a non-distinct projection
// from a distinct select, we have to select from a tmp
// table formed by a distinct subselect in the from clause;
// this subselect selects the pks of the candidate (to
// get unique candidate values) and needed field values and
// applies the where conditions; the outer select applies
// ordering, grouping, etc
boolean agg = exps.isAggregate();
boolean candidate = ProjectionExpressionVisitor.
hasCandidateProjections(exps.projections);
if (agg || (candidate
&& (exps.distinct & exps.DISTINCT_TRUE) == 0)) {
DBDictionary dict = ctx.store.getDBDictionary();
dict.assertSupport(dict.supportsSubselect,
"SupportsSubselect");
Select inner = sel;
sel = ctx.store.getSQLFactory().newSelect();
sel.setParent(parent, alias);
sel.setDistinct(agg
&& (exps.distinct & exps.DISTINCT_TRUE) != 0);
sel.setFromSelect(inner);