Skip to content

Commit

Permalink
[INLONG-10883][SDK] Transform SQL support InitCap function
Browse files Browse the repository at this point in the history
  • Loading branch information
MOONSakura0614 committed Sep 3, 2024
1 parent e394d74 commit d24df0b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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;

/**
* InitCapFunction
* description: initcap(string)--returns a new form of STRING with the first character of each word converted to
* uppercase and the rest characters to lowercase
*/
@TransformFunction(names = {"initcap, init_cap"})
public class InitCapFunction implements ValueParser {

private final ValueParser stringParser;

public InitCapFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
stringParser = OperatorTools.buildParser(expressions.get(0));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
String str = OperatorTools.parseString(stringParser.parse(sourceData, rowIndex, context));
if (str == null) {
return null;
}
StringBuilder result = new StringBuilder();
boolean capitalizeNext = true;
for (char c : str.toCharArray()) {
if (Character.isLetterOrDigit(c)) {
if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(c));
}
} else {
capitalizeNext = true;
result.append(c);
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,22 @@ public void testInsertFunction() throws Exception {
Assert.assertEquals("result=1278", output5.get(0));
}

@Test
public void testInitCapFunction() throws Exception {
String transformSql = "select initcap(string1) from source";
TransformConfig config = new TransformConfig(transformSql);
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case1: initcap('hello world')
List<String> output1 = processor.transform("hello world|banana|cloud|2|1|0", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=Hello World");

// case2: initcap('apache inLong')
List<String> output2 = processor.transform("apache inLong|banana|cloud|2|1|0", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=Apache Inlong");
}
}

0 comments on commit d24df0b

Please sign in to comment.