Search in sources :

Example 91 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class QueryIndexTest method testPredicateNotEqualWithIndex.

private void testPredicateNotEqualWithIndex(IMap<Integer, Value> map, boolean ordered) {
    map.addIndex("name", ordered);
    map.put(1, new Value("abc", 1));
    map.put(2, new Value("xyz", 2));
    map.put(3, new Value("aaa", 3));
    assertEquals(3, map.values(new SqlPredicate("name != 'aac'")).size());
    assertEquals(2, map.values(new SqlPredicate("index != 2")).size());
    assertEquals(3, map.values(new SqlPredicate("name <> 'aac'")).size());
    assertEquals(2, map.values(new SqlPredicate("index <> 2")).size());
    assertEquals(3, map.values(new PredicateBuilder().getEntryObject().get("name").notEqual("aac")).size());
    assertEquals(2, map.values(new PredicateBuilder().getEntryObject().get("index").notEqual(2)).size());
}
Also used : PredicateBuilder(com.hazelcast.query.PredicateBuilder) Value(com.hazelcast.query.SampleObjects.Value) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 92 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class QueryBasicTest method testSqlQueryUsingNested__KeyField.

@Test
public void testSqlQueryUsingNested__KeyField() {
    Config config = getConfig();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    factory.newHazelcastInstance(config);
    HazelcastInstance instance = factory.newHazelcastInstance(config);
    IMap<Object, Object> map = instance.getMap(randomMapName());
    Object key = new CustomAttribute(12, 123L);
    Object value = "value";
    map.put(key, value);
    Collection<Object> values = map.values(new SqlPredicate("__key.age = 12 and __key.height = 123"));
    assertEquals(1, values.size());
    assertEquals(value, values.iterator().next());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) EntryObject(com.hazelcast.query.EntryObject) ParentPortableObject(com.hazelcast.nio.serialization.PortableTest.ParentPortableObject) GrandParentPortableObject(com.hazelcast.nio.serialization.PortableTest.GrandParentPortableObject) ChildPortableObject(com.hazelcast.nio.serialization.PortableTest.ChildPortableObject) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 93 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class QueryBasicTest method doFunctionalQueryTest.

public static void doFunctionalQueryTest(IMap<String, Employee> map) {
    map.put("1", new Employee("joe", 33, false, 14.56));
    map.put("2", new Employee("ali", 23, true, 15.00));
    for (int i = 3; i < 103; i++) {
        map.put(String.valueOf(i), new Employee("name" + i, i % 60, ((i & 1) == 1), i));
    }
    Set<Map.Entry<String, Employee>> entries = map.entrySet();
    assertEquals(102, entries.size());
    int entryCount = 0;
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertNotNull(employee);
        entryCount++;
    }
    assertEquals(102, entryCount);
    EntryObject entryObject = new PredicateBuilder().getEntryObject();
    Predicate predicate = entryObject.is("active").and(entryObject.get("age").equal(23));
    entries = map.entrySet(predicate);
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
    map.remove("2");
    entries = map.entrySet(predicate);
    assertEquals(2, entries.size());
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
    entries = map.entrySet(new SqlPredicate(" (age >= " + 30 + ") AND (age <= " + 40 + ")"));
    assertEquals(23, entries.size());
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertTrue(employee.getAge() >= 30);
        assertTrue(employee.getAge() <= 40);
    }
}
Also used : Employee(com.hazelcast.query.SampleObjects.Employee) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) SqlPredicate(com.hazelcast.query.SqlPredicate) Map(java.util.Map) IMap(com.hazelcast.core.IMap) Predicate(com.hazelcast.query.Predicate) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 94 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class QueryBasicTest method testInvalidSqlPredicate.

@Test(timeout = 1000 * 60)
public void testInvalidSqlPredicate() {
    Config cfg = getConfig();
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
    HazelcastInstance instance = nodeFactory.newHazelcastInstance(cfg);
    IMap<Integer, Employee> map = instance.getMap("employee");
    map.put(1, new Employee("e", 1, false, 0));
    map.put(2, new Employee("e2", 1, false, 0));
    try {
        map.values(new SqlPredicate("invalid_sql"));
        fail("Should fail because of invalid SQL!");
    } catch (RuntimeException e) {
        assertContains(e.getMessage(), "There is no suitable accessor for 'invalid_sql'");
    }
    try {
        map.values(new SqlPredicate("invalid sql"));
        fail("Should fail because of invalid SQL!");
    } catch (RuntimeException e) {
        assertContains(e.getMessage(), "Invalid SQL: [invalid sql]");
    }
    try {
        map.values(new SqlPredicate("invalid and sql"));
        fail("Should fail because of invalid SQL!");
    } catch (RuntimeException e) {
        assertContains(e.getMessage(), "There is no suitable accessor for 'invalid'");
    }
    try {
        map.values(new SqlPredicate("invalid sql and"));
        fail("Should fail because of invalid SQL!");
    } catch (RuntimeException e) {
        assertContains(e.getMessage(), "There is no suitable accessor for 'invalid'");
    }
    try {
        map.values(new SqlPredicate(""));
        fail("Should fail because of invalid SQL!");
    } catch (RuntimeException e) {
        assertContains(e.getMessage(), "Invalid SQL: []");
    }
    assertEquals(2, map.values(new SqlPredicate("age=1 and name like 'e%'")).size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 95 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class QueryBasicTest method testSqlQueryUsing__KeyField.

@Test
public void testSqlQueryUsing__KeyField() {
    Config config = getConfig();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = factory.newHazelcastInstance(config);
    HazelcastInstance instance2 = factory.newHazelcastInstance(config);
    IMap<Object, Object> map = instance2.getMap(randomMapName());
    Object key = generateKeyOwnedBy(instance1);
    Object value = "value";
    map.put(key, value);
    Collection<Object> values = map.values(new SqlPredicate("__key = '" + key + "'"));
    assertEquals(1, values.size());
    assertEquals(value, values.iterator().next());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) EntryObject(com.hazelcast.query.EntryObject) ParentPortableObject(com.hazelcast.nio.serialization.PortableTest.ParentPortableObject) GrandParentPortableObject(com.hazelcast.nio.serialization.PortableTest.GrandParentPortableObject) ChildPortableObject(com.hazelcast.nio.serialization.PortableTest.ChildPortableObject) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

SqlPredicate (com.hazelcast.query.SqlPredicate)111 Test (org.junit.Test)93 QuickTest (com.hazelcast.test.annotation.QuickTest)87 ParallelTest (com.hazelcast.test.annotation.ParallelTest)83 HazelcastInstance (com.hazelcast.core.HazelcastInstance)73 Config (com.hazelcast.config.Config)36 Employee (com.hazelcast.mapreduce.helpers.Employee)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)24 MapIndexConfig (com.hazelcast.config.MapIndexConfig)22 IMap (com.hazelcast.core.IMap)21 Employee (com.hazelcast.query.SampleObjects.Employee)21 MapStoreConfig (com.hazelcast.config.MapStoreConfig)18 Collection (java.util.Collection)14 MapConfig (com.hazelcast.config.MapConfig)13 Predicate (com.hazelcast.query.Predicate)12 Map (java.util.Map)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 PortableEmployee (com.hazelcast.query.SampleObjects.PortableEmployee)10 AssertTask (com.hazelcast.test.AssertTask)10 Value (com.hazelcast.query.SampleObjects.Value)9