Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#allowsPrec()

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#allowsPrec() . 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: RelNodeConvertor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: RelDataTypeSystemImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType deriveSumType(RelDataTypeFactory typeFactory,
    RelDataType argumentType) {
  if (argumentType instanceof BasicSqlType) {
    SqlTypeName typeName = argumentType.getSqlTypeName();
    if (typeName.allowsPrec()
        && argumentType.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED) {
      int precision = typeFactory.getTypeSystem().getMaxPrecision(typeName);
      if (typeName.allowsScale()) {
        argumentType = typeFactory.createTypeWithNullability(
            typeFactory.createSqlType(typeName, precision, argumentType.getScale()),
            argumentType.isNullable());
      } else {
        argumentType = typeFactory.createTypeWithNullability(
            typeFactory.createSqlType(typeName, precision), argumentType.isNullable());
      }
    }
  }
  return argumentType;
}
 
Example 3
Source File: SqlDataTypeSpec.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    int leftPrec,
    int rightPrec) {
  String name = typeName.getSimple();
  if (SqlTypeName.get(name) != null) {
    SqlTypeName sqlTypeName = SqlTypeName.get(name);

    // we have a built-in data type
    writer.keyword(name);

    if (sqlTypeName.allowsPrec() && (precision >= 0)) {
      final SqlWriter.Frame frame =
          writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
      writer.print(precision);
      if (sqlTypeName.allowsScale() && (scale >= 0)) {
        writer.sep(",", true);
        writer.print(scale);
      }
      writer.endList(frame);
    }

    if (charSetName != null) {
      writer.keyword("CHARACTER SET");
      writer.identifier(charSetName);
    }

    if (collectionsTypeName != null) {
      writer.keyword(collectionsTypeName.getSimple());
    }
  } else if (name.startsWith("_")) {
    // We're generating a type for an alien system. For example,
    // UNSIGNED is a built-in type in MySQL.
    // (Need a more elegant way than '_' of flagging this.)
    writer.keyword(name.substring(1));
  } else {
    // else we have a user defined type
    typeName.unparse(writer, leftPrec, rightPrec);
  }
}
 
Example 4
Source File: HBTQueryConvertor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static RelDataType toType(String typeText, boolean nullable, Integer precision, Integer scale) {
    final RelDataTypeFactory typeFactory = MycatCalciteSupport.INSTANCE.TypeFactory;
    SqlTypeName sqlTypeName = HBTCalciteSupport.INSTANCE.getSqlTypeName(typeText);
    RelDataType sqlType = null;
    if (precision != null && scale != null) {
        if (sqlTypeName.allowsPrec()&&sqlTypeName.allowsScale()){
            sqlType = typeFactory.createSqlType(sqlTypeName, precision, scale);
        }else if (sqlTypeName.allowsPrec()&&!sqlTypeName.allowsScale()){
            sqlType = typeFactory.createSqlType(sqlTypeName, precision);
        }else if (sqlTypeName.allowsPrec()&&!sqlTypeName.allowsScale()){
            sqlType = typeFactory.createSqlType(sqlTypeName, precision);
        }else if (!sqlTypeName.allowsPrec()&&!sqlTypeName.allowsScale()){
            sqlType = typeFactory.createSqlType(sqlTypeName);
        }else {
            throw new IllegalArgumentException("sqlTypeName:"+sqlTypeName+" precision:"+precision+" scale:"+scale);
        }
    }
    if (precision != null && scale == null) {
        if (sqlTypeName.allowsPrec()){
            sqlType = typeFactory.createSqlType(sqlTypeName, precision);
        }else {
            sqlType = typeFactory.createSqlType(sqlTypeName);
        }
    }
    if (precision == null && scale == null) {
        sqlType = typeFactory.createSqlType(sqlTypeName);
    }
    if (sqlType == null) {
        throw new IllegalArgumentException("sqlTypeName:"+sqlTypeName);
    }

    return typeFactory.createTypeWithNullability(sqlType, nullable);
}
 
Example 5
Source File: FlinkSqlDataTypeSpec.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	final SqlIdentifier typeName = getTypeName();
	String name = typeName.getSimple();
	if (typeName instanceof ExtendedSqlType) {
		typeName.unparse(writer, leftPrec, rightPrec);
	} else if (SqlTypeName.get(name) != null) {
		SqlTypeName sqlTypeName = SqlTypeName.get(name);
		writer.keyword(name);
		if (sqlTypeName.allowsPrec() && this.getPrecision() >= 0) {
			SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
			writer.print(this.getPrecision());
			if (sqlTypeName.allowsScale() && this.getScale() >= 0) {
				writer.sep(",", true);
				writer.print(this.getScale());
			}

			writer.endList(frame);
		}

		if (this.getCharSetName() != null) {
			writer.keyword("CHARACTER SET");
			writer.identifier(this.getCharSetName(), false);
		}

		if (this.getCollectionsTypeName() != null) {
			// Fix up nullable attribute if this is a collection type.
			if (elementNullable != null && !elementNullable) {
				writer.keyword("NOT NULL");
			}
			writer.keyword(this.getCollectionsTypeName().getSimple());
		}
	} else if (name.startsWith("_")) {
		writer.keyword(name.substring(1));
	} else {
		this.getTypeName().unparse(writer, leftPrec, rightPrec);
	}
	if (getNullable() != null && !getNullable()) {
		writer.keyword("NOT NULL");
	}
}