From 2346a6b77d1f4f54b7719540a7a502cd5f108cde Mon Sep 17 00:00:00 2001 From: James Xu Date: Thu, 4 May 2017 22:15:02 +0800 Subject: [PATCH 1/6] implement the arithmetic operators --- .../BeamSqlArithmeticExpression.java | 113 +++++++ .../arithmetic/BeamSqlDivideExpression.java | 40 +++ .../arithmetic/BeamSqlMinusExpression.java | 40 +++ .../arithmetic/BeamSqlModExpression.java | 40 +++ .../arithmetic/BeamSqlMultiplyExpression.java | 40 +++ .../arithmetic/BeamSqlPlusExpression.java | 40 +++ .../operator/arithmetic/package-info.java | 22 ++ .../BeamSqlArithmeticExpressionTest.java | 286 ++++++++++++++++++ 8 files changed, 621 insertions(+) create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlDivideExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMinusExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlModExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMultiplyExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlPlusExpression.java create mode 100644 dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/package-info.java create mode 100644 dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java new file mode 100644 index 000000000000..918e88939d43 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java @@ -0,0 +1,113 @@ +/* + * 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.arithmetic; + +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 arithmetic operators. + */ +public abstract class BeamSqlArithmeticExpression extends BeamSqlExpression { + private BeamSqlArithmeticExpression(List operands, SqlTypeName outputType) { + super(operands, outputType); + } + + public BeamSqlArithmeticExpression(List operands) { + // the outputType can not be determined in constructor + // will be determined in evaluate() method. ANY here is just a placeholder. + super(operands, SqlTypeName.ANY); + } + + @Override public boolean accept() { + if (operands.size() != 2) { + return false; + } + + for (BeamSqlExpression operand : operands) { + // arithmetic operators support both numeric and string types. + if (!SqlTypeName.NUMERIC_TYPES.contains(operand.getOutputType()) + && !(SqlTypeName.VARCHAR == operand.getOutputType())) { + return false; + } + } + return true; + } + + /** + * https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html. + * @param inputRecord + * @return + */ + @Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { + BeamSqlExpression leftOp = operands.get(0); + BeamSqlExpression rightOp = operands.get(1); + + // In the case of -, +, and *, the result is calculated as Long if both + // operands are INT_TYPES(short, integer, long). + if (SqlTypeName.INT_TYPES.contains(leftOp.getOutputType()) + && SqlTypeName.INT_TYPES.contains(rightOp.getOutputType())) { + Long leftValue = Long.valueOf(leftOp.evaluate(inputRecord).getValue().toString()); + Long rightValue = Long.valueOf(rightOp.evaluate(inputRecord).getValue().toString()); + Long ret = calc(leftValue, rightValue); + return BeamSqlPrimitive.of(SqlTypeName.BIGINT, ret); + } else { + // If any of the operands of a +, -, /, *, % is a real or string value + // OR + // It is a division calculation + // we treat them as Double + double leftValue = getDouble(inputRecord, leftOp); + double rightValue = getDouble(inputRecord, rightOp); + return BeamSqlPrimitive.of(SqlTypeName.DOUBLE, calc(leftValue, rightValue)); + } + } + + private double getDouble(BeamSQLRow inputRecord, BeamSqlExpression op) { + Object raw = op.evaluate(inputRecord).getValue(); + Double ret = null; + if (SqlTypeName.NUMERIC_TYPES.contains(op.getOutputType())) { + ret = ((Number) raw).doubleValue(); + } else if (op.getOutputType() == SqlTypeName.VARCHAR) { + ret = Double.valueOf(raw.toString()); + } + + return ret; + } + + /** + * For {@link SqlTypeName#INT_TYPES} calculation of '+', '-', '*'. + * @param left + * @param right + * @return + */ + public abstract Long calc(Long left, Long right); + + + /** + * For other {@link SqlTypeName#NUMERIC_TYPES} & VARCHAR calculation of '+', '-', '*', '/'. + * @param left + * @param right + * @return + */ + public abstract Double calc(Number left, Number right); +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlDivideExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlDivideExpression.java new file mode 100644 index 000000000000..c23f54c724a1 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlDivideExpression.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.arithmetic; + +import java.util.List; + +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; + +/** + * '/' operator. + */ +public class BeamSqlDivideExpression extends BeamSqlArithmeticExpression { + public BeamSqlDivideExpression(List operands) { + super(operands); + } + + @Override public Long calc(Long left, Long right) { + return left / right; + } + + @Override public Double calc(Number left, Number right) { + return left.doubleValue() / right.doubleValue(); + } +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMinusExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMinusExpression.java new file mode 100644 index 000000000000..c6d7ca04d9a2 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMinusExpression.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.arithmetic; + +import java.util.List; + +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; + +/** + * '-' operator. + */ +public class BeamSqlMinusExpression extends BeamSqlArithmeticExpression { + public BeamSqlMinusExpression(List operands) { + super(operands); + } + + @Override public Long calc(Long left, Long right) { + return left - right; + } + + @Override public Double calc(Number left, Number right) { + return left.doubleValue() - right.doubleValue(); + } +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlModExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlModExpression.java new file mode 100644 index 000000000000..6323e95c2448 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlModExpression.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.arithmetic; + +import java.util.List; + +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; + +/** + * '%' operator. + */ +public class BeamSqlModExpression extends BeamSqlArithmeticExpression { + public BeamSqlModExpression(List operands) { + super(operands); + } + + @Override public Long calc(Long left, Long right) { + return left % right; + } + + @Override public Double calc(Number left, Number right) { + return left.doubleValue() % right.doubleValue(); + } +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMultiplyExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMultiplyExpression.java new file mode 100644 index 000000000000..42ba4a5cbdd8 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlMultiplyExpression.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.arithmetic; + +import java.util.List; + +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; + +/** + * '*' operator. + */ +public class BeamSqlMultiplyExpression extends BeamSqlArithmeticExpression { + public BeamSqlMultiplyExpression(List operands) { + super(operands); + } + + @Override public Long calc(Long left, Long right) { + return left * right; + } + + @Override public Double calc(Number left, Number right) { + return left.doubleValue() * right.doubleValue(); + } +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlPlusExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlPlusExpression.java new file mode 100644 index 000000000000..59be0532bfce --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlPlusExpression.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.arithmetic; + +import java.util.List; + +import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; + +/** + * '+' operator. + */ +public class BeamSqlPlusExpression extends BeamSqlArithmeticExpression { + public BeamSqlPlusExpression(List operands) { + super(operands); + } + + @Override public Double calc(Number left, Number right) { + return left.doubleValue() + right.doubleValue(); + } + + @Override public Long calc(Long left, Long right) { + return left + right; + } +} diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/package-info.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/package-info.java new file mode 100644 index 000000000000..b8f2175a1169 --- /dev/null +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/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. + */ + +/** + * Arithmetic operators. + */ +package org.apache.beam.dsls.sql.interpreter.operator.arithmetic; diff --git a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java new file mode 100644 index 000000000000..690ca50a5226 --- /dev/null +++ b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java @@ -0,0 +1,286 @@ +/* + * 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.arithmetic; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +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.Test; + +/** + * Tests for {@code BeamSqlArithmeticExpression}. + */ +public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { + + @Test public void testAccept_normal() { + List operands = new ArrayList<>(); + + // byte, short + operands.add(BeamSqlPrimitive.of(SqlTypeName.TINYINT, Byte.valueOf("1"))); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.MAX_VALUE)); + assertTrue(new BeamSqlPlusExpression(operands).accept()); + + // integer, long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertTrue(new BeamSqlPlusExpression(operands).accept()); + + // float, double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 1.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 1.1)); + assertTrue(new BeamSqlPlusExpression(operands).accept()); + + // varchar + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 1.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1")); + assertTrue(new BeamSqlPlusExpression(operands).accept()); + } + + @Test public void testAccept_exception() { + List operands = new ArrayList<>(); + + // more than 2 operands + operands.add(BeamSqlPrimitive.of(SqlTypeName.TINYINT, Byte.valueOf("1"))); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.MAX_VALUE)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.MAX_VALUE)); + assertFalse(new BeamSqlPlusExpression(operands).accept()); + + // boolean + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.TINYINT, Byte.valueOf("1"))); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BOOLEAN, true)); + assertFalse(new BeamSqlPlusExpression(operands).accept()); + } + + @Test public void testPlus() { + List operands = new ArrayList<>(); + + // integer + integer => long + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(2L, new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + + // integer + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + + // long + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + + // long + string => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); + assertEquals(Double.valueOf(2L + 1.1), + new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + + // float + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 1.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(Double.valueOf(Double.valueOf(1.1F) + 1), + new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + + // double + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 1.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2.1, new BeamSqlPlusExpression(operands).evaluate(record).getValue()); + } + + @Test public void testMinus() { + List operands = new ArrayList<>(); + + // integer + integer => long + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(1L, new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + + // integer + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(1L, new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + + // long + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(1L, new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + + // long + string => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); + assertEquals(Double.valueOf(2L - 1.1), + new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + + // float + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(Double.valueOf(Double.valueOf(2.1F) - 1), + new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + + // double + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(1.1, new BeamSqlMinusExpression(operands).evaluate(record).getValue()); + } + + @Test public void testMultiply() { + List operands = new ArrayList<>(); + + // integer + integer => long + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(2L, new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + + // integer + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + + // long + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + + // long + string => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); + assertEquals(Double.valueOf(2L * 1.1), + new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + + // float + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(Double.valueOf(Double.valueOf(2.1F) * 1), + new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + + // double + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2.1, new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); + } + + @Test public void testDivide() { + List operands = new ArrayList<>(); + + // integer + integer => long + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(2L, new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + + // integer + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + + // long + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2L, new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + + // long + string => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); + assertEquals(Double.valueOf(2L / 1.1), + new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + + // float + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(Double.valueOf(Double.valueOf(2.1F) / 1), + new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + + // double + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + assertEquals(2.1, new BeamSqlDivideExpression(operands).evaluate(record).getValue()); + } + + @Test public void testMod() { + List operands = new ArrayList<>(); + + // integer + integer => long + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + assertEquals(1L, new BeamSqlModExpression(operands).evaluate(record).getValue()); + + // integer + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + assertEquals(1L, new BeamSqlModExpression(operands).evaluate(record).getValue()); + + // long + long => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 3L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + assertEquals(1L, new BeamSqlModExpression(operands).evaluate(record).getValue()); + + // long + string => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); + assertEquals(Double.valueOf(2L % 1.1), + new BeamSqlModExpression(operands).evaluate(record).getValue()); + + // float + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 3.1F)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + assertEquals(Double.valueOf(Double.valueOf(3.1F) % 2), + new BeamSqlModExpression(operands).evaluate(record).getValue()); + + // double + long => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 3.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + assertEquals(1.1, new BeamSqlModExpression(operands).evaluate(record).getValue()); + } +} From f52216fa79671a7456ae998660d2f9c96ee20ee4 Mon Sep 17 00:00:00 2001 From: James Xu Date: Fri, 5 May 2017 15:37:08 +0800 Subject: [PATCH 2/6] fix javadoc --- .../operator/arithmetic/BeamSqlArithmeticExpression.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java index 918e88939d43..8ab0190fb339 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java @@ -56,8 +56,6 @@ public BeamSqlArithmeticExpression(List operands) { /** * https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html. - * @param inputRecord - * @return */ @Override public BeamSqlPrimitive evaluate(BeamSQLRow inputRecord) { BeamSqlExpression leftOp = operands.get(0); @@ -96,18 +94,12 @@ private double getDouble(BeamSQLRow inputRecord, BeamSqlExpression op) { /** * For {@link SqlTypeName#INT_TYPES} calculation of '+', '-', '*'. - * @param left - * @param right - * @return */ public abstract Long calc(Long left, Long right); /** * For other {@link SqlTypeName#NUMERIC_TYPES} & VARCHAR calculation of '+', '-', '*', '/'. - * @param left - * @param right - * @return */ public abstract Double calc(Number left, Number right); } From 18acf37496894e4ce010ef87cd11d4e9d7cc63ef Mon Sep 17 00:00:00 2001 From: James Xu Date: Fri, 5 May 2017 16:39:35 +0800 Subject: [PATCH 3/6] minor --- .../operator/arithmetic/BeamSqlArithmeticExpression.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java index 8ab0190fb339..c4a389502083 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java @@ -99,7 +99,7 @@ private double getDouble(BeamSQLRow inputRecord, BeamSqlExpression op) { /** - * For other {@link SqlTypeName#NUMERIC_TYPES} & VARCHAR calculation of '+', '-', '*', '/'. + * For other {@link SqlTypeName#NUMERIC_TYPES} and VARCHAR calculation of '+', '-', '*', '/'. */ public abstract Double calc(Number left, Number right); } From 19b84082ff356a62093839ef332efa2d8e3c5dc1 Mon Sep 17 00:00:00 2001 From: James Xu Date: Fri, 5 May 2017 17:41:34 +0800 Subject: [PATCH 4/6] register arithmetic operator expressions into BeamSQLFnExecutor --- .../sql/interpreter/BeamSQLFnExecutor.java | 21 ++++++- .../interpreter/BeamSQLFnExecutorTest.java | 55 +++++++++++++++---- 2 files changed, 63 insertions(+), 13 deletions(-) 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 32e2ffcb50d4..78663f8e7ec6 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 @@ -33,6 +33,11 @@ import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlNotEqualExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlOrExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlDivideExpression; +import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlMinusExpression; +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.rel.BeamFilterRel; import org.apache.beam.dsls.sql.rel.BeamProjectRel; import org.apache.beam.dsls.sql.rel.BeamRelNode; @@ -74,7 +79,7 @@ public BeamSQLFnExecutor(BeamRelNode relNode) { * {@link #buildExpression(RexNode)} visits the operands of {@link RexNode} recursively, * and represent each {@link SqlOperator} with a corresponding {@link BeamSqlExpression}. */ - private BeamSqlExpression buildExpression(RexNode rexNode) { + static BeamSqlExpression buildExpression(RexNode rexNode) { if (rexNode instanceof RexLiteral) { RexLiteral node = (RexLiteral) rexNode; return BeamSqlPrimitive.of(node.getTypeName(), node.getValue()); @@ -107,12 +112,24 @@ private BeamSqlExpression buildExpression(RexNode rexNode) { case "<=": return new BeamSqlLessThanEqualExpression(subExps); + // arithmetic operators + case "+": + return new BeamSqlPlusExpression(subExps); + case "-": + return new BeamSqlMinusExpression(subExps); + case "*": + return new BeamSqlMultiplyExpression(subExps); + case "/": + return new BeamSqlDivideExpression(subExps); + case "MOD": + return new BeamSqlModExpression(subExps); + case "IS NULL": return new BeamSqlIsNullExpression(subExps.get(0)); case "IS NOT NULL": return new BeamSqlIsNotNullExpression(subExps.get(0)); default: - throw new BeamSqlUnsupportedException(); + throw new BeamSqlUnsupportedException("Operator: " + opName + " not supported yet!"); } } else { throw new BeamSqlUnsupportedException( diff --git a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutorTest.java b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutorTest.java index abbe3f790711..8df0865395b1 100644 --- a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutorTest.java +++ b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/BeamSQLFnExecutorTest.java @@ -17,19 +17,28 @@ */ package org.apache.beam.dsls.sql.interpreter; +import static org.junit.Assert.assertTrue; + import java.math.BigDecimal; import java.util.Arrays; + import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlAndExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlEqualExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlInputRefExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlLessThanEqualExpression; import org.apache.beam.dsls.sql.interpreter.operator.BeamSqlPrimitive; +import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlDivideExpression; +import org.apache.beam.dsls.sql.interpreter.operator.arithmetic.BeamSqlMinusExpression; +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.rel.BeamFilterRel; import org.apache.beam.dsls.sql.rel.BeamProjectRel; import org.apache.beam.dsls.sql.rel.BeamRelNode; import org.apache.calcite.plan.RelTraitSet; import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.type.SqlTypeName; import org.junit.Assert; @@ -60,27 +69,27 @@ public void testBeamFilterRel() { Assert.assertEquals(1, executor.exps.size()); BeamSqlExpression l1Exp = executor.exps.get(0); - Assert.assertTrue(l1Exp instanceof BeamSqlAndExpression); + assertTrue(l1Exp instanceof BeamSqlAndExpression); Assert.assertEquals(SqlTypeName.BOOLEAN, l1Exp.getOutputType()); Assert.assertEquals(2, l1Exp.getOperands().size()); BeamSqlExpression l1Left = (BeamSqlExpression) l1Exp.getOperands().get(0); BeamSqlExpression l1Right = (BeamSqlExpression) l1Exp.getOperands().get(1); - Assert.assertTrue(l1Left instanceof BeamSqlLessThanEqualExpression); - Assert.assertTrue(l1Right instanceof BeamSqlEqualExpression); + assertTrue(l1Left instanceof BeamSqlLessThanEqualExpression); + assertTrue(l1Right instanceof BeamSqlEqualExpression); Assert.assertEquals(2, l1Left.getOperands().size()); BeamSqlExpression l1LeftLeft = (BeamSqlExpression) l1Left.getOperands().get(0); BeamSqlExpression l1LeftRight = (BeamSqlExpression) l1Left.getOperands().get(1); - Assert.assertTrue(l1LeftLeft instanceof BeamSqlInputRefExpression); - Assert.assertTrue(l1LeftRight instanceof BeamSqlPrimitive); + assertTrue(l1LeftLeft instanceof BeamSqlInputRefExpression); + assertTrue(l1LeftRight instanceof BeamSqlPrimitive); Assert.assertEquals(2, l1Right.getOperands().size()); BeamSqlExpression l1RightLeft = (BeamSqlExpression) l1Right.getOperands().get(0); BeamSqlExpression l1RightRight = (BeamSqlExpression) l1Right.getOperands().get(1); - Assert.assertTrue(l1RightLeft instanceof BeamSqlInputRefExpression); - Assert.assertTrue(l1RightRight instanceof BeamSqlPrimitive); + assertTrue(l1RightLeft instanceof BeamSqlInputRefExpression); + assertTrue(l1RightRight instanceof BeamSqlPrimitive); } @Test @@ -92,10 +101,34 @@ public void testBeamProjectRel() { executor.prepare(); Assert.assertEquals(4, executor.exps.size()); - Assert.assertTrue(executor.exps.get(0) instanceof BeamSqlInputRefExpression); - Assert.assertTrue(executor.exps.get(1) instanceof BeamSqlInputRefExpression); - Assert.assertTrue(executor.exps.get(2) instanceof BeamSqlInputRefExpression); - Assert.assertTrue(executor.exps.get(3) instanceof BeamSqlInputRefExpression); + assertTrue(executor.exps.get(0) instanceof BeamSqlInputRefExpression); + assertTrue(executor.exps.get(1) instanceof BeamSqlInputRefExpression); + assertTrue(executor.exps.get(2) instanceof BeamSqlInputRefExpression); + assertTrue(executor.exps.get(3) instanceof BeamSqlInputRefExpression); } + + @Test + public void testBuildExpression_arithmetic() { + testBuildArithmeticExpression(SqlStdOperatorTable.PLUS, BeamSqlPlusExpression.class); + testBuildArithmeticExpression(SqlStdOperatorTable.MINUS, BeamSqlMinusExpression.class); + testBuildArithmeticExpression(SqlStdOperatorTable.MULTIPLY, BeamSqlMultiplyExpression.class); + testBuildArithmeticExpression(SqlStdOperatorTable.DIVIDE, BeamSqlDivideExpression.class); + testBuildArithmeticExpression(SqlStdOperatorTable.MOD, BeamSqlModExpression.class); + } + + private void testBuildArithmeticExpression(SqlOperator fn, + Class clazz) { + RexNode rexNode; + BeamSqlExpression exp; + rexNode = rexBuilder.makeCall(fn, + Arrays.asList( + rexBuilder.makeBigintLiteral(new BigDecimal(1L)), + rexBuilder.makeBigintLiteral(new BigDecimal(1L)) + ) + ); + exp = BeamSQLFnExecutor.buildExpression(rexNode); + + assertTrue(exp.getClass().equals(clazz)); + } } From a30a4be54258857eeb4b99b5d797996330cd348f Mon Sep 17 00:00:00 2001 From: James Xu Date: Sat, 6 May 2017 19:51:09 +0800 Subject: [PATCH 5/6] fix comment --- .../operator/arithmetic/BeamSqlArithmeticExpression.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java index c4a389502083..ed8a642959db 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java @@ -62,7 +62,7 @@ public BeamSqlArithmeticExpression(List operands) { BeamSqlExpression rightOp = operands.get(1); // In the case of -, +, and *, the result is calculated as Long if both - // operands are INT_TYPES(short, integer, long). + // operands are INT_TYPES(byte, short, integer, long). if (SqlTypeName.INT_TYPES.contains(leftOp.getOutputType()) && SqlTypeName.INT_TYPES.contains(rightOp.getOutputType())) { Long leftValue = Long.valueOf(leftOp.evaluate(inputRecord).getValue().toString()); From 9e4d0148327fce1811ef4fc89a91ba1e4bf2d705 Mon Sep 17 00:00:00 2001 From: James Xu Date: Sun, 7 May 2017 16:00:50 +0800 Subject: [PATCH 6/6] arithmetic operators do not support string --- .../BeamSqlArithmeticExpression.java | 10 ++--- .../BeamSqlArithmeticExpressionTest.java | 37 +------------------ 2 files changed, 4 insertions(+), 43 deletions(-) diff --git a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java index ed8a642959db..5e1d068ff34c 100644 --- a/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java +++ b/dsls/sql/src/main/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpression.java @@ -45,9 +45,7 @@ public BeamSqlArithmeticExpression(List operands) { } for (BeamSqlExpression operand : operands) { - // arithmetic operators support both numeric and string types. - if (!SqlTypeName.NUMERIC_TYPES.contains(operand.getOutputType()) - && !(SqlTypeName.VARCHAR == operand.getOutputType())) { + if (!SqlTypeName.NUMERIC_TYPES.contains(operand.getOutputType())) { return false; } } @@ -70,7 +68,7 @@ public BeamSqlArithmeticExpression(List operands) { Long ret = calc(leftValue, rightValue); return BeamSqlPrimitive.of(SqlTypeName.BIGINT, ret); } else { - // If any of the operands of a +, -, /, *, % is a real or string value + // If any of the operands of a +, -, /, *, % is a real // OR // It is a division calculation // we treat them as Double @@ -85,8 +83,6 @@ private double getDouble(BeamSQLRow inputRecord, BeamSqlExpression op) { Double ret = null; if (SqlTypeName.NUMERIC_TYPES.contains(op.getOutputType())) { ret = ((Number) raw).doubleValue(); - } else if (op.getOutputType() == SqlTypeName.VARCHAR) { - ret = Double.valueOf(raw.toString()); } return ret; @@ -99,7 +95,7 @@ private double getDouble(BeamSQLRow inputRecord, BeamSqlExpression op) { /** - * For other {@link SqlTypeName#NUMERIC_TYPES} and VARCHAR calculation of '+', '-', '*', '/'. + * For other {@link SqlTypeName#NUMERIC_TYPES} of '+', '-', '*', '/'. */ public abstract Double calc(Number left, Number right); } diff --git a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java index 690ca50a5226..abebf1727531 100644 --- a/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java +++ b/dsls/sql/src/test/java/org/apache/beam/dsls/sql/interpreter/operator/arithmetic/BeamSqlArithmeticExpressionTest.java @@ -60,7 +60,7 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 1.1F)); operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1")); - assertTrue(new BeamSqlPlusExpression(operands).accept()); + assertFalse(new BeamSqlPlusExpression(operands).accept()); } @Test public void testAccept_exception() { @@ -99,13 +99,6 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); assertEquals(2L, new BeamSqlPlusExpression(operands).evaluate(record).getValue()); - // long + string => double - operands.clear(); - operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); - operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); - assertEquals(Double.valueOf(2L + 1.1), - new BeamSqlPlusExpression(operands).evaluate(record).getValue()); - // float + long => double operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 1.1F)); @@ -140,13 +133,6 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); assertEquals(1L, new BeamSqlMinusExpression(operands).evaluate(record).getValue()); - // long + string => double - operands.clear(); - operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); - operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); - assertEquals(Double.valueOf(2L - 1.1), - new BeamSqlMinusExpression(operands).evaluate(record).getValue()); - // float + long => double operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); @@ -181,13 +167,6 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); assertEquals(2L, new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); - // long + string => double - operands.clear(); - operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); - operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); - assertEquals(Double.valueOf(2L * 1.1), - new BeamSqlMultiplyExpression(operands).evaluate(record).getValue()); - // float + long => double operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); @@ -222,13 +201,6 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); assertEquals(2L, new BeamSqlDivideExpression(operands).evaluate(record).getValue()); - // long + string => double - operands.clear(); - operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); - operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); - assertEquals(Double.valueOf(2L / 1.1), - new BeamSqlDivideExpression(operands).evaluate(record).getValue()); - // float + long => double operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 2.1F)); @@ -263,13 +235,6 @@ public class BeamSqlArithmeticExpressionTest extends BeamSQLFnExecutorTestBase { operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); assertEquals(1L, new BeamSqlModExpression(operands).evaluate(record).getValue()); - // long + string => double - operands.clear(); - operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); - operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "1.1")); - assertEquals(Double.valueOf(2L % 1.1), - new BeamSqlModExpression(operands).evaluate(record).getValue()); - // float + long => double operands.clear(); operands.add(BeamSqlPrimitive.of(SqlTypeName.FLOAT, 3.1F));