|
17 | 17 | package org.apache.seatunnel.transform.sql.zeta.functions;
|
18 | 18 |
|
19 | 19 | import org.apache.seatunnel.api.table.type.ArrayType;
|
| 20 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
20 | 21 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
|
| 22 | +import org.apache.seatunnel.common.exception.CommonErrorCode; |
21 | 23 | import org.apache.seatunnel.common.utils.SeaTunnelException;
|
| 24 | +import org.apache.seatunnel.transform.exception.TransformException; |
22 | 25 |
|
23 | 26 | import net.sf.jsqlparser.expression.DoubleValue;
|
24 | 27 | import net.sf.jsqlparser.expression.Expression;
|
|
30 | 33 | import net.sf.jsqlparser.schema.Column;
|
31 | 34 |
|
32 | 35 | import java.util.ArrayList;
|
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Comparator; |
33 | 38 | import java.util.List;
|
34 | 39 |
|
35 | 40 | public class ArrayFunction {
|
36 | 41 |
|
| 42 | + public static Object arrayMax(List<Object> args) { |
| 43 | + if (args == null || args.isEmpty()) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + Object[] dataList = (Object[]) args.get(0); |
| 47 | + if (dataList == null || dataList.length == 0) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + if (dataList[0] instanceof String) { |
| 51 | + return Arrays.stream(dataList) |
| 52 | + .map(String.class::cast) |
| 53 | + .max(String::compareTo) |
| 54 | + .orElse(null); |
| 55 | + } else if (dataList[0] instanceof Number) { |
| 56 | + return Arrays.stream(dataList) |
| 57 | + .map(Number.class::cast) |
| 58 | + .max(Comparator.comparingDouble(Number::doubleValue)) |
| 59 | + .orElse(null); |
| 60 | + } |
| 61 | + throw new TransformException( |
| 62 | + CommonErrorCode.UNSUPPORTED_DATA_TYPE, |
| 63 | + String.format("Unsupported function max() arguments: %s", args)); |
| 64 | + } |
| 65 | + |
| 66 | + public static Object arrayMin(List<Object> args) { |
| 67 | + if (args == null || args.isEmpty()) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + Object[] dataList = (Object[]) args.get(0); |
| 71 | + if (dataList == null || dataList.length == 0) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + if (dataList[0] instanceof String) { |
| 75 | + return Arrays.stream(dataList) |
| 76 | + .map(String.class::cast) |
| 77 | + .min(String::compareTo) |
| 78 | + .orElse(null); |
| 79 | + } else if (dataList[0] instanceof Number) { |
| 80 | + return Arrays.stream(dataList) |
| 81 | + .map(Number.class::cast) |
| 82 | + .min(Comparator.comparingDouble(Number::doubleValue)) |
| 83 | + .orElse(null); |
| 84 | + } |
| 85 | + throw new TransformException( |
| 86 | + CommonErrorCode.UNSUPPORTED_DATA_TYPE, |
| 87 | + String.format("Unsupported function max() arguments: %s", args)); |
| 88 | + } |
| 89 | + |
37 | 90 | public static Object[] array(List<Object> args) {
|
38 | 91 | if (args == null || args.isEmpty()) {
|
39 | 92 | return new Object[0];
|
@@ -150,6 +203,14 @@ private static Class<?> getDataClassType(List<Object> args) {
|
150 | 203 | return arrayType == null ? String.class : arrayType;
|
151 | 204 | }
|
152 | 205 |
|
| 206 | + public static SeaTunnelDataType<?> getElementType( |
| 207 | + Function function, SeaTunnelRowType inputRowType) { |
| 208 | + String columnName = function.getParameters().getExpressions().get(0).toString(); |
| 209 | + int columnIndex = inputRowType.indexOf(columnName); |
| 210 | + ArrayType arrayType = (ArrayType) inputRowType.getFieldType(columnIndex); |
| 211 | + return arrayType.getElementType(); |
| 212 | + } |
| 213 | + |
153 | 214 | private static List<Class<?>> getFunctionArgs(
|
154 | 215 | Function function, SeaTunnelRowType inputRowType) {
|
155 | 216 | ExpressionList<Expression> expressionList =
|
|
0 commit comments