ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [03/50] [abbrv] incubator-ignite git commit: ignite-624
Date Thu, 09 Apr 2015 10:13:01 GMT
ignite-624


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/25f900ed
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/25f900ed
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/25f900ed

Branch: refs/heads/ignite-673
Commit: 25f900edac7d3a213ec30de673251777739848a5
Parents: 71fa3a2
Author: S.Vladykin <svladykin@gridgain.com>
Authored: Tue Apr 7 09:54:56 2015 +0300
Committer: S.Vladykin <svladykin@gridgain.com>
Committed: Tue Apr 7 09:54:56 2015 +0300

----------------------------------------------------------------------
 .../processors/query/h2/sql/GridSqlQuery.java   | 103 +++++++++----------
 .../query/h2/sql/GridSqlQueryParser.java        |  41 +++++---
 .../query/h2/sql/GridSqlQuerySplitter.java      |  96 ++++++++++++-----
 .../processors/query/h2/sql/GridSqlSelect.java  |  72 ++++++++-----
 .../processors/query/h2/sql/GridSqlUnion.java   |  42 +-------
 5 files changed, 192 insertions(+), 162 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25f900ed/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
index db1dcbf..5953d5e 100644
--- 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
@@ -17,6 +17,9 @@
 
 package org.apache.ignite.internal.processors.query.h2.sql;
 
+import org.apache.ignite.*;
+import org.h2.util.*;
+
 import java.util.*;
 
 /**
@@ -27,13 +30,7 @@ public abstract class GridSqlQuery implements Cloneable {
     protected boolean distinct;
 
     /** */
-    protected List<GridSqlElement> allExprs;
-
-    /** */
-    protected List<GridSqlElement> select = new ArrayList<>();
-
-    /** */
-    protected Map<GridSqlElement,GridSqlSortColumn> sort = new LinkedHashMap<>();
+    protected List<GridSqlSortColumn> sort = new ArrayList<>();
 
     /** */
     protected GridSqlElement offset;
@@ -89,66 +86,66 @@ public abstract class GridSqlQuery implements Cloneable {
     public abstract String getSQL();
 
     /**
-     * @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.
+     * @return Sort.
      */
-    public List<GridSqlElement> allExpressions() {
-        return allExprs;
+    public List<GridSqlSortColumn> sort() {
+        return sort;
     }
 
     /**
-     * @return Expressions.
+     *
      */
-    public List<GridSqlElement> select() {
-        return select;
+    public void clearSort() {
+        sort = new ArrayList<>();
     }
 
     /**
-     * Clears select list.
+     * @param sortCol The sort column.
      */
-    public void clearSelect() {
-        select = new ArrayList<>();
+    public void addSort(GridSqlSortColumn sortCol) {
+        sort.add(sortCol);
     }
 
     /**
-     * @param expression Expression.
+     * @param buff Statement builder.
      */
-    public void addSelectExpression(GridSqlElement expression) {
-        if (expression == null)
-            throw new NullPointerException();
-
-        select.add(expression);
-    }
+    protected void getSortLimitSQL(StatementBuilder buff) {
+        if (!sort.isEmpty()) {
+            buff.append("\nORDER BY ");
+
+            boolean first = true;
+
+            for (GridSqlSortColumn col : sort) {
+                if (first)
+                    first = false;
+                else
+                    buff.append(", ");
+
+                int idx = col.column();
+
+                if (idx >= 0)
+                    buff.append(idx + 1);
+                else {
+                    throw new IgniteException("Failed to generate query: " + buff);
+//                    buff.append('=').append(StringUtils.unEnclose(entry.getKey().getSQL()));
+                }
+
+                if (!col.asc())
+                    buff.append(" DESC");
+
+                if (col.nullsFirst())
+                    buff.append(" NULLS FIRST");
+                else if (col.nullsLast())
+                    buff.append(" NULLS LAST");
+            }
+        }
 
-    /**
-     * @return Sort.
-     */
-    public Map<GridSqlElement,GridSqlSortColumn> sort() {
-        return sort;
-    }
+        if (limit != null)
+            buff.append(" LIMIT ").append(StringUtils.unEnclose(limit.getSQL()));
 
-    /**
-     *
-     */
-    public void clearSort() {
-        sort = new LinkedHashMap<>();
-    }
+        if (offset != null)
+            buff.append(" OFFSET ").append(StringUtils.unEnclose(offset.getSQL()));
 
-    /**
-     * @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} */
@@ -157,9 +154,7 @@ public abstract class GridSqlQuery implements Cloneable {
         try {
             GridSqlQuery res = (GridSqlQuery)super.clone();
 
-            res.select = new ArrayList<>(select);
-            res.sort = new LinkedHashMap<>(sort);
-            res.allExprs = null;
+            res.sort = new ArrayList<>(sort);
 
             return res;
         }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25f900ed/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 fb341c1..ea3628f 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
@@ -293,22 +293,7 @@ public class GridSqlQueryParser {
         for (int i = 0; i < select.getColumnCount(); i++)
             res.addSelectExpression(parseExpression(expressions.get(i)));
 
-        SortOrder sortOrder = select.getSortOrder();
-
-        if (sortOrder != null) {
-            int[] indexes = sortOrder.getQueryColumnIndexes();
-            int[] sortTypes = sortOrder.getSortTypes();
-
-            for (int i = 0; i < indexes.length; i++) {
-                int colIdx = indexes[i];
-                int type = sortTypes[i];
-
-                res.addSort(parseExpression(expressions.get(colIdx)), new GridSqlSortColumn(colIdx,
-                    (type & SortOrder.DESCENDING) == 0,
-                    (type & SortOrder.NULLS_FIRST) != 0,
-                    (type & SortOrder.NULLS_LAST) != 0));
-            }
-        }
+        processSortOrder(select.getSortOrder(), res);
 
         res.limit(parseExpression(select.getLimit()));
         res.offset(parseExpression(select.getOffset()));
@@ -317,6 +302,28 @@ public class GridSqlQueryParser {
     }
 
     /**
+     * @param sortOrder Sort order.
+     * @param qry Query.
+     */
+    private void processSortOrder(SortOrder sortOrder, GridSqlQuery qry) {
+        if (sortOrder == null)
+            return;
+
+        int[] indexes = sortOrder.getQueryColumnIndexes();
+        int[] sortTypes = sortOrder.getSortTypes();
+
+        for (int i = 0; i < indexes.length; i++) {
+            int colIdx = indexes[i];
+            int type = sortTypes[i];
+
+            qry.addSort(new GridSqlSortColumn(colIdx,
+                (type & SortOrder.DESCENDING) == 0,
+                (type & SortOrder.NULLS_FIRST) != 0,
+                (type & SortOrder.NULLS_LAST) != 0));
+        }
+    }
+
+    /**
      * @param qry Select.
      */
     public GridSqlQuery parse(Query qry) {
@@ -348,7 +355,7 @@ public class GridSqlQueryParser {
         res.limit(parseExpression(union.getLimit()));
         res.offset(parseExpression(union.getOffset()));
 
-        assert UNION_SORT.get(union) == null; // todo IGNITE-624
+        processSortOrder(UNION_SORT.get(union), res);
 
         h2ObjToGridObj.put(union, res);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25f900ed/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 f343b01..05928c2 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
@@ -57,6 +57,17 @@ public class GridSqlQuerySplitter {
     }
 
     /**
+     * @param qry Query.
+     * @return Leftest simple query if this is UNION.
+     */
+    private static GridSqlSelect leftest(GridSqlQuery qry) {
+        if (qry instanceof GridSqlUnion)
+            return leftest(((GridSqlUnion)qry).left());
+
+        return (GridSqlSelect)qry;
+    }
+
+    /**
      * @param stmt Prepared statement.
      * @param params Parameters.
      * @return Two step query.
@@ -65,21 +76,58 @@ public class GridSqlQuerySplitter {
         if (params == null)
             params = GridCacheSqlQuery.EMPTY_PARAMS;
 
-        GridSqlQuery srcQry = GridSqlQueryParser.parse(stmt);
+        GridSqlQuery qry0 = GridSqlQueryParser.parse(stmt);
+
+        GridSqlSelect srcQry;
+
+        if (qry0 instanceof GridSqlSelect)
+            srcQry = (GridSqlSelect)qry0;
+        else {
+            srcQry = new GridSqlSelect().from(new GridSqlSubquery(qry0));
+
+            GridSqlSelect left = leftest(qry0);
+
+            int c = 0;
+
+            for (GridSqlElement expr : left.select()) {
+                String colName;
+
+                if (expr instanceof GridSqlAlias)
+                    colName = ((GridSqlAlias)expr).alias();
+                else if (expr instanceof GridSqlColumn)
+                    colName = ((GridSqlColumn)expr).columnName();
+                else {
+                    colName = columnName(c);
 
-        if (!(srcQry instanceof GridSqlSelect))
-            throw new UnsupportedOperationException(); // todo IGNITE-624 Support UNION,
+                    expr = alias(colName, expr);
 
-        GridSqlSelect srcSelect = (GridSqlSelect)srcQry;
+                    // Set generated alias to the expression.
+                    left.select().set(c, expr);
+                    left.allExpressions().set(c, expr);
+                }
+
+                srcQry.addSelectExpression(column(colName));
+
+                qry0.sort();
+
+                c++;
+            }
+
+            // ORDER BY
+            if (!qry0.sort().isEmpty()) {
+                for (GridSqlSortColumn col : qry0.sort())
+                    srcQry.addSort(col);
+            }
+        }
 
         final String mergeTable = TABLE_FUNC_NAME + "()"; // table(0); TODO
 
-        GridSqlSelect mapQry = srcSelect.clone();
+        GridSqlSelect mapQry = srcQry.clone();
         GridSqlSelect rdcQry = new GridSqlSelect().from(new GridSqlFunction("PUBLIC", TABLE_FUNC_NAME));
// table(mergeTable)); TODO
 
         // Split all select expressions into map-reduce parts.
-        List<GridSqlElement> mapExps = new ArrayList<>(srcSelect.allExpressions());
-        GridSqlElement[] rdcExps = new GridSqlElement[srcSelect.select().size()];
+        List<GridSqlElement> mapExps = new ArrayList<>(srcQry.allExpressions());
+        GridSqlElement[] rdcExps = new GridSqlElement[srcQry.select().size()];
 
         Set<String> colNames = new HashSet<>();
 
@@ -98,31 +146,31 @@ public class GridSqlQuerySplitter {
             rdcQry.addSelectExpression(rdcExp);
 
         // -- GROUP BY
-        if (!srcSelect.groups().isEmpty()) {
+        if (!srcQry.groups().isEmpty()) {
             mapQry.clearGroups();
 
-            for (int col : srcSelect.groupColumns())
+            for (int col : srcQry.groupColumns())
                 mapQry.addGroupExpression(column(((GridSqlAlias)mapExps.get(col)).alias()));
 
-            for (int col : srcSelect.groupColumns())
+            for (int col : srcQry.groupColumns())
                 rdcQry.addGroupExpression(column(((GridSqlAlias)mapExps.get(col)).alias()));
         }
 
         // -- HAVING
-        if (srcSelect.having() != null) {
+        if (srcQry.having() != null) {
             // TODO Find aggregate functions in HAVING clause.
-            rdcQry.whereAnd(column(columnName(srcSelect.havingColumn())));
+            rdcQry.whereAnd(column(columnName(srcQry.havingColumn())));
 
             mapQry.having(null);
         }
 
         // -- ORDER BY
-        if (!srcSelect.sort().isEmpty()) {
+        if (!srcQry.sort().isEmpty()) {
             if (aggregateFound) // Ordering over aggregates does not make sense.
                 mapQry.clearSort(); // Otherwise map sort will be used by offset-limit.
 
-            for (GridSqlSortColumn sortCol : srcSelect.sort().values())
-                rdcQry.addSort(column(((GridSqlAlias)mapExps.get(sortCol.column())).alias()),
sortCol);
+            for (GridSqlSortColumn sortCol : srcQry.sort())
+                rdcQry.addSort(sortCol);
         }
 
         // -- LIMIT
@@ -134,14 +182,14 @@ public class GridSqlQuerySplitter {
         }
 
         // -- OFFSET
-        if (srcSelect.offset() != null) {
+        if (srcQry.offset() != null) {
             mapQry.offset(null);
 
-            rdcQry.offset(srcSelect.offset());
+            rdcQry.offset(srcQry.offset());
         }
 
         // -- DISTINCT
-        if (srcSelect.distinct()) {
+        if (srcQry.distinct()) {
             mapQry.distinct(false);
             rdcQry.distinct(true);
         }
@@ -166,7 +214,12 @@ public class GridSqlQuerySplitter {
         if (qry instanceof GridSqlSelect)
             return findParams((GridSqlSelect)qry, params, target);
 
-        throw new UnsupportedOperationException(); // todo IGNITE-624
+        GridSqlUnion union = (GridSqlUnion)qry;
+
+        findParams(union.left(), params, target);
+        findParams(union.right(), params, target);
+
+        return target;
     }
 
     /**
@@ -179,7 +232,7 @@ public class GridSqlQuerySplitter {
         if (params.length == 0)
             return target;
 
-        for (GridSqlElement el : qry.select())
+        for (GridSqlElement el : qry.allExpressions())
             findParams(el, params, target);
 
         findParams(qry.from(), params, target);
@@ -190,9 +243,6 @@ public class GridSqlQuerySplitter {
 
         findParams(qry.having(), params, target);
 
-        for (GridSqlElement el : qry.sort().keySet())
-            findParams(el, params, target);
-
         findParams(qry.limit(), params, target);
         findParams(qry.offset(), params, target);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25f900ed/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
index 272f8b4..154659b 100644
--- 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
@@ -26,6 +26,12 @@ import java.util.*;
  */
 public class GridSqlSelect extends GridSqlQuery {
     /** */
+    private List<GridSqlElement> allExprs;
+
+    /** */
+    private List<GridSqlElement> select = new ArrayList<>();
+
+    /** */
     private List<GridSqlElement> groups = new ArrayList<>();
 
     /** */
@@ -79,42 +85,50 @@ public class GridSqlSelect extends GridSqlQuery {
         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();
+        getSortLimitSQL(buff);
 
-                int idx = select.indexOf(expression);
+        return buff.toString();
+    }
 
-                if (idx >= 0)
-                    buff.append(idx + 1);
-                else
-                    buff.append('=').append(StringUtils.unEnclose(expression.getSQL()));
+    /**
+     * @param expression Expression.
+     */
+    public void addExpression(GridSqlElement expression) {
+        if (allExprs == null)
+            allExprs = new ArrayList<>();
 
-                GridSqlSortColumn type = entry.getValue();
+        allExprs.add(expression);
+    }
 
-                if (!type.asc())
-                    buff.append(" DESC");
+    /**
+     * @return All expressions in select, group by, order by.
+     */
+    public List<GridSqlElement> allExpressions() {
+        return allExprs;
+    }
 
-                if (type.nullsFirst())
-                    buff.append(" NULLS FIRST");
-                else if (type.nullsLast())
-                    buff.append(" NULLS LAST");
-            }
-        }
+    /**
+     * @return Expressions.
+     */
+    public List<GridSqlElement> select() {
+        return select;
+    }
 
-        if (limit != null)
-            buff.append(" LIMIT ").append(StringUtils.unEnclose(limit.getSQL()));
+    /**
+     * Clears select list.
+     */
+    public void clearSelect() {
+        select = new ArrayList<>();
+    }
 
-        if (offset != null)
-            buff.append(" OFFSET ").append(StringUtils.unEnclose(offset.getSQL()));
+    /**
+     * @param expression Expression.
+     */
+    public void addSelectExpression(GridSqlElement expression) {
+        if (expression == null)
+            throw new NullPointerException();
 
-        return buff.toString();
+        select.add(expression);
     }
 
     /**
@@ -237,6 +251,8 @@ public class GridSqlSelect extends GridSqlQuery {
 
         res.groups = new ArrayList<>(groups);
         res.grpCols =  grpCols == null ? null : grpCols.clone();
+        res.select = new ArrayList<>(select);
+        res.allExprs = null;
 
         return res;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25f900ed/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlUnion.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlUnion.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlUnion.java
index 1eae1ef..6fe3f19 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlUnion.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlUnion.java
@@ -18,11 +18,9 @@
 package org.apache.ignite.internal.processors.query.h2.sql;
 
 import org.h2.command.dml.*;
-import org.h2.message.*;
 import org.h2.util.*;
 
 import javax.cache.*;
-import java.util.*;
 
 /**
  *
@@ -39,7 +37,7 @@ public class GridSqlUnion extends GridSqlQuery {
 
     /** {@inheritDoc} */
     @Override public String getSQL() {
-        StringBuilder buff = new StringBuilder();
+        StatementBuilder buff = new StatementBuilder();
 
         buff.append('(').append(left.getSQL()).append(')');
 
@@ -66,43 +64,7 @@ public class GridSqlUnion extends GridSqlQuery {
 
         buff.append('(').append(right.getSQL()).append(')');
 
-        if (!sort.isEmpty()) {
-            buff.append("\nORDER BY ");
-
-            boolean first = true;
-
-            for (Map.Entry<GridSqlElement, GridSqlSortColumn> entry : sort.entrySet())
{
-                if (first)
-                    first = false;
-                else
-                    buff.append(", ");
-
-                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()));
+        getSortLimitSQL(buff);
 
         return buff.toString();
     }


Mime
View raw message