Skip to content

Commit

Permalink
# IGNITE-624 Make GridSqlQueryParser able to parse SQL query with UNI…
Browse files Browse the repository at this point in the history
…ON expression. Created class GridSqlUnion.
  • Loading branch information
sevdokimov committed Mar 31, 2015
1 parent b297a4c commit 98de490
Show file tree
Hide file tree
Showing 6 changed files with 515 additions and 244 deletions.
Expand Up @@ -17,49 +17,29 @@


package org.apache.ignite.internal.processors.query.h2.sql; package org.apache.ignite.internal.processors.query.h2.sql;


import org.h2.util.*;

import java.util.*; import java.util.*;


/** /**
* Select query. * Select query.
*/ */
public class GridSqlQuery implements Cloneable { public abstract class GridSqlQuery implements Cloneable {
/** */
private boolean distinct;

/** */
private List<GridSqlElement> allExprs;

/** */
private List<GridSqlElement> select = new ArrayList<>();

/** */ /** */
private List<GridSqlElement> groups = new ArrayList<>(); protected boolean distinct;


/** */ /** */
private int[] grpCols; protected List<GridSqlElement> allExprs;


/** */ /** */
private GridSqlElement from; protected List<GridSqlElement> select = new ArrayList<>();


/** */ /** */
private GridSqlElement where; protected Map<GridSqlElement,GridSqlSortColumn> sort = new LinkedHashMap<>();


/** */ /** */
private GridSqlElement having; protected GridSqlElement offset;


/** */ /** */
private int havingCol = -1; protected GridSqlElement limit;

/** */
private Map<GridSqlElement,GridSqlSortColumn> sort = new LinkedHashMap<>();

/** */
private GridSqlElement offset;

/** */
private GridSqlElement limit;


/** /**
* @return Offset. * @return Offset.
Expand Down Expand Up @@ -106,78 +86,7 @@ public void distinct(boolean distinct) {
/** /**
* @return Generate sql. * @return Generate sql.
*/ */
public String getSQL() { public abstract String getSQL();
StatementBuilder buff = new StatementBuilder("SELECT");

if (distinct)
buff.append(" DISTINCT");

for (GridSqlElement expression : select) {
buff.appendExceptFirst(",");
buff.append('\n');
buff.append(StringUtils.indent(expression.getSQL(), 4, false));
}

buff.append("\nFROM ").append(from.getSQL());

if (where != null)
buff.append("\nWHERE ").append(StringUtils.unEnclose(where.getSQL()));

if (!groups.isEmpty()) {
buff.append("\nGROUP BY ");

buff.resetCount();

for (GridSqlElement expression : groups) {
buff.appendExceptFirst(", ");

if (expression instanceof GridSqlAlias)
buff.append(StringUtils.unEnclose((expression.child().getSQL())));
else
buff.append(StringUtils.unEnclose(expression.getSQL()));
}
}

if (having != null)
buff.append("\nHAVING ").append(StringUtils.unEnclose(having.getSQL()));

if (!sort.isEmpty()) {
buff.append("\nORDER BY ");

buff.resetCount();

for (Map.Entry<GridSqlElement,GridSqlSortColumn> entry : sort.entrySet()) {
buff.appendExceptFirst(", ");

GridSqlElement expression = entry.getKey();

int idx = select.indexOf(expression);

if (idx >= 0)
buff.append(idx + 1);
else
buff.append('=').append(StringUtils.unEnclose(expression.getSQL()));

GridSqlSortColumn type = entry.getValue();

if (!type.asc())
buff.append(" DESC");

if (type.nullsFirst())
buff.append(" NULLS FIRST");
else if (type.nullsLast())
buff.append(" NULLS LAST");
}
}

if (limit != null)
buff.append(" LIMIT ").append(StringUtils.unEnclose(limit.getSQL()));

if (offset != null)
buff.append(" OFFSET ").append(StringUtils.unEnclose(offset.getSQL()));

return buff.toString();
}


/** /**
* @param expression Expression. * @param expression Expression.
Expand Down Expand Up @@ -220,119 +129,6 @@ public void addSelectExpression(GridSqlElement expression) {
select.add(expression); select.add(expression);
} }


/**
* @return Expressions.
*/
public List<GridSqlElement> groups() {
return groups;
}

/**
*
*/
public void clearGroups() {
groups = new ArrayList<>();
grpCols = null;
}

/**
* @param expression Expression.
*/
public void addGroupExpression(GridSqlElement expression) {
if (expression == null)
throw new NullPointerException();

groups.add(expression);
}

/**
* @return Group columns.
*/
public int[] groupColumns() {
return grpCols;
}

/**
* @param grpCols Group columns.
*/
public void groupColumns(int[] grpCols) {
this.grpCols = grpCols;
}

/**
* @return Tables.
*/
public GridSqlElement from() {
return from;
}

/**
* @param from From element.
* @return {@code this}.
*/
public GridSqlQuery from(GridSqlElement from) {
this.from = from;

return this;
}

/**
* @return Where.
*/
public GridSqlElement where() {
return where;
}

/**
* @param where New where.
*/
public void where(GridSqlElement where) {
this.where = where;
}

/**
* @param condition Adds new WHERE condition using AND operator.
* @return {@code this}.
*/
public GridSqlQuery whereAnd(GridSqlElement condition) {
if (condition == null)
throw new NullPointerException();

GridSqlElement old = where();

where(old == null ? condition : new GridSqlOperation(GridSqlOperationType.AND, old, condition));

return this;
}

/**
* @return Having.
*/
public GridSqlElement having() {
return having;
}

/**
* @param having New having.
*/
public void having(GridSqlElement having) {
this.having = having;
}

/**
* @param col Index of HAVING column.
*/
public void havingColumn(int col) {
this.havingCol = col;
}

/**
* @return Index of HAVING column.
*/
public int havingColumn() {
return havingCol;
}

/** /**
* @return Sort. * @return Sort.
*/ */
Expand Down Expand Up @@ -362,7 +158,6 @@ public void addSort(GridSqlElement expression, GridSqlSortColumn sortType) {
GridSqlQuery res = (GridSqlQuery)super.clone(); GridSqlQuery res = (GridSqlQuery)super.clone();


res.select = new ArrayList<>(select); res.select = new ArrayList<>(select);
res.groups = new ArrayList<>(groups);
res.sort = new LinkedHashMap<>(sort); res.sort = new LinkedHashMap<>(sort);
res.allExprs = null; res.allExprs = null;


Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.h2.command.dml.*; import org.h2.command.dml.*;
import org.h2.engine.*; import org.h2.engine.*;
import org.h2.expression.*; import org.h2.expression.*;
import org.h2.expression.Parameter;
import org.h2.jdbc.*; import org.h2.jdbc.*;
import org.h2.result.*; import org.h2.result.*;
import org.h2.table.*; import org.h2.table.*;
Expand Down Expand Up @@ -153,6 +154,9 @@ public class GridSqlQueryParser {
/** */ /** */
private static final Getter<JdbcPreparedStatement,Command> COMMAND = getter(JdbcPreparedStatement.class, "command"); private static final Getter<JdbcPreparedStatement,Command> COMMAND = getter(JdbcPreparedStatement.class, "command");


/** */
private static final Getter<SelectUnion, SortOrder> UNION_SORT = getter(SelectUnion.class, "sort");

/** */ /** */
private static volatile Getter<Command,Prepared> prepared; private static volatile Getter<Command,Prepared> prepared;


Expand All @@ -171,14 +175,14 @@ public static GridSqlQuery parse(JdbcPreparedStatement stmt) {
if (p == null) { if (p == null) {
Class<? extends Command> cls = cmd.getClass(); Class<? extends Command> cls = cmd.getClass();


assert cls.getSimpleName().equals("CommandContainer"); assert "CommandContainer".equals(cls.getSimpleName());


prepared = p = getter(cls, "prepared"); prepared = p = getter(cls, "prepared");
} }


Prepared select = p.get(cmd); Prepared statement = p.get(cmd);


return new GridSqlQueryParser().parse((Select)select); return new GridSqlQueryParser().parse((Query)statement);
} }


/** /**
Expand All @@ -199,9 +203,8 @@ else if (tbl instanceof TableView) {


res = new GridSqlSubquery(parse((Select)qry)); res = new GridSqlSubquery(parse((Select)qry));
} }
else if (tbl instanceof FunctionTable) { else if (tbl instanceof FunctionTable)
res = parseExpression(FUNC_EXPR.get((FunctionTable)tbl)); res = parseExpression(FUNC_EXPR.get((FunctionTable)tbl));
}
else if (tbl instanceof RangeTable) { else if (tbl instanceof RangeTable) {
res = new GridSqlFunction(GridSqlFunctionType.SYSTEM_RANGE); res = new GridSqlFunction(GridSqlFunctionType.SYSTEM_RANGE);


Expand All @@ -225,13 +228,13 @@ else if (tbl instanceof RangeTable) {
/** /**
* @param select Select. * @param select Select.
*/ */
public GridSqlQuery parse(Select select) { public GridSqlSelect parse(Select select) {
GridSqlQuery res = (GridSqlQuery)h2ObjToGridObj.get(select); GridSqlSelect res = (GridSqlSelect)h2ObjToGridObj.get(select);


if (res != null) if (res != null)
return res; return res;


res = new GridSqlQuery(); res = new GridSqlSelect();


h2ObjToGridObj.put(select, res); h2ObjToGridObj.put(select, res);


Expand Down Expand Up @@ -313,6 +316,45 @@ public GridSqlQuery parse(Select select) {
return res; return res;
} }


/**
* @param qry Select.
*/
public GridSqlQuery parse(Query qry) {
if (qry instanceof Select)
return parse((Select)qry);

if (qry instanceof SelectUnion)
return parse((SelectUnion)qry);

throw new UnsupportedOperationException("Unknown query type: " + qry);
}

/**
* @param union Select.
*/
public GridSqlUnion parse(SelectUnion union) {
GridSqlUnion res = (GridSqlUnion)h2ObjToGridObj.get(union);

if (res != null)
return res;

res = new GridSqlUnion();

res.right(parse(union.getRight()));
res.left(parse(union.getLeft()));

res.unionType(union.getUnionType());

res.limit(parseExpression(union.getLimit()));
res.offset(parseExpression(union.getOffset()));

assert UNION_SORT.get(union) == null; // todo IGNITE-624

h2ObjToGridObj.put(union, res);

return res;
}

/** /**
* @param expression Expression. * @param expression Expression.
*/ */
Expand Down Expand Up @@ -495,8 +537,8 @@ private GridSqlElement parseExpression0(Expression expression) {
return res; return res;
} }


if (expression instanceof org.h2.expression.Parameter) if (expression instanceof Parameter)
return new GridSqlParameter(((org.h2.expression.Parameter)expression).getIndex()); return new GridSqlParameter(((Parameter)expression).getIndex());


if (expression instanceof Aggregate) { if (expression instanceof Aggregate) {
GridSqlAggregateFunction res = new GridSqlAggregateFunction(DISTINCT.get((Aggregate)expression), GridSqlAggregateFunction res = new GridSqlAggregateFunction(DISTINCT.get((Aggregate)expression),
Expand Down

0 comments on commit 98de490

Please sign in to comment.