use of org.h2.expression.Parameter in project ignite by apache.
the class GridSqlQueryParser method parseCreateTable.
/**
* Parse {@code CREATE TABLE} statement.
*
* @param createTbl {@code CREATE TABLE} statement.
* @see <a href="http://h2database.com/html/grammar.html#create_table">H2 {@code CREATE TABLE} spec.</a>
*/
private GridSqlCreateTable parseCreateTable(CreateTable createTbl) {
GridSqlCreateTable res = new GridSqlCreateTable();
res.templateName(QueryUtils.TEMPLATE_PARTITIONED);
Query qry = CREATE_TABLE_QUERY.get(createTbl);
if (qry != null)
throw new IgniteSQLException("CREATE TABLE ... AS ... syntax is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
List<DefineCommand> constraints = CREATE_TABLE_CONSTRAINTS.get(createTbl);
if (constraints.size() == 0)
throw new IgniteSQLException("No PRIMARY KEY defined for CREATE TABLE", IgniteQueryErrorCode.PARSING);
if (constraints.size() > 1)
throw new IgniteSQLException("Too many constraints - only PRIMARY KEY is supported for CREATE TABLE", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
DefineCommand constraint = constraints.get(0);
if (!(constraint instanceof AlterTableAddConstraint))
throw new IgniteSQLException("Unsupported type of constraint for CREATE TABLE - only PRIMARY KEY " + "is supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
AlterTableAddConstraint alterTbl = (AlterTableAddConstraint) constraint;
if (alterTbl.getType() != Command.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY)
throw new IgniteSQLException("Unsupported type of constraint for CREATE TABLE - only PRIMARY KEY " + "is supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
Schema schema = SCHEMA_COMMAND_SCHEMA.get(createTbl);
res.schemaName(schema.getName());
CreateTableData data = CREATE_TABLE_DATA.get(createTbl);
LinkedHashMap<String, GridSqlColumn> cols = new LinkedHashMap<>(data.columns.size());
for (Column col : data.columns) {
if (col.isAutoIncrement())
throw new IgniteSQLException("AUTO_INCREMENT columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (!col.isNullable())
throw new IgniteSQLException("Non nullable columns are forbidden [colName=" + col.getName() + ']', IgniteQueryErrorCode.PARSING);
if (COLUMN_IS_COMPUTED.get(col))
throw new IgniteSQLException("Computed columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (col.getDefaultExpression() != null)
throw new IgniteSQLException("DEFAULT expressions are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (col.getSequence() != null)
throw new IgniteSQLException("SEQUENCE columns are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (col.getSelectivity() != Constants.SELECTIVITY_DEFAULT)
throw new IgniteSQLException("SELECTIVITY column attr is not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (COLUMN_CHECK_CONSTRAINT.get(col) != null)
throw new IgniteSQLException("Column CHECK constraints are not supported [colName=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
GridSqlColumn gridCol = new GridSqlColumn(col, null, col.getName());
gridCol.resultType(GridSqlType.fromColumn(col));
cols.put(col.getName(), gridCol);
}
if (cols.containsKey(QueryUtils.KEY_FIELD_NAME.toUpperCase()) || cols.containsKey(QueryUtils.VAL_FIELD_NAME.toUpperCase()))
throw new IgniteSQLException("Direct specification of _KEY and _VAL columns is forbidden", IgniteQueryErrorCode.PARSING);
IndexColumn[] pkIdxCols = CREATE_TABLE_PK.get(createTbl);
if (F.isEmpty(pkIdxCols))
throw new AssertionError("No PRIMARY KEY columns specified");
LinkedHashSet<String> pkCols = new LinkedHashSet<>();
for (IndexColumn pkIdxCol : pkIdxCols) {
GridSqlColumn gridCol = cols.get(pkIdxCol.columnName);
assert gridCol != null;
pkCols.add(gridCol.columnName());
}
int valColsNum = cols.size() - pkCols.size();
if (valColsNum == 0)
throw new IgniteSQLException("No cache value related columns found");
res.columns(cols);
res.primaryKeyColumns(pkCols);
res.tableName(data.tableName);
res.ifNotExists(CREATE_TABLE_IF_NOT_EXISTS.get(createTbl));
List<String> extraParams = data.tableEngineParams != null ? new ArrayList<String>() : null;
if (data.tableEngineParams != null)
for (String s : data.tableEngineParams) extraParams.addAll(F.asList(s.split(",")));
res.params(extraParams);
if (!F.isEmpty(extraParams)) {
Map<String, String> params = new HashMap<>();
for (String p : extraParams) {
String[] parts = p.split(PARAM_NAME_VALUE_SEPARATOR);
if (parts.length > 2)
throw new IgniteSQLException("Invalid parameter (key[=value] expected): " + p, IgniteQueryErrorCode.PARSING);
String name = parts[0].trim().toUpperCase();
String val = parts.length > 1 ? parts[1].trim() : null;
if (F.isEmpty(name))
throw new IgniteSQLException("Invalid parameter (key[=value] expected): " + p, IgniteQueryErrorCode.PARSING);
if (params.put(name, val) != null)
throw new IgniteSQLException("Duplicate parameter: " + p, IgniteQueryErrorCode.PARSING);
}
for (Map.Entry<String, String> e : params.entrySet()) processExtraParam(e.getKey(), e.getValue(), res);
}
return res;
}
use of org.h2.expression.Parameter in project ignite by apache.
the class GridSqlQueryParser method processExtraParam.
/**
* @param name Param name.
* @param val Param value.
* @param res Table params to update.
*/
private static void processExtraParam(String name, String val, GridSqlCreateTable res) {
assert !F.isEmpty(name);
switch(name) {
case PARAM_TEMPLATE:
ensureNotEmpty(name, val);
res.templateName(val);
break;
case PARAM_BACKUPS:
ensureNotEmpty(name, val);
int backups = parseIntParam(PARAM_BACKUPS, val);
if (backups < 0)
throw new IgniteSQLException("\"" + PARAM_BACKUPS + "\" cannot be negative: " + backups, IgniteQueryErrorCode.PARSING);
res.backups(backups);
break;
case PARAM_ATOMICITY:
ensureNotEmpty(name, val);
CacheAtomicityMode mode;
if (CacheAtomicityMode.TRANSACTIONAL.name().equalsIgnoreCase(val))
mode = CacheAtomicityMode.TRANSACTIONAL;
else if (CacheAtomicityMode.ATOMIC.name().equalsIgnoreCase(val))
mode = CacheAtomicityMode.ATOMIC;
else
throw new IgniteSQLException("Invalid value of \"" + PARAM_ATOMICITY + "\" parameter " + "(should be either TRANSACTIONAL or ATOMIC): " + val, IgniteQueryErrorCode.PARSING);
res.atomicityMode(mode);
break;
default:
throw new IgniteSQLException("Unsupported parameter: " + name, IgniteQueryErrorCode.PARSING);
}
}
use of org.h2.expression.Parameter in project ignite by apache.
the class GridSqlQuerySplitter method split.
/**
* @param stmt Prepared statement.
* @param params Parameters.
* @param collocatedGrpBy Whether the query has collocated GROUP BY keys.
* @param distributedJoins If distributed joins enabled.
* @param enforceJoinOrder Enforce join order.
* @param h2 Indexing.
* @return Two step query.
* @throws SQLException If failed.
* @throws IgniteCheckedException If failed.
*/
public static GridCacheTwoStepQuery split(JdbcPreparedStatement stmt, Object[] params, boolean collocatedGrpBy, boolean distributedJoins, boolean enforceJoinOrder, IgniteH2Indexing h2) throws SQLException, IgniteCheckedException {
if (params == null)
params = GridCacheSqlQuery.EMPTY_PARAMS;
// Here we will just do initial query parsing. Do not use optimized
// subqueries because we do not have unique FROM aliases yet.
GridSqlQuery qry = parse(prepared(stmt), false);
String originalSql = qry.getSQL();
final boolean explain = qry.explain();
qry.explain(false);
GridSqlQuerySplitter splitter = new GridSqlQuerySplitter(params, collocatedGrpBy, h2.kernalContext());
// Normalization will generate unique aliases for all the table filters in FROM.
// Also it will collect all tables and schemas from the query.
splitter.normalizeQuery(qry);
Connection conn = stmt.getConnection();
// Here we will have correct normalized AST with optimized join order.
// The distributedJoins parameter is ignored because it is not relevant for
// the REDUCE query optimization.
qry = parse(optimize(h2, conn, qry.getSQL(), params, false, enforceJoinOrder), true);
// Do the actual query split. We will update the original query AST, need to be careful.
splitter.splitQuery(qry);
// We must have at least one map query.
assert !F.isEmpty(splitter.mapSqlQrys) : "map";
// We must have a reduce query.
assert splitter.rdcSqlQry != null : "rdc";
// distributed joins at all.
if (distributedJoins) {
boolean allCollocated = true;
for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) {
Prepared prepared = optimize(h2, conn, mapSqlQry.query(), mapSqlQry.parameters(params), true, enforceJoinOrder);
allCollocated &= isCollocated((Query) prepared);
mapSqlQry.query(parse(prepared, true).getSQL());
}
// We do not need distributed joins if all MAP queries are collocated.
if (allCollocated)
distributedJoins = false;
}
// Setup resulting two step query and return it.
GridCacheTwoStepQuery twoStepQry = new GridCacheTwoStepQuery(originalSql, splitter.tbls);
twoStepQry.reduceQuery(splitter.rdcSqlQry);
for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) twoStepQry.addMapQuery(mapSqlQry);
twoStepQry.skipMergeTable(splitter.rdcQrySimple);
twoStepQry.explain(explain);
twoStepQry.distributedJoins(distributedJoins);
// all map queries must have non-empty derivedPartitions to use this feature.
twoStepQry.derivedPartitions(mergePartitionsFromMultipleQueries(twoStepQry.mapQueries()));
return twoStepQry;
}
use of org.h2.expression.Parameter 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() + ']');
}
Aggregations