public Object computeForValue()

in seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLFunction.java [213:367]


    public Object computeForValue(Expression expression, Object[] inputFields) {
        if (expression instanceof NullValue) {
            return null;
        }
        if (expression instanceof TrimFunction) {
            TrimFunction function = (TrimFunction) expression;
            Column column = (Column) function.getExpression();
            List<Object> functionArgs = new ArrayList<>();
            if (column != null) {
                functionArgs.add(computeForValue(column, inputFields));
                if (function.getFromExpression() != null) {
                    functionArgs.add(((StringValue) function.getFromExpression()).getValue());
                }
            }
            return executeFunctionExpr(TRIM, functionArgs);
        }
        if (expression instanceof SignedExpression) {
            SignedExpression signedExpression = (SignedExpression) expression;
            if (signedExpression.getSign() == '-') {
                Object value = computeForValue(signedExpression.getExpression(), inputFields);
                if (value instanceof Integer) {
                    return -((Integer) value);
                }
                if (value instanceof Long) {
                    return -((Long) value);
                }
                if (value instanceof Double) {
                    return -((Double) value);
                }
                if (value instanceof Number) {
                    return -((Number) value).doubleValue();
                }
            } else {
                return computeForValue(signedExpression, inputFields);
            }
        }
        if (expression instanceof DoubleValue) {
            return ((DoubleValue) expression).getValue();
        }
        if (expression instanceof LongValue) {
            long longVal = ((LongValue) expression).getValue();
            if (longVal <= Integer.MAX_VALUE && longVal >= Integer.MIN_VALUE) {
                return (int) longVal;
            } else {
                return longVal;
            }
        }
        if (expression instanceof StringValue) {
            return ((StringValue) expression).getValue();
        }
        if (expression instanceof Column) {
            Column columnExp = (Column) expression;
            String columnName = columnExp.getColumnName();
            int index = inputRowType.indexOf(columnName, false);
            if (index == -1
                    && columnName.startsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)
                    && columnName.endsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)) {
                columnName = columnName.substring(1, columnName.length() - 1);
                index = inputRowType.indexOf(columnName, false);
            }
            if (index == -1
                    && ("true".equalsIgnoreCase(columnName)
                            || "false".equalsIgnoreCase(columnName))) {
                return Boolean.parseBoolean(columnName);
            }

            if (index != -1) {
                return inputFields[index];
            } else {
                String fullyQualifiedName = columnExp.getFullyQualifiedName();
                String[] columnNames = fullyQualifiedName.split("\\.");
                int deep = columnNames.length;
                SeaTunnelDataType parDataType = inputRowType;
                SeaTunnelRow parRowValues = new SeaTunnelRow(inputFields);
                Object res = parRowValues;
                for (int i = 0; i < deep; i++) {
                    String key = columnNames[i];
                    if (parDataType instanceof MapType) {
                        Map<String, Object> mapValue = ((Map) res);
                        if (mapValue.containsKey(key)) {
                            return mapValue.get(key);
                        } else if (key.startsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)
                                && key.endsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)) {
                            key = key.substring(1, key.length() - 1);
                            return mapValue.get(key);
                        }
                        return null;
                    }
                    parRowValues = (SeaTunnelRow) res;
                    int idx = ((SeaTunnelRowType) parDataType).indexOf(key, false);
                    if (idx == -1
                            && key.startsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)
                            && key.endsWith(ZetaSQLEngine.ESCAPE_IDENTIFIER)) {
                        key = key.substring(1, key.length() - 1);
                        idx = ((SeaTunnelRowType) parDataType).indexOf(key, false);
                    }
                    if (idx == -1) {
                        throw new IllegalArgumentException(
                                String.format("can't find field [%s]", fullyQualifiedName));
                    }
                    parDataType = ((SeaTunnelRowType) parDataType).getFieldType(idx);
                    res = parRowValues.getFields()[idx];
                }
                return res;
            }
        }
        if (expression instanceof Function) {
            Function function = (Function) expression;
            ExpressionList<Expression> expressionList =
                    (ExpressionList<Expression>) function.getParameters();
            List<Object> functionArgs = new ArrayList<>();
            if (expressionList != null) {
                for (Expression funcArgExpression : expressionList.getExpressions()) {
                    functionArgs.add(computeForValue(funcArgExpression, inputFields));
                }
            }
            return executeFunctionExpr(function.getName(), functionArgs);
        }
        if (expression instanceof TimeKeyExpression) {
            return executeTimeKeyExpr(((TimeKeyExpression) expression).getStringValue());
        }
        if (expression instanceof ExtractExpression) {
            ExtractExpression extract = (ExtractExpression) expression;
            List<Object> functionArgs = new ArrayList<>();
            functionArgs.add(computeForValue(extract.getExpression(), inputFields));
            functionArgs.add(extract.getName());
            return executeFunctionExpr(ZetaSQLFunction.EXTRACT, functionArgs);
        }
        if (expression instanceof Parenthesis) {
            Parenthesis parenthesis = (Parenthesis) expression;
            return computeForValue(parenthesis.getExpression(), inputFields);
        }
        // bytes not supported at the moment,use BINARY instead.
        if (expression instanceof CaseExpression) {
            CaseExpression caseExpression = (CaseExpression) expression;
            final Object value = executeCaseExpr(caseExpression, inputFields);
            SeaTunnelDataType<?> type = zetaSQLType.getExpressionType(expression);
            return SystemFunction.castAs(value, type);
        }
        if (expression instanceof BinaryExpression) {
            return executeBinaryExpr((BinaryExpression) expression, inputFields);
        }
        if (expression instanceof CastExpression) {
            CastExpression castExpression = (CastExpression) expression;
            Expression leftExpr = castExpression.getLeftExpression();
            Object leftValue = computeForValue(leftExpr, inputFields);
            if (castExpression.keyword.equalsIgnoreCase(TRY_CAST)) {
                return executeTryCastExpr(castExpression, leftValue);
            }
            return executeCastExpr(castExpression, leftValue);
        }
        throw new TransformException(
                CommonErrorCodeDeprecated.UNSUPPORTED_OPERATION,
                String.format("Unsupported SQL Expression: %s ", expression.toString()));
    }