You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2019/05/01 19:20:36 UTC

[metamodel] branch master updated (17fc1e8 -> 50044d0)

This is an automated email from the ASF dual-hosted git repository.

kaspersor pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/metamodel.git.


    from 17fc1e8  Improved HBase query building by scanning columns.
     new 30095d8  Added unit test which validates the working of a query which joins two tables and then uses a compound FilterItem in the where statement to filter out some more results.
     new 50044d0  Refactored logic which gets "applicable" filters, so it gets the SelectItems from compound FilterItems by recursively calling the getSelectItems method when a FilterItem has children.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/metamodel/MetaModelHelper.java | 25 +++++++-----
 .../apache/metamodel/pojo/PojoDataContextTest.java | 44 ++++++++++++++++++++++
 .../metamodel/pojo/{Person.java => Role.java}      | 30 ++++++---------
 .../metamodel/pojo/{Person.java => User.java}      | 44 ++++++++++++----------
 4 files changed, 94 insertions(+), 49 deletions(-)
 copy pojo/src/test/java/org/apache/metamodel/pojo/{Person.java => Role.java} (72%)
 copy pojo/src/test/java/org/apache/metamodel/pojo/{Person.java => User.java} (60%)


[metamodel] 01/02: Added unit test which validates the working of a query which joins two tables and then uses a compound FilterItem in the where statement to filter out some more results.

Posted by ka...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kaspersor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/metamodel.git

commit 30095d826eecd572851eb89d1a77176f3f525e4c
Author: Arjan Seijkens <a....@quadient.com>
AuthorDate: Wed Apr 24 14:28:16 2019 +0200

    Added unit test which validates the working of a query which joins two tables and then uses a compound FilterItem in the where statement to filter out some more results.
---
 .../apache/metamodel/pojo/PojoDataContextTest.java | 44 +++++++++++++++++
 .../test/java/org/apache/metamodel/pojo/Role.java  | 45 +++++++++++++++++
 .../test/java/org/apache/metamodel/pojo/User.java  | 57 ++++++++++++++++++++++
 3 files changed, 146 insertions(+)

diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java b/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
index d520556..6d1638f 100644
--- a/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/PojoDataContextTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.metamodel.pojo;
 
+import java.sql.Date;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -31,7 +32,13 @@ import org.apache.metamodel.DataContext;
 import org.apache.metamodel.UpdateCallback;
 import org.apache.metamodel.UpdateScript;
 import org.apache.metamodel.data.DataSet;
+import org.apache.metamodel.data.Row;
+import org.apache.metamodel.query.FilterItem;
+import org.apache.metamodel.query.LogicalOperator;
+import org.apache.metamodel.query.OperatorType;
 import org.apache.metamodel.query.Query;
+import org.apache.metamodel.query.SelectItem;
+import org.apache.metamodel.schema.Column;
 import org.apache.metamodel.schema.ColumnType;
 import org.apache.metamodel.schema.Schema;
 import org.apache.metamodel.schema.Table;
@@ -215,4 +222,41 @@ public class PojoDataContextTest extends TestCase {
         map.put("col3", c);
         return map;
     }
+
+    public void testJoinWithCompoundFilterItems() {
+        final List<Role> roles = new ArrayList<>();
+        roles.add(new Role(1, "admin"));
+        roles.add(new Role(2, "guest"));
+
+        final List<User> users = new ArrayList<>();
+        users.add(new User("pete", null, 1));
+        users.add(new User("jake", Date.valueOf("2018-1-1"), 1));
+        users.add(new User("susan", Date.valueOf("2020-1-1"), 2));
+
+        final DataContext dataContext = new PojoDataContext("userdb", new ObjectTableDataProvider<User>("users",
+                User.class, users), new ObjectTableDataProvider<Role>("roles", Role.class, roles));
+
+        final SelectItem expirationDateField = new SelectItem(dataContext
+                .getColumnByQualifiedLabel("userdb.users.expirationDate"));
+        final Column userRoleIdColumn = dataContext.getColumnByQualifiedLabel("userdb.users.roleId");
+        final Column roleIdColumn = dataContext.getColumnByQualifiedLabel("userdb.roles.id");
+
+        DataSet dataSet = dataContext
+                .query()
+                .from("users")
+                .and("roles")
+                .select("users.name")
+                .where(userRoleIdColumn)
+                .eq(roleIdColumn)
+                .where(new FilterItem(LogicalOperator.OR, new FilterItem(expirationDateField, OperatorType.EQUALS_TO,
+                        null), new FilterItem(expirationDateField, OperatorType.GREATER_THAN_OR_EQUAL, Date
+                                .valueOf("2019-1-1"))))
+                .orderBy("name")
+                .execute();
+
+        List<Row> rows = dataSet.toRows();
+        assertEquals(2, rows.size());
+        assertEquals("pete", rows.get(0).getValue(0));
+        assertEquals("susan", rows.get(1).getValue(0));
+    }
 }
\ No newline at end of file
diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/Role.java b/pojo/src/test/java/org/apache/metamodel/pojo/Role.java
new file mode 100644
index 0000000..06a5f80
--- /dev/null
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/Role.java
@@ -0,0 +1,45 @@
+/**
+ * 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.metamodel.pojo;
+
+class Role {
+    private int id; 
+    private String name;
+
+    public Role(final int id, final String name) {
+        this.name = name;
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(final int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+}
diff --git a/pojo/src/test/java/org/apache/metamodel/pojo/User.java b/pojo/src/test/java/org/apache/metamodel/pojo/User.java
new file mode 100644
index 0000000..37987c2
--- /dev/null
+++ b/pojo/src/test/java/org/apache/metamodel/pojo/User.java
@@ -0,0 +1,57 @@
+/**
+ * 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.metamodel.pojo;
+
+import java.util.Date;
+
+class User {
+    private String name;
+    private Date expirationDate;
+    private int roleId;
+
+    public User(final String name, final Date expirationDate, final int roleId) {
+        this.name = name;
+        this.expirationDate = expirationDate;
+        this.roleId = roleId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+
+    public Date getExpirationDate() {
+        return expirationDate;
+    }
+
+    public void setExpirationDate(final Date expirationDate) {
+        this.expirationDate = expirationDate;
+    }
+
+    public int getRoleId() {
+        return roleId;
+    }
+
+    public void setRole(final int roleId) {
+        this.roleId = roleId;
+    }
+}


[metamodel] 02/02: Refactored logic which gets "applicable" filters, so it gets the SelectItems from compound FilterItems by recursively calling the getSelectItems method when a FilterItem has children.

Posted by ka...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kaspersor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/metamodel.git

commit 50044d0bbeb001b8ac84ddfdd423048fa49b53df
Author: Arjan Seijkens <a....@quadient.com>
AuthorDate: Wed Apr 24 15:13:19 2019 +0200

    Refactored logic which gets "applicable" filters, so it gets the SelectItems from compound FilterItems by recursively calling the getSelectItems method when a FilterItem has children.
---
 .../java/org/apache/metamodel/MetaModelHelper.java | 25 +++++++++++++---------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/core/src/main/java/org/apache/metamodel/MetaModelHelper.java b/core/src/main/java/org/apache/metamodel/MetaModelHelper.java
index c484736..f633918 100644
--- a/core/src/main/java/org/apache/metamodel/MetaModelHelper.java
+++ b/core/src/main/java/org/apache/metamodel/MetaModelHelper.java
@@ -253,20 +253,25 @@ public final class MetaModelHelper {
      */
     private static Set<FilterItem> applicableFilters(Collection<FilterItem> filters,
             Collection<SelectItem> selectItemList) {
+        final Set<SelectItem> items = new HashSet<>(selectItemList);
 
-        Set<SelectItem> items = new HashSet<SelectItem>(selectItemList);
+        return filters.stream().filter(fi -> items.containsAll(getSelectItems(fi))).collect(Collectors.toSet());
+    }
 
-        return filters.stream().filter(fi -> {
-            Collection<SelectItem> fiSelectItems = new ArrayList<>();
-            fiSelectItems.add(fi.getSelectItem());
-            Object operand = fi.getOperand();
+    private static Set<SelectItem> getSelectItems(final FilterItem filterItem) {
+        final Set<SelectItem> itemsInFilter = new HashSet<>();
+        if (filterItem.getChildItemCount() == 0) {
+            itemsInFilter.add(filterItem.getSelectItem());
+            final Object operand = filterItem.getOperand();
             if (operand instanceof SelectItem) {
-                fiSelectItems.add((SelectItem) operand);
+                itemsInFilter.add((SelectItem) operand);
             }
-
-            return items.containsAll(fiSelectItems);
-
-        }).collect(Collectors.toSet());
+        } else {
+            for (FilterItem childFilterItem : filterItem.getChildItems()) {
+                itemsInFilter.addAll(getSelectItems(childFilterItem));
+            }
+        }
+        return itemsInFilter;
     }
 
     public static DataSet getFiltered(DataSet dataSet, Iterable<FilterItem> filterItems) {