Search in sources :

Example 1 with SqlPlan

use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.

the class PlanCacheTest method testBasicOperations.

@Test
public void testBasicOperations() {
    PlanCache cache = new PlanCache(10);
    PlanKey key = createKey("sql");
    // Put plan
    SqlPlan plan1 = createPlan(key, PART_MAP_1);
    cache.put(key, plan1);
    assertEquals(1, cache.size());
    assertSame(plan1, cache.get(key));
    // Overwrite plan
    SqlPlan plan2 = createPlan(key, PART_MAP_2);
    cache.put(key, plan2);
    assertEquals(1, cache.size());
    assertSame(plan2, cache.get(key));
    // Invalidate the plan that is no longer cached
    cache.invalidate(plan1);
    assertEquals(1, cache.size());
    assertSame(plan2, cache.get(key));
    // Invalidate cache plan
    cache.invalidate(plan2);
    assertEquals(0, cache.size());
    assertNull(cache.get(key));
    // Clear
    cache.put(key, plan1);
    assertEquals(1, cache.size());
    assertSame(plan1, cache.get(key));
    cache.clear();
    assertEquals(0, cache.size());
    assertNull(cache.get(key));
}
Also used : PlanKey(com.hazelcast.sql.impl.optimizer.PlanKey) SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) PlanCache(com.hazelcast.sql.impl.plan.cache.PlanCache) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with SqlPlan

use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.

the class SqlServiceImpl method prepare.

private SqlPlan prepare(String schema, String sql, List<Object> arguments, SqlExpectedResultType expectedResultType) {
    List<List<String>> searchPaths = prepareSearchPaths(schema);
    PlanKey planKey = new PlanKey(searchPaths, sql);
    SqlPlan plan = planCache.get(planKey);
    if (plan == null) {
        SqlCatalog catalog = new SqlCatalog(optimizer.tableResolvers());
        plan = optimizer.prepare(new OptimizationTask(sql, arguments, searchPaths, catalog));
        if (plan.isCacheable()) {
            planCache.put(planKey, plan);
        }
    }
    checkReturnType(plan, expectedResultType);
    return plan;
}
Also used : SqlCatalog(com.hazelcast.sql.impl.schema.SqlCatalog) PlanKey(com.hazelcast.sql.impl.optimizer.PlanKey) OptimizationTask(com.hazelcast.sql.impl.optimizer.OptimizationTask) SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List)

Example 3 with SqlPlan

use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.

the class SqlServiceImpl method query0.

private SqlResult query0(QueryId queryId, String schema, String sql, List<Object> args, long timeout, int pageSize, SqlExpectedResultType expectedResultType, SqlSecurityContext securityContext) {
    // Validate and normalize
    if (sql == null || sql.isEmpty()) {
        throw QueryException.error("SQL statement cannot be empty.");
    }
    List<Object> args0 = new ArrayList<>(args);
    if (timeout < 0) {
        throw QueryException.error("Timeout cannot be negative: " + timeout);
    }
    if (pageSize <= 0) {
        throw QueryException.error("Page size must be positive: " + pageSize);
    }
    // Prepare and execute
    SqlPlan plan = prepare(schema, sql, args0, expectedResultType);
    if (securityContext.isSecurityEnabled()) {
        plan.checkPermissions(securityContext);
    }
    // TODO: pageSize ?
    return plan.execute(queryId, args0, timeout);
}
Also used : SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) ArrayList(java.util.ArrayList)

Example 4 with SqlPlan

use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.

the class PlanCache method shrinkIfNeeded.

private void shrinkIfNeeded() {
    int oversize = plans.size() - maxSize;
    if (oversize <= 0) {
        return;
    }
    // Sort plans according to their last used timestamps
    TreeMap<Long, SqlPlan> sorted = new TreeMap<>();
    for (SqlPlan plan : plans.values()) {
        sorted.put(plan.getPlanLastUsed(), plan);
    }
    // Remove oldest plans
    for (SqlPlan plan : sorted.values()) {
        boolean removed = remove(plan);
        if (removed) {
            if (--oversize == 0) {
                break;
            }
        }
    }
}
Also used : SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) TreeMap(java.util.TreeMap)

Example 5 with SqlPlan

use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.

the class PlanCacheTestSupport method createPlan.

public static SqlPlan createPlan(PlanKey key, Map<UUID, PartitionIdSet> partitions, int... objectKeys) {
    Set<PlanObjectKey> objectKeys0 = new HashSet<>();
    if (objectKeys != null) {
        for (int objectId : objectKeys) {
            objectKeys0.add(createObjectId(objectId));
        }
    }
    SqlPlan plan = new TestPlan(key, objectKeys0, partitions);
    assertEquals(key, plan.getPlanKey());
    assertEquals(0L, plan.getPlanLastUsed());
    return plan;
}
Also used : PlanObjectKey(com.hazelcast.sql.impl.optimizer.PlanObjectKey) SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) HashSet(java.util.HashSet)

Aggregations

SqlPlan (com.hazelcast.sql.impl.optimizer.SqlPlan)9 PlanKey (com.hazelcast.sql.impl.optimizer.PlanKey)4 PlanCache (com.hazelcast.sql.impl.plan.cache.PlanCache)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 MemberSelectors (com.hazelcast.cluster.memberselector.MemberSelectors)1 AlterJobPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.AlterJobPlan)1 CreateIndexPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateIndexPlan)1 CreateJobPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateJobPlan)1 CreateMappingPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateMappingPlan)1 CreateSnapshotPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateSnapshotPlan)1 CreateViewPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateViewPlan)1 DmlPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan)1 DropIndexPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DropIndexPlan)1 DropJobPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DropJobPlan)1 DropMappingPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DropMappingPlan)1 DropSnapshotPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DropSnapshotPlan)1 DropViewPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DropViewPlan)1