Search in sources :

Example 1 with CalciteSqlBasicCall

use of com.alibaba.druid.support.calcite.CalciteSqlBasicCall 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)

Aggregations

MySqlForceIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)1 CalciteSqlBasicCall (com.alibaba.druid.support.calcite.CalciteSqlBasicCall)1 SqlCastFunction (org.apache.calcite.sql.fun.SqlCastFunction)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 DateString (org.apache.calcite.util.DateString)1 TimeString (org.apache.calcite.util.TimeString)1 TimestampString (org.apache.calcite.util.TimestampString)1