提交 001cd637 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix window definition lookup in some queries

上级 f2c645a3
...@@ -1335,7 +1335,7 @@ public class Parser { ...@@ -1335,7 +1335,7 @@ public class Parser {
} }
private Prepared parseHelp() { private Prepared parseHelp() {
Select select = new Select(session); Select select = new Select(session, null);
select.setWildcard(); select.setWildcard();
Table table = database.getSchema("INFORMATION_SCHEMA").resolveTableOrView(session, "HELP"); Table table = database.getSchema("INFORMATION_SCHEMA").resolveTableOrView(session, "HELP");
Function function = Function.getFunction(database, "UPPER"); Function function = Function.getFunction(database, "UPPER");
...@@ -1535,7 +1535,7 @@ public class Parser { ...@@ -1535,7 +1535,7 @@ public class Parser {
TableFilter sourceTableFilter = readSimpleTableFilter(0, null); TableFilter sourceTableFilter = readSimpleTableFilter(0, null);
command.setSourceTableFilter(sourceTableFilter); command.setSourceTableFilter(sourceTableFilter);
Select preparedQuery = new Select(session); Select preparedQuery = new Select(session, null);
preparedQuery.setWildcard(); preparedQuery.setWildcard();
TableFilter filter = new TableFilter(session, sourceTableFilter.getTable(), TableFilter filter = new TableFilter(session, sourceTableFilter.getTable(),
sourceTableFilter.getTableAlias(), rightsChecked, preparedQuery, 0, null); sourceTableFilter.getTableAlias(), rightsChecked, preparedQuery, 0, null);
...@@ -2689,7 +2689,7 @@ public class Parser { ...@@ -2689,7 +2689,7 @@ public class Parser {
} else if (readIf(TABLE)) { } else if (readIf(TABLE)) {
int start = lastParseIndex; int start = lastParseIndex;
Table table = readTableOrView(); Table table = readTableOrView();
Select command = new Select(session); Select command = new Select(session, currentSelect);
TableFilter filter = new TableFilter(session, table, null, rightsChecked, TableFilter filter = new TableFilter(session, table, null, rightsChecked,
command, orderInFrom++, null); command, orderInFrom++, null);
command.addTableFilter(filter, true); command.addTableFilter(filter, true);
...@@ -2703,7 +2703,7 @@ public class Parser { ...@@ -2703,7 +2703,7 @@ public class Parser {
} else { } else {
throw getSyntaxError(); throw getSyntaxError();
} }
Select command = new Select(session); Select command = new Select(session, currentSelect);
int start = lastParseIndex; int start = lastParseIndex;
Select oldSelect = currentSelect; Select oldSelect = currentSelect;
Prepared oldPrepared = currentPrepared; Prepared oldPrepared = currentPrepared;
...@@ -5867,7 +5867,7 @@ public class Parser { ...@@ -5867,7 +5867,7 @@ public class Parser {
} }
private Select parseValues() { private Select parseValues() {
Select command = new Select(session); Select command = new Select(session, currentSelect);
currentSelect = command; currentSelect = command;
TableFilter filter = parseValuesTable(0); TableFilter filter = parseValuesTable(0);
command.setWildcard(); command.setWildcard();
......
...@@ -419,7 +419,7 @@ public class MergeUsing extends Prepared { ...@@ -419,7 +419,7 @@ public class MergeUsing extends Prepared {
} }
// setup the targetMatchQuery - for detecting if the target row exists // setup the targetMatchQuery - for detecting if the target row exists
targetMatchQuery = new Select(session); targetMatchQuery = new Select(session, null);
ArrayList<Expression> expressions = new ArrayList<>(1); ArrayList<Expression> expressions = new ArrayList<>(1);
expressions.add(new ExpressionColumn(session.getDatabase(), targetTable.getSchema().getName(), expressions.add(new ExpressionColumn(session.getDatabase(), targetTable.getSchema().getName(),
targetTableFilter.getTableAlias(), "_ROWID_")); targetTableFilter.getTableAlias(), "_ROWID_"));
......
...@@ -78,6 +78,11 @@ public class Select extends Query { ...@@ -78,6 +78,11 @@ public class Select extends Query {
private final ArrayList<TableFilter> filters = Utils.newSmallArrayList(); private final ArrayList<TableFilter> filters = Utils.newSmallArrayList();
private final ArrayList<TableFilter> topFilters = Utils.newSmallArrayList(); private final ArrayList<TableFilter> topFilters = Utils.newSmallArrayList();
/**
* Parent select for selects in table filters.
*/
private Select parentSelect;
/** /**
* WHERE condition. * WHERE condition.
*/ */
...@@ -146,8 +151,9 @@ public class Select extends Query { ...@@ -146,8 +151,9 @@ public class Select extends Query {
private HashMap<String, Window> windows; private HashMap<String, Window> windows;
public Select(Session session) { public Select(Session session, Select parentSelect) {
super(session); super(session);
this.parentSelect = parentSelect;
} }
@Override @Override
...@@ -1956,4 +1962,14 @@ public class Select extends Query { ...@@ -1956,4 +1962,14 @@ public class Select extends Query {
return row; return row;
} }
} }
/**
* Returns parent select, or null.
*
* @return parent select, or null
*/
public Select getParentSelect() {
return parentSelect;
}
} }
...@@ -8,6 +8,7 @@ package org.h2.expression.analysis; ...@@ -8,6 +8,7 @@ package org.h2.expression.analysis;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.dml.Select;
import org.h2.command.dml.SelectOrderBy; import org.h2.command.dml.SelectOrderBy;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Expression; import org.h2.expression.Expression;
...@@ -104,10 +105,14 @@ public final class Window { ...@@ -104,10 +105,14 @@ public final class Window {
private void resolveWindows(ColumnResolver resolver) { private void resolveWindows(ColumnResolver resolver) {
if (parent != null) { if (parent != null) {
Window p = resolver.getSelect().getWindow(parent); Select select = resolver.getSelect();
if (p == null) { Window p;
while ((p = select.getWindow(parent)) == null) {
select = select.getParentSelect();
if (select == null) {
throw DbException.get(ErrorCode.WINDOW_NOT_FOUND_1, parent); throw DbException.get(ErrorCode.WINDOW_NOT_FOUND_1, parent);
} }
}
p.resolveWindows(resolver); p.resolveWindows(resolver);
if (partitionBy == null) { if (partitionBy == null) {
partitionBy = p.partitionBy; partitionBy = p.partitionBy;
......
...@@ -611,6 +611,13 @@ SELECT A, SUM(S) OVER (ORDER BY S) FROM ...@@ -611,6 +611,13 @@ SELECT A, SUM(S) OVER (ORDER BY S) FROM
> 3 20 > 3 20
> rows: 2 > rows: 2
SELECT A, SUM(A) OVER W SUM FROM (VALUES 1, 2) T(A) WINDOW W AS (ORDER BY A);
> A SUM
> - ---
> 1 1
> 2 3
> rows: 2
SELECT A, B, C FROM (SELECT A, B, C FROM (VALUES (1, 2, 3)) V(A, B, C)); SELECT A, B, C FROM (SELECT A, B, C FROM (VALUES (1, 2, 3)) V(A, B, C));
> A B C > A B C
> - - - > - - -
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论