From 5536b15e8db491954d54c5f3ce3ba9eb520786af Mon Sep 17 00:00:00 2001 From: tarushapptech Date: Fri, 26 May 2017 15:15:26 +0530 Subject: [PATCH 1/3] commiting changes for abs and sqrt math function --- .../sql/interpreter/BeamSQLFnExecutor.java | 7 ++ .../operator/BeamSqlExpression.java | 4 ++ .../operator/math/BeamSqlAbsExpression.java | 72 +++++++++++++++++++ .../math/BeamSqlMathUnaryExpression.java | 58 +++++++++++++++ .../operator/math/BeamSqlSqrtExpression.java | 40 +++++++++++ .../math/BeamSqlMathUnaryExpressionTest.java | 70 ++++++++++++++++++ 6 files changed, 251 insertions(+) create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlAbsExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlSqrtExpression.java create mode 100644 dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.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 9dcf003be423..51fe2c9b99ee 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.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 @@ static BeamSqlExpression buildExpression(RexNode rexNode) { 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); 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 54289e6b47fd..811e21b072d4 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 List getOperands() { public SqlTypeName getOutputType() { return outputType; } + + public int numberOfOperands() { + return operands.size(); + } } 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 000000000000..305e553fe40c --- /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 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; + +import java.math.BigDecimal; +import java.util.List; + +/** + * {@code BeamSqlMathUnaryExpression} for 'ABS' function. + */ +public class BeamSqlAbsExpression extends BeamSqlMathUnaryExpression { + + public BeamSqlAbsExpression(List 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; + } +} 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 000000000000..fb4cb79473c0 --- /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 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; + +import java.util.List; + +/** + * 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 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 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); +} 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 000000000000..ef3f175371eb --- /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 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 java.util.List; + +/** + * {@code BeamSqlMathUnaryExpression} for 'SQRT' function. + */ +public class BeamSqlSqrtExpression extends BeamSqlMathUnaryExpression { + + public BeamSqlSqrtExpression(List operands) { + super(operands); + } + + @Override public BeamSqlPrimitive calculate(BeamSqlPrimitive op) { + return BeamSqlPrimitive + .of(SqlTypeName.DOUBLE, Math.sqrt(Double.valueOf(op.getValue().toString()))); + } +} 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 000000000000..3d7b33c8be5b --- /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 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; + +import java.util.ArrayList; +import java.util.List; + +/** + * Test for {@link BeamSqlMathUnaryExpression}. + */ +public class BeamSqlMathUnaryExpressionTest extends BeamSQLFnExecutorTestBase { + + @Test public void testForGreaterThanOneOperands() { + List 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 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 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()); + } + +} From f8f7cbf61df96fa1a07907fbab58ab29e10813c6 Mon Sep 17 00:00:00 2001 From: tarushapptech Date: Fri, 26 May 2017 15:35:18 +0530 Subject: [PATCH 2/3] adding package info to math function package --- .../operator/math/package-info.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 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 000000000000..a7a5d0e3d173 --- /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; From 6e5ad90878ff69064ee8cfd1f624b1e927bdcb00 Mon Sep 17 00:00:00 2001 From: tarushapptech Date: Fri, 26 May 2017 15:41:40 +0530 Subject: [PATCH 3/3] commiting checkstyle issues --- .../sql/interpreter/operator/math/BeamSqlAbsExpression.java | 4 ++-- .../interpreter/operator/math/BeamSqlMathUnaryExpression.java | 2 +- .../sql/interpreter/operator/math/BeamSqlSqrtExpression.java | 2 +- .../operator/math/BeamSqlMathUnaryExpressionTest.java | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) 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 index 305e553fe40c..2c6e6b45373b 100644 --- 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 @@ -18,13 +18,13 @@ 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; -import java.math.BigDecimal; -import java.util.List; /** * {@code BeamSqlMathUnaryExpression} for 'ABS' function. 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 index fb4cb79473c0..e34d4e49627e 100644 --- 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 @@ -18,12 +18,12 @@ 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; -import java.util.List; /** * Base class for all unary functions such as 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 index ef3f175371eb..e87ba2cac74c 100644 --- 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 @@ -18,11 +18,11 @@ 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; -import java.util.List; /** * {@code BeamSqlMathUnaryExpression} for 'SQRT' function. 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 index 3d7b33c8be5b..c5753d326c5e 100644 --- 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 @@ -18,6 +18,8 @@ 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; @@ -25,8 +27,6 @@ import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; -import java.util.List; /** * Test for {@link BeamSqlMathUnaryExpression}.