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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations