diff --git a/jOOQ/src/main/java/org/jooq/impl/ArrayFilter.java b/jOOQ/src/main/java/org/jooq/impl/ArrayFilter.java index 1dd85342d5..edca0293cd 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ArrayFilter.java +++ b/jOOQ/src/main/java/org/jooq/impl/ArrayFilter.java @@ -104,6 +104,9 @@ implements case DUCKDB: return true; + case CLICKHOUSE: + return false; + default: return true; } @@ -135,6 +138,10 @@ implements ctx.visit(function(N_ARRAY_FILTER, getDataType(), array, DSL.field("{0}", OTHER, filter))); break; + case CLICKHOUSE: + ctx.visit(DSL.function(N_arrayFilter, getDataType(), DSL.field("{0}", OTHER, filter), array)); + break; + default: ctx.visit(function(N_ARRAY_FILTER, getDataType(), array, DSL.field("{0}", OTHER, filter))); break; diff --git a/jOOQ/src/main/java/org/jooq/impl/ArrayMap.java b/jOOQ/src/main/java/org/jooq/impl/ArrayMap.java new file mode 100644 index 0000000000..b099d499ca --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ArrayMap.java @@ -0,0 +1,214 @@ +/* + * 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.tools.*; + +import java.util.*; +import java.util.function.*; +import java.util.stream.*; + + +/** + * The ARRAY MAP statement. + */ +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class ArrayMap +extends + AbstractField +implements + QOM.ArrayMap +{ + + final Field array; + final Lambda1 mapper; + + ArrayMap( + Field array, + Lambda1 mapper + ) { + super( + N_ARRAY_MAP, + allNotNull((DataType) dataType(((DataType) OTHER).array(), array, false), array) + ); + + this.array = nullSafeNotNull(array, ((DataType) OTHER).array()); + this.mapper = mapper; + } + + // ------------------------------------------------------------------------- + // 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; + + case CLICKHOUSE: + return false; + + case TRINO: + 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(mapper.$result()), when(array.isNotNull(), DSL.cast(array(), getDataType())))) + .from(unnest(array).as(N_T, mapper.$arg1().getUnqualifiedName())) + )); + break; + + case DUCKDB: + ctx.visit(function(N_ARRAY_TRANSFORM, getDataType(), array, DSL.field("{0}", OTHER, mapper))); + break; + + case CLICKHOUSE: + ctx.visit(DSL.function(N_arrayMap, getDataType(), DSL.field("{0}", OTHER, mapper), array)); + break; + + case TRINO: + ctx.visit(function(N_TRANSFORM, getDataType(), array, DSL.field("{0}", OTHER, mapper))); + break; + + default: + ctx.visit(function(N_ARRAY_MAP, getDataType(), array, DSL.field("{0}", OTHER, mapper))); + break; + } + } + + + + + + + + + + + + + + + // ------------------------------------------------------------------------- + // XXX: Query Object Model + // ------------------------------------------------------------------------- + + @Override + public final Field $arg1() { + return array; + } + + @Override + public final Lambda1 $arg2() { + return mapper; + } + + @Override + public final QOM.ArrayMap $arg1(Field newValue) { + return $constructor().apply(newValue, $arg2()); + } + + @Override + public final QOM.ArrayMap $arg2(Lambda1 newValue) { + return $constructor().apply($arg1(), newValue); + } + + @Override + public final Function2, ? super Lambda1, ? extends QOM.ArrayMap> $constructor() { + return (a1, a2) -> new ArrayMap<>(a1, a2); + } + + // ------------------------------------------------------------------------- + // XXX: The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof QOM.ArrayMap o) { + return + StringUtils.equals($array(), o.$array()) && + StringUtils.equals($mapper(), o.$mapper()) + ; + } + else + return super.equals(that); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 002ba605d5..bc15fc9afd 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -22464,6 +22464,50 @@ public class DSL { return new ArrayFilter<>(array, filter); } + /** + * The ARRAY_MAP function. + *

+ * Filter elements out of an array. + */ + @NotNull + @Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB }) + public static Field arrayMap(T[] array, Function1, ? extends Field> mapper) { + return new ArrayMap<>(Tools.field(array), DSL.lambda(array, mapper)); + } + + /** + * The ARRAY_MAP function. + *

+ * Filter elements out of an array. + */ + @NotNull + @Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB }) + public static Field arrayMap(T[] array, Lambda1 mapper) { + return new ArrayMap<>(Tools.field(array), mapper); + } + + /** + * The ARRAY_MAP function. + *

+ * Filter elements out of an array. + */ + @NotNull + @Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB }) + public static Field arrayMap(Field array, Function1, ? extends Field> mapper) { + return new ArrayMap<>(array, DSL.lambda(array, mapper)); + } + + /** + * The ARRAY_MAP function. + *

+ * Filter elements out of an array. + */ + @NotNull + @Support({ CLICKHOUSE, DUCKDB, H2, HSQLDB, POSTGRES, TRINO, YUGABYTEDB }) + public static Field arrayMap(Field array, Lambda1 mapper) { + return new ArrayMap<>(array, mapper); + } + // ------------------------------------------------------------------------- // Utility functions // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 32e74f6c2c..b9e27860d6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -338,6 +338,7 @@ final class Names { static final Name N_ARRAY_GET = systemName("array_get"); static final Name N_ARRAY_INTERSECT = systemName("array_intersect"); static final Name N_ARRAY_LENGTH = systemName("array_length"); + static final Name N_ARRAY_MAP = systemName("array_map"); static final Name N_ARRAY_OVERLAP = systemName("array_overlap"); static final Name N_ARRAY_PREPEND = systemName("array_prepend"); static final Name N_ARRAY_REMOVE = systemName("array_remove"); @@ -649,6 +650,8 @@ final class Names { static final Name N_XMLSERIALIZE_CONTENT = systemName("xmlserialize_content"); static final Name N_XOR = systemName("xor"); static final Name N_arrayConcat = systemName("arrayConcat"); + static final Name N_arrayFilter = systemName("arrayFilter"); + static final Name N_arrayMap = systemName("arrayMap"); static final Name N_arrayPushBack = systemName("arrayPushBack"); static final Name N_arrayPushFront = systemName("arrayPushFront"); static final Name N_bitAnd = systemName("bitAnd"); diff --git a/jOOQ/src/main/java/org/jooq/impl/QOM.java b/jOOQ/src/main/java/org/jooq/impl/QOM.java index d87c4a356d..d87f4d8261 100644 --- a/jOOQ/src/main/java/org/jooq/impl/QOM.java +++ b/jOOQ/src/main/java/org/jooq/impl/QOM.java @@ -6352,6 +6352,26 @@ public final class QOM { @NotNull default ArrayFilter $filter(Lambda1 newFilter) { return $arg2(newFilter); } } + /** + * The ARRAY MAP function. + *

+ * Filter elements out of an array. + */ + public /*sealed*/ interface ArrayMap + extends + UOperator2, Lambda1, ArrayMap>, + org.jooq.Field + //permits + // ArrayMap + { + @NotNull default Field $array() { return $arg1(); } + @CheckReturnValue + @NotNull default ArrayMap $array(Field newArray) { return $arg1(newArray); } + @NotNull default Lambda1 $mapper() { return $arg2(); } + @CheckReturnValue + @NotNull default ArrayMap $mapper(Lambda1 newMapper) { return $arg2(newMapper); } + } + /** * The NVL function. *