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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#get() . 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: SqlDataTypeSpec.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Throws an error if the type is not found.
 */
public RelDataType deriveType(SqlValidator validator) {
  RelDataType type = null;
  if (typeName.isSimple()) {
    if (null != collectionsTypeName) {
      final String collectionName = collectionsTypeName.getSimple();
      if (SqlTypeName.get(collectionName) == null) {
        throw validator.newValidationError(this,
            RESOURCE.unknownDatatypeName(collectionName));
      }
    }

    RelDataTypeFactory typeFactory = validator.getTypeFactory();
    type = deriveType(typeFactory);
  }
  if (type == null) {
    type = validator.getValidatedNodeType(typeName);
  }
  return type;
}
 
Example 2
Source File: SqlHandlerUtil.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Create arrow field from sql column declaration
 *
 * @param config
 * @param column
 * @return
 */
public static Field fieldFromSqlColDeclaration(SqlHandlerConfig config, SqlColumnDeclaration column, String sql) {
  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == null) {
    throw SqlExceptionHelper.parseError(String.format("Invalid column type [%s] specified for column [%s].",
        column.getDataType(), column.getName().getSimple()),
        sql, column.getParserPosition()).buildSilently();
  }

  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == SqlTypeName.DECIMAL &&
      column.getDataType().getPrecision() > RelDataTypeSystemImpl.MAX_NUMERIC_PRECISION) {
    throw SqlExceptionHelper.parseError(String.format("Precision larger than %s is not supported.",
        RelDataTypeSystemImpl.MAX_NUMERIC_PRECISION), sql, column.getParserPosition()).buildSilently();
  }

  if (SqlTypeName.get(column.getDataType().getTypeName().getSimple()) == SqlTypeName.DECIMAL &&
      column.getDataType().getScale() > RelDataTypeSystemImpl.MAX_NUMERIC_SCALE) {
    throw SqlExceptionHelper.parseError(String.format("Scale larger than %s is not supported.",
        RelDataTypeSystemImpl.MAX_NUMERIC_SCALE), sql, column.getParserPosition()).buildSilently();
  }

  return CalciteArrowHelper.fieldFromCalciteRowType(column.getName().getSimple(), column.getDataType()
      .deriveType(config.getConverter().getTypeFactory())).orElseThrow(
      () -> SqlExceptionHelper.parseError(String.format("Invalid type [%s] specified for column [%s].",
          column.getDataType(), column.getName().getSimple()), sql, column.getParserPosition()).buildSilently());
}
 
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: ExprToRex.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCastExpression(CastExpression e, Void value) throws RuntimeException {
  RexNode convertedInput = e.getInput().accept(this, null);
  String typeStr = e.getMajorType().getMinorType().toString();

  if (SqlTypeName.get(typeStr) == null) {
    logger.debug("SqlTypeName could not find {}", typeStr);
  }

  SqlTypeName typeName = TypeInferenceUtils.getCalciteTypeFromDrillType(e.getMajorType().getMinorType());

  RelDataType targetType = TypeInferenceUtils.createCalciteTypeWithNullability(
      inputRel.getCluster().getTypeFactory(), typeName, true);
  return builder.makeCast(targetType, convertedInput);
}
 
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");
	}
}