Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
e647b73b
提交
e647b73b
authored
7月 13, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow expressions based on DISTINCT values in ORDER BY
上级
5da46ca4
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
128 行增加
和
1 行删除
+128
-1
Query.java
h2/src/main/org/h2/command/dml/Query.java
+54
-1
ConditionNot.java
h2/src/main/org/h2/expression/ConditionNot.java
+9
-0
Operation.java
h2/src/main/org/h2/expression/Operation.java
+11
-0
distinct.sql
h2/src/test/org/h2/test/scripts/distinct.sql
+54
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Query.java
浏览文件 @
e647b73b
...
@@ -14,9 +14,14 @@ import org.h2.engine.Database;
...
@@ -14,9 +14,14 @@ import org.h2.engine.Database;
import
org.h2.engine.Session
;
import
org.h2.engine.Session
;
import
org.h2.engine.Mode.ModeEnum
;
import
org.h2.engine.Mode.ModeEnum
;
import
org.h2.expression.Alias
;
import
org.h2.expression.Alias
;
import
org.h2.expression.Comparison
;
import
org.h2.expression.ConditionAndOr
;
import
org.h2.expression.ConditionNot
;
import
org.h2.expression.Expression
;
import
org.h2.expression.Expression
;
import
org.h2.expression.ExpressionColumn
;
import
org.h2.expression.ExpressionColumn
;
import
org.h2.expression.ExpressionVisitor
;
import
org.h2.expression.ExpressionVisitor
;
import
org.h2.expression.Function
;
import
org.h2.expression.Operation
;
import
org.h2.expression.Parameter
;
import
org.h2.expression.Parameter
;
import
org.h2.expression.ValueExpression
;
import
org.h2.expression.ValueExpression
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
...
@@ -480,7 +485,9 @@ public abstract class Query extends Prepared {
...
@@ -480,7 +485,9 @@ public abstract class Query extends Prepared {
if
(!
isAlias
)
{
if
(!
isAlias
)
{
if
(
mustBeInResult
)
{
if
(
mustBeInResult
)
{
if
(
session
.
getDatabase
().
getMode
().
getEnum
()
!=
ModeEnum
.
MySQL
)
{
if
(
session
.
getDatabase
().
getMode
().
getEnum
()
!=
ModeEnum
.
MySQL
)
{
throw
DbException
.
get
(
ErrorCode
.
ORDER_BY_NOT_IN_RESULT
,
e
.
getSQL
());
if
(!
checkOrderOther
(
session
,
e
,
expressionSQL
))
{
throw
DbException
.
get
(
ErrorCode
.
ORDER_BY_NOT_IN_RESULT
,
e
.
getSQL
());
}
}
}
}
}
expressions
.
add
(
e
);
expressions
.
add
(
e
);
...
@@ -492,6 +499,52 @@ public abstract class Query extends Prepared {
...
@@ -492,6 +499,52 @@ public abstract class Query extends Prepared {
}
}
}
}
private
static
boolean
checkOrderOther
(
Session
session
,
Expression
expr
,
ArrayList
<
String
>
expressionSQL
)
{
if
(
expr
.
isConstant
())
{
return
true
;
}
if
(
expressionSQL
!=
null
)
{
String
exprSQL
=
expr
.
getSQL
();
for
(
String
sql:
expressionSQL
)
{
if
(
session
.
getDatabase
().
equalsIdentifiers
(
exprSQL
,
sql
))
{
return
true
;
}
}
}
if
(
expr
instanceof
Function
)
{
Function
function
=
(
Function
)
expr
;
if
(!
function
.
isDeterministic
())
{
return
false
;
}
for
(
Expression
e
:
function
.
getArgs
())
{
if
(!
checkOrderOther
(
session
,
e
,
expressionSQL
))
{
return
false
;
}
}
return
true
;
}
if
(
expr
instanceof
Operation
)
{
Operation
operation
=
(
Operation
)
expr
;
Expression
right
=
operation
.
getExpression
(
false
);
return
checkOrderOther
(
session
,
operation
.
getExpression
(
true
),
expressionSQL
)
&&
(
right
==
null
||
checkOrderOther
(
session
,
right
,
expressionSQL
));
}
if
(
expr
instanceof
ConditionAndOr
)
{
ConditionAndOr
condition
=
(
ConditionAndOr
)
expr
;
return
checkOrderOther
(
session
,
condition
.
getExpression
(
true
),
expressionSQL
)
&&
checkOrderOther
(
session
,
condition
.
getExpression
(
false
),
expressionSQL
);
}
if
(
expr
instanceof
ConditionNot
)
{
return
checkOrderOther
(
session
,
((
ConditionNot
)
expr
).
getCondition
(),
expressionSQL
);
}
if
(
expr
instanceof
Comparison
)
{
Comparison
condition
=
(
Comparison
)
expr
;
return
checkOrderOther
(
session
,
condition
.
getExpression
(
true
),
expressionSQL
)
&&
checkOrderOther
(
session
,
condition
.
getExpression
(
false
),
expressionSQL
);
}
return
false
;
}
/**
/**
* Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
* Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
* objects. The expression list is extended if necessary.
* objects. The expression list is extended if necessary.
...
...
h2/src/main/org/h2/expression/ConditionNot.java
浏览文件 @
e647b73b
...
@@ -98,4 +98,13 @@ public class ConditionNot extends Condition {
...
@@ -98,4 +98,13 @@ public class ConditionNot extends Condition {
return
condition
.
getCost
();
return
condition
.
getCost
();
}
}
/**
* Get the sub-expression of this condition.
*
* @return the sub-expression
*/
public
Expression
getCondition
()
{
return
condition
;
}
}
}
h2/src/main/org/h2/expression/Operation.java
浏览文件 @
e647b73b
...
@@ -407,4 +407,15 @@ public class Operation extends Expression {
...
@@ -407,4 +407,15 @@ public class Operation extends Expression {
return
left
.
getCost
()
+
1
+
(
right
==
null
?
0
:
right
.
getCost
());
return
left
.
getCost
()
+
1
+
(
right
==
null
?
0
:
right
.
getCost
());
}
}
/**
* Get the left or the right sub-expression of this condition.
*
* @param getLeft true to get the left sub-expression, false to get the
* right sub-expression.
* @return the sub-expression
*/
public
Expression
getExpression
(
boolean
getLeft
)
{
return
getLeft
?
this
.
left
:
right
;
}
}
}
h2/src/test/org/h2/test/scripts/distinct.sql
浏览文件 @
e647b73b
...
@@ -15,6 +15,60 @@ CREATE TABLE TEST2(ID2 BIGINT);
...
@@ -15,6 +15,60 @@ CREATE TABLE TEST2(ID2 BIGINT);
INSERT
INTO
TEST2
VALUES
(
1
),
(
2
);
INSERT
INTO
TEST2
VALUES
(
1
),
(
2
);
>
update
count
:
2
>
update
count
:
2
SELECT
DISTINCT
NAME
FROM
TEST
ORDER
BY
NAME
;
>
NAME
>
----
>
B
>
a
>
c
>
rows
(
ordered
):
3
SELECT
DISTINCT
NAME
FROM
TEST
ORDER
BY
LOWER
(
NAME
);
>
NAME
>
----
>
a
>
B
>
c
>
rows
(
ordered
):
3
SELECT
DISTINCT
ID
FROM
TEST
ORDER
BY
ID
;
>
ID
>
--
>
1
>
2
>
3
>
rows
(
ordered
):
3
SELECT
DISTINCT
ID
FROM
TEST
ORDER
BY
-
ID
-
1
;
>
ID
>
--
>
3
>
2
>
1
>
rows
(
ordered
):
3
SELECT
DISTINCT
ID
FROM
TEST
ORDER
BY
(
-
ID
+
10
)
>
0
AND
NOT
(
ID
=
0
),
ID
;
>
ID
>
--
>
1
>
2
>
3
>
rows
(
ordered
):
3
SELECT
DISTINCT
NAME
,
ID
+
1
FROM
TEST
ORDER
BY
UPPER
(
NAME
)
||
(
ID
+
1
);
>
NAME
ID
+
1
>
---- ------
>
a
2
>
B
3
>
c
4
>
rows
(
ordered
):
3
SELECT
DISTINCT
ID
FROM
TEST
ORDER
BY
NAME
;
>
exception
ORDER_BY_NOT_IN_RESULT
SELECT
DISTINCT
ID
FROM
TEST
ORDER
BY
CURRENT_TIMESTAMP
;
>
exception
ORDER_BY_NOT_IN_RESULT
SET
MODE
MySQL
;
SET
MODE
MySQL
;
>
ok
>
ok
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论