Search in sources :

Example 86 with RelDataTypeField

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;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 87 with RelDataTypeField

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();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 88 with RelDataTypeField

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;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl)

Example 89 with RelDataTypeField

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);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 90 with RelDataTypeField

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;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFamily(org.apache.calcite.rel.type.RelDataTypeFamily)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)383 RelDataType (org.apache.calcite.rel.type.RelDataType)206 RexNode (org.apache.calcite.rex.RexNode)185 ArrayList (java.util.ArrayList)177 RelNode (org.apache.calcite.rel.RelNode)130 RexBuilder (org.apache.calcite.rex.RexBuilder)76 RexInputRef (org.apache.calcite.rex.RexInputRef)72 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)65 Pair (org.apache.calcite.util.Pair)55 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)47 HashMap (java.util.HashMap)39 Map (java.util.Map)35 AggregateCall (org.apache.calcite.rel.core.AggregateCall)35 SqlNode (org.apache.calcite.sql.SqlNode)32 ImmutableList (com.google.common.collect.ImmutableList)31 RelBuilder (org.apache.calcite.tools.RelBuilder)29 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)25 List (java.util.List)23 LinkedHashSet (java.util.LinkedHashSet)22 RelOptUtil (org.apache.calcite.plan.RelOptUtil)22