From 6bdfdf22f97d664a57e8fd6fd15557083f73fe55 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 8 Apr 2021 17:55:44 +0200 Subject: [PATCH] [jOOQ/jOOQ#11767] ROWNUM transformation should avoid creating derived tables if possible --- jOOQ/src/main/java/org/jooq/impl/Finder.java | 90 +++++++++++++++++++ .../java/org/jooq/impl/SelectQueryImpl.java | 41 +++++++-- 2 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Finder.java diff --git a/jOOQ/src/main/java/org/jooq/impl/Finder.java b/jOOQ/src/main/java/org/jooq/impl/Finder.java new file mode 100644 index 0000000000..499a5d8be2 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Finder.java @@ -0,0 +1,90 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import java.sql.SQLException; +import java.util.function.Predicate; + +import org.jooq.BindContext; +import org.jooq.Configuration; +import org.jooq.Field; +import org.jooq.Param; +import org.jooq.QueryPart; +import org.jooq.QueryPartInternal; + +/** + * A stub {@link BindContext} that acts as a collector of {@link Param} + * {@link QueryPart}'s + * + * @author Lukas Eder + */ +final class Finder extends AbstractBindContext { + + private final Predicate find; + private final Predicate enter; + private boolean found; + + Finder( + Configuration configuration, + Predicate find, + Predicate enter + ) { + super(configuration, null); + + this.find = find; + this.enter = enter; + } + + @Override + protected final void bindInternal(QueryPartInternal internal) { + if (found |= find.test(internal)) + return; + + if (enter.test(internal)) + super.bindInternal(internal); + } + + final boolean found() { + return found; + } + + @Override + protected final BindContext bindValue0(Object value, Field field) throws SQLException { + throw new UnsupportedOperationException(); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index 4c9653a7d1..a15fb43379 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -495,18 +495,38 @@ final class SelectQueryImpl extends AbstractResultQuery imp - private final SelectQueryImpl copyTo(CopyClause clause, SelectQueryImpl result) { - return copyBetween(CopyClause.START, clause, result); + + + + + + + + + + + + + + + + + + private final SelectQueryImpl copyTo(CopyClause clause, boolean scalarSelect, SelectQueryImpl result) { + return copyBetween(CopyClause.START, clause, scalarSelect, result); } - private final SelectQueryImpl copyAfter(CopyClause clause, SelectQueryImpl result) { - return copyBetween(clause, CopyClause.END, result); + private final SelectQueryImpl copyAfter(CopyClause clause, boolean scalarSelect, SelectQueryImpl result) { + return copyBetween(clause, CopyClause.END, scalarSelect, result); } - private final SelectQueryImpl copyBetween(CopyClause start, CopyClause end, SelectQueryImpl result) { + private final SelectQueryImpl copyBetween(CopyClause start, CopyClause end, boolean scalarSelect, SelectQueryImpl result) { if (CopyClause.START.between(start, end)) { result.from.addAll(from); result.condition.setWhere(condition.getWhere()); + + if (scalarSelect) + result.select.addAll(select); } if (CopyClause.WHERE.between(start, end)) { @@ -525,7 +545,9 @@ final class SelectQueryImpl extends AbstractResultQuery imp } if (CopyClause.QUALIFY.between(start, end)) { - result.select.addAll(select); + if (!scalarSelect) + result.select.addAll(select); + result.hint = hint; result.distinct = distinct; result.distinctOn = distinctOn; @@ -561,7 +583,7 @@ final class SelectQueryImpl extends AbstractResultQuery imp } private final SelectQueryImpl copy(Function, ? extends SelectQueryImpl> finisher) { - return finisher.apply(copyTo(CopyClause.END, new SelectQueryImpl<>(configuration(), with))); + return finisher.apply(copyTo(CopyClause.END, false, new SelectQueryImpl<>(configuration(), with))); } @@ -598,6 +620,11 @@ final class SelectQueryImpl extends AbstractResultQuery imp + + + + +