-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-2924: Lateral - fixed injection for tables and enhanced QueryExec API for easier testing. #2925
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.sparql.algebra.table; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.apache.jena.sparql.algebra.Table; | ||
import org.apache.jena.sparql.core.Var; | ||
import org.apache.jena.sparql.engine.QueryIterator; | ||
import org.apache.jena.sparql.engine.binding.Binding; | ||
import org.apache.jena.sparql.engine.join.ImmutableUniqueList; | ||
|
||
/** | ||
* Builder for immutable instances of {@link Table}. | ||
* This builder is not thread safe. | ||
*/ | ||
public class TableBuilder { | ||
private ImmutableUniqueList.Builder<Var> varsBuilder = ImmutableUniqueList.newUniqueListBuilder(Var.class); | ||
|
||
private List<Binding> rows = new ArrayList<>(); | ||
private boolean copyRowsOnNextMutation = false; | ||
|
||
// Vars ---- | ||
|
||
/** Returns an immutable snapshot of this builder's current variables. */ | ||
public List<Var> snapshotVars() { | ||
return varsBuilder.build(); | ||
} | ||
|
||
public int sizeVars() { | ||
return varsBuilder.size(); | ||
} | ||
|
||
public TableBuilder addVar(Var var) { | ||
varsBuilder.add(var); | ||
return this; | ||
} | ||
|
||
public TableBuilder addVars(Collection<Var> vars) { | ||
varsBuilder.addAll(vars); | ||
return this; | ||
} | ||
|
||
public TableBuilder addVars(Iterator<Var> vars) { | ||
vars.forEachRemaining(varsBuilder::add); | ||
return this; | ||
} | ||
|
||
/** Adds the variables of a binding but not the binding itself. */ | ||
public TableBuilder addVarsFromRow(Binding row) { | ||
row.vars().forEachRemaining(varsBuilder::add); | ||
return this; | ||
} | ||
|
||
// Rows ----- | ||
|
||
private void copyRowsIfNeeded() { | ||
if (copyRowsOnNextMutation) { | ||
rows = new ArrayList<>(rows); | ||
copyRowsOnNextMutation = false; | ||
} | ||
} | ||
|
||
/** Returns an immutable snapshot of this builder's current rows. */ | ||
public List<Binding> snapshotRows() { | ||
return List.copyOf(rows); | ||
} | ||
|
||
public int sizeRows() { | ||
return rows.size(); | ||
} | ||
|
||
public TableBuilder addRow(Binding row) { | ||
copyRowsIfNeeded(); | ||
rows.add(row); | ||
return this; | ||
} | ||
|
||
public TableBuilder addRows(Collection<Binding> newRows) { | ||
copyRowsIfNeeded(); | ||
rows.addAll(newRows); | ||
return this; | ||
} | ||
|
||
public TableBuilder addRows(Iterator<Binding> newRows) { | ||
copyRowsIfNeeded(); | ||
newRows.forEachRemaining(rows::add); | ||
return this; | ||
} | ||
|
||
// Rows and Vars ----- | ||
|
||
/** This method assumes prior call to copyRowsIfNeeded(). */ | ||
private void addRowAndVarsInternal(Binding row) { | ||
addVarsFromRow(row); | ||
rows.add(row); | ||
} | ||
|
||
public TableBuilder addRowAndVars(Binding row) { | ||
copyRowsIfNeeded(); | ||
addRowAndVarsInternal(row); | ||
return this; | ||
} | ||
|
||
public TableBuilder addRowsAndVars(Collection<Binding> newRows) { | ||
copyRowsIfNeeded(); | ||
newRows.forEach(this::addVarsFromRow); | ||
rows.addAll(newRows); | ||
return this; | ||
} | ||
|
||
public TableBuilder addRowsAndVars(Iterator<Binding> newRows) { | ||
copyRowsIfNeeded(); | ||
newRows.forEachRemaining(this::addRowAndVarsInternal); | ||
return this; | ||
} | ||
|
||
/** Add the rows and variables of another table. */ | ||
public TableBuilder addRowsAndVars(Table table) { | ||
addVars(table.getVars()); | ||
addRows(table.rows()); | ||
return this; | ||
} | ||
|
||
/** | ||
* Similar to {@link #addRowsAndVars(Iterator)} but | ||
* also closes the given QueryIterator when done. | ||
*/ | ||
public TableBuilder consumeRowsAndVars(QueryIterator qIter) { | ||
Objects.requireNonNull(qIter); | ||
try { | ||
addRowsAndVars(qIter); | ||
} finally { | ||
qIter.close(); | ||
} | ||
return this; | ||
} | ||
|
||
// General ----- | ||
|
||
public TableBuilder resetVars() { | ||
varsBuilder.clear(); | ||
return this; | ||
} | ||
|
||
public TableBuilder resetRows() { | ||
if (copyRowsOnNextMutation) { | ||
rows = new ArrayList<>(); | ||
copyRowsOnNextMutation = false; | ||
} else { | ||
rows.clear(); | ||
} | ||
return this; | ||
} | ||
|
||
/** Reset variables and rows. */ | ||
public TableBuilder reset() { | ||
resetVars(); | ||
resetRows(); | ||
return this; | ||
} | ||
|
||
public Table build() { | ||
List<Var> finalVars = snapshotVars(); | ||
List<Binding> finalRows = Collections.unmodifiableList(rows); | ||
copyRowsOnNextMutation = true; | ||
return new TableData(finalVars, finalRows); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,23 +18,21 @@ | |
|
||
package org.apache.jena.sparql.algebra.table ; | ||
|
||
import java.util.Collections; | ||
import java.util.List ; | ||
|
||
import org.apache.jena.sparql.ARQException ; | ||
import org.apache.jena.sparql.core.Var ; | ||
import org.apache.jena.sparql.engine.binding.Binding ; | ||
|
||
/** Immutable table. */ | ||
public class TableData extends TableN { | ||
public TableData(List<Var> variables, List<Binding> rows) { | ||
super(variables, rows) ; | ||
super(Collections.unmodifiableList(variables), Collections.unmodifiableList(rows)) ; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TableData appears to be intended as immutable but this was so far not enforced - one could call getRows() and modify them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor already existed. The wrappers prevent mutation via
its a step closer towards preventing incorrect use of |
||
} | ||
|
||
@Override | ||
public void addBinding(Binding binding) { | ||
throw new ARQException("Can't add bindings to an existing data table") ; | ||
} | ||
|
||
public List<Binding> getRows() { | ||
return rows ; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what going on. Could you explain this please?
This seems to duplicate the intent of
RowSet.materialize
andRowSet.rewindable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference (to my understanding) is that a Table acts as a Collection and a RowSet as an Iterator.
A Table can thus be seen as a factory for RowSets via
Table.toRowSet()
- each obtained RowSet is an independent iterator over the Table.My qualms with RowSet.materialize/rewindable is that these methods operate on the iterator level; it seemed clean to me to add Table as a collection of bindings.