Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#valueOf()
The following examples show how to use
org.apache.calcite.sql.type.SqlTypeName#valueOf() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalciteConvertors.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
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); } }
Example 2
Source File: JdbcSchema.java From calcite with Apache License 2.0 | 5 votes |
/** Given "INTEGER", returns BasicSqlType(INTEGER). * Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10). * Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2). */ private 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); } }
Example 3
Source File: MycatCalciteMySqlNodeVisitor.java From Mycat2 with GNU General Public License v3.0 | 4 votes |
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 { 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; }