public RelDataType resolveColumn(String name, SqlNode ctx) { final SqlValidatorNamespace selectNs = validator.getNamespace(select); final RelDataType rowType = selectNs.getRowType(); final RelDataTypeField field = validator.catalogReader.field(rowType, name); if (field != null) { return field.getType(); } final SqlValidatorScope selectScope = validator.getSelectScope(select); return selectScope.resolveColumn(name, ctx); }
// implement RelDataType public RelDataTypeField getField(String fieldName, boolean caseSensitive) { for (RelDataTypeField field : fieldList) { if (Util.match(caseSensitive, field.getName(), fieldName)) { return field; } } // Extra field if (fieldList.size() > 0) { final RelDataTypeField lastField = Iterables.getLast(fieldList); if (lastField.getName().equals("_extra")) { return new RelDataTypeFieldImpl(fieldName, -1, lastField.getType()); } } return null; }
/** * Looks up a field with a given name, returning null if not found. * * @param rowType Row type * @param columnName Field name * @return Field, or null if not found */ public static RelDataTypeField lookupField( boolean caseSensitive, final RelDataType rowType, String columnName) { RelDataTypeField field = rowType.getField(columnName, caseSensitive); if (field != null) { return field; } // If record type is flagged as having "any field you ask for", // return a type. (TODO: Better way to mark accommodating types.) RelDataTypeField extra = RelDataTypeImpl.extra(rowType); if (extra != null) { return new RelDataTypeFieldImpl(columnName, -1, extra.getType()); } return null; }
public RelDataType inferReturnType(SqlOperatorBinding opBinding) { assert opBinding.getOperandCount() == 1; final RelDataType recordType = opBinding.getOperandType(0); boolean isStruct = recordType.isStruct(); int fieldCount = recordType.getFieldCount(); assert isStruct && (fieldCount == 1); RelDataTypeField fieldType = recordType.getFieldList().get(0); assert fieldType != null : "expected a record type with one field: " + recordType; final RelDataType firstColType = fieldType.getType(); return opBinding.getTypeFactory().createTypeWithNullability(firstColType, true); }
/** * 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 RexNode[] generateCastExpressions( RexBuilder rexBuilder, RelDataType lhsRowType, RexNode[] rhsExps) { RelDataTypeField[] lhsFields = lhsRowType.getFields(); final int fieldCount = lhsFields.length; RexNode[] castExps = new RexNode[fieldCount]; assert fieldCount == rhsExps.length; for (int i = 0; i < fieldCount; ++i) { RelDataTypeField lhsField = lhsFields[i]; RelDataType lhsType = lhsField.getType(); RelDataType rhsType = rhsExps[i].getType(); if (lhsType.equals(rhsType)) { castExps[i] = rhsExps[i]; } else { castExps[i] = rexBuilder.makeCast(lhsType, rhsExps[i]); } } return castExps; }