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;
}
}
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations