From 74aec2ac91e097dbb785a57b3897dee8318b3cc5 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 10 Jan 2022 18:20:16 +0100 Subject: [PATCH] [jOOQ/jOOQ#7284] Replace common patterns in query object model - NOT NOT DISTINCT - Idempotent function repetitions - UPPER - LOWER - LTRIM - RTRIM - TRIM --- .../src/main/java/org/jooq/conf/Settings.java | 116 ++++++++++++++++-- .../org/jooq/impl/DefaultRenderContext.java | 77 +++++++++++- .../org/jooq/xsd/jooq-runtime-3.17.0.xsd | 40 ++++-- 3 files changed, 212 insertions(+), 21 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/conf/Settings.java b/jOOQ/src/main/java/org/jooq/conf/Settings.java index 920dd2ad87..86fbd3ff62 100644 --- a/jOOQ/src/main/java/org/jooq/conf/Settings.java +++ b/jOOQ/src/main/java/org/jooq/conf/Settings.java @@ -121,6 +121,10 @@ public class Settings protected Boolean transformPatternsNegNeg = true; @XmlElement(defaultValue = "true") protected Boolean transformPatternsBitNotBitNot = true; + @XmlElement(defaultValue = "true") + protected Boolean transformPatternsNotNotDistinct = true; + @XmlElement(defaultValue = "true") + protected Boolean transformPatternsIdempotentFunctionRepetition = true; @XmlElement(defaultValue = "false") protected Boolean transformAnsiJoinToTableLists = false; @XmlElement(defaultValue = "WHEN_NEEDED") @@ -1183,9 +1187,7 @@ public class Settings /** * Transform NOT(NOT(x)) to x *

- * For various reasons, there may be a redundant nesting of NOT unary operators, e.g. as a - * left over of some prior transformation, including manual ones. This transformation will simplify such - * redundant negations by removing them. + * This transformation removes a redundant logic negation. *

* To enable this feature, {@link #transformPatterns} must be enabled as well. *

@@ -1215,9 +1217,7 @@ public class Settings /** * Transform -(-(x)) to x *

- * For various reasons, there may be a redundant nesting of - unary operators, e.g. as a - * left over of some prior transformation, including manual ones. This transformation will simplify such - * redundant negations by removing them. + * This transformation removes a redundant arithmetic negation. *

* To enable this feature, {@link #transformPatterns} must be enabled as well. *

@@ -1245,11 +1245,9 @@ public class Settings } /** - * Transform ~(~(x)) to x + * Transform ~(~(x)) to x. *

- * For various reasons, there may be a redundant nesting of ~ unary operators, e.g. as a - * left over of some prior transformation, including manual ones. This transformation will simplify such - * redundant negations by removing them. + * This transformation removes a redundant bitwise negation. *

* To enable this feature, {@link #transformPatterns} must be enabled as well. *

@@ -1276,6 +1274,72 @@ public class Settings this.transformPatternsBitNotBitNot = value; } + /** + * Transform NOT (a IS NOT DISTINCT FROM b) to a IS DISTINCT FROM b + *

+ * This transformation removes a redundant logical negation from the DISTINCT predicate. + *

+ * This feature is available in the commercial distribution only. + * + * @return + * possible object is + * {@link Boolean } + * + */ + public Boolean isTransformPatternsNotNotDistinct() { + return transformPatternsNotNotDistinct; + } + + /** + * Sets the value of the transformPatternsNotNotDistinct property. + * + * @param value + * allowed object is + * {@link Boolean } + * + */ + public void setTransformPatternsNotNotDistinct(Boolean value) { + this.transformPatternsNotNotDistinct = value; + } + + /** + * Transform all repetitions of idempotent functions, such as UPPER(UPPER(s)) to UPPER(s) + *

+ * Idempotent functions that are covered so far, include: + *

+ *

+ * This feature is available in the commercial distribution only. + * + * @return + * possible object is + * {@link Boolean } + * + */ + public Boolean isTransformPatternsIdempotentFunctionRepetition() { + return transformPatternsIdempotentFunctionRepetition; + } + + /** + * Sets the value of the transformPatternsIdempotentFunctionRepetition property. + * + * @param value + * allowed object is + * {@link Boolean } + * + */ + public void setTransformPatternsIdempotentFunctionRepetition(Boolean value) { + this.transformPatternsIdempotentFunctionRepetition = value; + } + /** * Transform ANSI join to table lists if possible. *

@@ -3537,6 +3601,16 @@ public class Settings return this; } + public Settings withTransformPatternsNotNotDistinct(Boolean value) { + setTransformPatternsNotNotDistinct(value); + return this; + } + + public Settings withTransformPatternsIdempotentFunctionRepetition(Boolean value) { + setTransformPatternsIdempotentFunctionRepetition(value); + return this; + } + public Settings withTransformAnsiJoinToTableLists(Boolean value) { setTransformAnsiJoinToTableLists(value); return this; @@ -4365,6 +4439,8 @@ public class Settings builder.append("transformPatternsNotNot", transformPatternsNotNot); builder.append("transformPatternsNegNeg", transformPatternsNegNeg); builder.append("transformPatternsBitNotBitNot", transformPatternsBitNotBitNot); + builder.append("transformPatternsNotNotDistinct", transformPatternsNotNotDistinct); + builder.append("transformPatternsIdempotentFunctionRepetition", transformPatternsIdempotentFunctionRepetition); builder.append("transformAnsiJoinToTableLists", transformAnsiJoinToTableLists); builder.append("transformInConditionSubqueryWithLimitToDerivedTable", transformInConditionSubqueryWithLimitToDerivedTable); builder.append("transformQualify", transformQualify); @@ -4805,6 +4881,24 @@ public class Settings return false; } } + if (transformPatternsNotNotDistinct == null) { + if (other.transformPatternsNotNotDistinct!= null) { + return false; + } + } else { + if (!transformPatternsNotNotDistinct.equals(other.transformPatternsNotNotDistinct)) { + return false; + } + } + if (transformPatternsIdempotentFunctionRepetition == null) { + if (other.transformPatternsIdempotentFunctionRepetition!= null) { + return false; + } + } else { + if (!transformPatternsIdempotentFunctionRepetition.equals(other.transformPatternsIdempotentFunctionRepetition)) { + return false; + } + } if (transformAnsiJoinToTableLists == null) { if (other.transformAnsiJoinToTableLists!= null) { return false; @@ -5703,6 +5797,8 @@ public class Settings result = ((prime*result)+((transformPatternsNotNot == null)? 0 :transformPatternsNotNot.hashCode())); result = ((prime*result)+((transformPatternsNegNeg == null)? 0 :transformPatternsNegNeg.hashCode())); result = ((prime*result)+((transformPatternsBitNotBitNot == null)? 0 :transformPatternsBitNotBitNot.hashCode())); + result = ((prime*result)+((transformPatternsNotNotDistinct == null)? 0 :transformPatternsNotNotDistinct.hashCode())); + result = ((prime*result)+((transformPatternsIdempotentFunctionRepetition == null)? 0 :transformPatternsIdempotentFunctionRepetition.hashCode())); result = ((prime*result)+((transformAnsiJoinToTableLists == null)? 0 :transformAnsiJoinToTableLists.hashCode())); result = ((prime*result)+((transformInConditionSubqueryWithLimitToDerivedTable == null)? 0 :transformInConditionSubqueryWithLimitToDerivedTable.hashCode())); result = ((prime*result)+((transformQualify == null)? 0 :transformQualify.hashCode())); diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java index 39494ac294..f2b19e7546 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java @@ -84,10 +84,13 @@ import org.jooq.conf.SettingsTools; import org.jooq.exception.ControlFlowSignal; import org.jooq.exception.DataAccessException; import org.jooq.impl.QOM.BitNot; +import org.jooq.impl.QOM.IsNotDistinctFrom; import org.jooq.impl.QOM.Ltrim; import org.jooq.impl.QOM.Neg; import org.jooq.impl.QOM.Not; import org.jooq.impl.QOM.Rtrim; +import org.jooq.impl.QOM.Trim; +import org.jooq.impl.QOM.Upper; import org.jooq.impl.ScopeMarker.ScopeContent; import org.jooq.tools.JooqLogger; import org.jooq.tools.StringUtils; @@ -713,10 +716,11 @@ class DefaultRenderContext extends AbstractContext implements Ren if (isQuery == null) { isQuery = internal instanceof Query; + if (TRUE.equals(settings().isTransformPatterns()) && configuration().requireCommercial(() -> "SQL transformations are a commercial only feature. Please consider upgrading to the jOOQ Professional Edition or jOOQ Enterprise Edition.")) { - + } } @@ -832,6 +836,77 @@ class DefaultRenderContext extends AbstractContext implements Ren + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.17.0.xsd b/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.17.0.xsd index 8cffb83c14..ca94b1add3 100644 --- a/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.17.0.xsd +++ b/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.17.0.xsd @@ -295,9 +295,7 @@ This feature is available in the commercial distribution only.]]>< NOT(NOT(x)) to x

-For various reasons, there may be a redundant nesting of NOT unary operators, e.g. as a -left over of some prior transformation, including manual ones. This transformation will simplify such -redundant negations by removing them. +This transformation removes a redundant logic negation.

To enable this feature, {@link #transformPatterns} must be enabled as well.

@@ -307,9 +305,7 @@ This feature is available in the commercial distribution only.]]>< -(-(x)) to x

-For various reasons, there may be a redundant nesting of - unary operators, e.g. as a -left over of some prior transformation, including manual ones. This transformation will simplify such -redundant negations by removing them. +This transformation removes a redundant arithmetic negation.

To enable this feature, {@link #transformPatterns} must be enabled as well.

@@ -317,17 +313,41 @@ This feature is available in the commercial distribution only.]]>< - ~(~(x)) to x + ~(~(x)) to x.

-For various reasons, there may be a redundant nesting of ~ unary operators, e.g. as a -left over of some prior transformation, including manual ones. This transformation will simplify such -redundant negations by removing them. +This transformation removes a redundant bitwise negation.

To enable this feature, {@link #transformPatterns} must be enabled as well.

This feature is available in the commercial distribution only.]]> + + NOT (a IS NOT DISTINCT FROM b) to a IS DISTINCT FROM b +

+This transformation removes a redundant logical negation from the DISTINCT predicate. +

+This feature is available in the commercial distribution only.]]> + + + + UPPER(UPPER(s)) to UPPER(s) +

+Idempotent functions that are covered so far, include: +

+

+This feature is available in the commercial distribution only.]]> + +