use of org.h2.table.Column in project ignite by apache.
the class GridReduceQueryExecutor method planColumns.
/**
* @return Columns.
*/
private static ArrayList<Column> planColumns() {
ArrayList<Column> res = new ArrayList<>(1);
res.add(new Column("PLAN", Value.STRING));
return res;
}
use of org.h2.table.Column in project ignite by apache.
the class GridReduceQueryExecutor method createMergeTable.
/**
* @param conn Connection.
* @param qry Query.
* @param explain Explain.
* @return Table.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private GridMergeTable createMergeTable(JdbcConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
try {
Session ses = (Session) conn.getSession();
CreateTableData data = new CreateTableData();
data.tableName = "T___";
data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
data.create = true;
if (!explain) {
LinkedHashMap<String, ?> colsMap = qry.columns();
assert colsMap != null;
ArrayList<Column> cols = new ArrayList<>(colsMap.size());
for (Map.Entry<String, ?> e : colsMap.entrySet()) {
String alias = e.getKey();
GridSqlType t = (GridSqlType) e.getValue();
assert !F.isEmpty(alias);
Column c = new Column(alias, t.type(), t.precision(), t.scale(), t.displaySize());
cols.add(c);
}
data.columns = cols;
} else
data.columns = planColumns();
boolean sortedIndex = !F.isEmpty(qry.sortColumns());
GridMergeTable tbl = new GridMergeTable(data);
ArrayList<Index> idxs = new ArrayList<>(2);
if (explain) {
idxs.add(new GridMergeIndexUnsorted(ctx, tbl, sortedIndex ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
} else if (sortedIndex) {
List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
GridMergeIndexSorted sortedMergeIdx = new GridMergeIndexSorted(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
idxs.add(GridMergeTable.createScanIndex(sortedMergeIdx));
idxs.add(sortedMergeIdx);
} else
idxs.add(new GridMergeIndexUnsorted(ctx, tbl, MERGE_INDEX_UNSORTED));
tbl.indexes(idxs);
return tbl;
} catch (Exception e) {
U.closeQuiet(conn);
throw new IgniteCheckedException(e);
}
}
use of org.h2.table.Column in project ignite by apache.
the class GridSqlQueryParser method parseUpdate.
/**
* @param update Update.
* @see <a href="http://h2database.com/html/grammar.html#update">H2 update spec</a>
*/
private GridSqlUpdate parseUpdate(Update update) {
GridSqlUpdate res = (GridSqlUpdate) h2ObjToGridObj.get(update);
if (res != null)
return res;
res = new GridSqlUpdate();
h2ObjToGridObj.put(update, res);
GridSqlElement tbl = parseTableFilter(UPDATE_TARGET.get(update));
List<Column> srcCols = UPDATE_COLUMNS.get(update);
Map<Column, Expression> srcSet = UPDATE_SET.get(update);
ArrayList<GridSqlColumn> cols = new ArrayList<>(srcCols.size());
LinkedHashMap<String, GridSqlElement> set = new LinkedHashMap<>(srcSet.size());
for (Column c : srcCols) {
GridSqlColumn col = new GridSqlColumn(c, tbl, null, null, c.getName());
col.resultType(fromColumn(c));
cols.add(col);
set.put(col.columnName(), parseExpression(srcSet.get(c), true));
}
GridSqlElement where = parseExpression(UPDATE_WHERE.get(update), true);
GridSqlElement limit = parseExpression(UPDATE_LIMIT.get(update), true);
res.target(tbl).cols(cols).set(set).where(where).limit(limit);
return res;
}
use of org.h2.table.Column in project ignite by apache.
the class GridSqlQueryParser method parseExpression0.
/**
* @param expression Expression.
* @param calcTypes Calculate types for all the expressions.
* @return Parsed expression.
*/
private GridSqlElement parseExpression0(Expression expression, boolean calcTypes) {
if (expression instanceof ExpressionColumn) {
ExpressionColumn expCol = (ExpressionColumn) expression;
return new GridSqlColumn(expCol.getColumn(), parseTableFilter(expCol.getTableFilter()), SCHEMA_NAME.get(expCol), expCol.getOriginalTableAliasName(), expCol.getColumnName());
}
if (expression instanceof Alias)
return new GridSqlAlias(expression.getAlias(), parseExpression(expression.getNonAliasExpression(), calcTypes), true);
if (expression instanceof ValueExpression)
// == comparison is legit, see ValueExpression#getSQL()
return expression == ValueExpression.getDefault() ? GridSqlKeyword.DEFAULT : new GridSqlConst(expression.getValue(null));
if (expression instanceof Operation) {
Operation operation = (Operation) expression;
Integer type = OPERATION_TYPE.get(operation);
if (type == Operation.NEGATE) {
assert OPERATION_RIGHT.get(operation) == null;
return new GridSqlOperation(GridSqlOperationType.NEGATE, parseExpression(OPERATION_LEFT.get(operation), calcTypes));
}
return new GridSqlOperation(OPERATION_OP_TYPES[type], parseExpression(OPERATION_LEFT.get(operation), calcTypes), parseExpression(OPERATION_RIGHT.get(operation), calcTypes));
}
if (expression instanceof Comparison) {
Comparison cmp = (Comparison) expression;
GridSqlOperationType opType = COMPARISON_TYPES[COMPARISON_TYPE.get(cmp)];
assert opType != null : COMPARISON_TYPE.get(cmp);
Expression leftExp = COMPARISON_LEFT.get(cmp);
GridSqlElement left = parseExpression(leftExp, calcTypes);
if (opType.childrenCount() == 1)
return new GridSqlOperation(opType, left);
Expression rightExp = COMPARISON_RIGHT.get(cmp);
GridSqlElement right = parseExpression(rightExp, calcTypes);
return new GridSqlOperation(opType, left, right);
}
if (expression instanceof ConditionNot)
return new GridSqlOperation(NOT, parseExpression(expression.getNotIfPossible(null), calcTypes));
if (expression instanceof ConditionAndOr) {
ConditionAndOr andOr = (ConditionAndOr) expression;
int type = ANDOR_TYPE.get(andOr);
assert type == ConditionAndOr.AND || type == ConditionAndOr.OR;
return new GridSqlOperation(type == ConditionAndOr.AND ? AND : OR, parseExpression(ANDOR_LEFT.get(andOr), calcTypes), parseExpression(ANDOR_RIGHT.get(andOr), calcTypes));
}
if (expression instanceof Subquery) {
Query qry = ((Subquery) expression).getQuery();
return parseQueryExpression(qry);
}
if (expression instanceof ConditionIn) {
GridSqlOperation res = new GridSqlOperation(IN);
res.addChild(parseExpression(LEFT_CI.get((ConditionIn) expression), calcTypes));
List<Expression> vals = VALUE_LIST_CI.get((ConditionIn) expression);
for (Expression val : vals) res.addChild(parseExpression(val, calcTypes));
return res;
}
if (expression instanceof ConditionInConstantSet) {
GridSqlOperation res = new GridSqlOperation(IN);
res.addChild(parseExpression(LEFT_CICS.get((ConditionInConstantSet) expression), calcTypes));
List<Expression> vals = VALUE_LIST_CICS.get((ConditionInConstantSet) expression);
for (Expression val : vals) res.addChild(parseExpression(val, calcTypes));
return res;
}
if (expression instanceof ConditionInSelect) {
GridSqlOperation res = new GridSqlOperation(IN);
boolean all = ALL.get((ConditionInSelect) expression);
int compareType = COMPARE_TYPE.get((ConditionInSelect) expression);
assert0(!all, expression);
assert0(compareType == Comparison.EQUAL, expression);
res.addChild(parseExpression(LEFT_CIS.get((ConditionInSelect) expression), calcTypes));
Query qry = QUERY_IN.get((ConditionInSelect) expression);
res.addChild(parseQueryExpression(qry));
return res;
}
if (expression instanceof CompareLike) {
assert0(ESCAPE.get((CompareLike) expression) == null, expression);
boolean regexp = REGEXP_CL.get((CompareLike) expression);
return new GridSqlOperation(regexp ? REGEXP : LIKE, parseExpression(LEFT.get((CompareLike) expression), calcTypes), parseExpression(RIGHT.get((CompareLike) expression), calcTypes));
}
if (expression instanceof Function) {
Function f = (Function) expression;
GridSqlFunction res = new GridSqlFunction(null, f.getName());
if (f.getArgs() != null) {
if (f.getFunctionType() == Function.TABLE || f.getFunctionType() == Function.TABLE_DISTINCT) {
Column[] cols = FUNC_TBL_COLS.get((TableFunction) f);
Expression[] args = f.getArgs();
assert cols.length == args.length;
for (int i = 0; i < cols.length; i++) {
GridSqlElement arg = parseExpression(args[i], calcTypes);
GridSqlAlias alias = new GridSqlAlias(cols[i].getName(), arg, false);
alias.resultType(fromColumn(cols[i]));
res.addChild(alias);
}
} else {
for (Expression arg : f.getArgs()) {
if (arg == null) {
if (f.getFunctionType() != Function.CASE)
throw new IllegalStateException("Function type with null arg: " + f.getFunctionType());
res.addChild(GridSqlPlaceholder.EMPTY);
} else
res.addChild(parseExpression(arg, calcTypes));
}
}
}
if (f.getFunctionType() == Function.CAST || f.getFunctionType() == Function.CONVERT)
res.resultType(fromExpression(f));
return res;
}
if (expression instanceof JavaFunction) {
JavaFunction f = (JavaFunction) expression;
FunctionAlias alias = FUNC_ALIAS.get(f);
GridSqlFunction res = new GridSqlFunction(alias.getSchema().getName(), f.getName());
if (f.getArgs() != null) {
for (Expression arg : f.getArgs()) res.addChild(parseExpression(arg, calcTypes));
}
return res;
}
if (expression instanceof Parameter)
return new GridSqlParameter(((Parameter) expression).getIndex());
if (expression instanceof Aggregate) {
int typeId = TYPE.get((Aggregate) expression);
if (GridSqlAggregateFunction.isValidType(typeId)) {
GridSqlAggregateFunction res = new GridSqlAggregateFunction(DISTINCT.get((Aggregate) expression), typeId);
Expression on = ON.get((Aggregate) expression);
if (on != null)
res.addChild(parseExpression(on, calcTypes));
return res;
}
}
if (expression instanceof ExpressionList) {
Expression[] exprs = EXPR_LIST.get((ExpressionList) expression);
GridSqlArray res = new GridSqlArray(exprs.length);
for (Expression expr : exprs) res.addChild(parseExpression(expr, calcTypes));
return res;
}
if (expression instanceof ConditionExists) {
Query qry = QUERY_EXISTS.get((ConditionExists) expression);
GridSqlOperation res = new GridSqlOperation(EXISTS);
res.addChild(parseQueryExpression(qry));
return res;
}
throw new IgniteException("Unsupported expression: " + expression + " [type=" + expression.getClass().getSimpleName() + ']');
}
use of org.h2.table.Column 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);
Column[] srcKeys = MERGE_KEYS.get(merge);
GridSqlColumn[] keys = new GridSqlColumn[srcKeys.length];
for (int i = 0; i < srcKeys.length; i++) keys[i] = new GridSqlColumn(srcKeys[i], tbl, null, null, srcKeys[i].getName());
res.keys(keys);
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);
rows.add(row);
}
res.rows(rows);
} else {
res.rows(Collections.<GridSqlElement[]>emptyList());
res.query(parseQuery(MERGE_QUERY.get(merge)));
}
return res;
}
Aggregations