Skip to content

Commit

Permalink
[INLONG-10833][SDK] Transform SQL supports parsing of Mod methods and…
Browse files Browse the repository at this point in the history
… % expressions (apache#10853)
  • Loading branch information
Zkplo authored and PeterZh6 committed Aug 27, 2024
1 parent fbc4fd4 commit ef5b99f
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.util.List;

/**
* ModuloFunction
* description: MOD(NUMERIC1, NUMERIC2) : Return the remainder of numeric1 divided by numeric2.
*/
public class ModuloFunction implements ValueParser {

private ValueParser dividendParser;
private ValueParser divisorParser;

public ModuloFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
dividendParser = OperatorTools.buildParser(expressions.get(0));
divisorParser = OperatorTools.buildParser(expressions.get(1));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object dividendObj = dividendParser.parse(sourceData, rowIndex, context);
Object divisorObj = divisorParser.parse(sourceData, rowIndex, context);
BigDecimal dividend = OperatorTools.parseBigDecimal(dividendObj);
BigDecimal divisor = OperatorTools.parseBigDecimal(divisorObj);
return dividend.remainder(divisor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.inlong.sdk.transform.process.function.Log10Function;
import org.apache.inlong.sdk.transform.process.function.Log2Function;
import org.apache.inlong.sdk.transform.process.function.LogFunction;
import org.apache.inlong.sdk.transform.process.function.ModuloFunction;
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
import org.apache.inlong.sdk.transform.process.function.ReplicateFunction;
Expand All @@ -53,24 +54,30 @@
import org.apache.inlong.sdk.transform.process.parser.ColumnParser;
import org.apache.inlong.sdk.transform.process.parser.DateParser;
import org.apache.inlong.sdk.transform.process.parser.DivisionParser;
import org.apache.inlong.sdk.transform.process.parser.DoubleParser;
import org.apache.inlong.sdk.transform.process.parser.LongParser;
import org.apache.inlong.sdk.transform.process.parser.ModuloParser;
import org.apache.inlong.sdk.transform.process.parser.MultiplicationParser;
import org.apache.inlong.sdk.transform.process.parser.ParenthesisParser;
import org.apache.inlong.sdk.transform.process.parser.SignParser;
import org.apache.inlong.sdk.transform.process.parser.StringParser;
import org.apache.inlong.sdk.transform.process.parser.SubtractionParser;
import org.apache.inlong.sdk.transform.process.parser.TimestampParser;
import org.apache.inlong.sdk.transform.process.parser.ValueParser;

import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NotExpression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.SignedExpression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.Division;
import net.sf.jsqlparser.expression.operators.arithmetic.Modulo;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
Expand All @@ -92,7 +99,7 @@

/**
* OperatorTools
*
*
*/
public class OperatorTools {

Expand Down Expand Up @@ -144,6 +151,7 @@ public class OperatorTools {
functionMap.put("from_unixtime", FromUnixTimeFunction::new);
functionMap.put("unix_timestamp", UnixTimestampFunction::new);
functionMap.put("to_timestamp", ToTimestampFunction::new);
functionMap.put("mod", ModuloFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("length", LengthFunction::new);
}
Expand Down Expand Up @@ -180,6 +188,10 @@ public static ValueParser buildParser(Expression expr) {
return new StringParser((StringValue) expr);
} else if (expr instanceof LongValue) {
return new LongParser((LongValue) expr);
} else if (expr instanceof DoubleValue) {
return new DoubleParser((DoubleValue) expr);
} else if (expr instanceof SignedExpression) {
return new SignParser((SignedExpression) expr);
} else if (expr instanceof Parenthesis) {
return new ParenthesisParser((Parenthesis) expr);
} else if (expr instanceof Addition) {
Expand All @@ -190,6 +202,8 @@ public static ValueParser buildParser(Expression expr) {
return new MultiplicationParser((Multiplication) expr);
} else if (expr instanceof Division) {
return new DivisionParser((Division) expr);
} else if (expr instanceof Modulo) {
return new ModuloParser((Modulo) expr);
} else if (expr instanceof DateValue) {
return new DateParser((DateValue) expr);
} else if (expr instanceof TimestampValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.parser;

import org.apache.inlong.sdk.transform.decode.SourceData;
import org.apache.inlong.sdk.transform.process.Context;

import net.sf.jsqlparser.expression.DoubleValue;

/**
* LongParser
*/
public class DoubleParser implements ValueParser {

private final Double value;

public DoubleParser(DoubleValue expr) {
this.value = expr.getValue();
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.parser;

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 net.sf.jsqlparser.expression.operators.arithmetic.Modulo;

import java.math.BigDecimal;

/**
* ModuloParser
* description: analyze the % expression
*/
public class ModuloParser implements ValueParser {

private ValueParser left;

private ValueParser right;

public ModuloParser(Modulo expr) {
this.left = OperatorTools.buildParser(expr.getLeftExpression());
this.right = OperatorTools.buildParser(expr.getRightExpression());
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object leftObj = this.left.parse(sourceData, rowIndex, context);
Object rightObj = this.right.parse(sourceData, rowIndex, context);
BigDecimal leftValue = OperatorTools.parseBigDecimal(leftObj);
BigDecimal rightValue = OperatorTools.parseBigDecimal(rightObj);
return leftValue.remainder(rightValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.parser;

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 net.sf.jsqlparser.expression.SignedExpression;

import java.math.BigDecimal;

/**
* SignParser
*
*/
public class SignParser implements ValueParser {

private final Integer sign;
private final ValueParser number;

public SignParser(SignedExpression expr) {
sign = expr.getSign() == '-' ? -1 : 1;
number = OperatorTools.buildParser(expr.getExpression());
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object numberObject = number.parse(sourceData, rowIndex, context);
BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObject);
return numberValue.multiply(new BigDecimal(sign));
}
}
Loading

0 comments on commit ef5b99f

Please sign in to comment.