Search in sources :

Example 76 with SqlTypeName

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;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 77 with SqlTypeName

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;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString)

Example 78 with SqlTypeName

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;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ArrayList(java.util.ArrayList) FieldType(io.mycat.hbt.ast.base.FieldType) SneakyThrows(lombok.SneakyThrows)

Example 79 with SqlTypeName

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;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) CalciteSqlBasicCall(com.alibaba.druid.support.calcite.CalciteSqlBasicCall) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) TimestampString(org.apache.calcite.util.TimestampString) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)

Example 80 with SqlTypeName

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);
    }
}
Also used : ChronoField(java.time.temporal.ChronoField) SimpleDateFormat(java.text.SimpleDateFormat) MycatBuiltInMethodImpl(org.apache.calcite.mycat.MycatBuiltInMethodImpl) 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) Duration(java.time.Duration) Map(java.util.Map) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) 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) Period(java.time.Period) 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) MycatRexExecutor(io.mycat.calcite.MycatRexExecutor) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimeZone(java.util.TimeZone) TemporalField(java.time.temporal.TemporalField) 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) LocalDate(java.time.LocalDate) 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