Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHOENIX-5181 support Math sin/cos/tan functions #458

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.apache.phoenix.util.TestUtil.closeStmtAndConn;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.phoenix.expression.function.CosFunction;
import org.apache.phoenix.expression.function.SinFunction;
import org.apache.phoenix.expression.function.TanFunction;
import org.junit.Before;
import org.junit.Test;

/**
* End to end tests for
* {@link org.apache.phoenix.expression.function.CosFunction}
* {@link org.apache.phoenix.expression.function.SinFunction}
* {@link org.apache.phoenix.expression.function.TanFunction}
*/

public class MathTrigFunctionEnd2EndIT extends ParallelStatsDisabledIT {

private static final String KEY = "key";
private String tableName;

@Before
public void initTable() throws Exception {
Connection conn = null;
PreparedStatement stmt = null;
tableName = generateUniqueName();

try {
conn = DriverManager.getConnection(getUrl());
String ddl;
ddl =
"CREATE TABLE " + tableName + " (k VARCHAR NOT NULL PRIMARY KEY, doub DOUBLE)";
conn.createStatement().execute(ddl);
conn.commit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit is not needed after ddl statements.

} finally {
closeStmtAndConn(stmt, conn);
}
}

private void updateTableSpec(Connection conn, double data, String tableName) throws Exception {
PreparedStatement stmt =
conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?)");
stmt.setString(1, KEY);
stmt.setDouble(2, data);
stmt.executeUpdate();
conn.commit();
}

private void testNumberSpec(Connection conn, double data, String tableName) throws Exception {
updateTableSpec(conn, data, tableName);
ResultSet rs =
conn.createStatement().executeQuery(
"SELECT SIN(doub),COS(doub),TAN(doub) FROM " + tableName);
assertTrue(rs.next());
Double d = Double.valueOf(data);
assertTrue(twoDoubleEquals(rs.getDouble(1), Math.sin(data)));
assertTrue(twoDoubleEquals(rs.getDouble(2), Math.cos(data)));
assertTrue(twoDoubleEquals(rs.getDouble(3), Math.tan(data)));

assertTrue(!rs.next());
}

@Test
public void test() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
for (double d : new double[] { 0.0, 1.0, -1.0, 123.1234, -123.1234 }) {
testNumberSpec(conn, d, tableName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ public enum ExpressionType {
ArrayRemoveFunction(ArrayRemoveFunction.class),
TransactionProviderNameFunction(TransactionProviderNameFunction.class),
MathPIFunction(MathPIFunction.class),
SinFunction(SinFunction.class),
CosFunction(CosFunction.class),
TanFunction(TanFunction.class),
;

ExpressionType(Class<? extends Expression> clazz) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode.Argument;
import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;

@BuiltInFunction(name = CosFunction.NAME, args = { @Argument(allowedTypes = { PDouble.class,
PDecimal.class }) })
public class CosFunction extends JavaMathOneArgumentFunction {

public static final String NAME = "COS";

public CosFunction() {
}

public CosFunction(List<Expression> children) throws SQLException {
super(children);
}

@Override
public String getName() {
return NAME;
}

@Override
protected double compute(double firstArg) {
return Math.cos(firstArg);
}

@Override
public OrderPreserving preservesOrder() {
return OrderPreserving.YES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode.Argument;
import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;

@BuiltInFunction(name = SinFunction.NAME, args = { @Argument(allowedTypes = { PDouble.class,
PDecimal.class }) })
public class SinFunction extends JavaMathOneArgumentFunction {

public static final String NAME = "SIN";

public SinFunction() {
}

public SinFunction(List<Expression> children) throws SQLException {
super(children);
}

@Override
public String getName() {
return NAME;
}

@Override
protected double compute(double firstArg) {
return Math.sin(firstArg);
}

@Override
public OrderPreserving preservesOrder() {
return OrderPreserving.YES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.phoenix.expression.Expression;
import org.apache.phoenix.parse.FunctionParseNode.Argument;
import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
import org.apache.phoenix.schema.types.PDecimal;
import org.apache.phoenix.schema.types.PDouble;

@BuiltInFunction(name = TanFunction.NAME, args = { @Argument(allowedTypes = { PDouble.class,
PDecimal.class }) })
public class TanFunction extends JavaMathOneArgumentFunction {

public static final String NAME = "TAN";

public TanFunction() {
}

public TanFunction(List<Expression> children) throws SQLException {
super(children);
}

@Override
public String getName() {
return NAME;
}

@Override
protected double compute(double firstArg) {
return Math.tan(firstArg);
}

@Override
public OrderPreserving preservesOrder() {
return OrderPreserving.YES;
}
}
Loading