Search in sources :

Example 1 with IgniteTypeFactory

use of org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory in project ignite by apache.

the class RelJson method toRex.

/**
 */
RexNode toRex(RelInput relInput, Object o) {
    RelOptCluster cluster = relInput.getCluster();
    RexBuilder rexBuilder = cluster.getRexBuilder();
    if (o == null)
        return null;
    else if (o instanceof Map) {
        Map map = (Map) o;
        Map<String, Object> opMap = (Map) map.get("op");
        IgniteTypeFactory typeFactory = Commons.typeFactory(cluster);
        if (opMap != null) {
            if (map.containsKey("class"))
                opMap.put("class", map.get("class"));
            List operands = (List) map.get("operands");
            List<RexNode> rexOperands = toRexList(relInput, operands);
            Object jsonType = map.get("type");
            Map window = (Map) map.get("window");
            if (window != null) {
                SqlAggFunction operator = (SqlAggFunction) toOp(opMap);
                RelDataType type = toType(typeFactory, jsonType);
                List<RexNode> partitionKeys = new ArrayList<>();
                if (window.containsKey("partition"))
                    partitionKeys = toRexList(relInput, (List) window.get("partition"));
                List<RexFieldCollation> orderKeys = new ArrayList<>();
                if (window.containsKey("order"))
                    orderKeys = toRexFieldCollationList(relInput, (List) window.get("order"));
                RexWindowBound lowerBound;
                RexWindowBound upperBound;
                boolean physical;
                if (window.get("rows-lower") != null) {
                    lowerBound = toRexWindowBound(relInput, (Map) window.get("rows-lower"));
                    upperBound = toRexWindowBound(relInput, (Map) window.get("rows-upper"));
                    physical = true;
                } else if (window.get("range-lower") != null) {
                    lowerBound = toRexWindowBound(relInput, (Map) window.get("range-lower"));
                    upperBound = toRexWindowBound(relInput, (Map) window.get("range-upper"));
                    physical = false;
                } else {
                    // No ROWS or RANGE clause
                    lowerBound = null;
                    upperBound = null;
                    physical = false;
                }
                boolean distinct = (Boolean) map.get("distinct");
                return rexBuilder.makeOver(type, operator, rexOperands, partitionKeys, ImmutableList.copyOf(orderKeys), lowerBound, upperBound, physical, true, false, distinct, false);
            } else {
                SqlOperator operator = toOp(opMap);
                RelDataType type;
                if (jsonType != null)
                    type = toType(typeFactory, jsonType);
                else
                    type = rexBuilder.deriveReturnType(operator, rexOperands);
                return rexBuilder.makeCall(type, operator, rexOperands);
            }
        }
        Integer input = (Integer) map.get("input");
        if (input != null) {
            // Check if it is a local ref.
            if (map.containsKey("type")) {
                RelDataType type = toType(typeFactory, map.get("type"));
                return map.get("dynamic") == Boolean.TRUE ? rexBuilder.makeDynamicParam(type, input) : rexBuilder.makeLocalRef(type, input);
            }
            List<RelNode> inputNodes = relInput.getInputs();
            int i = input;
            for (RelNode inputNode : inputNodes) {
                RelDataType rowType = inputNode.getRowType();
                if (i < rowType.getFieldCount()) {
                    RelDataTypeField field = rowType.getFieldList().get(i);
                    return rexBuilder.makeInputRef(field.getType(), input);
                }
                i -= rowType.getFieldCount();
            }
            throw new RuntimeException("input field " + input + " is out of range");
        }
        String field = (String) map.get("field");
        if (field != null) {
            Object jsonExpr = map.get("expr");
            RexNode expr = toRex(relInput, jsonExpr);
            return rexBuilder.makeFieldAccess(expr, field, true);
        }
        String correl = (String) map.get("correl");
        if (correl != null) {
            RelDataType type = toType(typeFactory, map.get("type"));
            return rexBuilder.makeCorrel(type, new CorrelationId(correl));
        }
        if (map.containsKey("literal")) {
            Object literal = map.get("literal");
            RelDataType type = toType(typeFactory, map.get("type"));
            if (literal == null)
                return rexBuilder.makeNullLiteral(type);
            if (type.getSqlTypeName() == SqlTypeName.SYMBOL)
                literal = toEnum(literal);
            else if (type.getSqlTypeName().getFamily() == SqlTypeFamily.BINARY)
                literal = toByteString(literal);
            return rexBuilder.makeLiteral(literal, type, false);
        }
        throw new UnsupportedOperationException("cannot convert to rex " + o);
    } else if (o instanceof Boolean)
        return rexBuilder.makeLiteral((Boolean) o);
    else if (o instanceof String)
        return rexBuilder.makeLiteral((String) o);
    else if (o instanceof Number) {
        Number number = (Number) o;
        if (number instanceof Double || number instanceof Float)
            return rexBuilder.makeApproxLiteral(BigDecimal.valueOf(number.doubleValue()));
        else
            return rexBuilder.makeExactLiteral(BigDecimal.valueOf(number.longValue()));
    } else
        throw new UnsupportedOperationException("cannot convert to rex " + o);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlOperator(org.apache.calcite.sql.SqlOperator) IgniteTypeFactory(org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) ByteString(org.apache.calcite.avatica.util.ByteString) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexWindowBound(org.apache.calcite.rex.RexWindowBound) RexBuilder(org.apache.calcite.rex.RexBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CorrelationId(org.apache.calcite.rel.core.CorrelationId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with IgniteTypeFactory

use of org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory in project ignite by apache.

the class LogicalRelImplementor method visit.

/**
 * {@inheritDoc}
 */
@Override
public Node<Row> visit(IgniteTableScan rel) {
    RexNode condition = rel.condition();
    List<RexNode> projects = rel.projects();
    ImmutableBitSet requiredColunms = rel.requiredColumns();
    IgniteTable tbl = rel.getTable().unwrap(IgniteTable.class);
    IgniteTypeFactory typeFactory = ctx.getTypeFactory();
    RelDataType rowType = tbl.getRowType(typeFactory, requiredColunms);
    Predicate<Row> filters = condition == null ? null : expressionFactory.predicate(condition, rowType);
    Function<Row, Row> prj = projects == null ? null : expressionFactory.project(projects, rowType);
    ColocationGroup group = ctx.group(rel.sourceId());
    Iterable<Row> rowsIter = tbl.scan(ctx, group, filters, prj, requiredColunms);
    return new ScanNode<>(ctx, rowType, rowsIter);
}
Also used : ColocationGroup(org.apache.ignite.internal.processors.query.calcite.metadata.ColocationGroup) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) IgniteTable(org.apache.ignite.internal.processors.query.calcite.schema.IgniteTable) IgniteTypeFactory(org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) ScanNode(org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanNode) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with IgniteTypeFactory

use of org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory in project ignite by apache.

the class AccumulatorsFactory method cast0.

/**
 */
private static Function<Object, Object> cast0(Pair<RelDataType, RelDataType> types) {
    IgniteTypeFactory typeFactory = Commons.typeFactory();
    RelDataType from = types.left;
    RelDataType to = types.right;
    Class<?> fromType = Primitives.wrap((Class<?>) typeFactory.getJavaClass(from));
    Class<?> toType = Primitives.wrap((Class<?>) typeFactory.getJavaClass(to));
    if (toType.isAssignableFrom(fromType))
        return Function.identity();
    if (Void.class == toType)
        return o -> null;
    return compileCast(typeFactory, from, to);
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 4 with IgniteTypeFactory

use of org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory in project ignite 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 : Arrays(java.util.Arrays) QueryUtils(org.apache.ignite.internal.processors.query.QueryUtils) JoinConditionType(org.apache.calcite.sql.JoinConditionType) SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) IgniteTable(org.apache.ignite.internal.processors.query.calcite.schema.IgniteTable) IgniteResource(org.apache.ignite.internal.processors.query.calcite.util.IgniteResource) RESOURCE(org.apache.calcite.util.Static.RESOURCE) BigDecimal(java.math.BigDecimal) SqlCall(org.apache.calcite.sql.SqlCall) 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) Map(java.util.Map) IgniteTypeFactory(org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlSelect(org.apache.calcite.sql.SqlSelect) EnumSet(java.util.EnumSet) SqlKind(org.apache.calcite.sql.SqlKind) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) SqlMerge(org.apache.calcite.sql.SqlMerge) Set(java.util.Set) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) FamilyOperandTypeChecker(org.apache.calcite.sql.type.FamilyOperandTypeChecker) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlJoin(org.apache.calcite.sql.SqlJoin) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlDelete(org.apache.calcite.sql.SqlDelete) SqlOperandTypeInference(org.apache.calcite.sql.type.SqlOperandTypeInference) RelOptTable(org.apache.calcite.plan.RelOptTable) SelectScope(org.apache.calcite.sql.validate.SelectScope) CacheTableDescriptor(org.apache.ignite.internal.processors.query.calcite.schema.CacheTableDescriptor) IgniteCacheTable(org.apache.ignite.internal.processors.query.calcite.schema.IgniteCacheTable) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) F(org.apache.ignite.internal.util.typedef.F) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Prepare(org.apache.calcite.prepare.Prepare) 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) 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 5 with IgniteTypeFactory

use of org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory in project ignite by apache.

the class PrepareServiceImpl method explainFieldsMetadata.

/**
 */
private FieldsMetadata explainFieldsMetadata(PlanningContext ctx) {
    IgniteTypeFactory factory = ctx.typeFactory();
    RelDataType planStrDataType = factory.createSqlType(SqlTypeName.VARCHAR, PRECISION_NOT_SPECIFIED);
    T2<String, RelDataType> planField = new T2<>(ExplainPlan.PLAN_COL_NAME, planStrDataType);
    RelDataType planDataType = factory.createStructType(singletonList(planField));
    return queryFieldsMetadata(ctx, planDataType, null);
}
Also used : IgniteTypeFactory(org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

IgniteTypeFactory (org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory)88 Test (org.junit.Test)62 RelDataType (org.apache.calcite.rel.type.RelDataType)55 IgniteSchema (org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema)30 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)27 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)25 IgniteRel (org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)20 List (java.util.List)15 ArrayList (java.util.ArrayList)14 RexNode (org.apache.calcite.rex.RexNode)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 UUID (java.util.UUID)11 MappingQueryContext (org.apache.ignite.internal.processors.query.calcite.prepare.MappingQueryContext)11 F (org.apache.ignite.internal.util.typedef.F)11 IgniteDistribution (org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution)10 Before (org.junit.Before)10 Arrays (java.util.Arrays)9 ExecutionContext (org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext)9 TypeUtils (org.apache.ignite.internal.processors.query.calcite.util.TypeUtils)8