http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/date/BeamSqlExtractExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/date/BeamSqlExtractExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/date/BeamSqlExtractExpressionTest.java new file mode 100644 index 0000000..0ca7e3e --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/date/BeamSqlExtractExpressionTest.java @@ -0,0 +1,103 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.date; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.avatica.util.TimeUnitRange; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test for {@code BeamSqlExtractExpression}. + */ +public class BeamSqlExtractExpressionTest extends BeamSqlDateExpressionTestBase { + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + long time = str2LongTime("2017-05-22 16:17:18"); + + // YEAR + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.YEAR)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(2017L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // MONTH + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.MONTH)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(5L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // DAY + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.DAY)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(22L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // DAY_OF_WEEK + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.DOW)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(2L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // DAY_OF_YEAR + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.DOY)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(142L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // WEEK + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.WEEK)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(21L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + // QUARTER + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, TimeUnitRange.QUARTER)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, + time)); + assertEquals(2L, + new BeamSqlExtractExpression(operands) + .evaluate(BeamSqlFnExecutorTestBase.record).getValue()); + + } +}
http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/logical/BeamSqlNotExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/logical/BeamSqlNotExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/logical/BeamSqlNotExpressionTest.java new file mode 100644 index 0000000..a437db7 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/logical/BeamSqlNotExpressionTest.java @@ -0,0 +1,47 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.logical; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@code BeamSqlNotExpression}. + */ +public class BeamSqlNotExpressionTest extends BeamSqlFnExecutorTestBase { + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BOOLEAN, false)); + Assert.assertTrue(new BeamSqlNotExpression(operands).evaluate(record).getBoolean()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BOOLEAN, true)); + Assert.assertFalse(new BeamSqlNotExpression(operands).evaluate(record).getBoolean()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BOOLEAN, null)); + Assert.assertNull(new BeamSqlNotExpression(operands).evaluate(record).getValue()); + } +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathBinaryExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathBinaryExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathBinaryExpressionTest.java new file mode 100644 index 0000000..d42164e --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathBinaryExpressionTest.java @@ -0,0 +1,201 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.math; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlInputRefExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.runtime.SqlFunctions; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link BeamSqlMathBinaryExpression}. + */ +public class BeamSqlMathBinaryExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void testForGreaterThanTwoOperands() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // operands more than 2 not allowed + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 4)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 5)); + Assert.assertFalse(new BeamSqlRoundExpression(operands).accept()); + } + + @Test public void testForOneOperand() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // only one operand allowed in round function + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + } + + @Test public void testForOperandsType() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // varchar operand not allowed + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "2")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 4)); + Assert.assertFalse(new BeamSqlRoundExpression(operands).accept()); + } + + @Test public void testRoundFunction() { + // test round functions with operands of type bigint, int, + // tinyint, smallint, double, decimal + List<BeamSqlExpression> operands = new ArrayList<>(); + // round(double, double) => double + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.0)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 4.0)); + Assert.assertEquals(2.0, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + // round(integer,integer) => integer + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + Assert.assertEquals(2, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // round(long,long) => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 5L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 3L)); + Assert.assertEquals(5L, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // round(short) => short + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, new Short("4"))); + Assert.assertEquals(SqlFunctions.toShort(4), + new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // round(long,long) => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + Assert.assertEquals(2L, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // round(double, long) => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 1.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + Assert.assertEquals(1.1, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.368768)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + Assert.assertEquals(2.37, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 3.78683686458)); + Assert.assertEquals(4.0, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 378.683686458)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, -2)); + Assert.assertEquals(400.0, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 378.683686458)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, -1)); + Assert.assertEquals(380.0, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // round(integer, double) => integer + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.2)); + Assert.assertEquals(2, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + + // operand with a BeamSqlInputRefExpression + // to select a column value from row of a record + operands.clear(); + BeamSqlInputRefExpression ref0 = new BeamSqlInputRefExpression(SqlTypeName.BIGINT, 0); + operands.add(ref0); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + + Assert.assertEquals(1234567L, new BeamSqlRoundExpression(operands).evaluate(record).getValue()); + } + + @Test public void testPowerFunction() { + // test power functions with operands of type bigint, int, + // tinyint, smallint, double, decimal + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.0)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 4.0)); + Assert.assertEquals(16.0, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + // power(integer,integer) => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + Assert.assertEquals(4.0, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + // power(integer,long) => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 3L)); + Assert.assertEquals(8.0, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + + // power(long,long) => long + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 2L)); + Assert.assertEquals(4.0, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + + // power(double, int) => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 1.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + Assert.assertEquals(1.1, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + + // power(double, long) => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 1.1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, 1L)); + Assert.assertEquals(1.1, new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + + // power(integer, double) => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.2)); + Assert.assertEquals(Math.pow(2, 2.2), + new BeamSqlPowerExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForTruncate() { + List<BeamSqlExpression> operands = new ArrayList<>(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.0)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 4.0)); + Assert.assertEquals(2.0, new BeamSqlTruncateExpression(operands).evaluate(record).getValue()); + // truncate(double, integer) => double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.80685)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 4)); + Assert.assertEquals(2.8068, + new BeamSqlTruncateExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForAtan2() { + List<BeamSqlExpression> operands = new ArrayList<>(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.875)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.56)); + Assert.assertEquals(Math.atan2(0.875, 0.56), + new BeamSqlAtan2Expression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java new file mode 100644 index 0000000..3f3326b --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/math/BeamSqlMathUnaryExpressionTest.java @@ -0,0 +1,309 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.math; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.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()); + } + + @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()); + } + + @Test public void testForUnaryExpressions() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for sqrt function + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + + // test for abs function + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.BIGINT, -28965734597L)); + Assert + .assertEquals(28965734597L, new BeamSqlAbsExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForLnExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for LN function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals(Math.log(2), new BeamSqlLnExpression(operands).evaluate(record).getValue()); + + // test for LN function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert + .assertEquals(Math.log(2.4), new BeamSqlLnExpression(operands).evaluate(record).getValue()); + // test for LN function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.log(2.56), + new BeamSqlLnExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForLog10Expression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for log10 function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals(Math.log10(2), + new BeamSqlLogExpression(operands).evaluate(record).getValue()); + // test for log10 function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.log10(2.4), + new BeamSqlLogExpression(operands).evaluate(record).getValue()); + // test for log10 function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.log10(2.56), + new BeamSqlLogExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForExpExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert + .assertEquals(Math.exp(2), new BeamSqlExpExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.exp(2.4), + new BeamSqlExpExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.exp(2.56), + new BeamSqlExpExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForAcosExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert + .assertEquals(Double.NaN, new BeamSqlAcosExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.45)); + Assert.assertEquals(Math.acos(0.45), + new BeamSqlAcosExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(-0.367))); + Assert.assertEquals(Math.acos(-0.367), + new BeamSqlAcosExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForAsinExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type double + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.45)); + Assert.assertEquals(Math.asin(0.45), + new BeamSqlAsinExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(-0.367))); + Assert.assertEquals(Math.asin(-0.367), + new BeamSqlAsinExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForAtanExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type double + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.45)); + Assert.assertEquals(Math.atan(0.45), + new BeamSqlAtanExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(-0.367))); + Assert.assertEquals(Math.atan(-0.367), + new BeamSqlAtanExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForCosExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type double + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 0.45)); + Assert.assertEquals(Math.cos(0.45), + new BeamSqlCosExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(-0.367))); + Assert.assertEquals(Math.cos(-0.367), + new BeamSqlCosExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForCotExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type double + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, .45)); + Assert.assertEquals(1.0d / Math.tan(0.45), + new BeamSqlCotExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(-.367))); + Assert.assertEquals(1.0d / Math.tan(-0.367), + new BeamSqlCotExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForDegreesExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals(Math.toDegrees(2), + new BeamSqlDegreesExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.toDegrees(2.4), + new BeamSqlDegreesExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.toDegrees(2.56), + new BeamSqlDegreesExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForRadiansExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals(Math.toRadians(2), + new BeamSqlRadiansExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.toRadians(2.4), + new BeamSqlRadiansExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.toRadians(2.56), + new BeamSqlRadiansExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForSinExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert + .assertEquals(Math.sin(2), new BeamSqlSinExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.sin(2.4), + new BeamSqlSinExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.sin(2.56), + new BeamSqlSinExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForTanExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert + .assertEquals(Math.tan(2), new BeamSqlTanExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(Math.tan(2.4), + new BeamSqlTanExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(Math.tan(2.56), + new BeamSqlTanExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForSignExpression() { + List<BeamSqlExpression> operands = new ArrayList<>(); + + // test for exp function with operand type smallint + operands.add(BeamSqlPrimitive.of(SqlTypeName.SMALLINT, Short.valueOf("2"))); + Assert.assertEquals((short) 1, new BeamSqlSignExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type double + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.4)); + Assert.assertEquals(1.0, new BeamSqlSignExpression(operands).evaluate(record).getValue()); + // test for exp function with operand type decimal + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DECIMAL, BigDecimal.valueOf(2.56))); + Assert.assertEquals(BigDecimal.ONE, + new BeamSqlSignExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForPi() { + Assert.assertEquals(Math.PI, new BeamSqlPiExpression().evaluate(record).getValue()); + } + + @Test public void testForCeil() { + List<BeamSqlExpression> operands = new ArrayList<>(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.68687979)); + Assert.assertEquals(Math.ceil(2.68687979), + new BeamSqlCeilExpression(operands).evaluate(record).getValue()); + } + + @Test public void testForFloor() { + List<BeamSqlExpression> operands = new ArrayList<>(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.DOUBLE, 2.68687979)); + Assert.assertEquals(Math.floor(2.68687979), + new BeamSqlFloorExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlCharLengthExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlCharLengthExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlCharLengthExpressionTest.java new file mode 100644 index 0000000..118097f --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlCharLengthExpressionTest.java @@ -0,0 +1,44 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test for BeamSqlCharLengthExpression. + */ +public class BeamSqlCharLengthExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + assertEquals(5, + new BeamSqlCharLengthExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlConcatExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlConcatExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlConcatExpressionTest.java new file mode 100644 index 0000000..c3f8041 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlConcatExpressionTest.java @@ -0,0 +1,66 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for BeamSqlConcatExpression. + */ +public class BeamSqlConcatExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "world")); + assertTrue(new BeamSqlConcatExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "world")); + assertFalse(new BeamSqlConcatExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "world")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + assertFalse(new BeamSqlConcatExpression(operands).accept()); + } + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, " world")); + Assert.assertEquals("hello world", + new BeamSqlConcatExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlInitCapExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlInitCapExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlInitCapExpressionTest.java new file mode 100644 index 0000000..24f9945 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlInitCapExpressionTest.java @@ -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.beam.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test of BeamSqlInitCapExpression. + */ +public class BeamSqlInitCapExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello world")); + assertEquals("Hello World", + new BeamSqlInitCapExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hEllO wOrld")); + assertEquals("Hello World", + new BeamSqlInitCapExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello world")); + assertEquals("Hello World", + new BeamSqlInitCapExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlLowerExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlLowerExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlLowerExpressionTest.java new file mode 100644 index 0000000..e34fcc0 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlLowerExpressionTest.java @@ -0,0 +1,44 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test of BeamSqlLowerExpression. + */ +public class BeamSqlLowerExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "HELLO")); + assertEquals("hello", + new BeamSqlLowerExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlOverlayExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlOverlayExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlOverlayExpressionTest.java new file mode 100644 index 0000000..09bbdc8 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlOverlayExpressionTest.java @@ -0,0 +1,87 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for BeamSqlOverlayExpression. + */ +public class BeamSqlOverlayExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertTrue(new BeamSqlOverlayExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + assertTrue(new BeamSqlOverlayExpression(operands).accept()); + } + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "w3333333rce")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "resou")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + Assert.assertEquals("w3resou3rce", + new BeamSqlOverlayExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "w3333333rce")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "resou")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 4)); + Assert.assertEquals("w3resou33rce", + new BeamSqlOverlayExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "w3333333rce")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "resou")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 5)); + Assert.assertEquals("w3resou3rce", + new BeamSqlOverlayExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "w3333333rce")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "resou")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 3)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 7)); + Assert.assertEquals("w3resouce", + new BeamSqlOverlayExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlPositionExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlPositionExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlPositionExpressionTest.java new file mode 100644 index 0000000..4c21a71 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlPositionExpressionTest.java @@ -0,0 +1,84 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +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.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test for BeamSqlPositionExpression. + */ +public class BeamSqlPositionExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + assertTrue(new BeamSqlPositionExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertTrue(new BeamSqlPositionExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + assertFalse(new BeamSqlPositionExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertFalse(new BeamSqlPositionExpression(operands).accept()); + } + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + assertEquals(5, new BeamSqlPositionExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "worldhello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(5, new BeamSqlPositionExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "world")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals(-1, new BeamSqlPositionExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlStringUnaryExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlStringUnaryExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlStringUnaryExpressionTest.java new file mode 100644 index 0000000..b999ca1 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlStringUnaryExpressionTest.java @@ -0,0 +1,52 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test for BeamSqlStringUnaryExpression. + */ +public class BeamSqlStringUnaryExpressionTest { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + assertTrue(new BeamSqlCharLengthExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertFalse(new BeamSqlCharLengthExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + assertFalse(new BeamSqlCharLengthExpression(operands).accept()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlSubstringExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlSubstringExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlSubstringExpressionTest.java new file mode 100644 index 0000000..2fb451e --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlSubstringExpressionTest.java @@ -0,0 +1,101 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test for BeamSqlSubstringExpression. + */ +public class BeamSqlSubstringExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertTrue(new BeamSqlSubstringExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + assertTrue(new BeamSqlSubstringExpression(operands).accept()); + } + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + assertEquals("hello", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 2)); + assertEquals("he", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 5)); + assertEquals("hello", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 100)); + assertEquals("hello", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 0)); + assertEquals("", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, 1)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, -1)); + assertEquals("", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.INTEGER, -1)); + assertEquals("o", + new BeamSqlSubstringExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlTrimExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlTrimExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlTrimExpressionTest.java new file mode 100644 index 0000000..6f9c706 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlTrimExpressionTest.java @@ -0,0 +1,103 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +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.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.fun.SqlTrimFunction; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for BeamSqlTrimExpression. + */ +public class BeamSqlTrimExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void accept() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, " hello ")); + assertTrue(new BeamSqlTrimExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, SqlTrimFunction.Flag.BOTH)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "he")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hehe__hehe")); + assertTrue(new BeamSqlTrimExpression(operands).accept()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "he")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hehe__hehe")); + assertFalse(new BeamSqlTrimExpression(operands).accept()); + } + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, SqlTrimFunction.Flag.LEADING)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "he")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hehe__hehe")); + Assert.assertEquals("__hehe", + new BeamSqlTrimExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, SqlTrimFunction.Flag.TRAILING)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "he")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hehe__hehe")); + Assert.assertEquals("hehe__", + new BeamSqlTrimExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.SYMBOL, SqlTrimFunction.Flag.BOTH)); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "he")); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "__")); + Assert.assertEquals("__", + new BeamSqlTrimExpression(operands).evaluate(record).getValue()); + + operands.clear(); + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, " hello ")); + Assert.assertEquals("hello", + new BeamSqlTrimExpression(operands).evaluate(record).getValue()); + } + + @Test public void leadingTrim() throws Exception { + assertEquals("__hehe", + BeamSqlTrimExpression.leadingTrim("hehe__hehe", "he")); + } + + @Test public void trailingTrim() throws Exception { + assertEquals("hehe__", + BeamSqlTrimExpression.trailingTrim("hehe__hehe", "he")); + } + + @Test public void trim() throws Exception { + assertEquals("__", + BeamSqlTrimExpression.leadingTrim( + BeamSqlTrimExpression.trailingTrim("hehe__hehe", "he"), "he" + )); + } +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlUpperExpressionTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlUpperExpressionTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlUpperExpressionTest.java new file mode 100644 index 0000000..e69a3a5 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/interpreter/operator/string/BeamSqlUpperExpressionTest.java @@ -0,0 +1,44 @@ +/* + * 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.sdk.extensions.sql.impl.interpreter.operator.string; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.BeamSqlFnExecutorTestBase; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlExpression; +import org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.BeamSqlPrimitive; +import org.apache.calcite.sql.type.SqlTypeName; +import org.junit.Test; + +/** + * Test of BeamSqlUpperExpression. + */ +public class BeamSqlUpperExpressionTest extends BeamSqlFnExecutorTestBase { + + @Test public void evaluate() throws Exception { + List<BeamSqlExpression> operands = new ArrayList<>(); + + operands.add(BeamSqlPrimitive.of(SqlTypeName.VARCHAR, "hello")); + assertEquals("HELLO", + new BeamSqlUpperExpression(operands).evaluate(record).getValue()); + } + +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamIntersectRelTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamIntersectRelTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamIntersectRelTest.java new file mode 100644 index 0000000..5a3f65d --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamIntersectRelTest.java @@ -0,0 +1,119 @@ +/* + * 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.sdk.extensions.sql.impl.rel; + +import java.sql.Types; +import org.apache.beam.sdk.extensions.sql.BeamSqlCli; +import org.apache.beam.sdk.extensions.sql.BeamSqlEnv; +import org.apache.beam.sdk.extensions.sql.TestUtils; +import org.apache.beam.sdk.extensions.sql.mock.MockedBoundedTable; +import org.apache.beam.sdk.extensions.sql.schema.BeamSqlRow; +import org.apache.beam.sdk.testing.PAssert; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.sdk.values.PCollection; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +/** + * Test for {@code BeamIntersectRel}. + */ +public class BeamIntersectRelTest { + static BeamSqlEnv sqlEnv = new BeamSqlEnv(); + + @Rule + public final TestPipeline pipeline = TestPipeline.create(); + + @BeforeClass + public static void prepare() { + sqlEnv.registerTable("ORDER_DETAILS1", + MockedBoundedTable.of( + Types.BIGINT, "order_id", + Types.INTEGER, "site_id", + Types.DOUBLE, "price" + ).addRows( + 1L, 1, 1.0, + 1L, 1, 1.0, + 2L, 2, 2.0, + 4L, 4, 4.0 + ) + ); + + sqlEnv.registerTable("ORDER_DETAILS2", + MockedBoundedTable.of( + Types.BIGINT, "order_id", + Types.INTEGER, "site_id", + Types.DOUBLE, "price" + ).addRows( + 1L, 1, 1.0, + 2L, 2, 2.0, + 3L, 3, 3.0 + ) + ); + } + + @Test + public void testIntersect() throws Exception { + String sql = ""; + sql += "SELECT order_id, site_id, price " + + "FROM ORDER_DETAILS1 " + + " INTERSECT " + + "SELECT order_id, site_id, price " + + "FROM ORDER_DETAILS2 "; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, sqlEnv); + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.BIGINT, "order_id", + Types.INTEGER, "site_id", + Types.DOUBLE, "price" + ).addRows( + 1L, 1, 1.0, + 2L, 2, 2.0 + ).getRows()); + + pipeline.run().waitUntilFinish(); + } + + @Test + public void testIntersectAll() throws Exception { + String sql = ""; + sql += "SELECT order_id, site_id, price " + + "FROM ORDER_DETAILS1 " + + " INTERSECT ALL " + + "SELECT order_id, site_id, price " + + "FROM ORDER_DETAILS2 "; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, sqlEnv); + PAssert.that(rows).satisfies(new CheckSize(3)); + + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.BIGINT, "order_id", + Types.INTEGER, "site_id", + Types.DOUBLE, "price" + ).addRows( + 1L, 1, 1.0, + 1L, 1, 1.0, + 2L, 2, 2.0 + ).getRows()); + + pipeline.run(); + } +} http://git-wip-us.apache.org/repos/asf/beam/blob/febd044a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamJoinRelBoundedVsBoundedTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamJoinRelBoundedVsBoundedTest.java b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamJoinRelBoundedVsBoundedTest.java new file mode 100644 index 0000000..c4f6350 --- /dev/null +++ b/sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamJoinRelBoundedVsBoundedTest.java @@ -0,0 +1,204 @@ +/* + * 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.sdk.extensions.sql.impl.rel; + +import java.sql.Types; +import org.apache.beam.sdk.extensions.sql.BeamSqlCli; +import org.apache.beam.sdk.extensions.sql.BeamSqlEnv; +import org.apache.beam.sdk.extensions.sql.TestUtils; +import org.apache.beam.sdk.extensions.sql.mock.MockedBoundedTable; +import org.apache.beam.sdk.extensions.sql.schema.BeamSqlRow; +import org.apache.beam.sdk.testing.PAssert; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.sdk.values.PCollection; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +/** + * Bounded + Bounded Test for {@code BeamJoinRel}. + */ +public class BeamJoinRelBoundedVsBoundedTest { + @Rule + public final TestPipeline pipeline = TestPipeline.create(); + private static final BeamSqlEnv beamSqlEnv = new BeamSqlEnv(); + + public static final MockedBoundedTable ORDER_DETAILS1 = + MockedBoundedTable.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price" + ).addRows( + 1, 2, 3, + 2, 3, 3, + 3, 4, 5 + ); + + public static final MockedBoundedTable ORDER_DETAILS2 = + MockedBoundedTable.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price" + ).addRows( + 1, 2, 3, + 2, 3, 3, + 3, 4, 5 + ); + + @BeforeClass + public static void prepare() { + beamSqlEnv.registerTable("ORDER_DETAILS1", ORDER_DETAILS1); + beamSqlEnv.registerTable("ORDER_DETAILS2", ORDER_DETAILS2); + } + + @Test + public void testInnerJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1" + + " JOIN ORDER_DETAILS2 o2" + + " on " + + " o1.order_id=o2.site_id AND o2.price=o1.site_id" + ; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price", + Types.INTEGER, "order_id0", + Types.INTEGER, "site_id0", + Types.INTEGER, "price0" + ).addRows( + 2, 3, 3, 1, 2, 3 + ).getRows()); + pipeline.run(); + } + + @Test + public void testLeftOuterJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1" + + " LEFT OUTER JOIN ORDER_DETAILS2 o2" + + " on " + + " o1.order_id=o2.site_id AND o2.price=o1.site_id" + ; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + pipeline.enableAbandonedNodeEnforcement(false); + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price", + Types.INTEGER, "order_id0", + Types.INTEGER, "site_id0", + Types.INTEGER, "price0" + ).addRows( + 1, 2, 3, null, null, null, + 2, 3, 3, 1, 2, 3, + 3, 4, 5, null, null, null + ).getRows()); + pipeline.run(); + } + + @Test + public void testRightOuterJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1" + + " RIGHT OUTER JOIN ORDER_DETAILS2 o2" + + " on " + + " o1.order_id=o2.site_id AND o2.price=o1.site_id" + ; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price", + Types.INTEGER, "order_id0", + Types.INTEGER, "site_id0", + Types.INTEGER, "price0" + ).addRows( + 2, 3, 3, 1, 2, 3, + null, null, null, 2, 3, 3, + null, null, null, 3, 4, 5 + ).getRows()); + pipeline.run(); + } + + @Test + public void testFullOuterJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1" + + " FULL OUTER JOIN ORDER_DETAILS2 o2" + + " on " + + " o1.order_id=o2.site_id AND o2.price=o1.site_id" + ; + + PCollection<BeamSqlRow> rows = BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + PAssert.that(rows).containsInAnyOrder( + TestUtils.RowsBuilder.of( + Types.INTEGER, "order_id", + Types.INTEGER, "site_id", + Types.INTEGER, "price", + Types.INTEGER, "order_id0", + Types.INTEGER, "site_id0", + Types.INTEGER, "price0" + ).addRows( + 2, 3, 3, 1, 2, 3, + 1, 2, 3, null, null, null, + 3, 4, 5, null, null, null, + null, null, null, 2, 3, 3, + null, null, null, 3, 4, 5 + ).getRows()); + pipeline.run(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testException_nonEqualJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1" + + " JOIN ORDER_DETAILS2 o2" + + " on " + + " o1.order_id>o2.site_id" + ; + + pipeline.enableAbandonedNodeEnforcement(false); + BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + pipeline.run(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testException_crossJoin() throws Exception { + String sql = + "SELECT * " + + "FROM ORDER_DETAILS1 o1, ORDER_DETAILS2 o2"; + + pipeline.enableAbandonedNodeEnforcement(false); + BeamSqlCli.compilePipeline(sql, pipeline, beamSqlEnv); + pipeline.run(); + } +}