use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class JavaTypeFactoryImpl method toSql.
/**
* Converts a type in Java format to a SQL-oriented type.
*/
public static RelDataType toSql(final RelDataTypeFactory typeFactory, RelDataType type) {
if (type instanceof RelRecordType) {
return typeFactory.createTypeWithNullability(typeFactory.createStructType(type.getFieldList().stream().map(field -> toSql(typeFactory, field.getType())).collect(Collectors.toList()), type.getFieldNames()), type.isNullable());
} else if (type instanceof JavaType) {
SqlTypeName sqlTypeName = type.getSqlTypeName();
final RelDataType relDataType;
if (SqlTypeUtil.isArray(type)) {
// Transform to sql type, take care for two cases:
// 1. type.getJavaClass() is collection with erased generic type
// 2. ElementType returned by JavaType is also of JavaType,
// and needs conversion using typeFactory
final RelDataType elementType = toSqlTypeWithNullToAny(typeFactory, type.getComponentType());
relDataType = typeFactory.createArrayType(elementType, -1);
} else if (SqlTypeUtil.isMap(type)) {
final RelDataType keyType = toSqlTypeWithNullToAny(typeFactory, type.getKeyType());
final RelDataType valueType = toSqlTypeWithNullToAny(typeFactory, type.getValueType());
relDataType = typeFactory.createMapType(keyType, valueType);
} else {
relDataType = typeFactory.createSqlType(sqlTypeName);
}
return typeFactory.createTypeWithNullability(relDataType, type.isNullable());
}
return type;
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class CalciteConvertors method parseTypeString.
private static RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
int precision = -1;
int scale = -1;
int open = typeString.indexOf("(");
if (open >= 0) {
int close = typeString.indexOf(")", open);
if (close >= 0) {
String rest = typeString.substring(open + 1, close);
typeString = typeString.substring(0, open);
int comma = rest.indexOf(",");
if (comma >= 0) {
precision = Integer.parseInt(rest.substring(0, comma));
scale = Integer.parseInt(rest.substring(comma));
} else {
precision = Integer.parseInt(rest);
}
}
}
try {
final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
return typeName.allowsPrecScale(true, true) ? typeFactory.createSqlType(typeName, precision, scale) : typeName.allowsPrecScale(true, false) ? typeFactory.createSqlType(typeName, precision) : typeFactory.createSqlType(typeName);
} catch (IllegalArgumentException e) {
return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class CalciteConvertors method getRelDataType.
public static RelDataType getRelDataType(final List<SimpleColumnInfo> columnInfos, final RelDataTypeFactory factory) {
final RelDataTypeFactory.Builder builder = new RelDataTypeFactory.Builder(factory);
for (SimpleColumnInfo columnInfo : columnInfos) {
final JDBCType columnType = columnInfo.getJdbcType();
final RelDataType type;
if (columnType == JDBCType.VARCHAR) {
type = factory.createTypeWithCharsetAndCollation(factory.createSqlType(SqlTypeName.VARCHAR), StandardCharsets.UTF_8, SqlCollation.IMPLICIT);
} else if (columnType == JDBCType.LONGVARBINARY) {
type = factory.createSqlType(SqlTypeName.VARBINARY);
} else {
SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(columnType.getVendorTypeNumber());
if (sqlTypeName == null) {
sqlTypeName = SqlTypeName.VARCHAR;
}
type = factory.createSqlType(sqlTypeName);
}
builder.add(columnInfo.getColumnName(), factory.createTypeWithNullability(type, columnInfo.isNullable()));
}
return builder.build();
}
use of org.apache.calcite.sql.type.SqlTypeName in project druid by apache.
the class EarliestLatestAnySqlAggregator method toDruidAggregation.
@Nullable
@Override
public Aggregation toDruidAggregation(final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final String name, final AggregateCall aggregateCall, final Project project, final List<Aggregation> existingAggregations, final boolean finalizeAggregations) {
final List<RexNode> rexNodes = aggregateCall.getArgList().stream().map(i -> Expressions.fromFieldAccess(rowSignature, project, i)).collect(Collectors.toList());
final List<DruidExpression> args = Expressions.toDruidExpressions(plannerContext, rowSignature, rexNodes);
if (args == null) {
return null;
}
final String aggregatorName = finalizeAggregations ? Calcites.makePrefixedName(name, "a") : name;
final ColumnType outputType = Calcites.getColumnTypeForRelDataType(aggregateCall.getType());
if (outputType == null) {
throw new ISE("Cannot translate output sqlTypeName[%s] to Druid type for aggregator[%s]", aggregateCall.getType().getSqlTypeName(), aggregateCall.getName());
}
final String fieldName = getColumnName(plannerContext, virtualColumnRegistry, args.get(0), rexNodes.get(0));
final AggregatorFactory theAggFactory;
switch(args.size()) {
case 1:
theAggFactory = aggregatorType.createAggregatorFactory(aggregatorName, fieldName, null, outputType, -1);
break;
case 2:
theAggFactory = aggregatorType.createAggregatorFactory(aggregatorName, fieldName, null, outputType, RexLiteral.intValue(rexNodes.get(1)));
break;
default:
throw new IAE("aggregation[%s], Invalid number of arguments[%,d] to [%s] operator", aggregatorName, args.size(), aggregatorType.name());
}
return Aggregation.create(Collections.singletonList(theAggFactory), finalizeAggregations ? new FinalizingFieldAccessPostAggregator(name, aggregatorName) : null);
}
use of org.apache.calcite.sql.type.SqlTypeName in project druid by apache.
the class Expressions method literalToDruidExpression.
@Nullable
private static DruidExpression literalToDruidExpression(final PlannerContext plannerContext, final RexNode rexNode) {
final SqlTypeName sqlTypeName = rexNode.getType().getSqlTypeName();
// Translate literal.
final ColumnType columnType = Calcites.getColumnTypeForRelDataType(rexNode.getType());
if (RexLiteral.isNullLiteral(rexNode)) {
return DruidExpression.ofLiteral(columnType, DruidExpression.nullLiteral());
} else if (SqlTypeName.NUMERIC_TYPES.contains(sqlTypeName)) {
return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral((Number) RexLiteral.value(rexNode)));
} else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
// Calcite represents DAY-TIME intervals in milliseconds.
final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(milliseconds));
} else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
// Calcite represents YEAR-MONTH intervals in months.
final long months = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(months));
} else if (SqlTypeName.STRING_TYPES.contains(sqlTypeName)) {
return DruidExpression.ofStringLiteral(RexLiteral.stringValue(rexNode));
} else if (SqlTypeName.TIMESTAMP == sqlTypeName || SqlTypeName.DATE == sqlTypeName) {
if (RexLiteral.isNullLiteral(rexNode)) {
return DruidExpression.ofLiteral(columnType, DruidExpression.nullLiteral());
} else {
return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(Calcites.calciteDateTimeLiteralToJoda(rexNode, plannerContext.getTimeZone()).getMillis()));
}
} else if (SqlTypeName.BOOLEAN == sqlTypeName) {
return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(RexLiteral.booleanValue(rexNode) ? 1 : 0));
} else {
// Can't translate other literals.
return null;
}
}
Aggregations