[jOOQ/jOOQ#1049] Better GREATEST() and LEAST() emulations for SQL Server using MAX() or MIN() with VALUES() correlated derived table
This commit is contained in:
parent
5b26e7b827
commit
74208f3398
@ -54,15 +54,15 @@ import org.jooq.impl.QOM.UnmodifiableList;
|
||||
*/
|
||||
final class Greatest<T> extends AbstractField<T> implements QOM.Greatest<T> {
|
||||
|
||||
private final QueryPartListView<? extends Field<?>> args;
|
||||
private final QueryPartListView<Field<T>> args;
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
Greatest(Field<?>... args) {
|
||||
super(N_GREATEST, (DataType<T>) Tools.nullSafeDataType(args[0]));
|
||||
|
||||
this.args = QueryPartListView.wrap(args);
|
||||
this.args = (QueryPartListView) QueryPartListView.wrap(args);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
|
||||
@ -73,9 +73,11 @@ final class Greatest<T> extends AbstractField<T> implements QOM.Greatest<T> {
|
||||
}
|
||||
|
||||
switch (ctx.family()) {
|
||||
// This implementation has O(2^n) complexity. Better implementations
|
||||
// are very welcome
|
||||
// [#1049] TODO Fix this!
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -84,22 +86,7 @@ final class Greatest<T> extends AbstractField<T> implements QOM.Greatest<T> {
|
||||
|
||||
|
||||
case DERBY: {
|
||||
Field<T> first = (Field<T>) args.get(0);
|
||||
Field<T> other = (Field<T>) args.get(1);
|
||||
|
||||
if (args.size() > 2) {
|
||||
Field<?>[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD);
|
||||
|
||||
ctx.visit(DSL
|
||||
.when(first.gt(other), DSL.greatest(first, remaining))
|
||||
.otherwise(DSL.greatest(other, remaining)));
|
||||
}
|
||||
else
|
||||
ctx.visit(DSL
|
||||
.when(first.gt(other), first)
|
||||
.otherwise(other));
|
||||
|
||||
return;
|
||||
GreatestLeast.acceptCaseEmulation(ctx, args, DSL::greatest, Field::gt);
|
||||
}
|
||||
|
||||
case FIREBIRD:
|
||||
|
||||
98
jOOQ/src/main/java/org/jooq/impl/GreatestLeast.java
Normal file
98
jOOQ/src/main/java/org/jooq/impl/GreatestLeast.java
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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 static org.jooq.impl.DSL.select;
|
||||
import static org.jooq.impl.DSL.values;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.Context;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.Field;
|
||||
// ...
|
||||
import org.jooq.Row1;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
final class GreatestLeast<T> {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static final <T> void acceptCaseEmulation(
|
||||
Context<?> ctx,
|
||||
QueryPartListView<Field<T>> args,
|
||||
BiFunction<? super Field<T>, ? super Field<?>[], ? extends Field<T>> greatestLeast,
|
||||
BiFunction<? super Field<T>, ? super Field<T>, ? extends Condition> gtLt
|
||||
) {
|
||||
|
||||
// This implementation has O(2^n) complexity. Better implementations
|
||||
// are very welcome
|
||||
Field<T> first = args.get(0);
|
||||
Field<T> other = args.get(1);
|
||||
|
||||
if (args.size() > 2) {
|
||||
Field<?>[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD);
|
||||
|
||||
ctx.visit(DSL
|
||||
.when(gtLt.apply(first, other), greatestLeast.apply(first, remaining))
|
||||
.otherwise(greatestLeast.apply(other, remaining)));
|
||||
}
|
||||
else
|
||||
ctx.visit(DSL
|
||||
.when(gtLt.apply(first, other), first)
|
||||
.otherwise(other));
|
||||
}
|
||||
}
|
||||
@ -54,15 +54,15 @@ import org.jooq.impl.QOM.UnmodifiableList;
|
||||
*/
|
||||
final class Least<T> extends AbstractField<T> implements QOM.Least<T> {
|
||||
|
||||
private final QueryPartListView<? extends Field<?>> args;
|
||||
private final QueryPartListView<Field<T>> args;
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
Least(Field<?>... args) {
|
||||
super(N_LEAST, (DataType<T>) Tools.nullSafeDataType(args[0]));
|
||||
|
||||
this.args = QueryPartListView.wrap(args);
|
||||
this.args = (QueryPartListView) QueryPartListView.wrap(args);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final void accept(Context<?> ctx) {
|
||||
|
||||
@ -73,8 +73,11 @@ final class Least<T> extends AbstractField<T> implements QOM.Least<T> {
|
||||
}
|
||||
|
||||
switch (ctx.family()) {
|
||||
// This implementation has O(2^n) complexity. Better implementations
|
||||
// are very welcome
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -83,21 +86,7 @@ final class Least<T> extends AbstractField<T> implements QOM.Least<T> {
|
||||
|
||||
|
||||
case DERBY: {
|
||||
Field<T> first = (Field<T>) args.get(0);
|
||||
Field<T> other = (Field<T>) args.get(1);
|
||||
|
||||
if (args.size() > 2) {
|
||||
Field<?>[] remaining = args.subList(2, args.size()).toArray(Tools.EMPTY_FIELD);
|
||||
|
||||
ctx.visit(DSL
|
||||
.when(first.lt(other), DSL.least(first, remaining))
|
||||
.otherwise(DSL.least(other, remaining)));
|
||||
}
|
||||
else
|
||||
ctx.visit(DSL
|
||||
.when(first.lt(other), first)
|
||||
.otherwise(other));
|
||||
|
||||
GreatestLeast.acceptCaseEmulation(ctx, args, DSL::least, Field::lt);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user