Search in sources :

Example 26 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class CassandraTable method query.

/**
 * Executes a CQL query on the underlying table.
 *
 * @param session Cassandra session
 * @param fields List of fields to project
 * @param predicates A list of predicates which should be used in the query
 * @return Enumerator of results
 */
public Enumerable<Object> query(final Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
    // Build the type of the resulting row based on the provided fields
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    final RelDataType rowType = getRowType(typeFactory);
    Function1<String, Void> addField = new Function1<String, Void>() {

        public Void apply(String fieldName) {
            SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
            fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
            return null;
        }
    };
    if (selectFields.isEmpty()) {
        for (Map.Entry<String, Class> field : fields) {
            addField.apply(field.getKey());
        }
    } else {
        for (Map.Entry<String, String> field : selectFields) {
            addField.apply(field.getKey());
        }
    }
    final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
    // Construct the list of fields to project
    final String selectString;
    if (selectFields.isEmpty()) {
        selectString = "*";
    } else {
        selectString = Util.toString(new Iterable<String>() {

            public Iterator<String> iterator() {
                final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
                return new Iterator<String>() {

                    @Override
                    public boolean hasNext() {
                        return selectIterator.hasNext();
                    }

                    @Override
                    public String next() {
                        Map.Entry<String, String> entry = selectIterator.next();
                        return entry.getKey() + " AS " + entry.getValue();
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        }, "", ", ", "");
    }
    // Combine all predicates conjunctively
    String whereClause = "";
    if (!predicates.isEmpty()) {
        whereClause = " WHERE ";
        whereClause += Util.toString(predicates, "", " AND ", "");
    }
    // Build and issue the query and return an Enumerator over the results
    StringBuilder queryBuilder = new StringBuilder("SELECT ");
    queryBuilder.append(selectString);
    queryBuilder.append(" FROM \"" + columnFamily + "\"");
    queryBuilder.append(whereClause);
    if (!order.isEmpty()) {
        queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
    }
    int limit = offset;
    if (fetch >= 0) {
        limit += fetch;
    }
    if (limit > 0) {
        queryBuilder.append(" LIMIT " + limit);
    }
    queryBuilder.append(" ALLOW FILTERING");
    final String query = queryBuilder.toString();
    return new AbstractEnumerable<Object>() {

        public Enumerator<Object> enumerator() {
            final ResultSet results = session.execute(query);
            // Skip results until we get to the right offset
            int skip = 0;
            Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
            while (skip < offset && enumerator.moveNext()) {
                skip++;
            }
            return enumerator;
        }
    };
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Iterator(java.util.Iterator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) ResultSet(com.datastax.driver.core.ResultSet) Function1(org.apache.calcite.linq4j.function.Function1) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Map(java.util.Map)

Example 27 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project calcite by apache.

the class RexProgramTest method testSimplifyCastLiteral.

@Test
public void testSimplifyCastLiteral() {
    final List<RexLiteral> literals = new ArrayList<>();
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.ONE, typeFactory.createSqlType(SqlTypeName.INTEGER)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(2), typeFactory.createSqlType(SqlTypeName.BIGINT)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(3), typeFactory.createSqlType(SqlTypeName.SMALLINT)));
    literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(4), typeFactory.createSqlType(SqlTypeName.TINYINT)));
    literals.add(rexBuilder.makeExactLiteral(new BigDecimal("1234"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 4, 0)));
    literals.add(rexBuilder.makeExactLiteral(new BigDecimal("123.45"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 5, 2)));
    literals.add(rexBuilder.makeApproxLiteral(new BigDecimal("3.1415"), typeFactory.createSqlType(SqlTypeName.REAL)));
    literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.E), typeFactory.createSqlType(SqlTypeName.FLOAT)));
    literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.PI), typeFactory.createSqlType(SqlTypeName.DOUBLE)));
    literals.add(rexBuilder.makeLiteral(true));
    literals.add(rexBuilder.makeLiteral(false));
    literals.add(rexBuilder.makeLiteral("hello world"));
    literals.add(rexBuilder.makeLiteral("1969-07-20 12:34:56"));
    literals.add(rexBuilder.makeLiteral("1969-07-20"));
    literals.add(rexBuilder.makeLiteral("12:34:45"));
    literals.add((RexLiteral) rexBuilder.makeLiteral(new ByteString(new byte[] { 1, 2, -34, 0, -128 }), typeFactory.createSqlType(SqlTypeName.BINARY, 5), false));
    literals.add(rexBuilder.makeDateLiteral(new DateString(1974, 8, 9)));
    literals.add(rexBuilder.makeTimeLiteral(new TimeString(1, 23, 45), 0));
    literals.add(rexBuilder.makeTimestampLiteral(new TimestampString(1974, 8, 9, 1, 23, 45), 0));
    final Multimap<SqlTypeName, RexLiteral> map = LinkedHashMultimap.create();
    for (RexLiteral literal : literals) {
        map.put(literal.getTypeName(), literal);
    }
    final List<RelDataType> types = new ArrayList<>();
    types.add(typeFactory.createSqlType(SqlTypeName.INTEGER));
    types.add(typeFactory.createSqlType(SqlTypeName.BIGINT));
    types.add(typeFactory.createSqlType(SqlTypeName.SMALLINT));
    types.add(typeFactory.createSqlType(SqlTypeName.TINYINT));
    types.add(typeFactory.createSqlType(SqlTypeName.REAL));
    types.add(typeFactory.createSqlType(SqlTypeName.FLOAT));
    types.add(typeFactory.createSqlType(SqlTypeName.DOUBLE));
    types.add(typeFactory.createSqlType(SqlTypeName.BOOLEAN));
    types.add(typeFactory.createSqlType(SqlTypeName.VARCHAR, 10));
    types.add(typeFactory.createSqlType(SqlTypeName.CHAR, 5));
    types.add(typeFactory.createSqlType(SqlTypeName.VARBINARY, 60));
    types.add(typeFactory.createSqlType(SqlTypeName.BINARY, 3));
    types.add(typeFactory.createSqlType(SqlTypeName.TIMESTAMP));
    types.add(typeFactory.createSqlType(SqlTypeName.TIME));
    types.add(typeFactory.createSqlType(SqlTypeName.DATE));
    for (RelDataType fromType : types) {
        for (RelDataType toType : types) {
            if (SqlTypeAssignmentRules.instance(false).canCastFrom(toType.getSqlTypeName(), fromType.getSqlTypeName())) {
                for (RexLiteral literal : map.get(fromType.getSqlTypeName())) {
                    final RexNode cast = rexBuilder.makeCast(toType, literal);
                    if (cast instanceof RexLiteral) {
                        assertThat(cast.getType(), is(toType));
                        // makeCast already simplified
                        continue;
                    }
                    final RexNode simplified = simplify.simplify(cast);
                    boolean expectedSimplify = literal.getTypeName() != toType.getSqlTypeName() || (literal.getTypeName() == SqlTypeName.CHAR && ((NlsString) literal.getValue()).getValue().length() > toType.getPrecision()) || (literal.getTypeName() == SqlTypeName.BINARY && ((ByteString) literal.getValue()).length() > toType.getPrecision());
                    boolean couldSimplify = !cast.equals(simplified);
                    final String reason = (expectedSimplify ? "expected to simplify, but could not: " : "simplified, but did not expect to: ") + cast + " --> " + simplified;
                    assertThat(reason, couldSimplify, is(expectedSimplify));
                }
            }
        }
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) TimeString(org.apache.calcite.util.TimeString) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ByteString(org.apache.calcite.avatica.util.ByteString) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) DateString(org.apache.calcite.util.DateString) TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) TimeString(org.apache.calcite.util.TimeString) NlsString(org.apache.calcite.util.NlsString) BigDecimal(java.math.BigDecimal) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 28 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.

the class FindLimit0Visitor method getDirectScanRelIfFullySchemaed.

/**
 * If all field types of the given node are {@link #TYPES recognized types} and honored by execution, then this
 * method returns the tree: DrillDirectScanRel(field types). Otherwise, the method returns null.
 *
 * @param rel calcite logical rel tree
 * @return drill logical rel tree
 */
public static DrillRel getDirectScanRelIfFullySchemaed(RelNode rel) {
    final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
    final List<TypeProtos.MajorType> columnTypes = Lists.newArrayList();
    for (final RelDataTypeField field : fieldList) {
        final SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
        if (!TYPES.contains(sqlTypeName)) {
            return null;
        } else {
            final TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().setMode(field.getType().isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED).setMinorType(TypeInferenceUtils.getDrillTypeFromCalciteType(sqlTypeName));
            if (TypeInferenceUtils.isScalarStringType(sqlTypeName)) {
                builder.setPrecision(field.getType().getPrecision());
            }
            columnTypes.add(builder.build());
        }
    }
    final RelTraitSet traits = rel.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
    final RelDataTypeReader reader = new RelDataTypeReader(rel.getRowType().getFieldNames(), columnTypes);
    return new DrillDirectScanRel(rel.getCluster(), traits, new DirectGroupScan(reader, ScanStats.ZERO_RECORD_TABLE), rel.getRowType());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DirectGroupScan(org.apache.drill.exec.store.direct.DirectGroupScan) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillDirectScanRel(org.apache.drill.exec.planner.logical.DrillDirectScanRel)

Example 29 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.

the class RelDataTypeHolder method getField.

public RelDataTypeField getField(RelDataTypeFactory typeFactory, String fieldName) {
    /* First check if this field name exists in our field list */
    for (RelDataTypeField f : fields) {
        if (fieldName.equalsIgnoreCase(f.getName())) {
            return f;
        }
    }
    /* This field does not exist in our field list add it */
    final SqlTypeName typeName = DynamicRecordType.isDynamicStarColName(fieldName) ? SqlTypeName.DYNAMIC_STAR : SqlTypeName.ANY;
    // This field does not exist in our field list add it
    RelDataTypeField newField = new RelDataTypeFieldImpl(fieldName, fields.size(), typeFactory.createTypeWithNullability(typeFactory.createSqlType(typeName), true));
    /* Add the name to our list of field names */
    fields.add(newField);
    return newField;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl)

Example 30 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by axbaretto.

the class RecordDataType method getRowType.

/**
 * This method constructs a {@link org.apache.calcite.rel.type.RelDataType} based on the
 * {@link org.apache.drill.exec.store.RecordDataType}'s field sql types and field names.
 *
 * @param factory helps construct a {@link org.apache.calcite.rel.type.RelDataType}
 * @return the constructed type
 */
public final RelDataType getRowType(RelDataTypeFactory factory) {
    final List<SqlTypeName> types = getFieldSqlTypeNames();
    final List<String> names = getFieldNames();
    final List<RelDataType> fields = Lists.newArrayList();
    for (final SqlTypeName typeName : types) {
        switch(typeName) {
            case VARCHAR:
                fields.add(factory.createSqlType(typeName, Integer.MAX_VALUE));
                break;
            default:
                fields.add(factory.createSqlType(typeName));
        }
    }
    return factory.createStructType(fields, names);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)186 RelDataType (org.apache.calcite.rel.type.RelDataType)63 Test (org.junit.jupiter.api.Test)39 List (java.util.List)31 BigDecimal (java.math.BigDecimal)30 ArrayList (java.util.ArrayList)30 ImmutableList (com.google.common.collect.ImmutableList)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 RexNode (org.apache.calcite.rex.RexNode)26 Map (java.util.Map)25 NlsString (org.apache.calcite.util.NlsString)21 DateString (org.apache.calcite.util.DateString)18 TimeString (org.apache.calcite.util.TimeString)18 TimestampString (org.apache.calcite.util.TimestampString)18 ByteString (org.apache.calcite.avatica.util.ByteString)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)16 SqlKind (org.apache.calcite.sql.SqlKind)16 Calendar (java.util.Calendar)14 Charset (java.nio.charset.Charset)13 Objects (java.util.Objects)13