提交 d7ef09f4 authored 作者: Thomas Mueller's avatar Thomas Mueller

Support for ROW_NUMBER() OVER() as an alias for ROWNUM() for compatibility with…

Support for ROW_NUMBER() OVER() as an alias for ROWNUM() for compatibility with PostgreSQL and Apache Derby.
上级 0b7c2c21
...@@ -3627,11 +3627,11 @@ READONLY() ...@@ -3627,11 +3627,11 @@ READONLY()
" "
"Functions (System)","ROWNUM"," "Functions (System)","ROWNUM","
ROWNUM() { ROWNUM() } | { ROW_NUMBER() OVER() }
"," ","
Returns the number of the current row. Returns the number of the current row.
This method returns an int. This method returns a long.
This function is supported for SELECT statements, as well as for DELETE and UPDATE. It is supported for SELECT statements, as well as for DELETE and UPDATE.
The first row has the row number 1, and is calculated before ordering and grouping the result set, The first row has the row number 1, and is calculated before ordering and grouping the result set,
but after evaluating index conditions (even when the index conditions are specified in an outer query). but after evaluating index conditions (even when the index conditions are specified in an outer query).
To get the row number after ordering and grouping, use a subquery. To get the row number after ordering and grouping, use a subquery.
......
...@@ -2275,6 +2275,12 @@ public class Parser { ...@@ -2275,6 +2275,12 @@ public class Parser {
tf.setColumns(columns); tf.setColumns(columns);
break; break;
} }
case Function.ROW_NUMBER:
read(")");
read("OVER");
read("(");
read(")");
return new Rownum(currentSelect == null ? currentPrepared : currentSelect);
default: default:
if (!readIf(")")) { if (!readIf(")")) {
int i = 0; int i = 0;
......
...@@ -97,6 +97,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -97,6 +97,8 @@ public class Function extends Expression implements FunctionCall {
LINK_SCHEMA = 218, GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223, TABLE_DISTINCT = 224, LINK_SCHEMA = 218, GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223, TABLE_DISTINCT = 224,
FILE_READ = 225, TRANSACTION_ID = 226, TRUNCATE_VALUE = 227, NVL2 = 228; FILE_READ = 225, TRANSACTION_ID = 226, TRUNCATE_VALUE = 227, NVL2 = 228;
public static final int ROW_NUMBER = 300;
private static final int VAR_ARGS = -1; private static final int VAR_ARGS = -1;
private static final long PRECISION_UNKNOWN = -1; private static final long PRECISION_UNKNOWN = -1;
...@@ -336,6 +338,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -336,6 +338,9 @@ public class Function extends Expression implements FunctionCall {
// TableFunction // TableFunction
addFunctionWithNull("TABLE", TABLE, VAR_ARGS, Value.RESULT_SET); addFunctionWithNull("TABLE", TABLE, VAR_ARGS, Value.RESULT_SET);
addFunctionWithNull("TABLE_DISTINCT", TABLE_DISTINCT, VAR_ARGS, Value.RESULT_SET); addFunctionWithNull("TABLE_DISTINCT", TABLE_DISTINCT, VAR_ARGS, Value.RESULT_SET);
// pseudo function
addFunctionWithNull("ROW_NUMBER", ROW_NUMBER, 0, Value.LONG);
} }
protected Function(Database database, FunctionInfo info) { protected Function(Database database, FunctionInfo info) {
......
--- special grammar and test cases --------------------------------------------------------------------------------------------- --- special grammar and test cases ---------------------------------------------------------------------------------------------
create table test(id int primary key, name varchar(255), row_number int);
> ok
insert into test values(1, 'hello', 10), (2, 'world', 20);
> update count: 2
select row_number() over(), id, name from test order by id;
> ROWNUM() ID NAME
> -------- -- -----
> 1 1 hello
> 2 2 world
> rows (ordered): 2
select row_number() over(), id, name from test order by name;
> ROWNUM() ID NAME
> -------- -- -----
> 1 1 hello
> 2 2 world
> rows (ordered): 2
select row_number() over(), id, name from test order by name desc;
> ROWNUM() ID NAME
> -------- -- -----
> 2 2 world
> 1 1 hello
> rows (ordered): 2
drop table test;
> ok
create table test(x int) as select x from system_range(1, 2); create table test(x int) as select x from system_range(1, 2);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论