Skip to content

Commit

Permalink
Merge branch 'INLONG-10882' of https://github.com/MOONSakura0614/inlong
Browse files Browse the repository at this point in the history
… into INLONG-10882
  • Loading branch information
MOONSakura0614 committed Sep 10, 2024
2 parents 033b1bf + 5455346 commit eccee1d
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.apache.inlong.sdk.transform.process.function;

import org.apache.inlong.sdk.transform.decode.SourceData;
import org.apache.inlong.sdk.transform.process.Context;
import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
import org.apache.inlong.sdk.transform.process.parser.ValueParser;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;

import java.util.List;

/**
* ASCIIFunction
* description: ASCII(string) -- Returns the numeric value of the first character of string. Returns NULL if string is NULL.
*/
@TransformFunction(names = {"ascii"})
public class AsciiFunction implements ValueParser {

private final ValueParser stringParser;

/**
* Constructor
* @param expr
*/
public AsciiFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
stringParser = OperatorTools.buildParser(expressions.get(0));
}

/**
* parse
* @param sourceData
* @param rowIndex
* @return
*/
@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null) {
return null;
}
String str = OperatorTools.parseString(stringObj);
if (str == null || str.isEmpty()) {
return null;
}
return (int) str.charAt(0);
}
}

0 comments on commit eccee1d

Please sign in to comment.