You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ka...@apache.org on 2019/03/14 21:44:51 UTC
[phoenix] branch 4.x-HBase-1.3 updated: PHOENIX-5185 support Math
PI function
This is an automated email from the ASF dual-hosted git repository.
karanmehta93 pushed a commit to branch 4.x-HBase-1.3
in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.x-HBase-1.3 by this push:
new 3821a15 PHOENIX-5185 support Math PI function
3821a15 is described below
commit 3821a157dd408136b46267dac743c239c4cccbdd
Author: Xinyi Yan <He...@berkeley.edu>
AuthorDate: Thu Mar 14 14:44:47 2019 -0700
PHOENIX-5185 support Math PI function
---
.../phoenix/end2end/LnLogFunctionEnd2EndIT.java | 16 ------
.../phoenix/end2end/MathPIFunctionEnd2EndIT.java | 61 ++++++++++++++++++++
.../phoenix/end2end/PowerFunctionEnd2EndIT.java | 16 ------
.../apache/phoenix/expression/ExpressionType.java | 1 +
.../expression/function/MathPIFunction.java | 65 ++++++++++++++++++++++
.../apache/phoenix/expression/ExpFunctionTest.java | 19 +------
.../phoenix/expression/LnLogFunctionTest.java | 23 ++------
.../phoenix/expression/MathPIFunctionTest.java | 44 +++++++++++++++
.../phoenix/expression/PowerFunctionTest.java | 22 ++------
.../phoenix/expression/SqrtFunctionTest.java | 20 +------
.../java/org/apache/phoenix/query/BaseTest.java | 17 +++++-
11 files changed, 199 insertions(+), 105 deletions(-)
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/LnLogFunctionEnd2EndIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LnLogFunctionEnd2EndIT.java
index ddbe2ad..d3d1b51 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/LnLogFunctionEnd2EndIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LnLogFunctionEnd2EndIT.java
@@ -36,25 +36,9 @@ import org.junit.Test;
public class LnLogFunctionEnd2EndIT extends ParallelStatsDisabledIT {
private static final String KEY = "key";
- private static final double ZERO = 1e-9;
private String signedTableName;
private String unsignedTableName;
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
-
@Before
public void initTable() throws Exception {
Connection conn = null;
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/MathPIFunctionEnd2EndIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MathPIFunctionEnd2EndIT.java
new file mode 100644
index 0000000..9594aec
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MathPIFunctionEnd2EndIT.java
@@ -0,0 +1,61 @@
+/*
+ * 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.phoenix.end2end;
+
+import static org.junit.Assert.*;
+
+import java.sql.*;
+
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.expression.function.MathPIFunction;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * End to end tests for {@link MathPIFunction}
+ */
+public class MathPIFunctionEnd2EndIT extends ParallelStatsDisabledIT {
+
+ @Test
+ public void testGetMathPIValue() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ ResultSet rs = conn.createStatement().executeQuery("SELECT PI()");
+ assertTrue(rs.next());
+ assertTrue(twoDoubleEquals(rs.getDouble(1), Math.PI));
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void testMathPIRoundTwoDecimal() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ ResultSet rs = conn.createStatement().executeQuery("SELECT ROUND(PI(), 2)");
+ assertTrue(rs.next());
+ assertTrue(twoDoubleEquals(rs.getDouble(1), 3.14));
+ assertFalse(rs.next());
+ }
+
+ @Test
+ public void testMathPIFunctionWithIncorrectFormat() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ try {
+ conn.createStatement().executeQuery("SELECT PI(1)");
+ } catch (SQLException e) {
+ assertEquals(SQLExceptionCode.FUNCTION_UNDEFINED.getErrorCode(), e.getErrorCode());
+ }
+ }
+}
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/PowerFunctionEnd2EndIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PowerFunctionEnd2EndIT.java
index 2d26be8..cbc6355 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/PowerFunctionEnd2EndIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PowerFunctionEnd2EndIT.java
@@ -35,25 +35,9 @@ import org.junit.Test;
public class PowerFunctionEnd2EndIT extends ParallelStatsDisabledIT {
private static final String KEY = "key";
- private static final double ZERO = 1e-9;
private String signedTableName;
private String unsignedTableName;
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
-
@Before
public void initTable() throws Exception {
signedTableName = generateUniqueName();
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
index 59c10ad..a18928c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
@@ -187,6 +187,7 @@ public enum ExpressionType {
CollationKeyFunction(CollationKeyFunction.class),
ArrayRemoveFunction(ArrayRemoveFunction.class),
TransactionProviderNameFunction(TransactionProviderNameFunction.class),
+ MathPIFunction(MathPIFunction.class),
;
ExpressionType(Class<? extends Expression> clazz) {
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MathPIFunction.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MathPIFunction.java
new file mode 100644
index 0000000..1e276ed
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/MathPIFunction.java
@@ -0,0 +1,65 @@
+/*
+ * 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.phoenix.expression.function;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDouble;
+
+
+/**
+ *
+ * Function used to represent PI()
+ * The function returns a {@link org.apache.phoenix.schema.types.PDouble}
+ *
+ */
+@BuiltInFunction(name = MathPIFunction.NAME, args= {}
+)
+public class MathPIFunction extends ScalarFunction {
+
+ public static final String NAME = "PI";
+
+ public MathPIFunction() {}
+
+ public MathPIFunction(List<Expression> children) throws SQLException {
+ super(children);
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+ ptr.set(new byte[getDataType().getByteSize()]);
+ getDataType().getCodec().encodeDouble(Math.PI, ptr);
+ return true;
+ }
+
+ @Override
+ public PDataType getDataType() {
+ return PDouble.INSTANCE;
+ }
+}
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/ExpFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/ExpFunctionTest.java
index b7b95c2..9521730 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/ExpFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/ExpFunctionTest.java
@@ -27,6 +27,7 @@ import java.util.Random;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.expression.function.ExpFunction;
+import org.apache.phoenix.query.BaseTest;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
@@ -48,22 +49,6 @@ import com.google.common.collect.Lists;
* Unit tests for {@link ExpFunction}
*/
public class ExpFunctionTest {
- private static final double ZERO = 1e-9;
-
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
private static boolean testExpression(LiteralExpression literal, double expected)
throws SQLException {
@@ -74,7 +59,7 @@ public class ExpFunctionTest {
if (ret) {
Double result =
(Double) sqrtFunction.getDataType().toObject(ptr, sqrtFunction.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), expected));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), expected));
}
return ret;
}
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/LnLogFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/LnLogFunctionTest.java
index a5e6fc7..9713bbc 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/LnLogFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/LnLogFunctionTest.java
@@ -28,6 +28,7 @@ import java.util.Random;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.expression.function.LnFunction;
import org.apache.phoenix.expression.function.LogFunction;
+import org.apache.phoenix.query.BaseTest;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
@@ -49,25 +50,9 @@ import com.google.common.collect.Lists;
* Unit tests for {@link LnFunction} and {@link LogFunction}
*/
public class LnLogFunctionTest {
- private static final double ZERO = 1e-9;
private static final Expression THREE = LiteralExpression.newConstant(3);
private static final Expression DEFAULT_VALUE = LiteralExpression.newConstant(10.0);
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
-
private static boolean testExpression(LiteralExpression literal, LiteralExpression literal2,
LiteralExpression literal3, double exptForLn, double exptForLog10, double exptForLog3)
throws SQLException {
@@ -82,7 +67,7 @@ public class LnLogFunctionTest {
if (retLn) {
Double result =
(Double) lnFunction.getDataType().toObject(ptr, lnFunction.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptForLn));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptForLn));
}
Expression log10Function = new LogFunction(expressionsLog10);
@@ -91,7 +76,7 @@ public class LnLogFunctionTest {
Double result =
(Double) log10Function.getDataType()
.toObject(ptr, log10Function.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptForLog10));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptForLog10));
}
assertEquals(retLn, retLog10);
@@ -100,7 +85,7 @@ public class LnLogFunctionTest {
if (retLog3) {
Double result =
(Double) log3Function.getDataType().toObject(ptr, log3Function.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptForLog3));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptForLog3));
}
assertEquals(retLn, retLog3);
return retLn;
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/MathPIFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/MathPIFunctionTest.java
new file mode 100644
index 0000000..af223f2
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/MathPIFunctionTest.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.phoenix.expression;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.function.MathPIFunction;
+import org.apache.phoenix.query.BaseTest;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link MathPIFunction}
+ */
+public class MathPIFunctionTest {
+
+ @Test
+ public void testMathPIFunction() {
+ Expression mathPIFunction = new MathPIFunction();
+ ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+ boolean res = mathPIFunction.evaluate(null, ptr);
+ if (res) {
+ Double result = (Double) mathPIFunction.getDataType().toObject(ptr);
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), Math.PI));
+ }
+ assertTrue(res);
+ }
+}
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/PowerFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/PowerFunctionTest.java
index 9710e52..9349445 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/PowerFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/PowerFunctionTest.java
@@ -27,6 +27,7 @@ import java.util.Random;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.expression.function.PowerFunction;
+import org.apache.phoenix.query.BaseTest;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
@@ -48,25 +49,10 @@ import com.google.common.collect.Lists;
* Unit tests for {@link PowerFunction}
*/
public class PowerFunctionTest {
- private static final double ZERO = 1e-9;
private static final Expression ONE_POINT_FIVE = LiteralExpression.newConstant(1.5);
private static final Expression TWO = LiteralExpression.newConstant(2);
private static final Expression THREE = LiteralExpression.newConstant(3);
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
private static boolean testExpression(LiteralExpression literal, LiteralExpression literal2,
LiteralExpression literal3, double exptFor15, double exptFor2, double exptFor3)
@@ -83,7 +69,7 @@ public class PowerFunctionTest {
Double result =
(Double) powerFunction15.getDataType().toObject(ptr,
powerFunction15.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptFor15));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptFor15));
}
Expression powerFunction2 = new PowerFunction(expressions2);
@@ -92,7 +78,7 @@ public class PowerFunctionTest {
Double result =
(Double) powerFunction2.getDataType().toObject(ptr,
powerFunction2.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptFor2));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptFor2));
}
assertEquals(ret15, ret2);
@@ -102,7 +88,7 @@ public class PowerFunctionTest {
Double result =
(Double) powerFunction3.getDataType().toObject(ptr,
powerFunction3.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), exptFor3));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), exptFor3));
}
assertEquals(ret15, ret3);
return ret15;
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/SqrtFunctionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/SqrtFunctionTest.java
index 6b19ad8..ee065ab 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/SqrtFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/SqrtFunctionTest.java
@@ -27,6 +27,7 @@ import java.util.Random;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.phoenix.expression.function.SqrtFunction;
+import org.apache.phoenix.query.BaseTest;
import org.apache.phoenix.schema.SortOrder;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;
@@ -48,23 +49,6 @@ import com.google.common.collect.Lists;
* Unit tests for {@link SqrtFunction}
*/
public class SqrtFunctionTest {
- private static final double ZERO = 1e-9;
-
- private static boolean twoDoubleEquals(double a, double b) {
- if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
- if (Double.isNaN(a)) return true;
- if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
- if (Double.isInfinite(a)) {
- if ((a > 0) ^ (b > 0)) return false;
- else return true;
- }
- if (Math.abs(a - b) <= ZERO) {
- return true;
- } else {
- return false;
- }
- }
-
private static boolean testExpression(LiteralExpression literal, double expected)
throws SQLException {
List<Expression> expressions = Lists.newArrayList((Expression) literal);
@@ -74,7 +58,7 @@ public class SqrtFunctionTest {
if (ret) {
Double result =
(Double) sqrtFunction.getDataType().toObject(ptr, sqrtFunction.getSortOrder());
- assertTrue(twoDoubleEquals(result.doubleValue(), expected));
+ assertTrue(BaseTest.twoDoubleEquals(result.doubleValue(), expected));
}
return ret;
}
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
index a42d682..329cde2 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/query/BaseTest.java
@@ -179,7 +179,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
public abstract class BaseTest {
public static final String DRIVER_CLASS_NAME_ATTRIB = "phoenix.driver.class.name";
-
+ private static final double ZERO = 1e-9;
private static final Map<String,String> tableDDLMap;
private static final Logger logger = LoggerFactory.getLogger(BaseTest.class);
@ClassRule
@@ -685,6 +685,21 @@ public abstract class BaseTest {
return timestamp;
}
+ public static boolean twoDoubleEquals(double a, double b) {
+ if (Double.isNaN(a) ^ Double.isNaN(b)) return false;
+ if (Double.isNaN(a)) return true;
+ if (Double.isInfinite(a) ^ Double.isInfinite(b)) return false;
+ if (Double.isInfinite(a)) {
+ if ((a > 0) ^ (b > 0)) return false;
+ else return true;
+ }
+ if (Math.abs(a - b) <= ZERO) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
protected static void ensureTableCreated(String url, String tableName) throws SQLException {
ensureTableCreated(url, tableName, tableName, null, null, null);
}