Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
3de090e0
提交
3de090e0
authored
16 年前
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
User defined aggregate functions: the method getType expected internal data types.
上级
d6054c63
master
noel-pr1
plus33-master
pr/267
stumc-Issue#576
version-1.1.x
version-1.4.198
version-1.4.197
version-1.4.196
version-1.4.195
version-1.4.194
version-1.4.193
version-1.4.192
version-1.4.191
version-1.4.190
version-1.4.188
version-1.4.187
version-1.4.186
version-1.4.185
version-1.4.184
version-1.4.183
version-1.4.182
version-1.4.181
version-1.4.178
version-1.4.177
version-1.3
version-1.2
version-1.1
version-1.0
无相关合并请求
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
58 行增加
和
7 行删除
+58
-7
AggregateFunction.java
h2/src/main/org/h2/api/AggregateFunction.java
+2
-2
JavaAggregate.java
h2/src/main/org/h2/expression/JavaAggregate.java
+9
-3
JavaFunction.java
h2/src/main/org/h2/expression/JavaFunction.java
+2
-1
TestFunctions.java
h2/src/test/org/h2/test/db/TestFunctions.java
+45
-1
没有找到文件。
h2/src/main/org/h2/api/AggregateFunction.java
浏览文件 @
3de090e0
...
...
@@ -29,10 +29,10 @@ public interface AggregateFunction {
* The method should check here if the number of parameters passed is correct,
* and if not it should throw an exception.
*
* @param inputType the SQL type of the parameters
* @param inputType
s
the SQL type of the parameters
* @return the SQL type of the result
*/
int
getType
(
int
[]
inputType
)
throws
SQLException
;
int
getType
(
int
[]
inputType
s
)
throws
SQLException
;
/**
* This method is called once for each row.
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/expression/JavaAggregate.java
浏览文件 @
3de090e0
...
...
@@ -60,7 +60,7 @@ public class JavaAggregate extends Expression {
}
public
int
getScale
()
{
return
0
;
return
DataType
.
getDataType
(
dataType
).
defaultScale
;
}
public
String
getSQL
()
{
...
...
@@ -91,6 +91,9 @@ public class JavaAggregate extends Expression {
case
ExpressionVisitor
.
GET_DEPENDENCIES
:
visitor
.
addDependency
(
userAggregate
);
break
;
case
ExpressionVisitor
.
OPTIMIZABLE_MIN_MAX_COUNT_ALL
:
// user defined aggregate functions can not be optimized
return
false
;
default
:
}
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
...
...
@@ -111,13 +114,16 @@ public class JavaAggregate extends Expression {
public
Expression
optimize
(
Session
session
)
throws
SQLException
{
userConnection
=
session
.
createConnection
(
false
);
argTypes
=
new
int
[
args
.
length
];
int
[]
argSqlTypes
=
new
int
[
args
.
length
];
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
Expression
expr
=
args
[
i
];
args
[
i
]
=
expr
.
optimize
(
session
);
argTypes
[
i
]
=
expr
.
getType
();
int
type
=
expr
.
getType
();
argTypes
[
i
]
=
type
;
argSqlTypes
[
i
]
=
DataType
.
convertTypeToSQLType
(
type
);
}
aggregate
=
getInstance
();
dataType
=
aggregate
.
getType
(
argTypes
);
dataType
=
DataType
.
convertSQLTypeToValueType
(
aggregate
.
getType
(
argSqlTypes
)
);
return
this
;
}
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/expression/JavaFunction.java
浏览文件 @
3de090e0
...
...
@@ -13,6 +13,7 @@ import org.h2.engine.FunctionAlias;
import
org.h2.engine.Session
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.TableFilter
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
import
org.h2.value.ValueNull
;
import
org.h2.value.ValueResultSet
;
...
...
@@ -64,7 +65,7 @@ public class JavaFunction extends Expression implements FunctionCall {
}
public
int
getScale
()
{
return
0
;
return
DataType
.
getDataType
(
getType
()).
defaultScale
;
}
public
long
getPrecision
()
{
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/db/TestFunctions.java
浏览文件 @
3de090e0
...
...
@@ -11,6 +11,7 @@ import java.io.File;
import
java.io.FileOutputStream
;
import
java.io.FileReader
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.sql.Blob
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
...
...
@@ -30,7 +31,7 @@ import org.h2.util.IOUtils;
/**
* Tests for user defined functions and aggregates.
*/
public
class
TestFunctions
extends
TestBase
{
public
class
TestFunctions
extends
TestBase
implements
AggregateFunction
{
/**
* Run just this test.
...
...
@@ -42,6 +43,8 @@ public class TestFunctions extends TestBase {
}
public
void
test
()
throws
Exception
{
deleteDb
(
"functions"
);
testPrecision
();
testVarArgs
();
testAggregate
();
testFunctions
();
...
...
@@ -49,6 +52,23 @@ public class TestFunctions extends TestBase {
deleteDb
(
"functions"
);
}
private
void
testPrecision
()
throws
SQLException
{
Connection
conn
=
getConnection
(
"functions"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"create alias no_op for \""
+
getClass
().
getName
()+
".noOp\""
);
PreparedStatement
prep
=
conn
.
prepareStatement
(
"select * from dual where no_op(1.6)=?"
);
prep
.
setBigDecimal
(
1
,
new
BigDecimal
(
"1.6"
));
ResultSet
rs
=
prep
.
executeQuery
();
assertTrue
(
rs
.
next
());
stat
.
execute
(
"create aggregate agg_sum for \""
+
getClass
().
getName
()+
"\""
);
rs
=
stat
.
executeQuery
(
"select agg_sum(1), sum(1.6) from dual"
);
rs
.
next
();
assertEquals
(
1
,
rs
.
getMetaData
().
getScale
(
2
));
assertEquals
(
32767
,
rs
.
getMetaData
().
getScale
(
1
));
conn
.
close
();
}
private
void
testVarArgs
()
throws
SQLException
{
//## Java 1.5 begin ##
Connection
conn
=
getConnection
(
"functions"
);
...
...
@@ -433,6 +453,13 @@ public class TestFunctions extends TestBase {
return
1
;
}
/**
* This method is called via reflection from the database.
*/
public
static
BigDecimal
noOp
(
BigDecimal
dec
)
{
return
dec
;
}
/**
* This method is called via reflection from the database.
*/
...
...
@@ -473,4 +500,21 @@ public class TestFunctions extends TestBase {
}
//## Java 1.5 end ##
public
void
add
(
Object
value
)
throws
SQLException
{
}
public
Object
getResult
()
throws
SQLException
{
return
new
BigDecimal
(
"1.6"
);
}
public
int
getType
(
int
[]
inputTypes
)
throws
SQLException
{
if
(
inputTypes
.
length
!=
1
||
inputTypes
[
0
]
!=
Types
.
INTEGER
)
{
throw
new
SQLException
(
"unexpected data type"
);
}
return
Types
.
DECIMAL
;
}
public
void
init
(
Connection
conn
)
throws
SQLException
{
}
}
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论