Search in sources :

Example 86 with SqlTypeName

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

the class CastOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexNode operand = ((RexCall) rexNode).getOperands().get(0);
    final DruidExpression operandExpression = Expressions.toDruidExpression(plannerContext, rowSignature, operand);
    if (operandExpression == null) {
        return null;
    }
    final SqlTypeName fromType = operand.getType().getSqlTypeName();
    final SqlTypeName toType = rexNode.getType().getSqlTypeName();
    if (SqlTypeName.CHAR_TYPES.contains(fromType) && SqlTypeName.DATETIME_TYPES.contains(toType)) {
        return castCharToDateTime(plannerContext, operandExpression, toType, Calcites.getColumnTypeForRelDataType(rexNode.getType()));
    } else if (SqlTypeName.DATETIME_TYPES.contains(fromType) && SqlTypeName.CHAR_TYPES.contains(toType)) {
        return castDateTimeToChar(plannerContext, operandExpression, fromType, Calcites.getColumnTypeForRelDataType(rexNode.getType()));
    } else {
        // Handle other casts.
        final ExprType fromExprType = EXPRESSION_TYPES.get(fromType);
        final ExprType toExprType = EXPRESSION_TYPES.get(toType);
        if (fromExprType == null || toExprType == null) {
            // We have no runtime type for these SQL types.
            return null;
        }
        final DruidExpression typeCastExpression;
        if (fromExprType != toExprType) {
            // Ignore casts for simple extractions (use Function.identity) since it is ok in many cases.
            typeCastExpression = operandExpression.map(Function.identity(), expression -> StringUtils.format("CAST(%s, '%s')", expression, toExprType.toString()));
        } else {
            typeCastExpression = operandExpression;
        }
        if (toType == SqlTypeName.DATE) {
            // Floor to day when casting to DATE.
            return TimeFloorOperatorConversion.applyTimestampFloor(typeCastExpression, new PeriodGranularity(Period.days(1), null, plannerContext.getTimeZone()), plannerContext.getExprMacroTable());
        } else {
            return typeCastExpression;
        }
    }
}
Also used : DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ExprType(org.apache.druid.math.expr.ExprType) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) RexNode(org.apache.calcite.rex.RexNode)

Example 87 with SqlTypeName

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

the class RelParameterizerShuttle method bind.

private RexNode bind(RexNode node, RexBuilder builder, RelDataTypeFactory typeFactory) {
    if (node instanceof RexDynamicParam) {
        RexDynamicParam dynamicParam = (RexDynamicParam) node;
        // if we have a value for dynamic parameter, replace with a literal, else add to list of unbound parameters
        if (plannerContext.getParameters().size() > dynamicParam.getIndex()) {
            TypedValue param = plannerContext.getParameters().get(dynamicParam.getIndex());
            if (param == null) {
                throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
            }
            if (param.value == null) {
                return builder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.NULL));
            }
            SqlTypeName typeName = SqlTypeName.getNameForJdbcType(param.type.typeId);
            return builder.makeLiteral(param.value, typeFactory.createSqlType(typeName), true);
        } else {
            throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
        }
    }
    return node;
}
Also used : SqlPlanningException(org.apache.druid.sql.SqlPlanningException) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 88 with SqlTypeName

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

the class SqlParameterizerShuttle method visit.

@Override
public SqlNode visit(SqlDynamicParam param) {
    try {
        if (plannerContext.getParameters().size() > param.getIndex()) {
            TypedValue paramBinding = plannerContext.getParameters().get(param.getIndex());
            if (paramBinding == null) {
                throw new IAE("Parameter at position[%s] is not bound", param.getIndex());
            }
            if (paramBinding.value == null) {
                return SqlLiteral.createNull(param.getParserPosition());
            }
            SqlTypeName typeName = SqlTypeName.getNameForJdbcType(paramBinding.type.typeId);
            if (SqlTypeName.APPROX_TYPES.contains(typeName)) {
                return SqlLiteral.createApproxNumeric(paramBinding.value.toString(), param.getParserPosition());
            }
            if (SqlTypeName.TIMESTAMP.equals(typeName) && paramBinding.value instanceof Long) {
                return SqlLiteral.createTimestamp(TimestampString.fromMillisSinceEpoch((Long) paramBinding.value), 0, param.getParserPosition());
            }
            return typeName.createLiteral(paramBinding.value, param.getParserPosition());
        } else {
            throw new IAE("Parameter at position[%s] is not bound", param.getIndex());
        }
    } catch (ClassCastException ignored) {
    // suppress
    }
    return param;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) IAE(org.apache.druid.java.util.common.IAE) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 89 with SqlTypeName

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

the class OLAPProjectRel method buildColumnRowType.

ColumnRowType buildColumnRowType() {
    List<TblColRef> columns = Lists.newArrayList();
    List<TupleExpression> sourceColumns = Lists.newArrayList();
    OLAPRel olapChild = (OLAPRel) getInput();
    ColumnRowType inputColumnRowType = olapChild.getColumnRowType();
    boolean ifVerify = !hasSubQuery() && !afterAggregate;
    TupleExpressionVisitor visitor = new TupleExpressionVisitor(inputColumnRowType, ifVerify);
    for (int i = 0; i < this.rewriteProjects.size(); i++) {
        RexNode rex = this.rewriteProjects.get(i);
        RelDataTypeField columnField = this.rowType.getFieldList().get(i);
        String fieldName = columnField.getName();
        TupleExpression tupleExpr = rex.accept(visitor);
        TblColRef column = translateRexNode(rex, inputColumnRowType, tupleExpr, fieldName);
        if (!this.rewriting && !this.afterAggregate && !isMerelyPermutation) {
            Set<TblColRef> srcCols = ExpressionColCollector.collectColumns(tupleExpr);
            // remove cols not belonging to context tables
            Iterator<TblColRef> srcColIter = srcCols.iterator();
            while (srcColIter.hasNext()) {
                if (!context.belongToContextTables(srcColIter.next())) {
                    srcColIter.remove();
                }
            }
            this.context.allColumns.addAll(srcCols);
            if (this.context.isDynamicColumnEnabled() && tupleExpr.ifForDynamicColumn()) {
                SqlTypeName fSqlType = columnField.getType().getSqlTypeName();
                String dataType = OLAPTable.DATATYPE_MAPPING.get(fSqlType);
                column.getColumnDesc().setDatatype(dataType);
                this.context.dynamicFields.put(column, columnField.getType());
            }
        } else {
            tupleExpr = new NoneTupleExpression();
        }
        columns.add(column);
        sourceColumns.add(tupleExpr);
    }
    return new ColumnRowType(columns, sourceColumns);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) TupleExpression(org.apache.kylin.metadata.expression.TupleExpression) ConstantTupleExpression(org.apache.kylin.metadata.expression.ConstantTupleExpression) RexCallTupleExpression(org.apache.kylin.metadata.expression.RexCallTupleExpression) NoneTupleExpression(org.apache.kylin.metadata.expression.NoneTupleExpression) ColumnTupleExpression(org.apache.kylin.metadata.expression.ColumnTupleExpression) TupleExpressionVisitor(org.apache.kylin.query.relnode.visitor.TupleExpressionVisitor) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) NoneTupleExpression(org.apache.kylin.metadata.expression.NoneTupleExpression) TblColRef(org.apache.kylin.metadata.model.TblColRef) RexNode(org.apache.calcite.rex.RexNode)

Example 90 with SqlTypeName

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

the class RexLiteral method toLiteral.

/**
 * Converts a value to a temporary literal, for the purposes of generating a digest. Literals of
 * type ROW and MULTISET require that their components are also literals.
 */
private static RexLiteral toLiteral(RelDataType type, Comparable<?> value) {
    final SqlTypeName typeName = strictTypeName(type);
    switch(typeName) {
        case ROW:
            final List<Comparable<?>> fieldValues = (List) value;
            final List<RelDataTypeField> fields = type.getFieldList();
            final List<RexLiteral> fieldLiterals = FlatLists.of(Functions.generate(fieldValues.size(), i -> toLiteral(fields.get(i).getType(), fieldValues.get(i))));
            return new RexLiteral((Comparable) fieldLiterals, type, typeName);
        case MULTISET:
            final List<Comparable<?>> elementValues = (List) value;
            final List<RexLiteral> elementLiterals = FlatLists.of(Functions.generate(elementValues.size(), i -> toLiteral(type.getComponentType(), elementValues.get(i))));
            return new RexLiteral((Comparable) elementLiterals, type, typeName);
        default:
            return new RexLiteral(value, type, typeName);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) TimeUnit(org.apache.calcite.avatica.util.TimeUnit) ByteBuffer(java.nio.ByteBuffer) TimeString(org.apache.calcite.util.TimeString) BigDecimal(java.math.BigDecimal) Calendar(java.util.Calendar) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) DateTimeUtils(org.apache.calcite.avatica.util.DateTimeUtils) Geometries(org.apache.calcite.runtime.Geometries) Locale(java.util.Locale) Map(java.util.Map) SqlOperator(org.apache.calcite.sql.SqlOperator) CalciteSystemProperty(org.apache.calcite.config.CalciteSystemProperty) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) Functions(org.apache.calcite.linq4j.function.Functions) PrintWriter(java.io.PrintWriter) RelDataType(org.apache.calcite.rel.type.RelDataType) Litmus(org.apache.calcite.util.Litmus) Sarg(org.apache.calcite.util.Sarg) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimeZone(java.util.TimeZone) TimestampString(org.apache.calcite.util.TimestampString) ConversionUtil(org.apache.calcite.util.ConversionUtil) RelNode(org.apache.calcite.rel.RelNode) SqlCollation(org.apache.calcite.sql.SqlCollation) CompositeList(org.apache.calcite.util.CompositeList) Objects(java.util.Objects) List(java.util.List) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) FlatLists(org.apache.calcite.runtime.FlatLists) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Preconditions(com.google.common.base.Preconditions) Util(org.apache.calcite.util.Util) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ImmutableList(com.google.common.collect.ImmutableList) CompositeList(org.apache.calcite.util.CompositeList) List(java.util.List)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)184 RelDataType (org.apache.calcite.rel.type.RelDataType)62 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)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 Map (java.util.Map)25 RexNode (org.apache.calcite.rex.RexNode)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)15 Calendar (java.util.Calendar)14 Objects (java.util.Objects)13 Util (org.apache.calcite.util.Util)13