Skip to content

Commit

Permalink
Merge pull request #3986 from katzyn/cte
Browse files Browse the repository at this point in the history
Move CTEs to their own scope of identifiers
  • Loading branch information
katzyn committed Jan 28, 2024
2 parents d2c2b32 + bad447b commit 494bd66
Show file tree
Hide file tree
Showing 39 changed files with 356 additions and 589 deletions.
2 changes: 2 additions & 0 deletions h2/src/docsrc/html/changelog.html
Expand Up @@ -21,6 +21,8 @@ <h1>Change Log</h1>

<h2>Next Version (unreleased)</h2>
<ul>
<li>Issue #910: Common Table Expressions (CTE) inside WITH should have their own identifier scope
</li>
<li>Issue #3981: Unexpected result when using trigonometric functions
</li>
<li>Issue #3983: INVISIBLE columns should be ignored in INSERT statement without explicit column list
Expand Down
52 changes: 1 addition & 51 deletions h2/src/main/org/h2/command/CommandContainer.java
Expand Up @@ -7,8 +7,8 @@

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode;
import org.h2.command.dml.DataChangeStatement;
Expand All @@ -29,7 +29,6 @@
import org.h2.table.Column;
import org.h2.table.DataChangeDeltaTable.ResultOption;
import org.h2.table.Table;
import org.h2.table.TableView;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.Value;
Expand Down Expand Up @@ -80,35 +79,6 @@ public void addRow(Value... values) {
private boolean readOnlyKnown;
private boolean readOnly;

/**
* Clears CTE views for a specified statement.
*
* @param session the session
* @param prepared prepared statement
*/
static void clearCTE(SessionLocal session, Prepared prepared) {
List<TableView> cteCleanups = prepared.getCteCleanups();
if (cteCleanups != null) {
clearCTE(session, cteCleanups);
}
}

/**
* Clears CTE views.
*
* @param session the session
* @param views list of view
*/
static void clearCTE(SessionLocal session, List<TableView> views) {
for (TableView view : views) {
// check if view was previously deleted as their name is set to
// null
if (view.getName() != null) {
session.removeLocalTempTable(view);
}
}
}

public CommandContainer(SessionLocal session, String sql, Prepared prepared) {
super(session, sql);
prepared.setCommand(this);
Expand Down Expand Up @@ -255,19 +225,6 @@ public ResultInterface query(long maxrows) {
return result;
}

@Override
public void stop() {
super.stop();
// Clean up after the command was run in the session.
// Must restart query (and dependency construction) to reuse.
clearCTE(session, prepared);
}

@Override
public boolean canReuse() {
return super.canReuse() && prepared.getCteCleanups() == null;
}

@Override
public boolean isReadOnly() {
if (!readOnlyKnown) {
Expand All @@ -292,13 +249,6 @@ public int getCommandType() {
return prepared.getType();
}

/**
* Clean up any associated CTE.
*/
void clearCTE() {
clearCTE(session, prepared);
}

@Override
public Set<DbObject> getDependencies() {
HashSet<DbObject> dependencies = new HashSet<>();
Expand Down
3 changes: 0 additions & 3 deletions h2/src/main/org/h2/command/CommandList.java
Expand Up @@ -77,9 +77,6 @@ public ResultInterface query(long maxrows) {
@Override
public void stop() {
command.stop();
for (Prepared prepared : commands) {
CommandContainer.clearCTE(session, prepared);
}
if (remainingCommand != null) {
remainingCommand.stop();
}
Expand Down

0 comments on commit 494bd66

Please sign in to comment.