Search in sources :

Example 36 with SqlTypeName

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

the class RelDataTypeHolder method getFieldOrInsert.

/**
 * Get field if exists, otherwise inserts a new field. The new field by default will have "any"
 * type, except for the dynamic star field.
 *
 * @param fieldName Request field name
 * @param caseSensitive Case Sensitive
 * @return A pair of RelDataTypeField and Boolean. Boolean indicates whether a new field is added
 * to this holder.
 */
Pair<RelDataTypeField, Boolean> getFieldOrInsert(String fieldName, boolean caseSensitive) {
    // First check if this field name exists in our field list
    for (RelDataTypeField f : fields) {
        if (Util.matches(caseSensitive, f.getName(), fieldName)) {
            return Pair.of(f, false);
        }
    }
    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 Pair.of(newField, true);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 37 with SqlTypeName

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

the class DruidQuery method isValidLeafCast.

/**
 * @param rexNode rexNode
 *
 * @return true if the operand is an inputRef and it is a valid Druid Cast operation
 */
private static boolean isValidLeafCast(RexNode rexNode) {
    assert rexNode.isA(SqlKind.CAST);
    final RexNode input = ((RexCall) rexNode).getOperands().get(0);
    if (!input.isA(SqlKind.INPUT_REF)) {
        // it is not a leaf cast don't bother going further.
        return false;
    }
    final SqlTypeName toTypeName = rexNode.getType().getSqlTypeName();
    if (toTypeName.getFamily() == SqlTypeFamily.CHARACTER) {
        // CAST of input to character type
        return true;
    }
    if (toTypeName.getFamily() == SqlTypeFamily.NUMERIC) {
        // CAST of input to numeric type, it is part of a bounded comparison
        return true;
    }
    if (toTypeName.getFamily() == SqlTypeFamily.TIMESTAMP || toTypeName.getFamily() == SqlTypeFamily.DATETIME) {
        // CAST of literal to timestamp type
        return true;
    }
    if (toTypeName.getFamily().contains(input.getType())) {
        // same type it is okay to push it
        return true;
    }
    // Currently other CAST operations cannot be pushed to Druid
    return false;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexNode(org.apache.calcite.rex.RexNode)

Example 38 with SqlTypeName

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

the class DruidQuery method getJsonAggregation.

@Nullable
private static JsonAggregation getJsonAggregation(String name, AggregateCall aggCall, RexNode filterNode, String fieldName, String aggExpression, DruidQuery druidQuery) {
    final boolean fractional;
    final RelDataType type = aggCall.getType();
    final SqlTypeName sqlTypeName = type.getSqlTypeName();
    final JsonAggregation aggregation;
    final CalciteConnectionConfig config = druidQuery.getConnectionConfig();
    if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(sqlTypeName)) {
        fractional = true;
    } else if (SqlTypeFamily.INTEGER.getTypeNames().contains(sqlTypeName)) {
        fractional = false;
    } else if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(sqlTypeName)) {
        // Decimal
        assert sqlTypeName == SqlTypeName.DECIMAL;
        if (type.getScale() == 0) {
            fractional = false;
        } else {
            fractional = true;
        }
    } else {
        // Cannot handle this aggregate function type
        return null;
    }
    // Convert from a complex metric
    ComplexMetric complexMetric = druidQuery.druidTable.resolveComplexMetric(fieldName, aggCall);
    switch(aggCall.getAggregation().getKind()) {
        case COUNT:
            if (aggCall.isDistinct()) {
                if (aggCall.isApproximate() || config.approximateDistinctCount()) {
                    if (complexMetric == null) {
                        aggregation = new JsonCardinalityAggregation("cardinality", name, ImmutableList.of(fieldName));
                    } else {
                        aggregation = new JsonAggregation(complexMetric.getMetricType(), name, complexMetric.getMetricName(), null);
                    }
                    break;
                } else {
                    // when approximate results were not told be acceptable.
                    return null;
                }
            }
            if (aggCall.getArgList().size() == 1 && !aggCall.isDistinct()) {
                // case we have count(column) push it as count(*) where column is not null
                final DruidJsonFilter matchNulls;
                if (fieldName == null) {
                    matchNulls = new DruidJsonFilter.JsonExpressionFilter(aggExpression + " == null");
                } else {
                    matchNulls = DruidJsonFilter.getSelectorFilter(fieldName, null, null);
                }
                aggregation = new JsonFilteredAggregation(DruidJsonFilter.toNotDruidFilter(matchNulls), new JsonAggregation("count", name, fieldName, aggExpression));
            } else if (!aggCall.isDistinct()) {
                aggregation = new JsonAggregation("count", name, fieldName, aggExpression);
            } else {
                aggregation = null;
            }
            break;
        case SUM:
        case SUM0:
            aggregation = new JsonAggregation(fractional ? "doubleSum" : "longSum", name, fieldName, aggExpression);
            break;
        case MIN:
            aggregation = new JsonAggregation(fractional ? "doubleMin" : "longMin", name, fieldName, aggExpression);
            break;
        case MAX:
            aggregation = new JsonAggregation(fractional ? "doubleMax" : "longMax", name, fieldName, aggExpression);
            break;
        default:
            return null;
    }
    if (aggregation == null) {
        return null;
    }
    // translate filters
    if (filterNode != null) {
        DruidJsonFilter druidFilter = DruidJsonFilter.toDruidFilters(filterNode, druidQuery.table.getRowType(), druidQuery);
        if (druidFilter == null) {
            // can not translate filter
            return null;
        }
        return new JsonFilteredAggregation(druidFilter, aggregation);
    }
    return aggregation;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) RelDataType(org.apache.calcite.rel.type.RelDataType) Nullable(javax.annotation.Nullable)

Example 39 with SqlTypeName

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

the class DruidSqlCastConverter method toDruidExpression.

@Override
public String toDruidExpression(RexNode rexNode, RelDataType topRel, DruidQuery druidQuery) {
    final RexNode operand = ((RexCall) rexNode).getOperands().get(0);
    final String operandExpression = DruidExpressions.toDruidExpression(operand, topRel, druidQuery);
    if (operandExpression == null) {
        return null;
    }
    final SqlTypeName fromType = operand.getType().getSqlTypeName();
    final SqlTypeName toType = rexNode.getType().getSqlTypeName();
    final String timeZoneConf = druidQuery.getConnectionConfig().timeZone();
    final TimeZone timeZone = TimeZone.getTimeZone(timeZoneConf == null ? "UTC" : timeZoneConf);
    if (SqlTypeName.CHAR_TYPES.contains(fromType) && SqlTypeName.DATETIME_TYPES.contains(toType)) {
        // case chars to dates
        return castCharToDateTime(timeZone, operandExpression, toType);
    } else if (SqlTypeName.DATETIME_TYPES.contains(fromType) && SqlTypeName.CHAR_TYPES.contains(toType)) {
        // case dates to chars
        return castDateTimeToChar(timeZone, operandExpression, fromType);
    } else {
        // Handle other casts.
        final DruidType fromExprType = DruidExpressions.EXPRESSION_TYPES.get(fromType);
        final DruidType toExprType = DruidExpressions.EXPRESSION_TYPES.get(toType);
        if (fromExprType == null || toExprType == null) {
            // Unknown types bail out.
            return null;
        }
        final String typeCastExpression;
        if (fromExprType != toExprType) {
            typeCastExpression = DruidQuery.format("CAST(%s, '%s')", operandExpression, toExprType.toString());
        } else {
            // case it is the same type it is ok to skip CAST
            typeCastExpression = operandExpression;
        }
        if (toType == SqlTypeName.DATE) {
            // Floor to day when casting to DATE.
            return DruidExpressions.applyTimestampFloor(typeCastExpression, Period.days(1).toString(), "", TimeZone.getTimeZone(druidQuery.getConnectionConfig().timeZone()));
        } else {
            return typeCastExpression;
        }
    }
}
Also used : TimeZone(java.util.TimeZone) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexNode(org.apache.calcite.rex.RexNode)

Example 40 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.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)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)116 Test (org.junit.Test)38 RelDataType (org.apache.calcite.rel.type.RelDataType)28 RexNode (org.apache.calcite.rex.RexNode)18 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)12 List (java.util.List)11 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)8 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)8 ImmutableList (com.google.common.collect.ImmutableList)7 Map (java.util.Map)7 DateString (org.apache.calcite.util.DateString)7 TimeString (org.apache.calcite.util.TimeString)7 TimestampString (org.apache.calcite.util.TimestampString)7 ISE (io.druid.java.util.common.ISE)6 SqlKind (org.apache.calcite.sql.SqlKind)6 NlsString (org.apache.calcite.util.NlsString)6 Calendar (java.util.Calendar)5 Nullable (javax.annotation.Nullable)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5