use of org.h2.api.Aggregate 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.api.Aggregate in project h2database by h2database.
the class UserAggregate method getInstance.
public Aggregate getInstance() {
if (javaClass == null) {
javaClass = JdbcUtils.loadUserClass(className);
}
Object obj;
try {
obj = javaClass.newInstance();
Aggregate agg;
if (obj instanceof Aggregate) {
agg = (Aggregate) obj;
} else {
agg = new AggregateWrapper((AggregateFunction) obj);
}
return agg;
} catch (Exception e) {
throw DbException.convert(e);
}
}
use of org.h2.api.Aggregate in project h2database by h2database.
the class Parser method readFunction.
private Expression readFunction(Schema schema, String name) {
if (schema != null) {
return readJavaFunction(schema, name, true);
}
boolean allowOverride = database.isAllowBuiltinAliasOverride();
if (allowOverride) {
JavaFunction jf = readJavaFunction(null, name, false);
if (jf != null) {
return jf;
}
}
AggregateType agg = getAggregateType(name);
if (agg != null) {
return readAggregate(agg, name);
}
Function function = Function.getFunction(database, name);
if (function == null) {
UserAggregate aggregate = database.findAggregate(name);
if (aggregate != null) {
return readJavaAggregate(aggregate);
}
if (allowOverride) {
throw DbException.get(ErrorCode.FUNCTION_NOT_FOUND_1, name);
}
return readJavaFunction(null, name, true);
}
switch(function.getFunctionType()) {
case Function.CAST:
{
function.setParameter(0, readExpression());
read("AS");
Column type = parseColumnWithType(null);
function.setDataType(type);
read(")");
break;
}
case Function.CONVERT:
{
if (database.getMode().swapConvertFunctionParameters) {
Column type = parseColumnWithType(null);
function.setDataType(type);
read(",");
function.setParameter(0, readExpression());
read(")");
} else {
function.setParameter(0, readExpression());
read(",");
Column type = parseColumnWithType(null);
function.setDataType(type);
read(")");
}
break;
}
case Function.EXTRACT:
{
function.setParameter(0, ValueExpression.get(ValueString.get(currentToken)));
read();
read("FROM");
function.setParameter(1, readExpression());
read(")");
break;
}
case Function.DATE_ADD:
case Function.DATE_DIFF:
{
if (DateTimeFunctions.isDatePart(currentToken)) {
function.setParameter(0, ValueExpression.get(ValueString.get(currentToken)));
read();
} else {
function.setParameter(0, readExpression());
}
read(",");
function.setParameter(1, readExpression());
read(",");
function.setParameter(2, readExpression());
read(")");
break;
}
case Function.SUBSTRING:
{
// Different variants include:
// SUBSTRING(X,1)
// SUBSTRING(X,1,1)
// SUBSTRING(X FROM 1 FOR 1) -- Postgres
// SUBSTRING(X FROM 1) -- Postgres
// SUBSTRING(X FOR 1) -- Postgres
function.setParameter(0, readExpression());
if (readIf("FROM")) {
function.setParameter(1, readExpression());
if (readIf("FOR")) {
function.setParameter(2, readExpression());
}
} else if (readIf("FOR")) {
function.setParameter(1, ValueExpression.get(ValueInt.get(0)));
function.setParameter(2, readExpression());
} else {
read(",");
function.setParameter(1, readExpression());
if (readIf(",")) {
function.setParameter(2, readExpression());
}
}
read(")");
break;
}
case Function.POSITION:
{
// can't read expression because IN would be read too early
function.setParameter(0, readConcat());
if (!readIf(",")) {
read("IN");
}
function.setParameter(1, readExpression());
read(")");
break;
}
case Function.TRIM:
{
Expression space = null;
if (readIf("LEADING")) {
function = Function.getFunction(database, "LTRIM");
if (!readIf("FROM")) {
space = readExpression();
read("FROM");
}
} else if (readIf("TRAILING")) {
function = Function.getFunction(database, "RTRIM");
if (!readIf("FROM")) {
space = readExpression();
read("FROM");
}
} else if (readIf("BOTH")) {
if (!readIf("FROM")) {
space = readExpression();
read("FROM");
}
}
Expression p0 = readExpression();
if (readIf(",")) {
space = readExpression();
} else if (readIf("FROM")) {
space = p0;
p0 = readExpression();
}
function.setParameter(0, p0);
if (space != null) {
function.setParameter(1, space);
}
read(")");
break;
}
case Function.TABLE:
case Function.TABLE_DISTINCT:
{
int i = 0;
ArrayList<Column> columns = New.arrayList();
do {
String columnName = readAliasIdentifier();
Column column = parseColumnWithType(columnName);
columns.add(column);
read("=");
function.setParameter(i, readExpression());
i++;
} while (readIfMore(true));
TableFunction tf = (TableFunction) function;
tf.setColumns(columns);
break;
}
case Function.ROW_NUMBER:
read(")");
read("OVER");
read("(");
read(")");
if (currentSelect == null && currentPrepared == null) {
throw getSyntaxError();
}
return new Rownum(currentSelect == null ? currentPrepared : currentSelect);
default:
if (!readIf(")")) {
int i = 0;
do {
function.setParameter(i++, readExpression());
} while (readIfMore(true));
}
}
function.doneWithParameters();
return function;
}
use of org.h2.api.Aggregate in project h2database by h2database.
the class Parser method parseSelectSimpleSelectPart.
private void parseSelectSimpleSelectPart(Select command) {
Select temp = currentSelect;
// make sure aggregate functions will not work in TOP and LIMIT
currentSelect = null;
if (readIf("TOP")) {
// can't read more complex expressions here because
// SELECT TOP 1 +? A FROM TEST could mean
// SELECT TOP (1+?) A FROM TEST or
// SELECT TOP 1 (+?) AS A FROM TEST
Expression limit = readTerm().optimize(session);
command.setLimit(limit);
} else if (readIf("LIMIT")) {
Expression offset = readTerm().optimize(session);
command.setOffset(offset);
Expression limit = readTerm().optimize(session);
command.setLimit(limit);
}
currentSelect = temp;
if (readIf("DISTINCT")) {
command.setDistinct(true);
} else {
readIf("ALL");
}
ArrayList<Expression> expressions = New.arrayList();
do {
if (readIf("*")) {
expressions.add(new Wildcard(null, null));
} else {
Expression expr = readExpression();
if (readIf("AS") || currentTokenType == IDENTIFIER) {
String alias = readAliasIdentifier();
boolean aliasColumnName = database.getSettings().aliasColumnName;
aliasColumnName |= database.getMode().aliasColumnName;
expr = new Alias(expr, alias, aliasColumnName);
}
expressions.add(expr);
}
} while (readIf(","));
command.setExpressions(expressions);
}
use of org.h2.api.Aggregate in project h2database by h2database.
the class Parser method parseDrop.
private Prepared parseDrop() {
if (readIf("TABLE")) {
boolean ifExists = readIfExists(false);
String tableName = readIdentifierWithSchema();
DropTable command = new DropTable(session, getSchema());
command.setTableName(tableName);
while (readIf(",")) {
tableName = readIdentifierWithSchema();
DropTable next = new DropTable(session, getSchema());
next.setTableName(tableName);
command.addNextDropTable(next);
}
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
if (readIf("CASCADE")) {
command.setDropAction(ConstraintActionType.CASCADE);
readIf("CONSTRAINTS");
} else if (readIf("RESTRICT")) {
command.setDropAction(ConstraintActionType.RESTRICT);
} else if (readIf("IGNORE")) {
command.setDropAction(ConstraintActionType.SET_DEFAULT);
}
return command;
} else if (readIf("INDEX")) {
boolean ifExists = readIfExists(false);
String indexName = readIdentifierWithSchema();
DropIndex command = new DropIndex(session, getSchema());
command.setIndexName(indexName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
// Support for MySQL: DROP INDEX index_name ON tbl_name
if (readIf("ON")) {
readIdentifierWithSchema();
}
return command;
} else if (readIf("USER")) {
boolean ifExists = readIfExists(false);
DropUser command = new DropUser(session);
command.setUserName(readUniqueIdentifier());
ifExists = readIfExists(ifExists);
readIf("CASCADE");
command.setIfExists(ifExists);
return command;
} else if (readIf("SEQUENCE")) {
boolean ifExists = readIfExists(false);
String sequenceName = readIdentifierWithSchema();
DropSequence command = new DropSequence(session, getSchema());
command.setSequenceName(sequenceName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
} else if (readIf("CONSTANT")) {
boolean ifExists = readIfExists(false);
String constantName = readIdentifierWithSchema();
DropConstant command = new DropConstant(session, getSchema());
command.setConstantName(constantName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
} else if (readIf("TRIGGER")) {
boolean ifExists = readIfExists(false);
String triggerName = readIdentifierWithSchema();
DropTrigger command = new DropTrigger(session, getSchema());
command.setTriggerName(triggerName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
} else if (readIf("VIEW")) {
boolean ifExists = readIfExists(false);
String viewName = readIdentifierWithSchema();
DropView command = new DropView(session, getSchema());
command.setViewName(viewName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
ConstraintActionType dropAction = parseCascadeOrRestrict();
if (dropAction != null) {
command.setDropAction(dropAction);
}
return command;
} else if (readIf("ROLE")) {
boolean ifExists = readIfExists(false);
DropRole command = new DropRole(session);
command.setRoleName(readUniqueIdentifier());
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
} else if (readIf("ALIAS")) {
boolean ifExists = readIfExists(false);
String aliasName = readIdentifierWithSchema();
DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
command.setAliasName(aliasName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
} else if (readIf("SCHEMA")) {
boolean ifExists = readIfExists(false);
DropSchema command = new DropSchema(session);
command.setSchemaName(readUniqueIdentifier());
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
if (readIf("CASCADE")) {
command.setDropAction(ConstraintActionType.CASCADE);
} else if (readIf("RESTRICT")) {
command.setDropAction(ConstraintActionType.RESTRICT);
}
return command;
} else if (readIf("ALL")) {
read("OBJECTS");
DropDatabase command = new DropDatabase(session);
command.setDropAllObjects(true);
if (readIf("DELETE")) {
read("FILES");
command.setDeleteFiles(true);
}
return command;
} else if (readIf("DOMAIN")) {
return parseDropUserDataType();
} else if (readIf("TYPE")) {
return parseDropUserDataType();
} else if (readIf("DATATYPE")) {
return parseDropUserDataType();
} else if (readIf("AGGREGATE")) {
return parseDropAggregate();
} else if (readIf("SYNONYM")) {
boolean ifExists = readIfExists(false);
String synonymName = readIdentifierWithSchema();
DropSynonym command = new DropSynonym(session, getSchema());
command.setSynonymName(synonymName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
}
throw getSyntaxError();
}
Aggregations