Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
32d3ca3f
提交
32d3ca3f
authored
9月 18, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add WindowFrame class and move some logic into it
上级
249129a2
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
249 行增加
和
123 行删除
+249
-123
Parser.java
h2/src/main/org/h2/command/Parser.java
+11
-10
AbstractAggregate.java
...c/main/org/h2/expression/aggregate/AbstractAggregate.java
+12
-17
Window.java
h2/src/main/org/h2/expression/aggregate/Window.java
+4
-46
WindowFrame.java
h2/src/main/org/h2/expression/aggregate/WindowFrame.java
+209
-0
WindowFunction.java
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
+13
-50
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
32d3ca3f
...
...
@@ -175,7 +175,8 @@ import org.h2.expression.aggregate.Aggregate;
import
org.h2.expression.aggregate.Aggregate.AggregateType
;
import
org.h2.expression.aggregate.JavaAggregate
;
import
org.h2.expression.aggregate.Window
;
import
org.h2.expression.aggregate.Window.SimpleWindowFrame
;
import
org.h2.expression.aggregate.WindowFrame
;
import
org.h2.expression.aggregate.WindowFrame.SimpleExtent
;
import
org.h2.expression.aggregate.WindowFunction
;
import
org.h2.expression.aggregate.WindowFunction.WindowFunctionType
;
import
org.h2.index.Index
;
...
...
@@ -3062,7 +3063,7 @@ public class Parser {
}
else
if
(!
isAggregate
)
{
orderBy
=
new
ArrayList
<>(
0
);
}
Simple
WindowFrame
frame
;
WindowFrame
frame
;
if
(
aggregate
instanceof
WindowFunction
)
{
WindowFunction
w
=
(
WindowFunction
)
aggregate
;
switch
(
w
.
getFunctionType
())
{
...
...
@@ -3072,7 +3073,7 @@ public class Parser {
frame
=
readWindowFrame
();
break
;
default
:
frame
=
SimpleWindowFrame
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
frame
=
new
WindowFrame
(
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
)
;
}
}
else
{
frame
=
readWindowFrame
();
...
...
@@ -3088,8 +3089,8 @@ public class Parser {
}
}
private
Simple
WindowFrame
readWindowFrame
()
{
Simple
WindowFrame
frame
;
private
WindowFrame
readWindowFrame
()
{
Simple
Extent
extent
;
if
(
readIf
(
"RANGE"
))
{
read
(
"BETWEEN"
);
if
(
readIf
(
"UNBOUNDED"
))
{
...
...
@@ -3097,11 +3098,11 @@ public class Parser {
read
(
"AND"
);
if
(
readIf
(
"CURRENT"
))
{
read
(
"ROW"
);
frame
=
SimpleWindowFrame
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
extent
=
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
}
else
{
read
(
"UNBOUNDED"
);
read
(
"FOLLOWING"
);
frame
=
SimpleWindowFrame
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING
;
extent
=
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING
;
}
}
else
{
read
(
"CURRENT"
);
...
...
@@ -3109,12 +3110,12 @@ public class Parser {
read
(
"AND"
);
read
(
"UNBOUNDED"
);
read
(
"FOLLOWING"
);
frame
=
SimpleWindowFrame
.
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING
;
extent
=
SimpleExtent
.
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING
;
}
}
else
{
frame
=
SimpleWindowFrame
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
extent
=
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
}
return
frame
;
return
new
WindowFrame
(
extent
)
;
}
private
AggregateType
getAggregateType
(
String
name
)
{
...
...
h2/src/main/org/h2/expression/aggregate/AbstractAggregate.java
浏览文件 @
32d3ca3f
...
...
@@ -424,16 +424,23 @@ public abstract class AbstractAggregate extends Expression {
*/
protected
void
getOrderedResultLoop
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
switch
(
over
.
getWindowFrame
())
{
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW:
{
WindowFrame
frame
=
over
.
getWindowFrame
();
if
(
frame
.
isDefault
())
{
Object
aggregateData
=
createAggregateData
();
for
(
Value
[]
row
:
ordered
)
{
updateFromExpressions
(
session
,
aggregateData
,
row
);
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
getAggregatedValue
(
session
,
aggregateData
));
}
break
;
}
case
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING:
{
}
else
if
(
frame
.
isFullPartition
())
{
Object
aggregateData
=
createAggregateData
();
for
(
Value
[]
row
:
ordered
)
{
updateFromExpressions
(
session
,
aggregateData
,
row
);
}
Value
value
=
getAggregatedValue
(
session
,
aggregateData
);
for
(
Value
[]
row
:
ordered
)
{
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
value
);
}
}
else
{
// TODO optimize unordered aggregates
int
size
=
ordered
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
...
...
@@ -443,18 +450,6 @@ public abstract class AbstractAggregate extends Expression {
}
result
.
put
(
ordered
.
get
(
i
)[
rowIdColumn
].
getInt
(),
getAggregatedValue
(
session
,
aggregateData
));
}
break
;
}
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING:
{
Object
aggregateData
=
createAggregateData
();
for
(
Value
[]
row
:
ordered
)
{
updateFromExpressions
(
session
,
aggregateData
,
row
);
}
Value
value
=
getAggregatedValue
(
session
,
aggregateData
);
for
(
Value
[]
row
:
ordered
)
{
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
value
);
}
}
}
}
...
...
h2/src/main/org/h2/expression/aggregate/Window.java
浏览文件 @
32d3ca3f
...
...
@@ -22,53 +22,11 @@ import org.h2.value.ValueArray;
*/
public
final
class
Window
{
/**
* Simple window frame.
*/
public
enum
SimpleWindowFrame
{
/**
* RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW frame specification.
*/
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
(
"RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
),
/**
* RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING frame specification.
*/
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING
(
"RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING"
),
/**
* RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING frame
* specification.
*/
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING
(
"RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
),
;
private
final
String
sql
;
private
SimpleWindowFrame
(
String
sql
)
{
this
.
sql
=
sql
;
}
/**
* Returns SQL representation.
*
* @return SQL representation.
* @see Expression#getSQL()
*/
public
String
getSQL
()
{
return
sql
;
}
}
private
final
ArrayList
<
Expression
>
partitionBy
;
private
final
ArrayList
<
SelectOrderBy
>
orderBy
;
private
final
Simple
WindowFrame
frame
;
private
final
WindowFrame
frame
;
/**
* @param builder
...
...
@@ -100,7 +58,7 @@ public final class Window {
* @param frame
* window frame clause
*/
public
Window
(
ArrayList
<
Expression
>
partitionBy
,
ArrayList
<
SelectOrderBy
>
orderBy
,
Simple
WindowFrame
frame
)
{
public
Window
(
ArrayList
<
Expression
>
partitionBy
,
ArrayList
<
SelectOrderBy
>
orderBy
,
WindowFrame
frame
)
{
this
.
partitionBy
=
partitionBy
;
this
.
orderBy
=
orderBy
;
this
.
frame
=
frame
;
...
...
@@ -184,7 +142,7 @@ public final class Window {
*
* @return window frame
*/
public
Simple
WindowFrame
getWindowFrame
()
{
public
WindowFrame
getWindowFrame
()
{
return
frame
;
}
...
...
@@ -229,7 +187,7 @@ public final class Window {
builder
.
append
(
StringUtils
.
unEnclose
(
partitionBy
.
get
(
i
).
getSQL
()));
}
}
if
(
frame
!=
SimpleWindowFrame
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
)
{
if
(
!
frame
.
isDefault
()
)
{
builder
.
append
(
' '
).
append
(
frame
.
getSQL
());
}
appendOrderBy
(
builder
,
orderBy
);
...
...
h2/src/main/org/h2/expression/aggregate/WindowFrame.java
0 → 100644
浏览文件 @
32d3ca3f
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
expression
.
aggregate
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.NoSuchElementException
;
import
org.h2.message.DbException
;
import
org.h2.value.Value
;
/**
* Window frame clause.
*/
public
final
class
WindowFrame
{
/**
* Simple extent.
*/
public
enum
SimpleExtent
{
/**
* RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW frame specification.
*/
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
(
"RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
),
/**
* RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING frame specification.
*/
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING
(
"RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING"
),
/**
* RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING frame
* specification.
*/
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING
(
"RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
),
;
private
final
String
sql
;
private
SimpleExtent
(
String
sql
)
{
this
.
sql
=
sql
;
}
/**
* Returns SQL representation.
*
* @return SQL representation.
* @see org.h2.expression.Expression#getSQL()
*/
public
String
getSQL
()
{
return
sql
;
}
}
private
final
SimpleExtent
extent
;
/**
* Creates new instance of window frame clause.
*
* @param extent
* window frame extent
*/
public
WindowFrame
(
SimpleExtent
extent
)
{
this
.
extent
=
extent
;
}
/**
* Returns whether window frame specification can be omitted.
*
* @return whether window frame specification can be omitted
*/
public
boolean
isDefault
()
{
return
extent
==
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW
;
}
/**
* Returns whether window frame specification contains all rows in
* partition.
*
* @return whether window frame specification contains all rows in partition
*/
public
boolean
isFullPartition
()
{
return
extent
==
SimpleExtent
.
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING
;
}
/**
* Returns iterator.
*
* @param orderedRows
* ordered rows
* @param currentRow
* index of the current row
* @return iterator
*/
public
Iterator
<
Value
[]>
iterator
(
final
ArrayList
<
Value
[]>
orderedRows
,
int
currentRow
)
{
int
size
=
orderedRows
.
size
();
final
int
startIndex
,
endIndex
;
switch
(
extent
)
{
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW:
startIndex
=
0
;
endIndex
=
currentRow
;
break
;
case
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING:
startIndex
=
currentRow
;
endIndex
=
size
-
1
;
break
;
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING:
startIndex
=
0
;
endIndex
=
size
-
1
;
break
;
default
:
throw
DbException
.
getUnsupportedException
(
"window frame extent ="
+
extent
);
}
return
new
Iterator
<
Value
[]>()
{
private
int
cursor
=
startIndex
;
@Override
public
boolean
hasNext
()
{
return
cursor
<=
endIndex
;
}
@Override
public
Value
[]
next
()
{
if
(
cursor
>
endIndex
)
{
throw
new
NoSuchElementException
();
}
return
orderedRows
.
get
(
cursor
++);
}
@Override
public
void
remove
()
{
throw
new
UnsupportedOperationException
();
}
};
}
/**
* Returns iterator in descending order.
*
* @param orderedRows
* ordered rows
* @param currentRow
* index of the current row
* @return iterator in descending order
*/
public
Iterator
<
Value
[]>
reverseIterator
(
final
ArrayList
<
Value
[]>
orderedRows
,
int
currentRow
)
{
int
size
=
orderedRows
.
size
();
final
int
startIndex
,
endIndex
;
switch
(
extent
)
{
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW:
startIndex
=
0
;
endIndex
=
currentRow
;
break
;
case
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING:
startIndex
=
currentRow
;
endIndex
=
size
-
1
;
break
;
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING:
startIndex
=
0
;
endIndex
=
size
-
1
;
break
;
default
:
throw
DbException
.
getUnsupportedException
(
"window frame extent ="
+
extent
);
}
return
new
Iterator
<
Value
[]>()
{
private
int
cursor
=
endIndex
;
@Override
public
boolean
hasNext
()
{
return
cursor
>=
startIndex
;
}
@Override
public
Value
[]
next
()
{
if
(
cursor
<
startIndex
)
{
throw
new
NoSuchElementException
();
}
return
orderedRows
.
get
(
cursor
--);
}
@Override
public
void
remove
()
{
throw
new
UnsupportedOperationException
();
}
};
}
/**
* Returns SQL representation.
*
* @return SQL representation.
* @see org.h2.expression.Expression#getSQL()
*/
public
String
getSQL
()
{
return
extent
.
getSQL
();
}
}
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
浏览文件 @
32d3ca3f
...
...
@@ -7,6 +7,7 @@ package org.h2.expression.aggregate;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
org.h2.command.dml.Select
;
import
org.h2.engine.Session
;
...
...
@@ -130,34 +131,15 @@ public class WindowFunction extends AbstractAggregate {
}
}
private
static
Value
getNthValue
(
ArrayList
<
Value
[]>
ordered
,
int
startIndex
,
int
endIndex
,
int
number
,
boolean
fromLast
,
boolean
ignoreNulls
)
{
return
ignoreNulls
?
getNthValueIgnoreNulls
(
ordered
,
startIndex
,
endIndex
,
number
,
fromLast
)
:
ordered
.
get
(
fromLast
?
endIndex
-
number
:
startIndex
+
number
)[
0
];
}
private
static
Value
getNthValueIgnoreNulls
(
ArrayList
<
Value
[]>
ordered
,
int
startIndex
,
int
endIndex
,
int
number
,
boolean
fromLast
)
{
private
static
Value
getNthValue
(
Iterator
<
Value
[]>
iterator
,
int
number
,
boolean
ignoreNulls
)
{
Value
v
=
ValueNull
.
INSTANCE
;
int
cnt
=
0
;
if
(
fromLast
)
{
for
(
int
i
=
endIndex
;
i
>=
startIndex
;
i
--)
{
Value
t
=
ordered
.
get
(
i
)[
0
];
if
(
t
!=
ValueNull
.
INSTANCE
)
{
if
(
cnt
++
==
number
)
{
v
=
t
;
break
;
}
}
}
}
else
{
for
(
int
i
=
startIndex
;
i
<=
endIndex
;
i
++)
{
Value
t
=
ordered
.
get
(
i
)[
0
];
if
(
t
!=
ValueNull
.
INSTANCE
)
{
if
(
cnt
++
==
number
)
{
v
=
t
;
break
;
}
while
(
iterator
.
hasNext
())
{
Value
t
=
iterator
.
next
()[
0
];
if
(!
ignoreNulls
||
t
!=
ValueNull
.
INSTANCE
)
{
if
(
cnt
++
==
number
)
{
v
=
t
;
break
;
}
}
}
...
...
@@ -333,33 +315,17 @@ public class WindowFunction extends AbstractAggregate {
private
void
getNth
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
int
size
=
ordered
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
int
startIndex
,
endIndex
;
switch
(
over
.
getWindowFrame
())
{
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_CURRENT_ROW:
startIndex
=
0
;
endIndex
=
i
;
break
;
case
RANGE_BETWEEN_CURRENT_ROW_AND_UNBOUNDED_FOLLOWING:
startIndex
=
i
;
endIndex
=
size
-
1
;
break
;
case
RANGE_BETWEEN_UNBOUNDED_PRECEDING_AND_UNBOUNDED_FOLLOWING:
startIndex
=
0
;
endIndex
=
size
-
1
;
break
;
default
:
throw
DbException
.
getUnsupportedException
(
"window frame="
+
over
.
getWindowFrame
());
}
WindowFrame
frame
=
over
.
getWindowFrame
();
Value
[]
row
=
ordered
.
get
(
i
);
int
rowId
=
row
[
rowIdColumn
].
getInt
();
Value
v
;
switch
(
type
)
{
case
FIRST_VALUE:
{
v
=
getNthValue
(
ordered
,
startIndex
,
endIndex
,
0
,
false
,
ignoreNulls
);
v
=
getNthValue
(
frame
.
iterator
(
ordered
,
i
),
0
,
ignoreNulls
);
break
;
}
case
LAST_VALUE:
v
=
getNthValue
(
ordered
,
startIndex
,
endIndex
,
0
,
true
,
ignoreNulls
);
v
=
getNthValue
(
frame
.
reverseIterator
(
ordered
,
i
),
0
,
ignoreNulls
);
break
;
case
NTH_VALUE:
{
int
n
=
row
[
1
].
getInt
();
...
...
@@ -367,11 +333,8 @@ public class WindowFunction extends AbstractAggregate {
throw
DbException
.
getInvalidValueException
(
"nth row"
,
n
);
}
n
--;
if
(
n
>
endIndex
-
startIndex
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
v
=
getNthValue
(
ordered
,
startIndex
,
endIndex
,
n
,
fromLast
,
ignoreNulls
);
}
Iterator
<
Value
[]>
iter
=
fromLast
?
frame
.
reverseIterator
(
ordered
,
i
)
:
frame
.
iterator
(
ordered
,
i
);
v
=
getNthValue
(
iter
,
n
,
ignoreNulls
);
break
;
}
default
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论