[jOOQ/jOOQ#10175] Add support for CARDINALITY() function

This commit is contained in:
Lukas Eder 2020-06-22 11:03:00 +02:00
parent 0f2cadebb4
commit 5b881d717c
4 changed files with 106 additions and 2 deletions

View File

@ -0,0 +1,78 @@
/*
* 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.Names.N_ARRAY_LENGTH;
import static org.jooq.impl.Names.N_CARDINALITY;
import static org.jooq.impl.SQLDataType.INTEGER;
import org.jooq.Context;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
final class Cardinality extends AbstractField<Integer> {
/**
* Generated UID
*/
private static final long serialVersionUID = 2503480426872240632L;
private final Field<?> arg;
Cardinality(Field<? extends Object[]> arg) {
super(N_CARDINALITY, INTEGER);
this.arg = arg;
}
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
default:
ctx.visit(N_CARDINALITY).sql('(').visit(arg).sql(')');
break;
}
}
}

View File

@ -21117,6 +21117,15 @@ public class DSL {
return new Array<>(fields);
}
/**
* Calculate the cardinality of an array field.
*/
@NotNull
@Support({ H2, HSQLDB, POSTGRES })
public static Field<Integer> cardinality(Field<? extends Object[]> field) {
return new Cardinality(field);
}
/**
* Get the max value over a field: max(field).
*/

View File

@ -59,6 +59,7 @@ final class Names {
static final Name N_AGE = unquotedName("age");
static final Name N_ARRAY = unquotedName("array");
static final Name N_ARRAY_AGG = unquotedName("array_agg");
static final Name N_ARRAY_LENGTH = unquotedName("array_length");
static final Name N_ASC = unquotedName("asc");
static final Name N_ASCII = unquotedName("ascii");
static final Name N_ASCII_VAL = unquotedName("ascii_val");
@ -68,6 +69,7 @@ final class Names {
static final Name N_BIT_COUNT = unquotedName("bit_count");
static final Name N_BOOL_AND = unquotedName("bool_and");
static final Name N_BOOL_OR = unquotedName("bool_or");
static final Name N_CARDINALITY = unquotedName("cardinality");
static final Name N_CASE = unquotedName("case");
static final Name N_CAST = unquotedName("cast");
static final Name N_CEIL = unquotedName("ceil");

View File

@ -77,6 +77,7 @@ import static org.jooq.impl.DSL.bitOr;
import static org.jooq.impl.DSL.bitXNor;
import static org.jooq.impl.DSL.bitXor;
import static org.jooq.impl.DSL.boolOr;
import static org.jooq.impl.DSL.cardinality;
import static org.jooq.impl.DSL.cast;
import static org.jooq.impl.DSL.catalog;
import static org.jooq.impl.DSL.ceil;
@ -6481,6 +6482,7 @@ final class ParserImpl implements Parser {
private static final FieldOrRow parseTerm(ParserContext ctx, Type type) {
FieldOrRow field;
Field<?> arg;
Object value;
switch (ctx.characterUpper()) {
@ -6551,6 +6553,8 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseFieldCharLengthIf(ctx)) != null)
return field;
else if ((field = parseFieldCardinalityIf(ctx)) != null)
return field;
else if (parseFunctionNameIf(ctx, "CEILING") || parseFunctionNameIf(ctx, "CEIL"))
return ceil((Field) parseFieldNumericOpParenthesised(ctx));
else if (parseFunctionNameIf(ctx, "COSH"))
@ -7715,6 +7719,17 @@ final class ParserImpl implements Parser {
return jsonEntry(key, value);
}
private static final Field<Integer> parseFieldCardinalityIf(ParserContext ctx) {
if (parseKeywordIf(ctx, "CARDINALITY")) {
parse(ctx, '(');
Field<Object[]> f = (Field<Object[]>) parseField(ctx, A);
parse(ctx, ')');
return cardinality(f);
}
return null;
}
private static final Field<?> parseArrayValueConstructorIf(ParserContext ctx) {
if (parseKeywordIf(ctx, "ARRAY")) {
parse(ctx, '[');
@ -11690,8 +11705,8 @@ final class ParserImpl implements Parser {
return true;
}
private static final boolean parseFunctionNameIf(ParserContext ctx, String string) {
return peekKeyword(ctx, string, true, false, true);
private static final boolean parseFunctionNameIf(ParserContext ctx, String name) {
return peekKeyword(ctx, name, true, false, true);
}
private static final boolean parseOperator(ParserContext ctx, String operator) {