[jOOQ/jOOQ#7527] Add Settings.transformPatternsUnnecessaryDistinct

This commit is contained in:
Lukas Eder 2022-10-27 14:16:54 +02:00
parent 73534e8e42
commit bfb1f8533c
6 changed files with 117 additions and 7 deletions

View File

@ -228,6 +228,20 @@ extends
@Experimental
@NotNull Select<R> $distinct(boolean newDistinct);
/**
* Experimental query object model accessor method, see also {@link QOM}.
* Subject to change in future jOOQ versions, use at your own risk.
*/
@Experimental
UnmodifiableList<? extends SelectFieldOrAsterisk> $distinctOn();
/**
* Experimental query object model accessor method, see also {@link QOM}.
* Subject to change in future jOOQ versions, use at your own risk.
*/
@Experimental
@NotNull Select<R> $distinctOn(Collection<? extends SelectFieldOrAsterisk> newDistinctOn);
/**
* Experimental query object model accessor method, see also {@link QOM}.
* Subject to change in future jOOQ versions, use at your own risk.

View File

@ -143,6 +143,8 @@ public class Settings
@XmlElement(defaultValue = "true")
protected Boolean transformPatternsLogging = true;
@XmlElement(defaultValue = "true")
protected Boolean transformPatternsUnnecessaryDistinct = true;
@XmlElement(defaultValue = "true")
protected Boolean transformPatternsCountConstant = true;
@XmlElement(defaultValue = "true")
protected Boolean transformPatternsTrim = true;
@ -1624,6 +1626,37 @@ public class Settings
this.transformPatternsLogging = value;
}
/**
* Transform <code>SELECT DISTINCT a, b FROM t GROUP BY a, b</code> to <code>SELECT a, b FROM t GROUP BY a, b</code>.
* <p>
* The <code>GROUP BY</code> clause already removes duplicates, so if the <code>DISTINCT</code> clause
* contains at least all the columns from <code>GROUP BY</code> then it can be removed.
* <p>
* To enable this feature, {@link #transformPatterns} must be enabled as well.
* <p>
* This feature is available in the commercial distribution only.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isTransformPatternsUnnecessaryDistinct() {
return transformPatternsUnnecessaryDistinct;
}
/**
* Sets the value of the transformPatternsUnnecessaryDistinct property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setTransformPatternsUnnecessaryDistinct(Boolean value) {
this.transformPatternsUnnecessaryDistinct = value;
}
/**
* Transform <code>COUNT(1)</code> or any other <code>COUNT(const)</code> to <code>COUNT(*)</code>.
* <p>
@ -5013,6 +5046,11 @@ public class Settings
return this;
}
public Settings withTransformPatternsUnnecessaryDistinct(Boolean value) {
setTransformPatternsUnnecessaryDistinct(value);
return this;
}
public Settings withTransformPatternsCountConstant(Boolean value) {
setTransformPatternsCountConstant(value);
return this;
@ -6050,6 +6088,7 @@ public class Settings
builder.append("diagnosticsNullCondition", diagnosticsNullCondition);
builder.append("transformPatterns", transformPatterns);
builder.append("transformPatternsLogging", transformPatternsLogging);
builder.append("transformPatternsUnnecessaryDistinct", transformPatternsUnnecessaryDistinct);
builder.append("transformPatternsCountConstant", transformPatternsCountConstant);
builder.append("transformPatternsTrim", transformPatternsTrim);
builder.append("transformPatternsNotNot", transformPatternsNotNot);
@ -6616,6 +6655,15 @@ public class Settings
return false;
}
}
if (transformPatternsUnnecessaryDistinct == null) {
if (other.transformPatternsUnnecessaryDistinct!= null) {
return false;
}
} else {
if (!transformPatternsUnnecessaryDistinct.equals(other.transformPatternsUnnecessaryDistinct)) {
return false;
}
}
if (transformPatternsCountConstant == null) {
if (other.transformPatternsCountConstant!= null) {
return false;
@ -7848,6 +7896,7 @@ public class Settings
result = ((prime*result)+((diagnosticsNullCondition == null)? 0 :diagnosticsNullCondition.hashCode()));
result = ((prime*result)+((transformPatterns == null)? 0 :transformPatterns.hashCode()));
result = ((prime*result)+((transformPatternsLogging == null)? 0 :transformPatternsLogging.hashCode()));
result = ((prime*result)+((transformPatternsUnnecessaryDistinct == null)? 0 :transformPatternsUnnecessaryDistinct.hashCode()));
result = ((prime*result)+((transformPatternsCountConstant == null)? 0 :transformPatternsCountConstant.hashCode()));
result = ((prime*result)+((transformPatternsTrim == null)? 0 :transformPatternsTrim.hashCode()));
result = ((prime*result)+((transformPatternsNotNot == null)? 0 :transformPatternsNotNot.hashCode()));

View File

@ -1684,6 +1684,22 @@ package org.jooq.impl;

View File

@ -3423,6 +3423,16 @@ implements
return getDelegate().$distinct(newDistinct);
}
@Override
public final UnmodifiableList<? extends SelectFieldOrAsterisk> $distinctOn() {
return getDelegate().$distinctOn();
}
@Override
public final Select<R> $distinctOn(Collection<? extends SelectFieldOrAsterisk> newDistinctOn) {
return getDelegate().$distinctOn(newDistinctOn);
}
@Override
public final UnmodifiableList<? extends Table<?>> $from() {
return getDelegate().$from();

View File

@ -360,7 +360,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
private String hint;
private String option;
private boolean distinct;
private QueryPartList<SelectFieldOrAsterisk> distinctOn;
private final QueryPartList<SelectFieldOrAsterisk> distinctOn;
private ForLock forLock;
@ -424,6 +424,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
this.with = with;
this.distinct = distinct;
this.distinctOn = new SelectFieldList<>();
this.select = new SelectFieldList<>();
this.from = new TableList();
this.condition = new ConditionProviderImpl();
@ -556,7 +557,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
result.hint = hint;
result.distinct = distinct;
result.distinctOn = distinctOn;
result.distinctOn.addAll(distinctOn);
result.orderBy.addAll(orderBy);
@ -1262,7 +1263,6 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Select<?> distinctOnEmulation() {
// [#3564] TODO: Extract and merge this with getSelectResolveSomeAsterisks0()
@ -1275,7 +1275,7 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
Field<Integer> rn = rowNumber().over(partitionBy(partitionBy).orderBy(orderBy)).as("rn");
SelectQueryImpl<R> copy = copy(x -> {});
copy.distinctOn = null;
copy.distinctOn.clear();
copy.select.add(rn);
copy.orderBy.clear();
copy.limit.clear();
@ -3336,9 +3336,6 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
@Override
public final void addDistinctOn(Collection<? extends SelectFieldOrAsterisk> fields) {
if (distinctOn == null)
distinctOn = new QueryPartList<>();
distinctOn.addAll(fields);
}
@ -4579,6 +4576,19 @@ final class SelectQueryImpl<R extends Record> extends AbstractResultQuery<R> imp
return copy(s -> s.distinct = newDistinct);
}
@Override
public final UnmodifiableList<SelectFieldOrAsterisk> $distinctOn() {
return QOM.unmodifiable(distinctOn);
}
@Override
public final Select<R> $distinctOn(Collection<? extends SelectFieldOrAsterisk> newDistinctOn) {
return copy(s -> {
s.distinctOn.clear();
s.distinctOn.addAll(newDistinctOn);
});
}
@Override
public final UnmodifiableList<Table<?>> $from() {
return QOM.unmodifiable(from);

View File

@ -389,6 +389,17 @@ This feature is available in the commercial distribution only.]]></jxb:javadoc><
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Activate debug logging of the {@link #transformPatterns} feature.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="transformPatternsUnnecessaryDistinct" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Transform <code>SELECT DISTINCT a, b FROM t GROUP BY a, b</code> to <code>SELECT a, b FROM t GROUP BY a, b</code>.
<p>
The <code>GROUP BY</code> clause already removes duplicates, so if the <code>DISTINCT</code> clause
contains at least all the columns from <code>GROUP BY</code> then it can be removed.
<p>
To enable this feature, {@link #transformPatterns} must be enabled as well.
<p>
This feature is available in the commercial distribution only.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="transformPatternsCountConstant" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Transform <code>COUNT(1)</code> or any other <code>COUNT(const)</code> to <code>COUNT(*)</code>.
<p>