use of org.h2.table.Table in project ignite by apache.
the class UpdatePlanBuilder method planForUpdate.
/**
* Prepare update plan for UPDATE or DELETE.
*
* @param stmt UPDATE or DELETE statement.
* @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
private static UpdatePlan planForUpdate(GridSqlStatement stmt, @Nullable Integer errKeysPos) throws IgniteCheckedException {
GridSqlElement target;
FastUpdateArguments fastUpdate;
UpdateMode mode;
if (stmt instanceof GridSqlUpdate) {
// Let's verify that user is not trying to mess with key's columns directly
verifyUpdateColumns(stmt);
GridSqlUpdate update = (GridSqlUpdate) stmt;
target = update.target();
fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
mode = UpdateMode.UPDATE;
} else if (stmt instanceof GridSqlDelete) {
GridSqlDelete del = (GridSqlDelete) stmt;
target = del.from();
fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
mode = UpdateMode.DELETE;
} else
throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
GridSqlTable tbl = gridTableForElement(target);
GridH2Table gridTbl = tbl.dataTable();
GridH2RowDescriptor desc = gridTbl.rowDescriptor();
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + gridTbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
if (fastUpdate != null)
return UpdatePlan.forFastUpdate(mode, gridTbl, fastUpdate);
else {
GridSqlSelect sel;
if (stmt instanceof GridSqlUpdate) {
List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
int valColIdx = -1;
String[] colNames = new String[updatedCols.size()];
int[] colTypes = new int[updatedCols.size()];
for (int i = 0; i < updatedCols.size(); i++) {
colNames[i] = updatedCols.get(i).columnName();
colTypes[i] = updatedCols.get(i).resultType().type();
Column column = updatedCols.get(i).column();
if (desc.isValueColumn(column.getColumnId()))
valColIdx = i;
}
boolean hasNewVal = (valColIdx != -1);
// Statement updates distinct properties if it does not have _val in updated columns list
// or if its list of updated columns includes only _val, i.e. is single element.
boolean hasProps = !hasNewVal || updatedCols.size() > 1;
// Index of new _val in results of SELECT
if (hasNewVal)
valColIdx += 2;
int newValColIdx = (hasNewVal ? valColIdx : 1);
KeyValueSupplier newValSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
return UpdatePlan.forUpdate(gridTbl, colNames, colTypes, newValSupplier, valColIdx, sel.getSQL());
} else {
sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
return UpdatePlan.forDelete(gridTbl, sel.getSQL());
}
}
}
use of org.h2.table.Table in project ignite by apache.
the class GridH2CollocationModel method calculate.
/**
* Do the needed calculations.
*/
private void calculate() {
if (type != null)
return;
if (view) {
// We are at (sub-)query model.
assert childFilters != null;
boolean collocated = true;
boolean partitioned = false;
int maxMultiplier = MULTIPLIER_COLLOCATED;
for (int i = 0; i < childFilters.length; i++) {
GridH2CollocationModel child = child(i, true);
Type t = child.type(true);
if (child.multiplier == MULTIPLIER_REPLICATED_NOT_LAST)
maxMultiplier = child.multiplier;
if (t.isPartitioned()) {
partitioned = true;
if (!t.isCollocated()) {
collocated = false;
int m = child.multiplier(true);
if (m > maxMultiplier) {
maxMultiplier = m;
if (maxMultiplier == MULTIPLIER_REPLICATED_NOT_LAST)
break;
}
}
}
}
type = Type.of(partitioned, collocated);
multiplier = maxMultiplier;
} else {
assert upper != null;
assert childFilters == null;
// We are at table instance.
Table tbl = filter().getTable();
// Only partitioned tables will do distributed joins.
if (!(tbl instanceof GridH2Table) || !((GridH2Table) tbl).isPartitioned()) {
type = Type.REPLICATED;
multiplier = MULTIPLIER_COLLOCATED;
return;
}
// to all the affinity nodes the "base" does not need to get remote results.
if (!upper.findPartitionedTableBefore(filter)) {
type = Type.PARTITIONED_COLLOCATED;
multiplier = MULTIPLIER_COLLOCATED;
} else {
// collocated. If we at least have affinity key condition, then we do unicast which is cheaper.
switch(upper.joinedWithCollocated(filter)) {
case COLLOCATED_JOIN:
type = Type.PARTITIONED_COLLOCATED;
multiplier = MULTIPLIER_COLLOCATED;
break;
case HAS_AFFINITY_CONDITION:
type = Type.PARTITIONED_NOT_COLLOCATED;
multiplier = MULTIPLIER_UNICAST;
break;
case NONE:
type = Type.PARTITIONED_NOT_COLLOCATED;
multiplier = MULTIPLIER_BROADCAST;
break;
default:
throw new IllegalStateException();
}
}
if (upper.previousReplicated(filter))
multiplier = MULTIPLIER_REPLICATED_NOT_LAST;
}
}
use of org.h2.table.Table in project ignite by apache.
the class GridH2CollocationModel method isAffinityColumn.
/**
* @param f Table filter.
* @param expCol Expression column.
* @param validate Query validation flag.
* @return {@code true} It it is an affinity column.
*/
private static boolean isAffinityColumn(TableFilter f, ExpressionColumn expCol, boolean validate) {
Column col = expCol.getColumn();
if (col == null)
return false;
Table t = col.getTable();
if (t.isView()) {
Query qry;
if (f.getIndex() != null)
qry = getSubQuery(f);
else
qry = GridSqlQueryParser.VIEW_QUERY.get((TableView) t);
return isAffinityColumn(qry, expCol, validate);
}
if (t instanceof GridH2Table) {
if (validate && ((GridH2Table) t).rowDescriptor().context().customAffinityMapper())
throw customAffinityError(((GridH2Table) t).cacheName());
IndexColumn affCol = ((GridH2Table) t).getAffinityKeyColumn();
return affCol != null && col.getColumnId() == affCol.column.getColumnId();
}
return false;
}
use of org.h2.table.Table in project ignite by apache.
the class GridQueryParsingTest method assertCreateTableEquals.
/**
* Parse SQL and compare it to expected instance of DROP TABLE.
*/
private void assertCreateTableEquals(GridSqlCreateTable exp, String sql) throws Exception {
Prepared prepared = parse(sql);
GridSqlStatement stmt = new GridSqlQueryParser(false).parse(prepared);
assertTrue(stmt instanceof GridSqlCreateTable);
assertCreateTableEquals(exp, (GridSqlCreateTable) stmt);
}
Aggregations