Search in sources :

Example 6 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class CassandraSchema method getRelDataType.

RelProtoDataType getRelDataType(String columnFamily, boolean view) {
    List<ColumnMetadata> columns;
    if (view) {
        columns = getKeyspace().getMaterializedView(columnFamily).getColumns();
    } else {
        columns = getKeyspace().getTable(columnFamily).getColumns();
    }
    // Temporary type factory, just for the duration of this method. Allowable
    // because we're creating a proto-type, not a type; before being used, the
    // proto-type will be copied into a real type factory.
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    for (ColumnMetadata column : columns) {
        final String columnName = column.getName();
        final DataType type = column.getType();
        // TODO: This mapping of types can be done much better
        SqlTypeName typeName = SqlTypeName.ANY;
        if (type == DataType.uuid() || type == DataType.timeuuid()) {
            // We currently rely on this in CassandraFilter to detect UUID columns.
            // That is, these fixed length literals should be unquoted in CQL.
            typeName = SqlTypeName.CHAR;
        } else if (type == DataType.ascii() || type == DataType.text() || type == DataType.varchar()) {
            typeName = SqlTypeName.VARCHAR;
        } else if (type == DataType.cint() || type == DataType.varint()) {
            typeName = SqlTypeName.INTEGER;
        } else if (type == DataType.bigint()) {
            typeName = SqlTypeName.BIGINT;
        } else if (type == DataType.cdouble() || type == DataType.cfloat() || type == DataType.decimal()) {
            typeName = SqlTypeName.DOUBLE;
        }
        fieldInfo.add(columnName, typeFactory.createSqlType(typeName)).nullable(true);
    }
    return RelDataTypeImpl.proto(fieldInfo.build());
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) DataType(com.datastax.driver.core.DataType)

Example 7 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class JdbcSchema method getRelDataType.

RelProtoDataType getRelDataType(DatabaseMetaData metaData, String catalogName, String schemaName, String tableName) throws SQLException {
    final ResultSet resultSet = metaData.getColumns(catalogName, schemaName, tableName, null);
    // Temporary type factory, just for the duration of this method. Allowable
    // because we're creating a proto-type, not a type; before being used, the
    // proto-type will be copied into a real type factory.
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    while (resultSet.next()) {
        final String columnName = resultSet.getString(4);
        final int dataType = resultSet.getInt(5);
        final String typeString = resultSet.getString(6);
        final int precision;
        final int scale;
        switch(SqlType.valueOf(dataType)) {
            case TIMESTAMP:
            case TIME:
                // SCALE
                precision = resultSet.getInt(9);
                scale = 0;
                break;
            default:
                // SIZE
                precision = resultSet.getInt(7);
                // SCALE
                scale = resultSet.getInt(9);
                break;
        }
        RelDataType sqlType = sqlType(typeFactory, dataType, precision, scale, typeString);
        boolean nullable = resultSet.getInt(11) != DatabaseMetaData.columnNoNulls;
        fieldInfo.add(columnName, sqlType).nullable(nullable);
    }
    resultSet.close();
    return RelDataTypeImpl.proto(fieldInfo.build());
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) ResultSet(java.sql.ResultSet) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 8 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class RelMdColumnUniqueness method areColumnsUnique.

public Boolean areColumnsUnique(Project rel, RelMetadataQuery mq, ImmutableBitSet columns, boolean ignoreNulls) {
    // LogicalProject maps a set of rows to a different set;
    // Without knowledge of the mapping function(whether it
    // preserves uniqueness), it is only safe to derive uniqueness
    // info from the child of a project when the mapping is f(a) => a.
    // 
    // Also need to map the input column set to the corresponding child
    // references
    List<RexNode> projExprs = rel.getProjects();
    ImmutableBitSet.Builder childColumns = ImmutableBitSet.builder();
    for (int bit : columns) {
        RexNode projExpr = projExprs.get(bit);
        if (projExpr instanceof RexInputRef) {
            childColumns.set(((RexInputRef) projExpr).getIndex());
        } else if (projExpr instanceof RexCall && ignoreNulls) {
            // If the expression is a cast such that the types are the same
            // except for the nullability, then if we're ignoring nulls,
            // it doesn't matter whether the underlying column reference
            // is nullable.  Check that the types are the same by making a
            // nullable copy of both types and then comparing them.
            RexCall call = (RexCall) projExpr;
            if (call.getOperator() != SqlStdOperatorTable.CAST) {
                continue;
            }
            RexNode castOperand = call.getOperands().get(0);
            if (!(castOperand instanceof RexInputRef)) {
                continue;
            }
            RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
            RelDataType castType = typeFactory.createTypeWithNullability(projExpr.getType(), true);
            RelDataType origType = typeFactory.createTypeWithNullability(castOperand.getType(), true);
            if (castType.equals(origType)) {
                childColumns.set(((RexInputRef) castOperand).getIndex());
            }
        } else {
            // projection, then skip it.
            continue;
        }
    }
    // If no columns can affect uniqueness, then return unknown
    if (childColumns.cardinality() == 0) {
        return null;
    }
    return mq.areColumnsUnique(rel.getInput(), childColumns.build(), ignoreNulls);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 9 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class Aggregate method deriveRowType.

/**
 * Computes the row type of an {@code Aggregate} before it exists.
 *
 * @param typeFactory Type factory
 * @param inputRowType Input row type
 * @param indicator Whether row type should include indicator fields to
 *                 indicate which grouping set is active; must be true if
 *                 aggregate is not simple
 * @param groupSet Bit set of grouping fields
 * @param groupSets List of all grouping sets; null for just {@code groupSet}
 * @param aggCalls Collection of calls to aggregate functions
 * @return Row type of the aggregate
 */
public static RelDataType deriveRowType(RelDataTypeFactory typeFactory, final RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, final List<AggregateCall> aggCalls) {
    final List<Integer> groupList = groupSet.asList();
    assert groupList.size() == groupSet.cardinality();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    final List<RelDataTypeField> fieldList = inputRowType.getFieldList();
    final Set<String> containedNames = Sets.newHashSet();
    for (int groupKey : groupList) {
        final RelDataTypeField field = fieldList.get(groupKey);
        containedNames.add(field.getName());
        builder.add(field);
        if (groupSets != null && !allContain(groupSets, groupKey)) {
            builder.nullable(true);
        }
    }
    if (indicator) {
        for (int groupKey : groupList) {
            final RelDataType booleanType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), false);
            final String base = "i$" + fieldList.get(groupKey).getName();
            String name = base;
            int i = 0;
            while (containedNames.contains(name)) {
                name = base + "_" + i++;
            }
            containedNames.add(name);
            builder.add(name, booleanType);
        }
    }
    for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
        final String base;
        if (aggCall.e.name != null) {
            base = aggCall.e.name;
        } else {
            base = "$f" + (groupList.size() + aggCall.i);
        }
        String name = base;
        int i = 0;
        while (containedNames.contains(name)) {
            name = base + "_" + i++;
        }
        containedNames.add(name);
        builder.add(name, aggCall.e.type);
    }
    return builder.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 10 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class AggregateCall method create.

/**
 * Creates an AggregateCall, inferring its type if {@code type} is null.
 */
public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, boolean approximate, List<Integer> argList, int filterArg, int groupCount, RelNode input, RelDataType type, String name) {
    if (type == null) {
        final RelDataTypeFactory typeFactory = input.getCluster().getTypeFactory();
        final List<RelDataType> types = SqlTypeUtil.projectTypes(input.getRowType(), argList);
        final Aggregate.AggCallBinding callBinding = new Aggregate.AggCallBinding(typeFactory, aggFunction, types, groupCount, filterArg >= 0);
        type = aggFunction.inferReturnType(callBinding);
    }
    return create(aggFunction, distinct, approximate, argList, filterArg, type, name);
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)128 RelDataType (org.apache.calcite.rel.type.RelDataType)97 RexNode (org.apache.calcite.rex.RexNode)47 RexBuilder (org.apache.calcite.rex.RexBuilder)38 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)30 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 RelNode (org.apache.calcite.rel.RelNode)20 AggregateCall (org.apache.calcite.rel.core.AggregateCall)15 SqlNode (org.apache.calcite.sql.SqlNode)14 SqlTypeFactoryImpl (org.apache.calcite.sql.type.SqlTypeFactoryImpl)14 BigDecimal (java.math.BigDecimal)12 List (java.util.List)10 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)8 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)8 Calendar (java.util.Calendar)7 RexInputRef (org.apache.calcite.rex.RexInputRef)7 Pair (org.apache.calcite.util.Pair)7 TimestampString (org.apache.calcite.util.TimestampString)7