提交 3e77d2fb authored 作者: Noel Grandin's avatar Noel Grandin

couple of cleanups to the CTE merge

上级 5a428e25
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue#479 Allow non-recursive CTEs (WITH statements), patch from stumc
</li>
<li>Fix startup issue when using "CHECK" as a column name. <li>Fix startup issue when using "CHECK" as a column name.
</li> </li>
<li>Issue #423: ANALYZE performed multiple times on one table during execution of the same statement. <li>Issue #423: ANALYZE performed multiple times on one table during execution of the same statement.
......
...@@ -18,7 +18,6 @@ import java.util.Comparator; ...@@ -18,7 +18,6 @@ import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.command.ddl.AlterIndexRename; import org.h2.command.ddl.AlterIndexRename;
...@@ -4859,9 +4858,9 @@ public class Parser { ...@@ -4859,9 +4858,9 @@ public class Parser {
private Query parseWith() { private Query parseWith() {
List<TableView> viewsCreated = new ArrayList<TableView>(); List<TableView> viewsCreated = new ArrayList<TableView>();
readIf("RECURSIVE"); readIf("RECURSIVE");
do{ do {
viewsCreated.add(parseSingleCommonTableExpression()); viewsCreated.add(parseSingleCommonTableExpression());
} while(readIf(",")); } while (readIf(","));
Query q = parseSelectUnion(); Query q = parseSelectUnion();
q.setPrepareAlways(true); q.setPrepareAlways(true);
...@@ -4877,10 +4876,11 @@ public class Parser { ...@@ -4877,10 +4876,11 @@ public class Parser {
// column names are now optional - they can be inferred from the named // column names are now optional - they can be inferred from the named
// query if not supplied // query if not supplied
if(readIf("(")){ if (readIf("(")) {
cols = parseColumnList(); cols = parseColumnList();
for (String c : cols) { for (String c : cols) {
// we dont really know the type of the column, so string will have to do // we dont really know the type of the column, so string will
// have to do
columns.add(new Column(c, Value.STRING)); columns.add(new Column(c, Value.STRING));
} }
} }
...@@ -4898,7 +4898,8 @@ public class Parser { ...@@ -4898,7 +4898,8 @@ public class Parser {
session.removeLocalTempTable(old); session.removeLocalTempTable(old);
} }
// this table is created as a work around because recursive // this table is created as a work around because recursive
// table expressions need to reference something that look like themselves // table expressions need to reference something that look like
// themselves
// to work (its removed after creation in this method) // to work (its removed after creation in this method)
CreateTableData data = new CreateTableData(); CreateTableData data = new CreateTableData();
data.id = database.allocateObjectId(); data.id = database.allocateObjectId();
...@@ -4922,26 +4923,26 @@ public class Parser { ...@@ -4922,26 +4923,26 @@ public class Parser {
querySQL = StringUtils.cache(withQuery.getPlanSQL()); querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions(); ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) { for (int i = 0; i < withExpressions.size(); ++i) {
String columnName = cols != null ? cols[i] : withExpressions.get(i).getColumnName(); String columnName = cols != null ? cols[i]
columnTemplateList.add(new Column(columnName, withExpressions.get(i).getType())); : withExpressions.get(i).getColumnName();
columnTemplateList.add(new Column(columnName,
withExpressions.get(i).getType()));
} }
} finally { } finally {
session.removeLocalTempTable(recursiveTable); session.removeLocalTempTable(recursiveTable);
} }
int id = database.allocateObjectId(); int id = database.allocateObjectId();
boolean isRecursive = true; // No easy way to determine if this is a recursive query up front, so we just compile
TableView view = null; // it twice - once without the flag set, and if we didn't see a recursive term,
do{ // then we just compile it again.
view = new TableView(schema, id, tempViewName, querySQL, TableView view = new TableView(schema, id, tempViewName, querySQL,
parameters, columnTemplateList.toArray(new Column[0]), session, parameters, columnTemplateList.toArray(new Column[0]), session,
isRecursive); true/* recursive */);
if(view.isRecursiveQueryDetected()!=isRecursive){ if (!view.isRecursiveQueryDetected()) {
isRecursive = view.isRecursiveQueryDetected(); view = new TableView(schema, id, tempViewName, querySQL, parameters,
view = null; columnTemplateList.toArray(new Column[0]), session,
continue; false/* recursive */);
} }
} while(view==null);
view.setTableExpression(true); view.setTableExpression(true);
view.setTemporary(true); view.setTemporary(true);
session.addLocalTempTable(view); session.addLocalTempTable(view);
......
...@@ -205,12 +205,11 @@ public class TableView extends Table { ...@@ -205,12 +205,11 @@ public class TableView extends Table {
} catch (DbException e) { } catch (DbException e) {
e.addSQL(getCreateSQL()); e.addSQL(getCreateSQL());
createException = e; createException = e;
// if it can't be compiled, then it's a 'zero column table' // If it can't be compiled, then it's a 'zero column table'
// this avoids problems when creating the view when opening the // this avoids problems when creating the view when opening the
// database // database.
// If it can not be compiled - it could also be a recursive common table expression query.
// if it can not be compiled - it could also be a recursive common table expression query if (isRecursiveQueryExceptionDetected(createException)) {
if(isRecursiveQueryExceptionDetected(createException)){
this.isRecursiveQueryDetected = true; this.isRecursiveQueryDetected = true;
} }
tables = New.arrayList(); tables = New.arrayList();
...@@ -674,25 +673,23 @@ public class TableView extends Table { ...@@ -674,25 +673,23 @@ public class TableView extends Table {
} }
/** /**
* If query recursion is detected (for recursion detection) * Was query recursion detected during compiling.
* @return is Recursive Query Flag Set
*/ */
public boolean isRecursiveQueryDetected(){ public boolean isRecursiveQueryDetected() {
return isRecursiveQueryDetected; return isRecursiveQueryDetected;
} }
/** /**
* If query an exception indicates query recursion * Does exception indicate query recursion?
* @return is Recursive Query Exception Detected
*/ */
private boolean isRecursiveQueryExceptionDetected(DbException exception){ private boolean isRecursiveQueryExceptionDetected(DbException exception) {
if (exception==null){ if (exception == null) {
return false; return false;
} }
if (exception.getErrorCode()!=ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1){ if (exception.getErrorCode() != ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1) {
return false; return false;
} }
if (! exception.getMessage().contains("\""+this.getName()+"\"")){ if (!exception.getMessage().contains("\"" + this.getName() + "\"")) {
return false; return false;
} }
return true; return true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论