Repository: incubator-ignite Updated Branches: refs/heads/ignite-437-sqltests-p2 ca6fd6986 -> a577e89bc
ignite-437: the same data content Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a577e89b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a577e89b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a577e89b Branch: refs/heads/ignite-437-sqltests-p2 Commit: a577e89bc53a0de9173b77661a45b9519ce9c4fe Parents: ca6fd69 Author: Artem Shutak <[email protected]> Authored: Mon Apr 6 21:33:31 2015 +0300 Committer: Artem Shutak <[email protected]> Committed: Mon Apr 6 21:33:31 2015 +0300 ---------------------------------------------------------------------- .../query/h2/sql/H2CompareBigQueryTest.java | 442 ++++++++++++++++--- 1 file changed, 380 insertions(+), 62 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a577e89b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/H2CompareBigQueryTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/H2CompareBigQueryTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/H2CompareBigQueryTest.java index 08f7597..b172e97 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/H2CompareBigQueryTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/sql/H2CompareBigQueryTest.java @@ -18,26 +18,31 @@ package org.apache.ignite.internal.processors.query.h2.sql; import org.apache.ignite.cache.*; +import org.apache.ignite.cache.affinity.*; import org.apache.ignite.cache.query.annotations.*; import org.apache.ignite.configuration.*; import java.io.*; +import java.sql.Date; import java.sql.*; import java.util.*; -import java.util.Date; +import java.util.concurrent.atomic.*; /** * Executes one big query (and subqueries of the big query) to compare query results from h2 database instance and * * mixed ignite caches (replicated and partitioned) which have the same data models and data content. */ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { + private static final int ROOT_ORDER_CNT = 1; + private static final int DATES_CNT = 5; + /** Big repitable select of the big query. */ private static final String BSELECT = - " select date, orderId, rootOrderId, origOrderId \n" + - " from \"part\".OrderT1 where alias='CUSTOM'\n" + + " select date, orderId, rootOrderId, origOrderId, archSeq \n" + + " from \"part\".CustOrder where alias='CUSTOM'\n" + " union all\n" + - " select date, orderId, rootOrderId, origOrderId \n" + - " from \"part\".OrderT2 where alias='CUSTOM'"; + " select date, orderId, rootOrderId, origOrderId, archSeq \n" + + " from \"part\".ReplaceOrder where alias='CUSTOM'"; /** Big query "cop" part. */ private static final String BQ_COP = @@ -59,9 +64,9 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { private static final String BQ_OEP = " select e.date, e.rootOrderId as eRootOrderId, e.rootOrderId, sum(e.execShares) as execShares, \n" + " sum(e.execShares*e.price)/sum(e.execShares) as price,\n" + - " max(e.execDatetime) as execTime, min(e.execDatetime) as ordTime, max(e.status) as status,\n" + +// " max(e.execDatetime) as execTime, min(e.execDatetime) as ordTime, max(e.status) as status,\n" + " case when min(e.lastMkt) = max(e.lastMkt) then min(e.lastMkt) else min('XOFF') end as execMeet\n" + - " from Exec e\n" + + " from \"repl\".Exec e\n" + " group by e.date, e.rootOrderId"; /** Big query "cc" part. */ @@ -124,12 +129,14 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { @Override protected void setIndexedTypes(CacheConfiguration<?, ?> cc, CacheMode mode) { if (mode == CacheMode.PARTITIONED) cc.setIndexedTypes( - Integer.class, OrderT1.class, - Integer.class, OrderT2.class + Integer.class, CustOrder.class, + AffinityKey.class, ReplaceOrder.class, + AffinityKey.class, OrderParams.class, + AffinityKey.class, Cancel.class ); else if (mode == CacheMode.REPLICATED) cc.setIndexedTypes( -// Integer.class, Product.class + Integer.class, Exec.class ); else throw new IllegalStateException("mode: " + mode); @@ -138,46 +145,107 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected void initCacheAndDbData() throws SQLException { - int idGen = 0; + final AtomicInteger idGen = new AtomicInteger(); - final Collection<OrderT1> ordsT1 = new ArrayList<>(); + final Collection<Integer> rootOrderIds = new ArrayList<Integer>(){{ + for (int i = 0; i < ROOT_ORDER_CNT; i++) + add(idGen.incrementAndGet()); + }}; - for (int i = 0; i < 10; i++) { - int id = idGen++; + final Date curDate = new Date(new java.util.Date().getTime()); - OrderT1 order = new OrderT1(id, 2000 + id, id % 2 == 0 ? "CUSTOM" : "OTHER"); + final List<Date> dates = new ArrayList<Date>(){{ + for (int i = 0; i < DATES_CNT; i++) + add(new Date(curDate.getTime() - i * 24 * 60 * 60 * 1000)); // Minus i days. + }}; - ordsT1.add(order); + final Collection<CustOrder> ords = new ArrayList<CustOrder>(){{ + for (int rootOrderId : rootOrderIds) { + // Generate 1 - 5 orders for 1 root order. + for (int i = 0; i < rootOrderId % 5; i++) { + int orderId = idGen.incrementAndGet(); + + CustOrder order = new CustOrder(orderId, rootOrderId, dates.get(orderId % dates.size()) , + orderId % 2 == 0 ? "CUSTOM" : "OTHER"); - pCache.put(order.orderId, order); + add(order); - insertInDb(order); - } + pCache.put(order.orderId, order); - final Collection<OrderT2> ordsT2 = new ArrayList<>(); + insertInDb(order); + } + } + }}; - for (int i = 0; i < 10; i++) { - int id = idGen++; + final Collection<OrderParams> params = new ArrayList<OrderParams>(){{ + for (CustOrder o : ords) { + OrderParams op = new OrderParams(idGen.incrementAndGet(), o.orderId, o.date, + o.orderId % 2 == 0 ? "Algo 1" : "Algo 2"); - OrderT2 order = new OrderT2(id, 3000 + id, id % 2 == 0 ? "CUSTOM" : "OTHER"); + add(op); - ordsT2.add(order); + pCache.put(op.key(), op); - pCache.put(order.orderId, order); + insertInDb(op); + } + }}; + + final Collection<ReplaceOrder> replaces = new ArrayList<ReplaceOrder>(){{ + for (CustOrder o : ords) { + if (o.orderId % 7 == 0) { + ReplaceOrder replace = new ReplaceOrder(idGen.incrementAndGet(), o.orderId, o.rootOrderId, o.alias, + new Date(o.date.getTime() + 12 * 60 * 60 * 1000)); // Plus a half of day. - insertInDb(order); - } + add(replace); + + pCache.put(replace.key(), replace); + + insertInDb(replace); + } + } + }}; + + final Collection<Cancel> cancels = new ArrayList<Cancel>(){{ + for (CustOrder o : ords) { + if (o.orderId % 9 == 0) { + Cancel c = new Cancel(idGen.incrementAndGet(), o.orderId, + new Date(o.date.getTime() + 12 * 60 * 60 * 1000));// Plus a half of day. + + add(c); + + pCache.put(c.key(), c); + + insertInDb(c); + } + } + }}; + + final Collection<Exec> execs = new ArrayList<Exec>(){{ + for (int rootOrderId : rootOrderIds) { + int execShares = 10000 + rootOrderId; + int price = 1000 + rootOrderId; + int latsMkt = 3000 + rootOrderId; + + Exec exec = new Exec(rootOrderId, dates.get(rootOrderId % dates.size()), execShares, price, latsMkt); + + add(exec); + + rCache.put(exec.rootOrderId, exec); + + insertInDb(exec); + } + }}; } /** * @throws Exception If failed. */ @Override protected void checkAllDataEquals() throws Exception { - compareQueryRes0("select _key, _val, orderId, rootOrderId, date, alias from \"part\".ORDERT1"); - compareQueryRes0("select _key, _val, orderId, rootOrderId, date, alias from \"part\".ORDERT2"); -// compareQueryRes0("select _key, _val, orderId, rootOrderId, date, alias from \"repl\".OrderParams\n"); -// compareQueryRes0("select _key, _val, orderId, rootOrderId, date, alias from \"part\".Exec\n"); -// compareQueryRes0("select _key, _val, orderId, rootOrderId, date, alias from \"part\".Cancel\n"); + compareQueryRes0("select _key, _val, date, orderId, rootOrderId, alias from \"part\".CustOrder");// , archSeq + compareQueryRes0("select _key, _val, id, date, orderId, rootOrderId, alias from \"part\".ReplaceOrder");// , archSeq + compareQueryRes0("select _key, _val, id, date, orderId, parentAlgo from \"part\".OrderParams\n"); + compareQueryRes0("select _key, _val, id, date, refOrderId from \"part\".Cancel\n"); + compareQueryRes0(rCache, "select _key, _val, date, rootOrderId, execShares, price, lastMkt from \"repl\".Exec\n"); } /** @@ -186,11 +254,11 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { public void testUnionAllOrders() throws Exception { compareQueryRes0( " select date, orderId, rootOrderId " + - " from \"part\".OrderT1 where alias='CUSTOM'"); + " from \"part\".CustOrder where alias='CUSTOM'"); compareQueryRes0( " select date, orderId, rootOrderId " + - " from \"part\".OrderT2 where alias='CUSTOM'"); + " from \"part\".ReplaceOrder where alias='CUSTOM'"); compareQueryRes0( " select 10" + @@ -201,19 +269,21 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { compareQueryRes0( " select date, orderId, rootOrderId " + - " from \"part\".OrderT1 where alias='CUSTOM'" + + " from \"part\".CustOrder where alias='CUSTOM'" + " union all" + " select date, orderId, rootOrderId " + - " from \"part\".OrderT2 where alias='CUSTOM'"); + " from \"part\".ReplaceOrder where alias='CUSTOM'"); } /** * @throws Exception If failed. */ public void testBigQuery() throws Exception { - compareQueryRes0(BIG_QUERY); + List<List<?>> res = compareQueryRes0(BIG_QUERY); + + assertTrue(!res.isEmpty()); // Ensure we set good testing data at database. } /** @@ -230,7 +300,7 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { st.execute("CREATE SCHEMA \"part\""); st.execute("CREATE SCHEMA \"repl\""); - st.execute("create table \"part\".ORDERT1" + + st.execute("create table \"part\".CustOrder" + " (" + " _key int not null," + " _val other not null," + @@ -240,27 +310,58 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { " alias varchar(255)" + " )"); - st.execute("create table \"part\".ORDERT2" + + st.execute("create table \"part\".ReplaceOrder" + " (" + - " _key int not null," + + " _key other not null," + " _val other not null," + - " orderId int unique," + + " id int unique," + + " orderId int ," + " rootOrderId int," + " date Date, " + " alias varchar(255)" + " )"); + st.execute("create table \"part\".OrderParams" + + " (" + + " _key other not null," + + " _val other not null," + + " id int unique," + + " orderId int ," + + " date Date, " + + " parentAlgo varchar(255)" + + " )"); + + st.execute("create table \"part\".Cancel" + + " (" + + " _key other not null," + + " _val other not null," + + " id int unique," + + " date Date, " + + " refOrderId int" + + " )"); + + st.execute("create table \"repl\".Exec" + + " (" + + " _key int not null," + + " _val other not null," + + " rootOrderId int unique," + + " date Date, " + + " execShares int," + + " price int," + + " lastMkt int" + + " )"); + conn.commit(); } /** - * Insert {@link OrderT1} at h2 database. + * Insert {@link CustOrder} at h2 database. * - * @param o OrderT1. + * @param o CustOrder. */ - private void insertInDb(OrderT1 o) throws SQLException { + private void insertInDb(CustOrder o) throws SQLException { try(PreparedStatement st = conn.prepareStatement( - "insert into \"part\".OrderT1 (_key, _val, orderId, rootOrderId, date, alias) values(?, ?, ?, ?, ?, ?)")) { + "insert into \"part\".CustOrder (_key, _val, orderId, rootOrderId, date, alias) values(?, ?, ?, ?, ?, ?)")) { st.setObject(1, o.orderId); st.setObject(2, o); st.setObject(3, o.orderId); @@ -273,25 +374,93 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { } /** - * Insert {@link OrderT2} at h2 database. + * Insert {@link ReplaceOrder} at h2 database. * - * @param o OrderT2. + * @param o ReplaceOrder. */ - private void insertInDb(OrderT2 o) throws SQLException { + private void insertInDb(ReplaceOrder o) throws SQLException { try(PreparedStatement st = conn.prepareStatement( - "insert into \"part\".OrderT2 (_key, _val, orderId, rootOrderId, date, alias) values(?, ?, ?, ?, ?, ?)")) { - st.setObject(1, o.orderId); - st.setObject(2, o); - st.setObject(3, o.orderId); - st.setObject(4, o.rootOrderId); - st.setObject(5, o.date); - st.setObject(6, o.alias); + "insert into \"part\".ReplaceOrder (_key, _val, id, orderId, rootOrderId, date, alias) " + + "values(?, ?, ?, ?, ?, ?, ?)")) { + int i = 0; + + st.setObject(++i, o.key()); + st.setObject(++i, o); + st.setObject(++i, o.id); + st.setObject(++i, o.orderId); + st.setObject(++i, o.rootOrderId); + st.setObject(++i, o.date); + st.setObject(++i, o.alias); st.executeUpdate(); } } - static class OrderT1 implements Serializable { + /** + * Insert {@link OrderParams} at h2 database. + * + * @param o OrderParams. + */ + private void insertInDb(OrderParams o) throws SQLException { + try(PreparedStatement st = conn.prepareStatement( + "insert into \"part\".OrderParams (_key, _val, id, date, orderId, parentAlgo) values(?, ?, ?, ?, ?, ?)")) { + int i = 0; + + st.setObject(++i, o.key()); + st.setObject(++i, o); + st.setObject(++i, o.id); + st.setObject(++i, o.date); + st.setObject(++i, o.orderId); + st.setObject(++i, o.parentAlgo); + + st.executeUpdate(); + } + } + + /** + * Insert {@link Cancel} at h2 database. + * + * @param o Cancel. + */ + private void insertInDb(Cancel o) throws SQLException { + try(PreparedStatement st = conn.prepareStatement( + "insert into \"part\".Cancel (_key, _val, id, date, refOrderId) values(?, ?, ?, ?, ?)")) { + int i = 0; + + st.setObject(++i, o.key()); + st.setObject(++i, o); + st.setObject(++i, o.id); + st.setObject(++i, o.date); + st.setObject(++i, o.refOrderId); + + st.executeUpdate(); + } + } + + /** + * Insert {@link Exec} at h2 database. + * + * @param o Execution. + */ + private void insertInDb(Exec o) throws SQLException { + try(PreparedStatement st = conn.prepareStatement( + "insert into \"repl\".Exec (_key, _val, date, rootOrderId, execShares, price, lastMkt) " + + "values(?, ?, ?, ?, ?, ?, ?)")) { + int i = 0; + + st.setObject(++i, o.rootOrderId); + st.setObject(++i, o); + st.setObject(++i, o.date); + st.setObject(++i, o.rootOrderId); + st.setObject(++i, o.execShares); + st.setObject(++i, o.price); + st.setObject(++i, o.lastMkt); + + st.executeUpdate(); + } + } + + static class CustOrder implements Serializable { /** Primary key. */ @QuerySqlField(index = true) private int orderId; @@ -311,17 +480,19 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { /** * @param orderId Order id. * @param rootOrderId Root order id. + * @param date Date. * @param alias Alias. */ - OrderT1(int orderId, int rootOrderId, String alias) { + CustOrder(int orderId, int rootOrderId, Date date, String alias) { this.orderId = orderId; this.rootOrderId = rootOrderId; + this.date = date; this.alias = alias; } /** {@inheritDoc} */ @Override public boolean equals(Object o) { - return this == o || o instanceof OrderT1 && orderId == ((OrderT1)o).orderId; + return this == o || o instanceof CustOrder && orderId == ((CustOrder)o).orderId; } /** {@inheritDoc} */ @@ -330,9 +501,13 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { } } - static class OrderT2 implements Serializable { + static class ReplaceOrder implements Serializable { /** Primary key. */ @QuerySqlField(index = true) + private int id; + + /** Order id. */ + @QuerySqlField(index = true) private int orderId; /** Root order ID*/ @@ -347,20 +522,163 @@ public class H2CompareBigQueryTest extends AbstractH2CompareQueryTest { @QuerySqlField private String alias = "CUSTOM"; - OrderT2(int orderId, int rootOrderId, String alias) { + ReplaceOrder(int id, int orderId, int rootOrderId, String alias, Date date) { + this.id = id; this.orderId = orderId; this.rootOrderId = rootOrderId; + this.date = date; this.alias = alias; } + /** + * @return Afinity key. + */ + public AffinityKey<Integer> key() { + return new AffinityKey<>(id, orderId); + } + /** {@inheritDoc} */ @Override public boolean equals(Object o) { - return this == o || o instanceof OrderT2 && orderId == ((OrderT2)o).orderId; + return this == o || o instanceof ReplaceOrder && id == ((ReplaceOrder)o).id; } /** {@inheritDoc} */ @Override public int hashCode() { - return orderId; + return id; + } + } + + static class OrderParams implements Serializable { + /** Primary key. */ + @QuerySqlField(index = true) + private int id; + + /** Order id. */ + @QuerySqlField(index = true) + private int orderId; + + /** Date */ + @QuerySqlField + private Date date ; + + /** */ + @QuerySqlField + private String parentAlgo = "CUSTOM_ALGO"; + + OrderParams(int id, int orderId, Date date, String parentAlgo) { + this.id = id; + this.orderId = orderId; + this.date = date; + this.parentAlgo = parentAlgo; + } + + /** + * @return Afinity key. + */ + public AffinityKey<Integer> key() { + return new AffinityKey<>(id, orderId); + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + return this == o || o instanceof OrderParams && id == ((OrderParams)o).id; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return id; + } + } + + static class Cancel implements Serializable { + /** Primary key. */ + @QuerySqlField(index = true) + private int id; + + /** Order id. */ + @QuerySqlField(index = true) + private int refOrderId; + + /** Date */ + @QuerySqlField + private Date date; + + /** + * @param id + * @param refOrderId Reference order id. + * @param date Date. + */ + Cancel(int id, int refOrderId, Date date) { + this.id = id; + this.refOrderId = refOrderId; + this.date = date; + } + + /** + * @return Afinity key. + */ + public AffinityKey<Integer> key() { + return new AffinityKey<>(id, refOrderId); + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + return this == o || o instanceof Cancel && id == ((Cancel)o).id; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return id; + } + } + + /** + * Execute information about root query. + */ + static class Exec implements Serializable { + /** Primary key. */ + @QuerySqlField + private int rootOrderId; + + /** Date */ + @QuerySqlField + private Date date ; + + /** */ + @QuerySqlField + private int execShares; + + /** */ + @QuerySqlField + private int price; + + /** */ + @QuerySqlField + private int lastMkt; + + /** + * @param rootOrderId Root order id. + * @param date Date. + * @param execShares Execute shares. + * @param price Price. + * @param lastMkt Last mkt. + */ + Exec(int rootOrderId, Date date, int execShares, int price, int lastMkt) { + this.rootOrderId = rootOrderId; + this.date = date; + this.execShares = execShares; + this.price = price; + this.lastMkt = lastMkt; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + return this == o || o instanceof Exec && rootOrderId == ((Exec)o).rootOrderId; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return rootOrderId; } } }
