use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge in project ignite by apache.
the class UpdatePlanBuilder method planForInsert.
/**
* Prepare update plan for INSERT or MERGE.
*
* @param stmt INSERT or MERGE statement.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(GridSqlStatement stmt) throws IgniteCheckedException {
GridSqlQuery sel;
GridSqlElement target;
GridSqlColumn[] cols;
boolean isTwoStepSubqry;
int rowsNum;
GridSqlTable tbl;
GridH2RowDescriptor desc;
if (stmt instanceof GridSqlInsert) {
GridSqlInsert ins = (GridSqlInsert) stmt;
target = ins.into();
tbl = gridTableForElement(target);
desc = tbl.dataTable().rowDescriptor();
cols = ins.columns();
sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query(), desc);
isTwoStepSubqry = (ins.query() != null);
rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
} else if (stmt instanceof GridSqlMerge) {
GridSqlMerge merge = (GridSqlMerge) stmt;
target = merge.into();
tbl = gridTableForElement(target);
desc = tbl.dataTable().rowDescriptor();
// This check also protects us from attempts to update key or its fields directly -
// when no key except cache key can be used, it will serve only for uniqueness checks,
// not for updates, and hence will allow putting new pairs only.
// We don't quote _key and _val column names on CREATE TABLE, so they are always uppercase here.
GridSqlColumn[] keys = merge.keys();
if (keys.length != 1 || !desc.isKeyColumn(tbl.dataTable().getColumn(keys[0].columnName()).getColumnId()))
throw new CacheException("SQL MERGE does not support arbitrary keys");
cols = merge.columns();
sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query(), desc);
isTwoStepSubqry = (merge.query() != null);
rowsNum = isTwoStepSubqry ? 0 : merge.rows().size();
} else
throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
// Let's set the flag only for subqueries that have their FROM specified.
isTwoStepSubqry = (isTwoStepSubqry && (sel instanceof GridSqlUnion || (sel instanceof GridSqlSelect && ((GridSqlSelect) sel).from() != null)));
int keyColIdx = -1;
int valColIdx = -1;
boolean hasKeyProps = false;
boolean hasValProps = false;
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.dataTable().getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
GridCacheContext<?, ?> cctx = desc.context();
String[] colNames = new String[cols.length];
int[] colTypes = new int[cols.length];
for (int i = 0; i < cols.length; i++) {
GridSqlColumn col = cols[i];
String colName = col.columnName();
colNames[i] = colName;
colTypes[i] = col.resultType().type();
int colId = col.column().getColumnId();
if (desc.isKeyColumn(colId)) {
keyColIdx = i;
continue;
}
if (desc.isValueColumn(colId)) {
valColIdx = i;
continue;
}
GridQueryProperty prop = desc.type().property(colName);
assert prop != null : "Property '" + colName + "' not found.";
if (prop.key())
hasKeyProps = true;
else
hasValProps = true;
}
KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
if (stmt instanceof GridSqlMerge)
return UpdatePlan.forMerge(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
else
return UpdatePlan.forInsert(tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, sel.getSQL(), !isTwoStepSubqry, rowsNum);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge in project ignite by apache.
the class UpdatePlanBuilder method planForInsert.
/**
* Prepare update plan for INSERT or MERGE.
*
* @param stmt INSERT or MERGE statement.
* @param loc Local query flag.
* @param idx Indexing.
* @param conn Connection.
* @param fieldsQuery Original query.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(GridSqlStatement stmt, boolean loc, IgniteH2Indexing idx, @Nullable Connection conn, @Nullable SqlFieldsQuery fieldsQuery) throws IgniteCheckedException {
GridSqlQuery sel;
GridSqlElement target;
GridSqlColumn[] cols;
boolean isTwoStepSubqry;
int rowsNum;
GridSqlTable tbl;
GridH2RowDescriptor desc;
List<GridSqlElement[]> elRows = null;
if (stmt instanceof GridSqlInsert) {
GridSqlInsert ins = (GridSqlInsert) stmt;
target = ins.into();
tbl = DmlAstUtils.gridTableForElement(target);
desc = tbl.dataTable().rowDescriptor();
cols = ins.columns();
sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query());
if (sel == null)
elRows = ins.rows();
isTwoStepSubqry = (ins.query() != null);
rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
} else if (stmt instanceof GridSqlMerge) {
GridSqlMerge merge = (GridSqlMerge) stmt;
target = merge.into();
tbl = DmlAstUtils.gridTableForElement(target);
desc = tbl.dataTable().rowDescriptor();
cols = merge.columns();
sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query());
if (sel == null)
elRows = merge.rows();
isTwoStepSubqry = (merge.query() != null);
rowsNum = isTwoStepSubqry ? 0 : merge.rows().size();
} else {
throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
}
// Let's set the flag only for subqueries that have their FROM specified.
isTwoStepSubqry &= (sel != null && (sel instanceof GridSqlUnion || (sel instanceof GridSqlSelect && ((GridSqlSelect) sel).from() != null)));
int keyColIdx = -1;
int valColIdx = -1;
boolean hasKeyProps = false;
boolean hasValProps = false;
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.dataTable().getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
GridCacheContext<?, ?> cctx = desc.context();
String[] colNames = new String[cols.length];
int[] colTypes = new int[cols.length];
for (int i = 0; i < cols.length; i++) {
GridSqlColumn col = cols[i];
String colName = col.columnName();
colNames[i] = colName;
colTypes[i] = col.resultType().type();
int colId = col.column().getColumnId();
if (desc.isKeyColumn(colId)) {
keyColIdx = i;
continue;
}
if (desc.isValueColumn(colId)) {
valColIdx = i;
continue;
}
GridQueryProperty prop = desc.type().property(colName);
assert prop != null : "Property '" + colName + "' not found.";
if (prop.key())
hasKeyProps = true;
else
hasValProps = true;
}
KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
String selectSql = sel != null ? sel.getSQL() : null;
DmlDistributedPlanInfo distributed = (rowsNum == 0 && !F.isEmpty(selectSql)) ? checkPlanCanBeDistributed(idx, conn, fieldsQuery, loc, selectSql, tbl.dataTable().cacheName()) : null;
UpdateMode mode = stmt instanceof GridSqlMerge ? UpdateMode.MERGE : UpdateMode.INSERT;
List<List<DmlArgument>> rows = null;
if (elRows != null) {
assert sel == null;
rows = new ArrayList<>(elRows.size());
for (GridSqlElement[] elRow : elRows) {
List<DmlArgument> row = new ArrayList<>(cols.length);
for (GridSqlElement el : elRow) {
DmlArgument arg = DmlArguments.create(el);
row.add(arg);
}
rows.add(row);
}
}
return new UpdatePlan(mode, tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, selectSql, !isTwoStepSubqry, rows, rowsNum, null, distributed);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge in project ignite by apache.
the class GridSqlQueryParser method parseMerge.
/**
* @param merge Merge.
* @see <a href="http://h2database.com/html/grammar.html#merge">H2 merge spec</a>
*/
private GridSqlMerge parseMerge(Merge merge) {
GridSqlMerge res = (GridSqlMerge) h2ObjToGridObj.get(merge);
if (res != null)
return res;
res = new GridSqlMerge();
h2ObjToGridObj.put(merge, res);
Table srcTbl = MERGE_TABLE.get(merge);
GridSqlElement tbl = parseTable(srcTbl);
res.into(tbl);
Column[] srcCols = MERGE_COLUMNS.get(merge);
GridSqlColumn[] cols = new GridSqlColumn[srcCols.length];
for (int i = 0; i < srcCols.length; i++) {
cols[i] = new GridSqlColumn(srcCols[i], tbl, null, null, srcCols[i].getName());
cols[i].resultType(fromColumn(srcCols[i]));
}
res.columns(cols);
if (!F.isEmpty(MERGE_KEYS.get(merge))) {
log.warning("The search row by explicit KEY isn't supported. The primary key is always used to search row " + "[sql=" + merge.getSQL() + ']');
}
List<Expression[]> srcRows = MERGE_ROWS.get(merge);
if (!srcRows.isEmpty()) {
List<GridSqlElement[]> rows = new ArrayList<>(srcRows.size());
for (Expression[] srcRow : srcRows) {
GridSqlElement[] row = new GridSqlElement[srcRow.length];
for (int i = 0; i < srcRow.length; i++) {
row[i] = parseExpression(srcRow[i], false);
if (row[i] == null) {
throw new IgniteSQLException("Explicit DEFAULT values are unsupported for MERGE.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
}
rows.add(row);
}
res.rows(rows);
} else {
res.rows(Collections.emptyList());
res.query(parseQuery(MERGE_QUERY.get(merge)));
}
return res;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge in project ignite by apache.
the class GridSqlQueryParser method tablesForDml.
/**
* Extract all tables participating in DML statement.
*
* @return List of tables participate at query.
* @throws IgniteSQLException in case query contains virtual tables.
*/
public List<GridH2Table> tablesForDml() throws IgniteSQLException {
Collection<?> parserObjects = h2ObjToGridObj.values();
List<GridH2Table> tbls = new ArrayList<>(parserObjects.size());
// check all involved caches
for (Object o : parserObjects) {
if (o instanceof GridSqlMerge)
o = ((GridSqlMerge) o).into();
else if (o instanceof GridSqlInsert)
o = ((GridSqlInsert) o).into();
else if (o instanceof GridSqlUpdate)
o = ((GridSqlUpdate) o).target();
else if (o instanceof GridSqlDelete)
o = ((GridSqlDelete) o).from();
if (o instanceof GridSqlAlias)
o = GridSqlAlias.unwrap((GridSqlAst) o);
if (o instanceof GridSqlTable) {
GridH2Table h2tbl = ((GridSqlTable) o).dataTable();
if (h2tbl == null) {
// Check for virtual tables.
throw new IgniteSQLException("Operation not supported for table '" + ((GridSqlTable) o).tableName() + "'", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
tbls.add(h2tbl);
}
}
return tbls;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlMerge in project ignite by apache.
the class UpdatePlanBuilder method planForInsert.
/**
* Prepare update plan for INSERT or MERGE.
*
* @param planKey Plan key.
* @param stmt INSERT or MERGE statement.
* @param idx Indexing.
* @param mvccEnabled Mvcc flag.
* @return Update plan.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings("ConstantConditions")
private static UpdatePlan planForInsert(QueryDescriptor planKey, GridSqlStatement stmt, IgniteH2Indexing idx, boolean mvccEnabled, IgniteLogger log, boolean forceFillAbsentPKsWithDefaults) throws IgniteCheckedException {
GridSqlQuery sel = null;
GridSqlElement target;
GridSqlColumn[] cols;
boolean isTwoStepSubqry;
int rowsNum;
GridSqlTable tbl;
GridH2RowDescriptor desc;
List<GridSqlElement[]> elRows = null;
UpdateMode mode;
if (stmt instanceof GridSqlInsert) {
mode = UpdateMode.INSERT;
GridSqlInsert ins = (GridSqlInsert) stmt;
target = ins.into();
tbl = DmlAstUtils.gridTableForElement(target);
GridH2Table h2Tbl = tbl.dataTable();
assert h2Tbl != null;
desc = h2Tbl.rowDescriptor();
cols = ins.columns();
if (noQuery(ins.rows()))
elRows = ins.rows();
else
sel = DmlAstUtils.selectForInsertOrMerge(cols, ins.rows(), ins.query());
isTwoStepSubqry = (ins.query() != null);
rowsNum = isTwoStepSubqry ? 0 : ins.rows().size();
} else if (stmt instanceof GridSqlMerge) {
mode = UpdateMode.MERGE;
GridSqlMerge merge = (GridSqlMerge) stmt;
target = merge.into();
tbl = DmlAstUtils.gridTableForElement(target);
desc = tbl.dataTable().rowDescriptor();
cols = merge.columns();
if (noQuery(merge.rows()))
elRows = merge.rows();
else
sel = DmlAstUtils.selectForInsertOrMerge(cols, merge.rows(), merge.query());
isTwoStepSubqry = (merge.query() != null);
rowsNum = isTwoStepSubqry ? 0 : merge.rows().size();
} else {
throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
}
// Let's set the flag only for subqueries that have their FROM specified.
isTwoStepSubqry &= (sel != null && (sel instanceof GridSqlUnion || (sel instanceof GridSqlSelect && ((GridSqlSelect) sel).from() != null)));
int keyColIdx = -1;
int valColIdx = -1;
boolean hasKeyProps = false;
boolean hasValProps = false;
if (desc == null)
throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.dataTable().getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
GridCacheContext<?, ?> cctx = desc.context();
String[] colNames = new String[cols.length];
int[] colTypes = new int[cols.length];
GridQueryTypeDescriptor type = desc.type();
Set<String> rowKeys = desc.getRowKeyColumnNames();
boolean onlyVisibleColumns = true;
for (int i = 0; i < cols.length; i++) {
GridSqlColumn col = cols[i];
if (!col.column().getVisible())
onlyVisibleColumns = false;
String colName = col.columnName();
colNames[i] = colName;
colTypes[i] = col.resultType().type();
rowKeys.remove(colName);
int colId = col.column().getColumnId();
if (desc.isKeyColumn(colId)) {
keyColIdx = i;
continue;
}
if (desc.isValueColumn(colId)) {
valColIdx = i;
continue;
}
GridQueryProperty prop = desc.type().property(colName);
assert prop != null : "Property '" + colName + "' not found.";
if (prop.key())
hasKeyProps = true;
else
hasValProps = true;
}
rowKeys.removeIf(rowKey -> desc.type().property(rowKey).defaultValue() != null);
boolean fillAbsentPKsWithNullsOrDefaults = type.fillAbsentPKsWithDefaults() || forceFillAbsentPKsWithDefaults;
if (fillAbsentPKsWithNullsOrDefaults && onlyVisibleColumns && !rowKeys.isEmpty()) {
String[] extendedColNames = new String[rowKeys.size() + colNames.length];
int[] extendedColTypes = new int[rowKeys.size() + colTypes.length];
System.arraycopy(colNames, 0, extendedColNames, 0, colNames.length);
System.arraycopy(colTypes, 0, extendedColTypes, 0, colTypes.length);
int currId = colNames.length;
for (String key : rowKeys) {
Column col = tbl.dataTable().getColumn(key);
extendedColNames[currId] = col.getName();
extendedColTypes[currId] = col.getType();
currId++;
}
colNames = extendedColNames;
colTypes = extendedColTypes;
}
verifyDmlColumns(tbl.dataTable(), F.viewReadOnly(Arrays.asList(cols), TO_H2_COL));
KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
String selectSql = sel != null ? sel.getSQL() : null;
DmlDistributedPlanInfo distributed = null;
if (rowsNum == 0 && !F.isEmpty(selectSql)) {
distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
}
List<List<DmlArgument>> rows = null;
if (elRows != null) {
assert sel == null;
rows = new ArrayList<>(elRows.size());
for (GridSqlElement[] elRow : elRows) {
List<DmlArgument> row = new ArrayList<>(cols.length);
for (GridSqlElement el : elRow) {
DmlArgument arg = DmlArguments.create(el);
row.add(arg);
}
rows.add(row);
}
}
return new UpdatePlan(mode, tbl.dataTable(), colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, selectSql, !isTwoStepSubqry, rows, rowsNum, null, distributed, false, fillAbsentPKsWithNullsOrDefaults);
}
Aggregations