forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-10833][SDK] Transform SQL supports parsing of Mod methods and…
… % expressions (apache#10853)
- Loading branch information
Showing
6 changed files
with
355 additions
and
1 deletion.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...rm-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ModuloFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...nsform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/parser/DoubleParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...nsform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/parser/ModuloParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ransform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/parser/SignParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.