use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class CalcitePrepareImpl method getColumnMetaDataList.
private List<ColumnMetaData> getColumnMetaDataList(JavaTypeFactory typeFactory, RelDataType x, RelDataType jdbcType, List<List<String>> originList) {
final List<ColumnMetaData> columns = new ArrayList<>();
for (Ord<RelDataTypeField> pair : Ord.zip(jdbcType.getFieldList())) {
final RelDataTypeField field = pair.e;
final RelDataType type = field.getType();
final RelDataType fieldType = x.isStruct() ? x.getFieldList().get(pair.i).getType() : type;
columns.add(metaData(typeFactory, columns.size(), field.getName(), type, fieldType, originList.get(pair.i)));
}
return columns;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlTypeUtil method sameNamedType.
/**
* Tests whether two types have the same name and structure, possibly with
* differing modifiers. For example, VARCHAR(1) and VARCHAR(10) are
* considered the same, while VARCHAR(1) and CHAR(1) are considered
* different. Likewise, VARCHAR(1) MULTISET and VARCHAR(10) MULTISET are
* considered the same.
*
* @return true if types have same name and structure
*/
public static boolean sameNamedType(RelDataType t1, RelDataType t2) {
if (t1.isStruct() || t2.isStruct()) {
if (!t1.isStruct() || !t2.isStruct()) {
return false;
}
if (t1.getFieldCount() != t2.getFieldCount()) {
return false;
}
List<RelDataTypeField> fields1 = t1.getFieldList();
List<RelDataTypeField> fields2 = t2.getFieldList();
for (int i = 0; i < fields1.size(); ++i) {
if (!sameNamedType(fields1.get(i).getType(), fields2.get(i).getType())) {
return false;
}
}
return true;
}
RelDataType comp1 = t1.getComponentType();
RelDataType comp2 = t2.getComponentType();
if ((comp1 != null) || (comp2 != null)) {
if ((comp1 == null) || (comp2 == null)) {
return false;
}
if (!sameNamedType(comp1, comp2)) {
return false;
}
}
return t1.getSqlTypeName() == t2.getSqlTypeName();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlTypeUtil method flattenFields.
private static boolean flattenFields(RelDataTypeFactory typeFactory, RelDataType type, List<RelDataTypeField> list, int[] flatteningMap) {
boolean nested = false;
if (needsNullIndicator(type)) {
// NOTE jvs 9-Mar-2005: other code
// (e.g. RelStructuredTypeFlattener) relies on the
// null indicator field coming first.
RelDataType indicatorType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
if (type.isNullable()) {
indicatorType = typeFactory.createTypeWithNullability(indicatorType, true);
}
RelDataTypeField nullIndicatorField = new RelDataTypeFieldImpl("NULL_VALUE", 0, indicatorType);
list.add(nullIndicatorField);
nested = true;
}
for (RelDataTypeField field : type.getFieldList()) {
if (flatteningMap != null) {
flatteningMap[field.getIndex()] = list.size();
}
if (field.getType().isStruct()) {
nested = true;
flattenFields(typeFactory, field.getType(), list, null);
} else if (field.getType().getComponentType() != null) {
nested = true;
// TODO jvs 14-Feb-2005: generalize to any kind of
// collection type
RelDataType flattenedCollectionType = typeFactory.createMultisetType(flattenRecordType(typeFactory, field.getType().getComponentType(), null), -1);
field = new RelDataTypeFieldImpl(field.getName(), field.getIndex(), flattenedCollectionType);
list.add(field);
} else {
list.add(field);
}
}
return nested;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlTypeUtil method flattenRecordType.
/**
* Flattens a record type by recursively expanding any fields which are
* themselves record types. For each record type, a representative null
* value field is also prepended (with state NULL for a null value and FALSE
* for non-null), and all component types are asserted to be nullable, since
* SQL doesn't allow NOT NULL to be specified on attributes.
*
* @param typeFactory factory which should produced flattened type
* @param recordType type with possible nesting
* @param flatteningMap if non-null, receives map from unflattened ordinal
* to flattened ordinal (must have length at least
* recordType.getFieldList().size())
* @return flattened equivalent
*/
public static RelDataType flattenRecordType(RelDataTypeFactory typeFactory, RelDataType recordType, int[] flatteningMap) {
if (!recordType.isStruct()) {
return recordType;
}
List<RelDataTypeField> fieldList = new ArrayList<RelDataTypeField>();
boolean nested = flattenFields(typeFactory, recordType, fieldList, flatteningMap);
if (!nested) {
return recordType;
}
List<RelDataType> types = new ArrayList<RelDataType>();
List<String> fieldNames = new ArrayList<String>();
int i = -1;
for (RelDataTypeField field : fieldList) {
++i;
types.add(field.getType());
fieldNames.add(field.getName() + "_" + i);
}
return typeFactory.createStructType(types, fieldNames);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.
the class SqlTypeUtil method isComparable.
/**
* Returns whether two types are comparable. They need to be scalar types of
* the same family, or struct types whose fields are pairwise comparable.
*
* @param type1 First type
* @param type2 Second type
* @return Whether types are comparable
*/
public static boolean isComparable(RelDataType type1, RelDataType type2) {
if (type1.isStruct() != type2.isStruct()) {
return false;
}
if (type1.isStruct()) {
int n = type1.getFieldCount();
if (n != type2.getFieldCount()) {
return false;
}
for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
if (!isComparable(pair.left.getType(), pair.right.getType())) {
return false;
}
}
return true;
}
RelDataTypeFamily family1 = null;
RelDataTypeFamily family2 = null;
// the Saffron type system happy.
if (type1.getSqlTypeName() != null) {
family1 = type1.getSqlTypeName().getFamily();
}
if (type2.getSqlTypeName() != null) {
family2 = type2.getSqlTypeName().getFamily();
}
if (family1 == null) {
family1 = type1.getFamily();
}
if (family2 == null) {
family2 = type2.getFamily();
}
if (family1 == family2) {
return true;
}
// If one of the arguments is of type 'ANY', return true.
if (family1 == SqlTypeFamily.ANY || family2 == SqlTypeFamily.ANY) {
return true;
}
// If one of the arguments is of type 'NULL', return true.
if (family1 == SqlTypeFamily.NULL || family2 == SqlTypeFamily.NULL) {
return true;
}
// We can implicitly convert from character to date
if (family1 == SqlTypeFamily.CHARACTER && canConvertStringInCompare(family2) || family2 == SqlTypeFamily.CHARACTER && canConvertStringInCompare(family1)) {
return true;
}
return false;
}
Aggregations