提交 995ae3c4 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Allow usage of SAMPLE_SIZE together with OFFSET / FETCH

上级 ea6ce813
...@@ -10,10 +10,10 @@ FROM tableExpression [,...] [ WHERE expression ] ...@@ -10,10 +10,10 @@ FROM tableExpression [,...] [ WHERE expression ]
[ GROUP BY expression [,...] ] [ HAVING expression ] [ GROUP BY expression [,...] ] [ HAVING expression ]
[ { UNION [ ALL ] | MINUS | EXCEPT | INTERSECT } select ] [ { UNION [ ALL ] | MINUS | EXCEPT | INTERSECT } select ]
[ ORDER BY order [,...] ] [ ORDER BY order [,...] ]
[ { LIMIT expression [ OFFSET expression ] [ SAMPLE_SIZE rowCountInt ] } [ { { LIMIT expression [ OFFSET expression ] }
| { [ OFFSET expression { ROW | ROWS } ] | { [ OFFSET expression { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ expression [ PERCENT ] ] { ROW | ROWS } [ FETCH { FIRST | NEXT } [ expression [ PERCENT ] ] { ROW | ROWS }
{ ONLY | WITH TIES } ] } ] { ONLY | WITH TIES } ] } } [ SAMPLE_SIZE rowCountInt ] ]
[ FOR UPDATE ] [ FOR UPDATE ]
"," ","
Selects data from a table or multiple tables. Selects data from a table or multiple tables.
......
...@@ -2361,7 +2361,7 @@ public class Parser { ...@@ -2361,7 +2361,7 @@ public class Parser {
// make sure aggregate functions will not work here // make sure aggregate functions will not work here
Select temp = currentSelect; Select temp = currentSelect;
currentSelect = null; currentSelect = null;
// http://sqlpro.developpez.com/SQL2008/ // Standard SQL OFFSET / FETCH
if (readIf(OFFSET)) { if (readIf(OFFSET)) {
command.setOffset(readExpression().optimize(session)); command.setOffset(readExpression().optimize(session));
if (!readIf("ROW")) { if (!readIf("ROW")) {
...@@ -2391,11 +2391,8 @@ public class Parser { ...@@ -2391,11 +2391,8 @@ public class Parser {
read("ONLY"); read("ONLY");
} }
} }
currentSelect = temp; // MySQL-style LIMIT / OFFSET
if (readIf(LIMIT)) { if (readIf(LIMIT)) {
temp = currentSelect;
// make sure aggregate functions will not work here
currentSelect = null;
Expression limit = readExpression().optimize(session); Expression limit = readExpression().optimize(session);
command.setLimit(limit); command.setLimit(limit);
if (readIf(OFFSET)) { if (readIf(OFFSET)) {
...@@ -2408,12 +2405,12 @@ public class Parser { ...@@ -2408,12 +2405,12 @@ public class Parser {
command.setOffset(offset); command.setOffset(offset);
command.setLimit(limit); command.setLimit(limit);
} }
}
if (readIf("SAMPLE_SIZE")) { if (readIf("SAMPLE_SIZE")) {
Expression sampleSize = readExpression().optimize(session); Expression sampleSize = readExpression().optimize(session);
command.setSampleSize(sampleSize); command.setSampleSize(sampleSize);
} }
currentSelect = temp; currentSelect = temp;
}
if (readIf(FOR)) { if (readIf(FOR)) {
if (readIf("UPDATE")) { if (readIf("UPDATE")) {
if (readIf("OF")) { if (readIf("OF")) {
......
...@@ -115,14 +115,13 @@ public class Analyze extends DefineCommand { ...@@ -115,14 +115,13 @@ public class Analyze extends DefineCommand {
} }
buff.append(" FROM ").append(table.getSQL()); buff.append(" FROM ").append(table.getSQL());
if (sample > 0) { if (sample > 0) {
buff.append(" LIMIT ? SAMPLE_SIZE ? "); buff.append(" FETCH NEXT ROW ONLY SAMPLE_SIZE ? ");
} }
String sql = buff.toString(); String sql = buff.toString();
Prepared command = session.prepare(sql); Prepared command = session.prepare(sql);
if (sample > 0) { if (sample > 0) {
ArrayList<Parameter> params = command.getParameters(); ArrayList<Parameter> params = command.getParameters();
params.get(0).setValue(ValueInt.get(1)); params.get(0).setValue(ValueInt.get(sample));
params.get(1).setValue(ValueInt.get(sample));
} }
ResultInterface result = command.query(0); ResultInterface result = command.query(0);
result.next(); result.next();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论