use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class ModifiableViewTable method getNewColumnMapping.
/**
* Creates a mapping from the view index to the index in the underlying table.
*/
private static ImmutableIntList getNewColumnMapping(Table underlying, ImmutableIntList oldColumnMapping, List<RelDataTypeField> extendedColumns, RelDataTypeFactory typeFactory) {
final List<RelDataTypeField> baseColumns = underlying.getRowType(typeFactory).getFieldList();
final Map<String, Integer> nameToIndex = mapNameToIndex(baseColumns);
final ImmutableList.Builder<Integer> newMapping = ImmutableList.builder();
newMapping.addAll(oldColumnMapping);
int newMappedIndex = baseColumns.size();
for (RelDataTypeField extendedColumn : extendedColumns) {
if (nameToIndex.containsKey(extendedColumn.getName())) {
// The extended column duplicates a column in the underlying table.
// Map to the index in the underlying table.
newMapping.add(nameToIndex.get(extendedColumn.getName()));
} else {
// The extended column is not in the underlying table.
newMapping.add(newMappedIndex++);
}
}
return ImmutableIntList.copyOf(newMapping.build());
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method generateCastExpressions.
/**
* Generates a cast for a row type.
*
* @param rexBuilder RexBuilder to use for constructing casts
* @param lhsRowType target row type
* @param rhsExps expressions to be cast
* @return cast expressions
*/
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, List<RexNode> rhsExps) {
List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
List<RexNode> castExps = new ArrayList<>();
for (Pair<RelDataTypeField, RexNode> pair : Pair.zip(lhsFields, rhsExps, true)) {
RelDataTypeField lhsField = pair.left;
RelDataType lhsType = lhsField.getType();
final RexNode rhsExp = pair.right;
RelDataType rhsType = rhsExp.getType();
if (lhsType.equals(rhsType)) {
castExps.add(rhsExp);
} else {
castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
}
}
return castExps;
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method generateCastExpressions.
/**
* Generates a cast from one row type to another
*
* @param rexBuilder RexBuilder to use for constructing casts
* @param lhsRowType target row type
* @param rhsRowType source row type; fields must be 1-to-1 with lhsRowType,
* in same order
* @return cast expressions
*/
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, RelDataType rhsRowType) {
final List<RelDataTypeField> fieldList = rhsRowType.getFieldList();
int n = fieldList.size();
assert n == lhsRowType.getFieldCount() : "field count: lhs [" + lhsRowType + "] rhs [" + rhsRowType + "]";
List<RexNode> rhsExps = new ArrayList<>();
for (RelDataTypeField field : fieldList) {
rhsExps.add(rexBuilder.makeInputRef(field.getType(), field.getIndex()));
}
return generateCastExpressions(rexBuilder, lhsRowType, rhsExps);
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class RexUtil method compatibleTypes.
/**
* Returns whether the type of an array of expressions is compatible with a
* struct type.
*
* @param exprs Array of expressions
* @param type Type
* @param litmus What to do if an error is detected (there is a mismatch)
*
* @return Whether every expression has the same type as the corresponding
* member of the struct type
*
* @see RelOptUtil#eq(String, RelDataType, String, RelDataType, org.apache.calcite.util.Litmus)
*/
public static boolean compatibleTypes(List<RexNode> exprs, RelDataType type, Litmus litmus) {
final List<RelDataTypeField> fields = type.getFieldList();
if (exprs.size() != fields.size()) {
return litmus.fail("rowtype mismatches expressions");
}
for (int i = 0; i < fields.size(); i++) {
final RelDataType exprType = exprs.get(i).getType();
final RelDataType fieldType = fields.get(i).getType();
if (!RelOptUtil.eq("type1", exprType, "type2", fieldType, litmus)) {
return litmus.fail(null);
}
}
return litmus.succeed();
}
use of org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlDotOperator method deriveType.
@Override
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
RelDataType nodeType = validator.deriveType(scope, call.getOperandList().get(0));
assert nodeType != null;
final String fieldName = call.getOperandList().get(1).toString();
RelDataTypeField field = nodeType.getField(fieldName, false, false);
if (field == null) {
throw SqlUtil.newContextException(SqlParserPos.ZERO, Static.RESOURCE.unknownField(fieldName));
}
RelDataType type = field.getType();
// Validate and determine coercibility and resulting collation
// name of binary operator if needed.
type = adjustType(validator, call, type);
SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
return type;
}
Aggregations