Repository: incubator-ignite Updated Branches: refs/heads/ignite-624 [created] 98de490fb
# IGNITE-624 Rename GridSqlSelect --> GridSqlQuery Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/b297a4c4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/b297a4c4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/b297a4c4 Branch: refs/heads/ignite-624 Commit: b297a4c4fe8db2880733b418c012b7e662ee4b83 Parents: c6b3380 Author: sevdokimov <sergey.evdoki...@jetbrains.com> Authored: Tue Mar 31 21:17:06 2015 +0300 Committer: sevdokimov <sergey.evdoki...@jetbrains.com> Committed: Tue Mar 31 21:17:06 2015 +0300 ---------------------------------------------------------------------- .../processors/query/h2/sql/GridSqlQuery.java | 375 +++++++++++++++++++ .../query/h2/sql/GridSqlQueryParser.java | 9 +- .../query/h2/sql/GridSqlQuerySplitter.java | 8 +- .../processors/query/h2/sql/GridSqlSelect.java | 375 ------------------- .../query/h2/sql/GridSqlSubquery.java | 8 +- .../query/h2/sql/GridQueryParsingTest.java | 2 +- 6 files changed, 388 insertions(+), 389 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuery.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuery.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuery.java new file mode 100644 index 0000000..da71bcb --- /dev/null +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuery.java @@ -0,0 +1,375 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.query.h2.sql; + +import org.h2.util.*; + +import java.util.*; + +/** + * Select query. + */ +public class GridSqlQuery implements Cloneable { + /** */ + private boolean distinct; + + /** */ + private List<GridSqlElement> allExprs; + + /** */ + private List<GridSqlElement> select = new ArrayList<>(); + + /** */ + private List<GridSqlElement> groups = new ArrayList<>(); + + /** */ + private int[] grpCols; + + /** */ + private GridSqlElement from; + + /** */ + private GridSqlElement where; + + /** */ + private GridSqlElement having; + + /** */ + private int havingCol = -1; + + /** */ + private Map<GridSqlElement,GridSqlSortColumn> sort = new LinkedHashMap<>(); + + /** */ + private GridSqlElement offset; + + /** */ + private GridSqlElement limit; + + /** + * @return Offset. + */ + public GridSqlElement offset() { + return offset; + } + + /** + * @param offset Offset. + */ + public void offset(GridSqlElement offset) { + this.offset = offset; + } + + /** + * @param limit Limit. + */ + public void limit(GridSqlElement limit) { + this.limit = limit; + } + + /** + * @return Limit. + */ + public GridSqlElement limit() { + return limit; + } + + /** + * @return Distinct. + */ + public boolean distinct() { + return distinct; + } + + /** + * @param distinct New distinct. + */ + public void distinct(boolean distinct) { + this.distinct = distinct; + } + + /** + * @return Generate sql. + */ + public 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. + */ + public void addExpression(GridSqlElement expression) { + if (allExprs == null) + allExprs = new ArrayList<>(); + + allExprs.add(expression); + } + + /** + * @return All expressions in select, group by, order by. + */ + public List<GridSqlElement> allExpressions() { + return allExprs; + } + + /** + * @return Expressions. + */ + public List<GridSqlElement> select() { + return select; + } + + /** + * Clears select list. + */ + public void clearSelect() { + select = new ArrayList<>(); + } + + /** + * @param expression Expression. + */ + public void addSelectExpression(GridSqlElement expression) { + if (expression == null) + throw new NullPointerException(); + + 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. + */ + public Map<GridSqlElement,GridSqlSortColumn> sort() { + return sort; + } + + /** + * + */ + public void clearSort() { + sort = new LinkedHashMap<>(); + } + + /** + * @param expression Expression. + * @param sortType The sort type bit mask (SortOrder.DESCENDING, SortOrder.NULLS_FIRST, SortOrder.NULLS_LAST). + */ + public void addSort(GridSqlElement expression, GridSqlSortColumn sortType) { + sort.put(expression, sortType); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"CloneCallsConstructors", "CloneDoesntDeclareCloneNotSupportedException"}) + @Override public GridSqlQuery clone() { + try { + GridSqlQuery res = (GridSqlQuery)super.clone(); + + res.select = new ArrayList<>(select); + res.groups = new ArrayList<>(groups); + res.sort = new LinkedHashMap<>(sort); + res.allExprs = null; + + return res; + } + catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Never thrown. + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java index d84666d..d3cd038 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java @@ -22,7 +22,6 @@ import org.h2.command.*; import org.h2.command.dml.*; import org.h2.engine.*; import org.h2.expression.*; -import org.h2.expression.Parameter; import org.h2.jdbc.*; import org.h2.result.*; import org.h2.table.*; @@ -164,7 +163,7 @@ public class GridSqlQueryParser { * @param stmt Prepared statement. * @return Parsed select. */ - public static GridSqlSelect parse(JdbcPreparedStatement stmt) { + public static GridSqlQuery parse(JdbcPreparedStatement stmt) { Command cmd = COMMAND.get(stmt); Getter<Command,Prepared> p = prepared; @@ -226,13 +225,13 @@ public class GridSqlQueryParser { /** * @param select Select. */ - public GridSqlSelect parse(Select select) { - GridSqlSelect res = (GridSqlSelect)h2ObjToGridObj.get(select); + public GridSqlQuery parse(Select select) { + GridSqlQuery res = (GridSqlQuery)h2ObjToGridObj.get(select); if (res != null) return res; - res = new GridSqlSelect(); + res = new GridSqlQuery(); h2ObjToGridObj.put(select, res); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java index 47e5e05..6c32e13 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQuerySplitter.java @@ -64,12 +64,12 @@ public class GridSqlQuerySplitter { if (params == null) params = GridCacheSqlQuery.EMPTY_PARAMS; - GridSqlSelect srcQry = GridSqlQueryParser.parse(stmt); + GridSqlQuery srcQry = GridSqlQueryParser.parse(stmt); final String mergeTable = TABLE_FUNC_NAME + "()"; // table(0); TODO - GridSqlSelect mapQry = srcQry.clone(); - GridSqlSelect rdcQry = new GridSqlSelect().from(new GridSqlFunction("PUBLIC", TABLE_FUNC_NAME)); // table(mergeTable)); TODO + GridSqlQuery mapQry = srcQry.clone(); + GridSqlQuery rdcQry = new GridSqlQuery().from(new GridSqlFunction("PUBLIC", TABLE_FUNC_NAME)); // table(mergeTable)); TODO // Split all select expressions into map-reduce parts. List<GridSqlElement> mapExps = new ArrayList<>(srcQry.allExpressions()); @@ -147,7 +147,7 @@ public class GridSqlQuerySplitter { * @param target Extracted parameters. * @return Extracted parameters list. */ - private static List<Object> findParams(GridSqlSelect qry, Object[] params, ArrayList<Object> target) { + private static List<Object> findParams(GridSqlQuery qry, Object[] params, ArrayList<Object> target) { if (params.length == 0) return target; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSelect.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSelect.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSelect.java deleted file mode 100644 index 0e5bb31..0000000 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSelect.java +++ /dev/null @@ -1,375 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.processors.query.h2.sql; - -import org.h2.util.*; - -import java.util.*; - -/** - * Select query. - */ -public class GridSqlSelect implements Cloneable { - /** */ - private boolean distinct; - - /** */ - private List<GridSqlElement> allExprs; - - /** */ - private List<GridSqlElement> select = new ArrayList<>(); - - /** */ - private List<GridSqlElement> groups = new ArrayList<>(); - - /** */ - private int[] grpCols; - - /** */ - private GridSqlElement from; - - /** */ - private GridSqlElement where; - - /** */ - private GridSqlElement having; - - /** */ - private int havingCol = -1; - - /** */ - private Map<GridSqlElement,GridSqlSortColumn> sort = new LinkedHashMap<>(); - - /** */ - private GridSqlElement offset; - - /** */ - private GridSqlElement limit; - - /** - * @return Offset. - */ - public GridSqlElement offset() { - return offset; - } - - /** - * @param offset Offset. - */ - public void offset(GridSqlElement offset) { - this.offset = offset; - } - - /** - * @param limit Limit. - */ - public void limit(GridSqlElement limit) { - this.limit = limit; - } - - /** - * @return Limit. - */ - public GridSqlElement limit() { - return limit; - } - - /** - * @return Distinct. - */ - public boolean distinct() { - return distinct; - } - - /** - * @param distinct New distinct. - */ - public void distinct(boolean distinct) { - this.distinct = distinct; - } - - /** - * @return Generate sql. - */ - public 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. - */ - public void addExpression(GridSqlElement expression) { - if (allExprs == null) - allExprs = new ArrayList<>(); - - allExprs.add(expression); - } - - /** - * @return All expressions in select, group by, order by. - */ - public List<GridSqlElement> allExpressions() { - return allExprs; - } - - /** - * @return Expressions. - */ - public List<GridSqlElement> select() { - return select; - } - - /** - * Clears select list. - */ - public void clearSelect() { - select = new ArrayList<>(); - } - - /** - * @param expression Expression. - */ - public void addSelectExpression(GridSqlElement expression) { - if (expression == null) - throw new NullPointerException(); - - 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 GridSqlSelect 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 GridSqlSelect 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. - */ - public Map<GridSqlElement,GridSqlSortColumn> sort() { - return sort; - } - - /** - * - */ - public void clearSort() { - sort = new LinkedHashMap<>(); - } - - /** - * @param expression Expression. - * @param sortType The sort type bit mask (SortOrder.DESCENDING, SortOrder.NULLS_FIRST, SortOrder.NULLS_LAST). - */ - public void addSort(GridSqlElement expression, GridSqlSortColumn sortType) { - sort.put(expression, sortType); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"CloneCallsConstructors", "CloneDoesntDeclareCloneNotSupportedException"}) - @Override public GridSqlSelect clone() { - try { - GridSqlSelect res = (GridSqlSelect)super.clone(); - - res.select = new ArrayList<>(select); - res.groups = new ArrayList<>(groups); - res.sort = new LinkedHashMap<>(sort); - res.allExprs = null; - - return res; - } - catch (CloneNotSupportedException e) { - throw new RuntimeException(e); // Never thrown. - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSubquery.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSubquery.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSubquery.java index 7861abe..4b60969 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSubquery.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlSubquery.java @@ -22,12 +22,12 @@ package org.apache.ignite.internal.processors.query.h2.sql; */ public class GridSqlSubquery extends GridSqlElement { /** */ - private GridSqlSelect select; + private GridSqlQuery select; /** * @param select Select. */ - public GridSqlSubquery(GridSqlSelect select) { + public GridSqlSubquery(GridSqlQuery select) { this.select = select; } @@ -39,14 +39,14 @@ public class GridSqlSubquery extends GridSqlElement { /** * @return Select. */ - public GridSqlSelect select() { + public GridSqlQuery select() { return select; } /** * @param select New select. */ - public void select(GridSqlSelect select) { + public void select(GridSqlQuery select) { this.select = select; } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b297a4c4/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java index eada55a..db8e8bd 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/GridQueryParsingTest.java @@ -207,7 +207,7 @@ public class GridQueryParsingTest extends GridCommonAbstractTest { GridSqlQueryParser ses = new GridSqlQueryParser(); - GridSqlSelect gridSelect = ses.parse(select); + GridSqlQuery gridSelect = ses.parse(select); //System.out.println(select.getPlanSQL()); System.out.println(gridSelect.getSQL());