Search in sources :

Example 1 with PlanKey

use of com.hazelcast.sql.impl.optimizer.PlanKey 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 PlanKey

use of com.hazelcast.sql.impl.optimizer.PlanKey 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 PlanKey

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

the class PlanKeyTest method testEquals.

@Test
public void testEquals() {
    PlanKey key = new PlanKey(singletonList(singletonList("schema1")), "sql1");
    checkEquals(key, new PlanKey(singletonList(singletonList("schema1")), "sql1"), true);
    checkEquals(key, new PlanKey(singletonList(singletonList("schema2")), "sql1"), false);
    checkEquals(key, new PlanKey(singletonList(singletonList("schema1")), "sql2"), false);
}
Also used : PlanKey(com.hazelcast.sql.impl.optimizer.PlanKey) Test(org.junit.Test)

Example 4 with PlanKey

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

the class CalciteSqlOptimizer method createPlan.

@SuppressWarnings("checkstyle:returncount")
private SqlPlan createPlan(OptimizationTask task, QueryParseResult parseResult, OptimizerContext context) {
    // TODO [sasha] : refactor this.
    SqlNode node = parseResult.getNode();
    PlanKey planKey = new PlanKey(task.getSearchPaths(), task.getSql());
    if (node instanceof SqlCreateMapping) {
        return toCreateMappingPlan(planKey, (SqlCreateMapping) node);
    } else if (node instanceof SqlDropMapping) {
        return toDropMappingPlan(planKey, (SqlDropMapping) node);
    } else if (node instanceof SqlCreateIndex) {
        return toCreateIndexPlan(planKey, (SqlCreateIndex) node);
    } else if (node instanceof SqlDropIndex) {
        return toDropIndexPlan(planKey, (SqlDropIndex) node);
    } else if (node instanceof SqlCreateJob) {
        return toCreateJobPlan(planKey, parseResult, context, task.getSql());
    } else if (node instanceof SqlAlterJob) {
        return toAlterJobPlan(planKey, (SqlAlterJob) node);
    } else if (node instanceof SqlDropJob) {
        return toDropJobPlan(planKey, (SqlDropJob) node);
    } else if (node instanceof SqlCreateSnapshot) {
        return toCreateSnapshotPlan(planKey, (SqlCreateSnapshot) node);
    } else if (node instanceof SqlDropSnapshot) {
        return toDropSnapshotPlan(planKey, (SqlDropSnapshot) node);
    } else if (node instanceof SqlCreateView) {
        return toCreateViewPlan(planKey, context, (SqlCreateView) node);
    } else if (node instanceof SqlDropView) {
        return toDropViewPlan(planKey, (SqlDropView) node);
    } else if (node instanceof SqlShowStatement) {
        return toShowStatementPlan(planKey, (SqlShowStatement) node);
    } else if (node instanceof SqlExplainStatement) {
        return toExplainStatementPlan(planKey, context, parseResult);
    } else {
        QueryConvertResult convertResult = context.convert(parseResult.getNode());
        return toPlan(planKey, parseResult.getParameterMetadata(), convertResult.getRel(), convertResult.getFieldNames(), context, parseResult.isInfiniteRows(), false, task.getSql());
    }
}
Also used : SqlCreateView(com.hazelcast.jet.sql.impl.parse.SqlCreateView) SqlDropView(com.hazelcast.jet.sql.impl.parse.SqlDropView) SqlDropJob(com.hazelcast.jet.sql.impl.parse.SqlDropJob) SqlAlterJob(com.hazelcast.jet.sql.impl.parse.SqlAlterJob) SqlDropMapping(com.hazelcast.jet.sql.impl.parse.SqlDropMapping) SqlCreateSnapshot(com.hazelcast.jet.sql.impl.parse.SqlCreateSnapshot) SqlCreateMapping(com.hazelcast.jet.sql.impl.parse.SqlCreateMapping) SqlDropIndex(com.hazelcast.jet.sql.impl.parse.SqlDropIndex) SqlCreateJob(com.hazelcast.jet.sql.impl.parse.SqlCreateJob) PlanKey(com.hazelcast.sql.impl.optimizer.PlanKey) SqlDropSnapshot(com.hazelcast.jet.sql.impl.parse.SqlDropSnapshot) QueryConvertResult(com.hazelcast.jet.sql.impl.parse.QueryConvertResult) SqlShowStatement(com.hazelcast.jet.sql.impl.parse.SqlShowStatement) SqlExplainStatement(com.hazelcast.jet.sql.impl.parse.SqlExplainStatement) SqlCreateIndex(com.hazelcast.jet.sql.impl.parse.SqlCreateIndex) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with PlanKey

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

the class CalciteSqlOptimizer method toCreateMappingPlan.

private SqlPlan toCreateMappingPlan(PlanKey planKey, SqlCreateMapping sqlCreateMapping) {
    List<MappingField> mappingFields = sqlCreateMapping.columns().map(field -> new MappingField(field.name(), field.type(), field.externalName())).collect(toList());
    Mapping mapping = new Mapping(sqlCreateMapping.nameWithoutSchema(), sqlCreateMapping.externalName(), sqlCreateMapping.type(), mappingFields, sqlCreateMapping.options());
    return new CreateMappingPlan(planKey, mapping, sqlCreateMapping.getReplace(), sqlCreateMapping.ifNotExists(), planExecutor);
}
Also used : SelectByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SelectByKeyMapPhysicalRel) DropViewPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropViewPlan) SelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.SelectPlan) SqlCreateIndex(com.hazelcast.jet.sql.impl.parse.SqlCreateIndex) CreateDagVisitor(com.hazelcast.jet.sql.impl.opt.physical.CreateDagVisitor) Operation(org.apache.calcite.rel.core.TableModify.Operation) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) Collections.singletonList(java.util.Collections.singletonList) AbstractMapTable(com.hazelcast.sql.impl.schema.map.AbstractMapTable) SlidingWindow(com.hazelcast.jet.sql.impl.opt.SlidingWindow) TableModify(org.apache.calcite.rel.core.TableModify) QueryUtils(com.hazelcast.sql.impl.QueryUtils) OptimizationTask(com.hazelcast.sql.impl.optimizer.OptimizationTask) CreateJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateJobPlan) CreateMappingPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateMappingPlan) RootRel(com.hazelcast.jet.sql.impl.opt.physical.RootRel) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DropMappingPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropMappingPlan) SqlOptimizer(com.hazelcast.sql.impl.optimizer.SqlOptimizer) RelVisitor(org.apache.calcite.rel.RelVisitor) IMapUpdatePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapUpdatePlan) IMapSelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSelectPlan) PlanKey(com.hazelcast.sql.impl.optimizer.PlanKey) SinkMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SinkMapPhysicalRel) Permission(java.security.Permission) SqlCreateJob(com.hazelcast.jet.sql.impl.parse.SqlCreateJob) DeleteByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.DeleteByKeyMapPhysicalRel) SqlDropView(com.hazelcast.jet.sql.impl.parse.SqlDropView) PhysicalRules(com.hazelcast.jet.sql.impl.opt.physical.PhysicalRules) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) IMapSinkPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSinkPlan) SqlConnectorCache(com.hazelcast.jet.sql.impl.connector.SqlConnectorCache) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) LogicalRel(com.hazelcast.jet.sql.impl.opt.logical.LogicalRel) DropIndexPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropIndexPlan) IMapInsertPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapInsertPlan) SqlDropJob(com.hazelcast.jet.sql.impl.parse.SqlDropJob) ActionConstants(com.hazelcast.security.permission.ActionConstants) Nullable(javax.annotation.Nullable) SqlCreateMapping(com.hazelcast.jet.sql.impl.parse.SqlCreateMapping) HazelcastRelMetadataQuery(com.hazelcast.jet.sql.impl.opt.metadata.HazelcastRelMetadataQuery) DropSnapshotPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropSnapshotPlan) Aggregate(org.apache.calcite.rel.core.Aggregate) ShowStatementPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.ShowStatementPlan) ExplainStatementPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.ExplainStatementPlan) SqlCreateView(com.hazelcast.jet.sql.impl.parse.SqlCreateView) MetadataResolver(com.hazelcast.jet.sql.impl.connector.map.MetadataResolver) Conventions(com.hazelcast.jet.sql.impl.opt.Conventions) Convention(org.apache.calcite.plan.Convention) QueryConvertResult(com.hazelcast.jet.sql.impl.parse.QueryConvertResult) LogicalRules(com.hazelcast.jet.sql.impl.opt.logical.LogicalRules) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) CreateIndexPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateIndexPlan) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) SqlNode(org.apache.calcite.sql.SqlNode) SqlExplainStatement(com.hazelcast.jet.sql.impl.parse.SqlExplainStatement) ViewTable(com.hazelcast.jet.sql.impl.connector.virtual.ViewTable) AlterJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.AlterJobPlan) QueryResultRegistry(com.hazelcast.sql.impl.state.QueryResultRegistry) PostgresqlSqlDialect(org.apache.calcite.sql.dialect.PostgresqlSqlDialect) Objects(java.util.Objects) TableResolver(com.hazelcast.sql.impl.schema.TableResolver) List(java.util.List) SqlDropIndex(com.hazelcast.jet.sql.impl.parse.SqlDropIndex) DropJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropJobPlan) MemberSelectors(com.hazelcast.cluster.memberselector.MemberSelectors) DmlPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan) SqlDropMapping(com.hazelcast.jet.sql.impl.parse.SqlDropMapping) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) QueryParseResult(com.hazelcast.jet.sql.impl.parse.QueryParseResult) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) SqlString(org.apache.calcite.sql.util.SqlString) TablesStorage(com.hazelcast.jet.sql.impl.schema.TablesStorage) MapPermission(com.hazelcast.security.permission.MapPermission) Project(org.apache.calcite.rel.core.Project) TableScan(org.apache.calcite.rel.core.TableScan) WatermarkedFields(com.hazelcast.jet.sql.impl.opt.metadata.WatermarkedFields) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) SqlShowStatement(com.hazelcast.jet.sql.impl.parse.SqlShowStatement) RelOptUtil(org.apache.calcite.plan.RelOptUtil) ILogger(com.hazelcast.logging.ILogger) IMapDeletePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapDeletePlan) SqlCreateSnapshot(com.hazelcast.jet.sql.impl.parse.SqlCreateSnapshot) QueryException(com.hazelcast.sql.impl.QueryException) SqlPlan(com.hazelcast.sql.impl.optimizer.SqlPlan) SqlDropSnapshot(com.hazelcast.jet.sql.impl.parse.SqlDropSnapshot) SqlAlterJob(com.hazelcast.jet.sql.impl.parse.SqlAlterJob) NodeEngine(com.hazelcast.spi.impl.NodeEngine) CreateViewPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateViewPlan) TableResolverImpl(com.hazelcast.jet.sql.impl.schema.TableResolverImpl) OptUtils(com.hazelcast.jet.sql.impl.opt.OptUtils) InsertMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.InsertMapPhysicalRel) RelNode(org.apache.calcite.rel.RelNode) CreateSnapshotPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateSnapshotPlan) IMapResolver(com.hazelcast.sql.impl.schema.IMapResolver) Mapping(com.hazelcast.sql.impl.schema.Mapping) Collectors.toList(java.util.stream.Collectors.toList) MappingField(com.hazelcast.sql.impl.schema.MappingField) UpdateByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.UpdateByKeyMapPhysicalRel) PhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.PhysicalRel) SqlCreateMapping(com.hazelcast.jet.sql.impl.parse.SqlCreateMapping) SqlDropMapping(com.hazelcast.jet.sql.impl.parse.SqlDropMapping) Mapping(com.hazelcast.sql.impl.schema.Mapping) CreateMappingPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateMappingPlan) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Aggregations

PlanKey (com.hazelcast.sql.impl.optimizer.PlanKey)7 SqlPlan (com.hazelcast.sql.impl.optimizer.SqlPlan)4 Test (org.junit.Test)4 PlanCache (com.hazelcast.sql.impl.plan.cache.PlanCache)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 QueryConvertResult (com.hazelcast.jet.sql.impl.parse.QueryConvertResult)2 SqlAlterJob (com.hazelcast.jet.sql.impl.parse.SqlAlterJob)2 SqlCreateIndex (com.hazelcast.jet.sql.impl.parse.SqlCreateIndex)2 SqlCreateJob (com.hazelcast.jet.sql.impl.parse.SqlCreateJob)2 SqlCreateMapping (com.hazelcast.jet.sql.impl.parse.SqlCreateMapping)2 SqlCreateSnapshot (com.hazelcast.jet.sql.impl.parse.SqlCreateSnapshot)2 SqlCreateView (com.hazelcast.jet.sql.impl.parse.SqlCreateView)2 SqlDropIndex (com.hazelcast.jet.sql.impl.parse.SqlDropIndex)2 SqlDropJob (com.hazelcast.jet.sql.impl.parse.SqlDropJob)2 SqlDropMapping (com.hazelcast.jet.sql.impl.parse.SqlDropMapping)2 SqlDropSnapshot (com.hazelcast.jet.sql.impl.parse.SqlDropSnapshot)2 SqlDropView (com.hazelcast.jet.sql.impl.parse.SqlDropView)2 SqlExplainStatement (com.hazelcast.jet.sql.impl.parse.SqlExplainStatement)2 SqlShowStatement (com.hazelcast.jet.sql.impl.parse.SqlShowStatement)2