Skip to content

Commit

Permalink
[INLONG-11238][SDK] Transform SQL supports FORMAT function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZKpLo committed Oct 2, 2024
1 parent e0d7f8d commit 234afb4
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

/**
* FormatFunction -> FORMAT(X,D)
* description:
* return NULL if X or D is NULL.
* return the result of formatting the number X to "#,###,###.##" format, rounded to D decimal places.
*/
@TransformFunction(names = {"format"})
public class FormatFunction implements ValueParser {

private ValueParser numberParser;
private ValueParser reservedDigitsParser;

public FormatFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
numberParser = OperatorTools.buildParser(expressions.get(0));
reservedDigitsParser = OperatorTools.buildParser(expressions.get(1));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object numberObj = numberParser.parse(sourceData, rowIndex, context);
Object reservedDigitsObj = reservedDigitsParser.parse(sourceData, rowIndex, context);
if (numberObj == null || reservedDigitsObj == null) {
return null;
}
BigDecimal number = OperatorTools.parseBigDecimal(numberObj);
int reservedDigits = OperatorTools.parseBigDecimal(reservedDigitsObj).intValue();
if (reservedDigits < 0) {
reservedDigits = 0;
}
// build format
StringBuilder pattern = new StringBuilder("#,###");
if (reservedDigits > 0) {
pattern.append(".");
for (int i = 0; i < reservedDigits; i++) {
pattern.append("0");
}
}
number = number.setScale(reservedDigits, RoundingMode.HALF_UP);
DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
df.applyPattern(pattern.toString());
return df.format(number);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.string;

import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory;
import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory;
import org.apache.inlong.sdk.transform.pojo.TransformConfig;
import org.apache.inlong.sdk.transform.process.TransformProcessor;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public class TestFormatFunction extends AbstractFunctionStringTestBase {

@Test
public void testFormatFunction() throws Exception {
String transformSql = null, data = null;
TransformConfig config = null;
TransformProcessor<String, String> processor = null;
List<String> output = null;

transformSql = "select format(string1,string2) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: FORMAT(12332.123456, 4)
data = "12332.123456|4|";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=12,332.1235", output.get(0));

// case2: FORMAT(12332.1,4)
data = "12332.1|4|";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=12,332.1000", output.get(0));

// case3: FORMAT(12332.2,0)
data = "12332.1|0|";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=12,332", output.get(0));

// case4: FORMAT(12332.2,-3)
data = "12332.1|-3|";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=12,332", output.get(0));
}
}

0 comments on commit 234afb4

Please sign in to comment.