提交 179736c2 authored 作者: Thomas Mueller's avatar Thomas Mueller

PostgreSQL compatibility: LOG(x) is base 10 in the PostgreSQL mode.

上级 a305387a
......@@ -2625,6 +2625,7 @@ FLOOR(A)
{ LOG | LN } (numeric)
","
See also Java ""Math.log"".
In the PostgreSQL mode, LOG(x) is base 10.
This method returns a double.
","
LOG(A)
......
......@@ -18,7 +18,7 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>-
<ul><li>PostgreSQL compatibility: LOG(x) is base 10 in the PostgreSQL mode.
</li></ul>
<h2>Version 1.3.164 (2012-02-03)</h2>
......
......@@ -1133,6 +1133,7 @@ or the SQL statement <code>SET MODE PostgreSQL</code>.
digits are not be truncated, but the value is rounded.
</li><li>The system columns <code>CTID</code> and
<code>OID</code> are supported.
</li><li>LOG(x) is base 10 in this mode.
</li></ul>
<h2 id="auto_reconnect">Auto-Reconnect</h2>
......
......@@ -114,6 +114,11 @@ public class Mode {
*/
public boolean allowPlusForStringConcat;
/**
* The function LOG() uses base 10 instead of E.
*/
public boolean logIsLogBase10;
private String name;
static {
......@@ -167,6 +172,7 @@ public class Mode {
mode.roundWhenConvertToLong = true;
mode.supportOffsetFetch = true;
mode.systemColumns = true;
mode.logIsLogBase10 = true;
add(mode);
}
......
......@@ -74,7 +74,7 @@ public class Function extends Expression implements FunctionCall {
CEILING = 8, COS = 9, COT = 10, DEGREES = 11, EXP = 12, FLOOR = 13, LOG = 14, LOG10 = 15, MOD = 16,
PI = 17, POWER = 18, RADIANS = 19, RAND = 20, ROUND = 21, ROUNDMAGIC = 22, SIGN = 23, SIN = 24, SQRT = 25,
TAN = 26, TRUNCATE = 27, SECURE_RAND = 28, HASH = 29, ENCRYPT = 30, DECRYPT = 31, COMPRESS = 32,
EXPAND = 33, ZERO = 34, RANDOM_UUID = 35, COSH = 36, SINH = 37, TANH = 38;
EXPAND = 33, ZERO = 34, RANDOM_UUID = 35, COSH = 36, SINH = 37, TANH = 38, LN = 39;
public static final int ASCII = 50, BIT_LENGTH = 51, CHAR = 52, CHAR_LENGTH = 53, CONCAT = 54, DIFFERENCE = 55,
HEXTORAW = 56, INSERT = 57, INSTR = 58, LCASE = 59, LEFT = 60, LENGTH = 61, LOCATE = 62, LTRIM = 63,
......@@ -182,7 +182,7 @@ public class Function extends Expression implements FunctionCall {
addFunction("EXP", EXP, 1, Value.DOUBLE);
addFunction("FLOOR", FLOOR, 1, Value.DOUBLE);
addFunction("LOG", LOG, 1, Value.DOUBLE);
addFunction("LN", LOG, 1, Value.DOUBLE);
addFunction("LN", LN, 1, Value.DOUBLE);
addFunction("LOG10", LOG10, 1, Value.DOUBLE);
addFunction("MOD", MOD, 2, Value.LONG);
addFunction("PI", PI, 0, Value.DOUBLE);
......@@ -495,8 +495,15 @@ public class Function extends Expression implements FunctionCall {
case FLOOR:
result = ValueDouble.get(Math.floor(v0.getDouble()));
break;
case LN:
result = ValueDouble.get(Math.log(v0.getDouble()));
break;
case LOG:
if (database.getMode().logIsLogBase10) {
result = ValueDouble.get(Math.log10(v0.getDouble()));
} else {
result = ValueDouble.get(Math.log(v0.getDouble()));
}
break;
case LOG10:
result = ValueDouble.get(log10(v0.getDouble()));
......
......@@ -43,6 +43,7 @@ public class TestCompatibility extends TestBase {
testColumnAlias();
testUniqueIndexSingleNull();
testUniqueIndexOracle();
testPostgreSQL();
testHsqlDb();
testMySQL();
testDB2();
......@@ -177,6 +178,9 @@ public class TestCompatibility extends TestBase {
private void testHsqlDb() throws SQLException {
Statement stat = conn.createStatement();
stat.execute("set mode hsqldb");
testLog(Math.log(10), stat);
stat.execute("DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT PRIMARY KEY); ");
stat.execute("CALL CURRENT_TIME");
stat.execute("CALL CURRENT_TIMESTAMP");
......@@ -191,7 +195,24 @@ public class TestCompatibility extends TestBase {
prep.setInt(1, 2);
prep.executeQuery();
stat.execute("DROP TABLE TEST IF EXISTS");
}
private void testLog(double expected, Statement stat) throws SQLException {
stat.execute("create table log(id int)");
stat.execute("insert into log values(1)");
ResultSet rs = stat.executeQuery("select log(10) from log");
rs.next();
assertEquals((int) (expected * 100), (int) (rs.getDouble(1) * 100));
rs = stat.executeQuery("select ln(10) from log");
rs.next();
assertEquals((int) (Math.log(10) * 100), (int) (rs.getDouble(1) * 100));
stat.execute("drop table log");
}
private void testPostgreSQL() throws SQLException {
Statement stat = conn.createStatement();
stat.execute("SET MODE PostgreSQL");
testLog(Math.log10(10), stat);
}
private void testMySQL() throws SQLException {
......@@ -213,6 +234,9 @@ public class TestCompatibility extends TestBase {
// need to reconnect, because meta data tables may be initialized
conn.close();
conn = getConnection("compatibility;MODE=MYSQL");
stat = conn.createStatement();
testLog(Math.log(10), stat);
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
assertResult("test", stat, "SHOW TABLES");
ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
......@@ -271,6 +295,9 @@ public class TestCompatibility extends TestBase {
private void testDB2() throws SQLException {
conn = getConnection("compatibility;MODE=DB2");
Statement stat = conn.createStatement();
testLog(Math.log(10), stat);
ResultSet res = conn.createStatement().executeQuery("SELECT 1 FROM sysibm.sysdummy1");
res.next();
assertEquals("1", res.getString(1));
......@@ -280,11 +307,11 @@ public class TestCompatibility extends TestBase {
executeQuery("SELECT 1 FROM sysibm.sysdummy1");
conn.close();
conn = getConnection("compatibility;MODE=DB2");
Statement stmt = conn.createStatement();
stmt.execute("drop table test");
stmt.execute("create table test(id varchar)");
stmt.execute("insert into test values ('3'),('1'),('2')");
res = stmt.executeQuery("select id from test order by id fetch next 2 rows only");
stat = conn.createStatement();
stat.execute("drop table test");
stat.execute("create table test(id varchar)");
stat.execute("insert into test values ('3'),('1'),('2')");
res = stat.executeQuery("select id from test order by id fetch next 2 rows only");
conn = getConnection("compatibility");
res.next();
assertEquals("1", res.getString(1));
......@@ -295,6 +322,9 @@ public class TestCompatibility extends TestBase {
private void testDerby() throws SQLException {
conn = getConnection("compatibility;MODE=Derby");
Statement stat = conn.createStatement();
testLog(Math.log(10), stat);
ResultSet res = conn.createStatement().executeQuery("SELECT 1 FROM sysibm.sysdummy1 fetch next 1 row only");
res.next();
assertEquals("1", res.getString(1));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论