use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class HBTQueryConvertor method tryGetRelDataTypeByParse.
private RelDataType tryGetRelDataTypeByParse(String sql) {
try {
SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
if (sqlStatement instanceof SQLSelectStatement) {
SQLSelectQueryBlock firstQueryBlock = ((SQLSelectStatement) sqlStatement).getSelect().getFirstQueryBlock();
final RelDataTypeFactory typeFactory = MycatCalciteSupport.INSTANCE.TypeFactory;
final RelDataTypeFactory.Builder builder = typeFactory.builder();
for (SQLSelectItem sqlSelectItem : firstQueryBlock.getSelectList()) {
SQLDataType sqlDataType = sqlSelectItem.computeDataType();
if (sqlDataType == null) {
return null;
}
SqlTypeName type = HBTCalciteSupport.INSTANCE.getSqlTypeByJdbcValue(sqlDataType.jdbcType());
if (type == null) {
return null;
}
builder.add(sqlSelectItem.toString(), type);
}
return builder.build();
}
} catch (Throwable e) {
log.warn("", e);
}
return null;
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class RelNodeConvertor method getFields.
private static List<FieldType> getFields(RelNode relNode) {
RelDataType rowType = relNode.getRowType();
List<RelDataTypeField> fieldList = rowType.getFieldList();
ArrayList<FieldType> fieldSchemas = new ArrayList<>(fieldList.size());
for (RelDataTypeField relDataTypeField : fieldList) {
String name = relDataTypeField.getName();
RelDataType type = relDataTypeField.getType();
SqlTypeName sqlTypeName = type.getSqlTypeName();
boolean nullable = type.isNullable();
Integer precision = null;
Integer scale = null;
if (sqlTypeName.allowsPrec()) {
precision = type.getPrecision();
}
if (sqlTypeName.allowsScale()) {
scale = type.getScale();
}
fieldSchemas.add(new FieldType(name, ExprExplain.type(sqlTypeName), nullable, precision, scale));
}
return fieldSchemas;
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class FieldTypes method getFieldTypes.
@SneakyThrows
public static List<FieldType> getFieldTypes(MycatRowMetaData metaData) {
int columnCount = metaData.getColumnCount();
ArrayList<FieldType> fieldTypes = new ArrayList<>(columnCount);
for (int i = 0; i < columnCount; i++) {
final String columnName = metaData.getColumnName(i);
SqlTypeName sqlTypeName = Objects.requireNonNull(HBTCalciteSupport.INSTANCE.getSqlTypeByJdbcValue(metaData.getColumnType(i)), "type is not existed,类型不存在");
final String columnType = sqlTypeName.getName();
final boolean nullable = metaData.isNullable(i);
final Integer precision = metaData.getPrecision(i);
final Integer scale = metaData.getScale(i);
fieldTypes.add(new FieldType(columnName, columnType, nullable, precision, scale));
}
return fieldTypes;
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
the class MycatCalciteMySqlNodeVisitor method visit.
public boolean visit(SQLCastExpr x) {
SqlLiteral functionQualifier = null;
SqlNode sqlNode = convertToSqlNode(x.getExpr());
SQLDataType dataType = x.getDataType();
String typeName = dataType.getName().toUpperCase();
if (dataType.nameHashCode64() == FnvHash.Constants.INT) {
typeName = "INTEGER";
} else if (dataType.nameHashCode64() == FnvHash.Constants.NUMERIC) {
typeName = "DECIMAL";
}
SqlIdentifier dataTypeNode = (SqlIdentifier) convertToSqlNode(new SQLIdentifierExpr(typeName));
int scale = -1;
int precision = -1;
List<SQLExpr> arguments = dataType.getArguments();
if (arguments != null && !arguments.isEmpty()) {
scale = ((SQLNumericLiteralExpr) arguments.get(0)).getNumber().intValue();
if (arguments.size() > 1) {
precision = ((SQLNumericLiteralExpr) arguments.get(1)).getNumber().intValue();
}
}
SqlNode sqlDataTypeSpec;
if (typeName.equalsIgnoreCase("SIGNED")) {
sqlDataTypeSpec = new SqlDataTypeSpec(new SqlBasicTypeNameSpec(SqlTypeName.BIGINT, precision, scale, null, SqlParserPos.ZERO), SqlParserPos.ZERO);
} else {
if ("datetime".equalsIgnoreCase(typeName)) {
typeName = "TIMESTAMP";
} else if ("BINARY".equalsIgnoreCase(typeName)) {
typeName = "VARCHAR";
} else if ("UNSIGNED".equalsIgnoreCase(typeName)) {
typeName = "DECIMAL";
}
SqlTypeName sqlTypeName = SqlTypeName.valueOf(typeName);
SqlBasicTypeNameSpec sqlBasicTypeNameSpec = new SqlBasicTypeNameSpec(sqlTypeName, precision, scale, null, SqlParserPos.ZERO);
sqlDataTypeSpec = new SqlDataTypeSpec(sqlBasicTypeNameSpec, SqlParserPos.ZERO);
}
SqlOperator sqlOperator = new SqlCastFunction();
this.sqlNode = new CalciteSqlBasicCall(sqlOperator, new SqlNode[] { sqlNode, sqlDataTypeSpec }, SqlParserPos.ZERO, false, functionQualifier);
return false;
}
use of org.apache.calcite.sql.type.SqlTypeName in project Mycat2 by MyCATApache.
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