Search in sources :

Example 46 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class IgniteSqlValidator method createSourceSelectForUpdate.

/**
 * {@inheritDoc}
 */
@Override
protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) {
    final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
    final SqlIdentifier targetTable = (SqlIdentifier) call.getTargetTable();
    final SqlValidatorTable table = getCatalogReader().getTable(targetTable.names);
    SqlIdentifier alias = call.getAlias() != null ? call.getAlias() : new SqlIdentifier(deriveAlias(targetTable, 0), SqlParserPos.ZERO);
    table.unwrap(IgniteTable.class).descriptor().selectForUpdateRowType((IgniteTypeFactory) typeFactory).getFieldNames().stream().map(name -> alias.plus(name, SqlParserPos.ZERO)).forEach(selectList::add);
    int ordinal = 0;
    // Force unique aliases to avoid a duplicate for Y with SET X=Y
    for (SqlNode exp : call.getSourceExpressionList()) {
        selectList.add(SqlValidatorUtil.addAlias(exp, SqlUtil.deriveAliasFromOrdinal(ordinal++)));
    }
    SqlNode sourceTable = call.getTargetTable();
    if (call.getAlias() != null) {
        sourceTable = SqlValidatorUtil.addAlias(sourceTable, call.getAlias().getSimple());
    }
    return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, call.getCondition(), null, null, null, null, null, null, null);
}
Also used : SqlUpdate(org.apache.calcite.sql.SqlUpdate) ArrayUtils.nullOrEmpty(org.apache.ignite.internal.util.ArrayUtils.nullOrEmpty) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlDelete(org.apache.calcite.sql.SqlDelete) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) RESOURCE(org.apache.calcite.util.Static.RESOURCE) RelOptTable(org.apache.calcite.plan.RelOptTable) BigDecimal(java.math.BigDecimal) SqlCall(org.apache.calcite.sql.SqlCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) SqlUtil(org.apache.calcite.sql.SqlUtil) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) SqlSelect(org.apache.calcite.sql.SqlSelect) EnumSet(java.util.EnumSet) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) TableDescriptor(org.apache.ignite.internal.sql.engine.schema.TableDescriptor) IgniteResource(org.apache.ignite.internal.sql.engine.util.IgniteResource) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Set(java.util.Set) Prepare(org.apache.calcite.prepare.Prepare) Nullable(org.jetbrains.annotations.Nullable) IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) Collections(java.util.Collections) SqlNodeList(org.apache.calcite.sql.SqlNodeList) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) SqlSelect(org.apache.calcite.sql.SqlSelect) SqlValidatorTable(org.apache.calcite.sql.validate.SqlValidatorTable) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 47 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class ProjectableFilterableTableScan method pushUpPredicate.

/**
 * PushUpPredicate.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
public RexNode pushUpPredicate() {
    if (condition == null || projects == null) {
        return replaceLocalRefs(condition);
    }
    IgniteTypeFactory typeFactory = Commons.typeFactory(getCluster());
    IgniteTable tbl = getTable().unwrap(IgniteTable.class);
    Mappings.TargetMapping mapping = RexUtils.inversePermutation(projects, tbl.getRowType(typeFactory, requiredColumns), true);
    RexShuttle shuttle = new RexShuttle() {

        @Override
        public RexNode visitLocalRef(RexLocalRef ref) {
            int targetRef = mapping.getSourceOpt(ref.getIndex());
            if (targetRef == -1) {
                throw new ControlFlowException();
            }
            return new RexInputRef(targetRef, ref.getType());
        }
    };
    List<RexNode> conjunctions = new ArrayList<>();
    for (RexNode conjunction : RelOptUtil.conjunctions(condition)) {
        try {
            conjunctions.add(shuttle.apply(conjunction));
        } catch (ControlFlowException ignore) {
        // No-op
        }
    }
    return RexUtil.composeConjunction(builder(getCluster()), conjunctions, true);
}
Also used : RexShuttle(org.apache.calcite.rex.RexShuttle) IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) Mappings(org.apache.calcite.util.mapping.Mappings) ControlFlowException(org.apache.calcite.util.ControlFlowException) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ArrayList(java.util.ArrayList) RexLocalRef(org.apache.calcite.rex.RexLocalRef) RexInputRef(org.apache.calcite.rex.RexInputRef) RelHint(org.apache.calcite.rel.hint.RelHint) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class AbstractAggregatePlannerTest method createBroadcastTable.

/**
 * CreateBroadcastTable.
 *
 * @return REPLICATED test table (ID, VAL0, VAL1, GRP0, GRP1)
 */
@NotNull
protected TestTable createBroadcastTable() {
    IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
    TestTable tbl = new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("VAL0", f.createJavaType(Integer.class)).add("VAL1", f.createJavaType(Integer.class)).add("GRP0", f.createJavaType(Integer.class)).add("GRP1", f.createJavaType(Integer.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    };
    return tbl;
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class CorrelatedNestedLoopJoinPlannerTest method testInvalidIndexExpressions.

/**
 * Check join with not equi condition. Current implementation of the CorrelatedNestedLoopJoinTest is not applicable
 * for such case.
 */
@Test
public void testInvalidIndexExpressions() throws Exception {
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
    publicSchema.addTable("T0", new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("JID", f.createJavaType(Integer.class)).add("VAL", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    }.addIndex(RelCollations.of(ImmutableIntList.of(1, 0)), "t0_jid_idx"));
    publicSchema.addTable("T1", new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("JID", f.createJavaType(Integer.class)).add("VAL", f.createJavaType(String.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.broadcast();
        }
    }.addIndex(RelCollations.of(ImmutableIntList.of(1, 0)), "t1_jid_idx"));
    String sql = "select * " + "from t0 " + "join t1 on t0.jid + 2 > t1.jid * 2";
    IgniteRel phys = physicalPlan(sql, publicSchema, "MergeJoinConverter", "NestedLoopJoinConverter", "FilterSpoolMergeRule");
    assertNotNull(phys);
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Test(org.junit.jupiter.api.Test)

Example 50 with IgniteTypeFactory

use of org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory in project ignite-3 by apache.

the class HashAggregatePlannerTest method subqueryWithAggregate.

/**
 * SubqueryWithAggregate.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 *
 * @throws Exception If failed.
 */
@Test
public void subqueryWithAggregate() throws Exception {
    IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
    TestTable employer = new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).add("SALARY", f.createJavaType(Double.class)).build()) {

        @Override
        public IgniteDistribution distribution() {
            return IgniteDistributions.affinity(0, "Employers", "hash");
        }
    };
    IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
    publicSchema.addTable("EMPS", employer);
    String sql = "SELECT * FROM emps WHERE emps.salary = (SELECT AVG(emps.salary) FROM emps)";
    IgniteRel phys = physicalPlan(sql, publicSchema);
    assertNotNull(phys);
    IgniteReduceHashAggregate rdcAgg = findFirstNode(phys, byClass(IgniteReduceHashAggregate.class));
    IgniteMapHashAggregate mapAgg = findFirstNode(phys, byClass(IgniteMapHashAggregate.class));
    assertNotNull(rdcAgg, "Invalid plan\n" + RelOptUtil.toString(phys));
    assertNotNull(mapAgg, "Invalid plan\n" + RelOptUtil.toString(phys));
    assertThat("Invalid plan\n" + RelOptUtil.toString(phys), first(rdcAgg.getAggregateCalls()).getAggregation(), IsInstanceOf.instanceOf(SqlAvgAggFunction.class));
    assertThat("Invalid plan\n" + RelOptUtil.toString(phys), first(mapAgg.getAggCallList()).getAggregation(), IsInstanceOf.instanceOf(SqlAvgAggFunction.class));
}
Also used : SqlAvgAggFunction(org.apache.calcite.sql.fun.SqlAvgAggFunction) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) IgniteRel(org.apache.ignite.internal.sql.engine.rel.IgniteRel) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) IgniteReduceHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteReduceHashAggregate) IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) IgniteMapHashAggregate(org.apache.ignite.internal.sql.engine.rel.agg.IgniteMapHashAggregate) Test(org.junit.jupiter.api.Test)

Aggregations

IgniteTypeFactory (org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory)80 Test (org.junit.jupiter.api.Test)48 RelDataType (org.apache.calcite.rel.type.RelDataType)47 IgniteSchema (org.apache.ignite.internal.sql.engine.schema.IgniteSchema)27 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)19 IgniteRel (org.apache.ignite.internal.sql.engine.rel.IgniteRel)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 RexNode (org.apache.calcite.rex.RexNode)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 ArrayList (java.util.ArrayList)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 IgniteDistribution (org.apache.ignite.internal.sql.engine.trait.IgniteDistribution)8 PlanningContext (org.apache.ignite.internal.sql.engine.prepare.PlanningContext)7 List (java.util.List)6 MappingQueryContext (org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext)6 MultiStepPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan)6 MultiStepQueryPlan (org.apache.ignite.internal.sql.engine.prepare.MultiStepQueryPlan)6 QueryTemplate (org.apache.ignite.internal.sql.engine.prepare.QueryTemplate)6