[jOOQ/jOOQ#16499] Prototype API and implement ARRAY_FILTER

This commit is contained in:
Lukas Eder 2024-03-25 10:58:25 +01:00
parent 84971d3b69
commit 89b9c5a464
5 changed files with 505 additions and 0 deletions

View File

@ -0,0 +1,59 @@
/*
* 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
*
* https://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: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import org.jetbrains.annotations.NotNull;
/**
* A lambda expression of degree 1.
*
* @author Lukas Eder
*/
public interface Lambda1<T1, R> extends QueryPart {
/**
* The first argument of the lambda.
*/
@NotNull Field<T1> $arg1();
/**
* The lambda result.
*/
@NotNull Field<R> $result();
}

View File

@ -0,0 +1,203 @@
/*
* 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
*
* https://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: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.Internal.*;
import static org.jooq.impl.Keywords.*;
import static org.jooq.impl.Names.*;
import static org.jooq.impl.SQLDataType.*;
import static org.jooq.impl.Tools.*;
import static org.jooq.impl.Tools.BooleanDataKey.*;
import static org.jooq.impl.Tools.ExtendedDataKey.*;
import static org.jooq.impl.Tools.SimpleDataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Function1;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.impl.QOM.*;
import org.jooq.tools.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
/**
* The <code>ARRAY FILTER</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class ArrayFilter<T>
extends
AbstractField<T[]>
implements
QOM.ArrayFilter<T>
{
final Field<T[]> array;
final Lambda1<T, Boolean> filter;
ArrayFilter(
Field<T[]> array,
Lambda1<T, Boolean> filter
) {
super(
N_ARRAY_FILTER,
allNotNull((DataType) dataType(((DataType) OTHER).array(), array, false), array)
);
this.array = nullSafeNotNull(array, ((DataType) OTHER).array());
this.filter = filter;
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
final boolean parenthesised(Context<?> ctx) {
switch (ctx.family()) {
case H2:
case HSQLDB:
case POSTGRES:
case YUGABYTEDB:
return false;
case DUCKDB:
return true;
default:
return true;
}
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case H2:
case HSQLDB:
case POSTGRES:
case YUGABYTEDB:
ctx.visit(DSL.field(
select(DSL.coalesce(arrayAgg(filter.$arg1()), when(array.isNotNull(), DSL.cast(array(), getDataType()))))
.from(unnest(array).as(N_T, filter.$arg1().getUnqualifiedName()))
.where(filter.$result())
));
break;
case DUCKDB:
ctx.visit(function(N_ARRAY_FILTER, getDataType(), array, DSL.field("{0}", OTHER, filter)));
break;
default:
ctx.visit(function(N_ARRAY_FILTER, getDataType(), array, DSL.field("{0}", OTHER, filter)));
break;
}
}
// -------------------------------------------------------------------------
// XXX: Query Object Model
// -------------------------------------------------------------------------
@Override
public final Field<T[]> $arg1() {
return array;
}
@Override
public final Lambda1<T, Boolean> $arg2() {
return filter;
}
@Override
public final QOM.ArrayFilter<T> $arg1(Field<T[]> newValue) {
return $constructor().apply(newValue, $arg2());
}
@Override
public final QOM.ArrayFilter<T> $arg2(Lambda1<T, Boolean> newValue) {
return $constructor().apply($arg1(), newValue);
}
@Override
public final Function2<? super Field<T[]>, ? super Lambda1<T, Boolean>, ? extends QOM.ArrayFilter<T>> $constructor() {
return (a1, a2) -> new ArrayFilter<>(a1, a2);
}
// -------------------------------------------------------------------------
// XXX: The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof QOM.ArrayFilter<?> o) {
return
StringUtils.equals($array(), o.$array()) &&
StringUtils.equals($filter(), o.$filter())
;
}
else
return super.equals(that);
}
}

View File

@ -110,6 +110,7 @@ import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.impl.Names.N_COUNT;
import static org.jooq.impl.Names.N_CUME_DIST;
import static org.jooq.impl.Names.N_DENSE_RANK;
import static org.jooq.impl.Names.N_E;
import static org.jooq.impl.Names.N_IF;
import static org.jooq.impl.Names.N_IIF;
import static org.jooq.impl.Names.N_PERCENTILE_CONT;
@ -223,6 +224,7 @@ import org.jooq.False;
import org.jooq.Field;
import org.jooq.FieldOrRow;
// ...
import org.jooq.Function1;
import org.jooq.Geography;
import org.jooq.Geometry;
import org.jooq.GroupConcatOrderByStep;
@ -269,6 +271,7 @@ import org.jooq.JSONTableColumnsFirstStep;
import org.jooq.JSONValueOnStep;
import org.jooq.Keyword;
// ...
import org.jooq.Lambda1;
// ...
// ...
import org.jooq.Merge;
@ -14029,6 +14032,29 @@ public class DSL {
// -------------------------------------------------------------------------
// XXX: Lambda constructors
// -------------------------------------------------------------------------
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
@NotNull
static <T1, R> Lambda1<T1, R> lambda(T1[] field, org.jooq.Function1<? super Field<T1>, ? extends Field<R>> function) {
return lambda(Tools.field(field), function);
}
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
@NotNull
static <T1, R> Lambda1<T1, R> lambda(Field<T1[]> field, org.jooq.Function1<? super Field<T1>, ? extends Field<R>> function) {
Field<T1> e = (Field<T1>) DSL.field(N_E, field.getDataType().getArrayBaseDataType());
return lambda(e, function.apply(e));
}
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
@NotNull
public static <T1, R> Lambda1<T1, R> lambda(Field<T1> arg1, Field<R> result) {
return new org.jooq.impl.LambdaImpl1<>(arg1, result);
}
// -------------------------------------------------------------------------
// XXX: Routine parameter constructors
@ -22394,6 +22420,50 @@ public class DSL {
return new ArrayReplace<>(arg1, arg2, arg3);
}
/**
* The <code>ARRAY_FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
@NotNull
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
public static <T> Field<T[]> arrayFilter(T[] array, Function1<? super Field<T>, ? extends Field<Boolean>> filter) {
return new ArrayFilter<>(Tools.field(array), DSL.lambda(array, filter));
}
/**
* The <code>ARRAY_FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
@NotNull
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
public static <T> Field<T[]> arrayFilter(T[] array, Lambda1<T, Boolean> filter) {
return new ArrayFilter<>(Tools.field(array), filter);
}
/**
* The <code>ARRAY_FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
@NotNull
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
public static <T> Field<T[]> arrayFilter(Field<T[]> array, Function1<? super Field<T>, ? extends Field<Boolean>> filter) {
return new ArrayFilter<>(array, DSL.lambda(array, filter));
}
/**
* The <code>ARRAY_FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
@NotNull
@Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB })
public static <T> Field<T[]> arrayFilter(Field<T[]> array, Lambda1<T, Boolean> filter) {
return new ArrayFilter<>(array, filter);
}
// -------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------

View File

@ -0,0 +1,109 @@
/*
* 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
*
* https://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: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.Names.N_E;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Function1;
import org.jooq.Lambda1;
import org.jooq.QueryPart;
// ...
// ...
/**
* @author Lukas Eder
*/
final class LambdaImpl1<T1, R>
extends
AbstractQueryPart
implements
Lambda1<T1, R>
{
final Field<T1> arg1;
final Field<R> result;
LambdaImpl1(Field<T1> arg1, Field<R> result) {
this.arg1 = arg1;
this.result = result;
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(arg1).sql(" -> ").visit(result);
}
// -------------------------------------------------------------------------
// XXX: Query Object Model
// -------------------------------------------------------------------------
@Override
public final Field<T1> $arg1() {
return arg1;
}
@Override
public final Field<R> $result() {
return result;
}
// -------------------------------------------------------------------------
// XXX: Object API
// -------------------------------------------------------------------------
}

View File

@ -103,6 +103,7 @@ import org.jooq.JSON;
import org.jooq.JSONB;
import org.jooq.JSONEntry;
import org.jooq.Keyword;
import org.jooq.Lambda1;
// ...
import org.jooq.Name;
import org.jooq.Operator;
@ -228,6 +229,22 @@ public final class QOM {
// XXX: Model
// -------------------------------------------------------------------------
// public interface Lambda1<T1, R>
// extends
// org.jooq.QueryPart
// {
//
// /**
// * The first argument of the lambda.
// */
// @NotNull Field<T1> $arg1();
//
// /**
// * The lambda result.
// */
// @NotNull Field<R> $result();
// }
// This class uses a lot of fully qualified types, because of some javac bug
// In Java 1.8.0_302, which hasn't been analysed and reproduced yet in a more
// minimal example. Without the qualification, the types cannot be found
@ -11757,6 +11774,53 @@ public final class QOM {
// ArrayReplace
{}
/**
* The <code>ARRAY FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
public static final <T> ArrayFilter<T> ArrayFilter() {
return new org.jooq.impl.ArrayFilter<>(
null,
null
);
}
/**
* The <code>ARRAY FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
public static final <T> ArrayFilter<T> ArrayFilter(
Field<T[]> array,
Lambda1<T, Boolean> filter
) {
return new org.jooq.impl.ArrayFilter<>(
array,
filter
);
}
/**
* The <code>ARRAY FILTER</code> function.
* <p>
* Filter elements out of an array.
*/
public /*sealed*/ interface ArrayFilter<T>
extends
UOperator2<Field<T[]>, Lambda1<T, Boolean>, ArrayFilter<T>>,
org.jooq.Field<T[]>
//permits
// ArrayFilter
{
@NotNull default Field<T[]> $array() { return $arg1(); }
@CheckReturnValue
@NotNull default ArrayFilter<T> $array(Field<T[]> newArray) { return $arg1(newArray); }
@NotNull default Lambda1<T, Boolean> $filter() { return $arg2(); }
@CheckReturnValue
@NotNull default ArrayFilter<T> $filter(Lambda1<T, Boolean> newFilter) { return $arg2(newFilter); }
}
/**
* The <code>NVL</code> function.
* <p>