ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject [9/9] incubator-ignite git commit: ignite-sql-tests - params fix
Date Sun, 08 Feb 2015 21:24:16 GMT
ignite-sql-tests - params fix


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

Branch: refs/heads/ignite-sql-tests
Commit: 61eb6811fee442a3f722aba845adf47640482609
Parents: 90d61e0
Author: S.Vladykin <svladykin@gridgain.com>
Authored: Mon Feb 9 00:23:47 2015 +0300
Committer: S.Vladykin <svladykin@gridgain.com>
Committed: Mon Feb 9 00:23:47 2015 +0300

----------------------------------------------------------------------
 .../cache/query/GridCacheSqlQuery.java          |  2 +-
 .../processors/query/h2/sql/GridSqlElement.java | 14 +++--
 .../query/h2/sql/GridSqlFunction.java           |  4 +-
 .../processors/query/h2/sql/GridSqlJoin.java    | 13 ++---
 .../query/h2/sql/GridSqlOperationType.java      |  6 +--
 .../query/h2/sql/GridSqlQuerySplitter.java      | 57 +++++++++++++++++++-
 .../cache/GridCacheCrossCacheQuerySelfTest.java | 19 +++++++
 .../query/h2/sql/GridQueryParsingTest.java      |  9 +++-
 8 files changed, 97 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheSqlQuery.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheSqlQuery.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheSqlQuery.java
index bffc18e..0131fb0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheSqlQuery.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheSqlQuery.java
@@ -28,7 +28,7 @@ import java.io.*;
  */
 public class GridCacheSqlQuery implements Externalizable {
     /** */
-    private static final Object[] EMPTY_PARAMS = {};
+    public static final Object[] EMPTY_PARAMS = {};
 
     /** */
     String alias;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlElement.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlElement.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlElement.java
index 5a0a333..62370ba 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlElement.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlElement.java
@@ -22,7 +22,7 @@ import java.util.*;
 /**
  * Abstract SQL element.
  */
-public abstract class GridSqlElement implements Cloneable {
+public abstract class GridSqlElement implements Cloneable, Iterable<GridSqlElement>
{
     /** */
     protected List<GridSqlElement> children = new ArrayList<>();
 
@@ -30,13 +30,6 @@ public abstract class GridSqlElement implements Cloneable {
     public abstract String getSQL();
 
     /**
-     * @return Children.
-     */
-    public List<GridSqlElement> children() {
-        return children;
-    }
-
-    /**
      * Clears all children.
      */
     public void clearChildren() {
@@ -101,4 +94,9 @@ public abstract class GridSqlElement implements Cloneable {
     public int size() {
         return children.size();
     }
+
+    /** {@inheritDoc} */
+    @Override public Iterator<GridSqlElement> iterator() {
+        return children.iterator();
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlFunction.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlFunction.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlFunction.java
index a5111fb..5854ac0 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlFunction.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlFunction.java
@@ -111,13 +111,13 @@ public class GridSqlFunction extends GridSqlElement {
 
         if (type == CAST) {
             assert !F.isEmpty(castType) : castType;
-            assert children().size() == 1;
+            assert size() == 1;
 
             buff.append(child().getSQL()).append(" AS ").append(castType);
         }
         else if (type == CONVERT) {
             assert !F.isEmpty(castType) : castType;
-            assert children().size() == 1;
+            assert size() == 1;
 
             buff.append(child().getSQL()).append(',').append(castType);
         }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlJoin.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlJoin.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlJoin.java
index dce3a69..69ebdbf 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlJoin.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlJoin.java
@@ -61,7 +61,7 @@ public class GridSqlJoin extends GridSqlElement {
      * @return {@code JOIN ON} condition.
      */
     @Nullable public GridSqlElement on() {
-        return children.size() < 3 ? null : child(2);
+        return size() < 3 ? null : child(2);
     }
 
     /** {@inheritDoc} */
@@ -76,15 +76,8 @@ public class GridSqlJoin extends GridSqlElement {
 
         GridSqlElement on = on();
 
-        if (on != null) {
-            String onSql = on.getSQL();
-
-            // This is needed for parsing tests to work correctly.
-            if (onSql.charAt(0) == '(' && onSql.charAt(onSql.length() - 1) == ')')
-                onSql = onSql.substring(1, onSql.length() - 1);
-
-            buff.append(" \n ON ").append(onSql);
-        }
+        if (on != null)
+            buff.append(" \n ON ").append(StringUtils.unEnclose(on.getSQL()));
 
         return buff.toString();
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java
index e1f0744..7aefbec 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlOperationType.java
@@ -190,15 +190,15 @@ public enum GridSqlOperationType {
 
             buff.append(operation.child(0).getSQL()).append(" IN(");
 
-            assert operation.children().size() > 1;
+            assert operation.size() > 1;
 
-            if (operation.children().size() == 2) {
+            if (operation.size() == 2) {
                 String child = operation.child(1).getSQL();
 
                 buff.append(' ').append(StringUtils.unEnclose(child)).append(' ');
             }
             else {
-                for (int i = 1; i < operation.children().size(); i++) {
+                for (int i = 1; i < operation.size(); i++) {
                     buff.appendExceptFirst(", ");
                     buff.append(operation.child(i).getSQL());
                 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/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 f09cbb4..6c83fbe 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
@@ -124,14 +124,67 @@ public class GridSqlQuerySplitter {
         }
 
         // Build resulting two step query.
-        GridCacheTwoStepQuery res = new GridCacheTwoStepQuery(rdcQry.getSQL());
+        GridCacheTwoStepQuery res = new GridCacheTwoStepQuery(rdcQry.getSQL(),
+            findParams(rdcQry, params, new ArrayList<>()).toArray());
 
-        res.addMapQuery(mergeTable, mapQry.getSQL(), params);
+        res.addMapQuery(mergeTable, mapQry.getSQL(),
+            findParams(mapQry, params, new ArrayList<>(params.length)).toArray());
 
         return res;
     }
 
     /**
+     * @param qry Select.
+     * @param params Parameters.
+     * @param target Extracted parameters.
+     * @return Extracted parameters list.
+     */
+    private static List<Object> findParams(GridSqlSelect qry, Object[] params, ArrayList<Object>
target) {
+        for (GridSqlElement el : qry.select())
+            findParams(el, params, target);
+
+        findParams(qry.from(), params, target);
+        findParams(qry.where(), params, target);
+
+        for (GridSqlElement el : qry.groups())
+            findParams(el, params, target);
+
+        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);
+
+        return target;
+    }
+
+    /**
+     * @param el Element.
+     * @param params Parameters.
+     * @param target Extracted parameters.
+     */
+    private static void findParams(GridSqlElement el, Object[] params, ArrayList<Object>
target) {
+        if (el == null)
+            return;
+
+        if (el instanceof GridSqlParameter) {
+            // H2 Supports queries like "select ?5" but first 4 non-existing parameters are
need to be set to any value.
+            // Here we will set them to NULL.
+            int idx = ((GridSqlParameter)el).index();
+
+            while (target.size() < idx)
+                target.add(null);
+
+            target.add(idx, params[idx]);
+        }
+        else
+            for (GridSqlElement child : el)
+                findParams(child, params, target);
+    }
+
+    /**
      * @param mapSelect Selects for map query.
      * @param rdcSelect Selects for reduce query.
      * @param idx Index.

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheCrossCacheQuerySelfTest.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheCrossCacheQuerySelfTest.java
b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheCrossCacheQuerySelfTest.java
index 87a28a1..4cbcc9b 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheCrossCacheQuerySelfTest.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheCrossCacheQuerySelfTest.java
@@ -35,6 +35,7 @@ import java.util.*;
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
 import static org.apache.ignite.cache.CacheDistributionMode.*;
 import static org.apache.ignite.cache.CachePreloadMode.*;
+import static org.apache.ignite.cache.query.Query.*;
 
 /**
  * Tests cross cache queries.
@@ -215,6 +216,24 @@ public class GridCacheCrossCacheQuerySelfTest extends GridCommonAbstractTest
{
     }
 
     /**
+     * @throws Exception If failed.
+     */
+    public void testApiQueries() throws Exception {
+        fillCaches();
+
+        IgniteCache<Object,Object> c = ignite.jcache("partitioned");
+
+        c.queryFields(sql("select cast(? as varchar) from FactPurchase").setArgs("aaa")).getAll();
+
+        List<List<?>> res = c.queryFields(sql("select cast(? as varchar), id
" +
+            "from FactPurchase order by id limit ? offset ?").setArgs("aaa", 1, 1)).getAll();
+
+        assertEquals(1, res.size());
+        assertEquals("aaa", res.get(0).get(0));
+        assertEquals(8, res.get(0).get(1));
+    }
+
+    /**
      * @param l List.
      * @param idx Index.
      * @return Int.

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/61eb6811/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 0b3ea80..bb7550b 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
@@ -108,13 +108,20 @@ public class GridQueryParsingTest extends GridCommonAbstractTest {
     /**
      *
      */
-    public void testAllExampless() throws Exception {
+    public void testAllExamples() throws Exception {
+        checkQuery("select ? limit ? offset ?");
+
         checkQuery("select cool1()");
         checkQuery("select cool1() z");
 
         checkQuery("select b,a from table0('aaa', 100)");
         checkQuery("select * from table0('aaa', 100)");
         checkQuery("select * from table0('aaa', 100) t0");
+        checkQuery("select x.a, y.b from table0('aaa', 100) x natural join table0('bbb',
100) y");
+        checkQuery("select * from table0('aaa', 100) x join table0('bbb', 100) y on x.a=y.a
and x.b = 'bbb'");
+        checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y on
x.a=y.a and x.b = 'bbb'");
+        checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y on
x.a=y.a where x.b = 'bbb'");
+        checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y where
x.b = 'bbb'");
 
         checkQuery("select avg(old) from Person left join Address on Person.addrId = Address.id
" +
             "where lower(Address.street) = lower(?)");


Mime
View raw message