hive cast函数源码
时间: 2023-11-11 11:05:09 浏览: 26
以下是 Hive 中 cast 函数的源代码:
```java
public class CastExprNode extends ExprNodeGenericFuncDesc {
public CastExprNode(ExprNodeDesc child, TypeInfo type) throws UDFArgumentException {
super(new GenericUDFToTypeInfo(type), new ExprNodeDesc[]{child});
}
@Override
protected ExprNodeDesc uncheckedCastToBoolean(ExprNodeDesc expr) throws UDFArgumentTypeException {
GenericUDF udf = new GenericUDFToBoolean();
return new ExprNodeGenericFuncDesc(udf, new ExprNodeDesc[]{expr});
}
@Override
protected ExprNodeDesc uncheckedCastToDouble(ExprNodeDesc expr) throws UDFArgumentTypeException {
GenericUDF udf = new GenericUDFToDouble();
return new ExprNodeGenericFuncDesc(udf, new ExprNodeDesc[]{expr});
}
@Override
protected ExprNodeDesc uncheckedCastToDecimal(ExprNodeDesc expr) throws UDFArgumentTypeException {
GenericUDF udf = new GenericUDFToDecimal();
return new ExprNodeGenericFuncDesc(udf, new ExprNodeDesc[]{expr});
}
@Override
protected ExprNodeDesc uncheckedCastToLong(ExprNodeDesc expr) throws UDFArgumentTypeException {
GenericUDF udf = new GenericUDFToLong();
return new ExprNodeGenericFuncDesc(udf, new ExprNodeDesc[]{expr});
}
@Override
protected ExprNodeDesc uncheckedCastToTimestamp(ExprNodeDesc expr) throws UDFArgumentTypeException {
GenericUDF udf = new GenericUDFToTimestamp();
return new ExprNodeGenericFuncDesc(udf, new ExprNodeDesc[]{expr});
}
@Override
public String getExprString() {
return "CAST(" + getChildExprs().get(0).getExprString() + " AS " + getTypeInfo().getTypeName() + ")";
}
}
```
`CastExprNode` 继承了 `ExprNodeGenericFuncDesc` 类,这个类是所有通用函数节点的父类。在 `CastExprNode` 中,我们可以看到 `ExprNodeDesc` 类型的参数 `child` 和 `TypeInfo` 类型的参数 `type` 传递给了 `ExprNodeGenericFuncDesc` 的构造函数。
此外,`CastExprNode` 还重写了一些方法,如 `uncheckedCastToBoolean`、`uncheckedCastToDouble`、`uncheckedCastToDecimal`、`uncheckedCastToLong`、`uncheckedCastToTimestamp`,用于将表达式转换为特定类型。
最后,`getExprString` 方法返回一个字符串,该字符串表示 `CAST` 函数的 SQL 表达式。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)