Repository: beam Updated Branches: refs/heads/DSL_SQL 9395fbb3c -> fcc80ce84
[BEAM-2329] Add ABS and SQRT math functions Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/5ace3467 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/5ace3467 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/5ace3467 Branch: refs/heads/DSL_SQL Commit: 5ace3467fd6ea5a1336bb492fdd32f12abb8b269 Parents: 9395fbb Author: tarushapptech <[email protected]> Authored: Fri May 26 15:15:26 2017 +0530 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Tue Jun 6 10:38:20 2017 +0200 ---------------------------------------------------------------------- .../dsls/sql/interpreter/BeamSQLFnExecutor.java | 7 ++ .../interpreter/operator/BeamSqlExpression.java | 4 ++ .../operator/math/BeamSqlAbsExpression.java | 72 ++++++++++++++++++++ .../math/BeamSqlMathUnaryExpression.java | 58 ++++++++++++++++ .../operator/math/BeamSqlSqrtExpression.java | 40 +++++++++++ .../interpreter/operator/math/package-info.java | 22 ++++++ .../math/BeamSqlMathUnaryExpressionTest.java | 70 +++++++++++++++++++ 7 files changed, 273 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutor.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutor.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutor.java index 9dcf003..51fe2c9 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutor.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutor.java @@ -44,6 +44,8 @@ import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlMinusExpr import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlModExpression; import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlMultiplyExpression; import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlPlusExpression; +import org.apache.beam.dsls.sql.interpreter.operator.math.BeamSqlAbsExpression; +import org.apache.beam.dsls.sql.interpreter.operator.math.BeamSqlSqrtExpression; import org.apache.beam.dsls.sql.interpreter.operator.string.BeamSqlCharLengthExpression; import org.apache.beam.dsls.sql.interpreter.operator.string.BeamSqlConcatExpression; import org.apache.beam.dsls.sql.interpreter.operator.string.BeamSqlInitCapExpression; @@ -150,6 +152,11 @@ public class BeamSQLFnExecutor implements BeamSQLExpressionExecutor { case "MOD": return new BeamSqlModExpression(subExps); + case "ABS": + return new BeamSqlAbsExpression(subExps); + case "SQRT": + return new BeamSqlSqrtExpression(subExps); + // string operators case "||": return new BeamSqlConcatExpression(subExps); http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/BeamSqlExpression.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/BeamSqlExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/BeamSqlExpression.java index 54289e6..811e21b 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/BeamSqlExpression.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/BeamSqlExpression.java @@ -71,4 +71,8 @@ public abstract class BeamSqlExpression implements Serializable{ public SqlTypeName getOutputType() { return outputType; } + + public int numberOfOperands() { + return operands.size(); + } } http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlAbsExpression.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlAbsExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlAbsExpression.java new file mode 100644 index 0000000..2c6e6b4 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlAbsExpression.java @@ -0,0 +1,72 @@ +/* + * 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.beam.dsls.sql.interpreter.operator.math; + +import java.math.BigDecimal; +import java.util.List; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.runtime.SqlFunctions; +import org.apache.calcite.sql.type.SqlTypeName; + + +/** + * {@code BeamSqlMathUnaryExpression} for 'ABS' function. + */ +public class BeamSqlAbsExpression extends BeamSqlMathUnaryExpression { + + public BeamSqlAbsExpression(List<BeamSqlExpression> operands) { + super(operands); + } + + @Override public BeamSqlPrimitive calculate(BeamSqlPrimitive op) { + BeamSqlPrimitive result = null; + switch (op.getOutputType()) { + case INTEGER: + result = BeamSqlPrimitive + .of(SqlTypeName.INTEGER, SqlFunctions.abs(op.getInteger())); + break; + case BIGINT: + result = BeamSqlPrimitive + .of(SqlTypeName.BIGINT, SqlFunctions.abs(op.getLong())); + break; + case TINYINT: + result = BeamSqlPrimitive + .of(SqlTypeName.TINYINT, SqlFunctions.abs(op.getByte())); + break; + case SMALLINT: + result = BeamSqlPrimitive + .of(SqlTypeName.SMALLINT, SqlFunctions.abs(op.getShort())); + break; + case FLOAT: + result = BeamSqlPrimitive + .of(SqlTypeName.FLOAT, SqlFunctions.abs(op.getFloat())); + break; + case DECIMAL: + result = BeamSqlPrimitive + .of(SqlTypeName.DECIMAL, SqlFunctions.abs(new BigDecimal(op.getValue().toString()))); + break; + case DOUBLE: + result = BeamSqlPrimitive + .of(SqlTypeName.DOUBLE, SqlFunctions.abs(op.getDouble())); + break; + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpression.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpression.java new file mode 100644 index 0000000..e34d4e4 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpression.java @@ -0,0 +1,58 @@ +/* + * 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.beam.dsls.sql.interpreter.operator.math; + +import java.util.List; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.beam.dsls.sql.schema.BeamSQLRow; +import org.apache.calcite.sql.type.SqlTypeName; + + +/** + * Base class for all unary functions such as + * ABS, SQRT, LN, LOG10, EXP, CEIL, FLOOR, RAND, ACOS, + * ASIN, ATAN, COS, COT, DEGREES, RADIANS, SIGN, SIN, TAN. + */ +public abstract class BeamSqlMathUnaryExpression extends BeamSqlExpression { + + public BeamSqlMathUnaryExpression(List<BeamSqlExpression> operands) { + super(operands, SqlTypeName.ANY); + } + + @Override public boolean accept() { + boolean acceptance = false; + + if (numberOfOperands() == 1 && SqlTypeName.NUMERIC_TYPES.contains(opType(0))) { + acceptance = true; + } + return acceptance; + } + + @Override public BeamSqlPrimitive<? extends Number> evaluate(BeamSQLRow inputRecord) { + BeamSqlExpression operand = op(0); + return calculate(operand.evaluate(inputRecord)); + } + + /** + * For the operands of other type {@link SqlTypeName#NUMERIC_TYPES}. + * */ + + public abstract BeamSqlPrimitive calculate(BeamSqlPrimitive op); +} http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlSqrtExpression.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlSqrtExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlSqrtExpression.java new file mode 100644 index 0000000..e87ba2c --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlSqrtExpression.java @@ -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.beam.dsls.sql.interpreter.operator.math; + +import java.util.List; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; + + +/** + * {@code BeamSqlMathUnaryExpression} for 'SQRT' function. + */ +public class BeamSqlSqrtExpression extends BeamSqlMathUnaryExpression { + + public BeamSqlSqrtExpression(List<BeamSqlExpression> operands) { + super(operands); + } + + @Override public BeamSqlPrimitive calculate(BeamSqlPrimitive op) { + return BeamSqlPrimitive + .of(SqlTypeName.DOUBLE, Math.sqrt(Double.valueOf(op.getValue().toString()))); + } +} http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/package-info.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/package-info.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/package-info.java new file mode 100644 index 0000000..a7a5d0e --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * MATH functions/operators. + */ +package org.apache.beam.dsls.sql.interpreter.operator.math; http://git-wip-us.apache.org/repos/asf/beam/blob/5ace3467/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java ---------------------------------------------------------------------- diff --git a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java new file mode 100644 index 0000000..c5753d3 --- /dev/null +++ b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java @@ -0,0 +1,70 @@ +/* + * 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.beam.dsls.sql.interpreter.operator.math; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.dsls.sql.interpreter.BeamSQLFnExecutorTestBase; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + + +/** + * Test for {@link BeamSqlMathUnaryExpression}. + */ +public class BeamSqlMathUnaryExpressionTest extends BeamSQLFnExecutorTestBase { + + @Test public void testForGreaterThanOneOperands() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // operands more than 1 not allowed + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 4)); + Assert.assertFalse(new BeamSqlAbsExpression(operands).accept()); + Assert.assertFalse(new BeamSqlSqrtExpression(operands).accept()); + } + + @Test public void testForOperandsType() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // varchar operand not allowed + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "2")); + Assert.assertFalse(new BeamSqlAbsExpression(operands).accept()); + Assert.assertFalse(new BeamSqlSqrtExpression(operands).accept()); + } + + @Test public void testForUnaryExpressions() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for sqrt function + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals(1.4142135623730951, + new BeamSqlSqrtExpression(operands).evaluate(record).getValue()); + + // test for abs function + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, -28965734597L)); + Assert + .assertEquals(28965734597L, new BeamSqlAbsExpression(operands).evaluate(record).getValue()); + } + +}
