fix: param check

This commit is contained in:
factosea 2025-03-10 15:08:55 +08:00
parent cc0a14f5ab
commit 7e6edb82ba
4 changed files with 29 additions and 12 deletions

View File

@ -612,14 +612,13 @@ GREATEST(expr1, expr2[, expr]...)
**Return Type**Refer to the comparison rules. The comparison type is the final return type.
**Applicable Data Types**:
- Numeric types: including bool, integer and floating point types
- nchar and varchar types.
- Numeric types: timestamp, bool, integer and floating point types
- Strings types: nchar and varchar types.
**Comparison rules**: The following rules describe the conversion method of the comparison operation:
- If any parameter is NULL, the comparison result is NULL.
- If all parameters in the comparison operation are string types, compare them as string types
- If all parameters are numeric types, compare them as numeric types.
- TIMESTAMP type is also a numeric type. When the types involved in the comparison with TIMESTAMP are all integer types, compare them as TIMESTAMP;
- If there are both string types and numeric types in the parameters, according to the `compareAsStrInGreatest` configuration item, they are uniformly compared as strings or numeric values. By default, they are compared as strings.
- In all cases, when different types are compared, the comparison type will choose the type with a larger range for comparison. For example, when comparing integer types, if there is a BIGINT type, BIGINT will definitely be selected as the comparison type.
@ -631,7 +630,7 @@ GREATEST(expr1, expr2[, expr]...)
LEAST(expr1, expr2[, expr]...)
```
**Function Description**Get the minimum value of all input parameters. The arguments are compared using the same rules as for LEAST(). The rest of the description is the same as the greatest function.
**Function Description**Get the minimum value of all input parameters. The arguments are compared using the same rules as for GREATEST(). The rest of the description is the same as the greatest function.
### String Functions

View File

@ -591,13 +591,12 @@ GREATEST(expr1, expr2[, expr]...)
**适用数据类型**
- 数值类型:包括 bool 型,整型和浮点型
- nchar 和 varchar 类型。
- 字符串类型:包括 nchar 和 varchar 类型。
**比较规则**:以下规则描述了比较操作的转换方式:
- 如果有任何一个参数为 NULL则比较结果为 NULL。
- 如果比较操作中的所有参数都是字符串类型,按照字符串类型比较
- 如果所有参数都是数值类型,则将它们作为数值类型进行比较。
- TIMESTAMP 类型也是数值类型,当和 TIMESTAMP 参与比较的类型都是整数类型时,按照 TIMESTAMP 进行比较;
- 如果参数中既有字符串类型,也有数值类型,根据 compareAsStrInGreatest 配置项,统一作为字符串或者数值进行比较。默认按照字符串比较。
- 在所有情况下,不同类型比较,比较类型会选择范围更大的类型进行比较,例如作为整数类型比较时,如果存在 BIGINT 类型,必定会选择 BIGINT 作为比较类型。

View File

@ -1750,9 +1750,7 @@ static int32_t translateHistogramPartial(SFunctionNode* pFunc, char* pErrBuf, in
#define NUMERIC_TO_STRINGS_LEN 25
static int32_t translateGreatestleast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
if (LIST_LENGTH(pFunc->pParameterList) < 2) {
return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
}
FUNC_ERR_RET(validateParam(pFunc, pErrBuf, len));
bool mixTypeToStrings = tsCompareAsStrInGreatest;
@ -1785,7 +1783,7 @@ static int32_t translateGreatestleast(SFunctionNode* pFunc, char* pErrBuf, int32
// last res is strings, para is numeric and mixTypeToStrings is true
res.bytes = TMAX(res.bytes, NUMERIC_TO_STRINGS_LEN);
}
} else if (IS_COMPARE_STR_DATA_TYPE(para->type)) {
} else {
if (IS_COMPARE_STR_DATA_TYPE(res.type)) {
int32_t resType = vectorGetConvertType(res.type, para->type);
res.type = resType == 0 ? res.type : resType;
@ -1800,8 +1798,6 @@ static int32_t translateGreatestleast(SFunctionNode* pFunc, char* pErrBuf, int32
res.type = resType == 0 ? res.type : resType;
res.bytes = tDataTypes[resType].bytes;
}
} else {
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
}
}
pFunc->node.resType = res;
@ -5723,6 +5719,17 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.name = "greatest",
.type = FUNCTION_TYPE_GREATEST,
.classification = FUNC_MGT_SCALAR_FUNC,
.parameters = {.minParamNum = 2,
.maxParamNum = -1,
.paramInfoPattern = 1,
.inputParaInfo[0][0] = {.isLastParam = true,
.startParam = 1,
.endParam = -1,
.validDataType = FUNC_PARAM_SUPPORT_NUMERIC_TYPE | FUNC_PARAM_SUPPORT_NULL_TYPE | FUNC_PARAM_SUPPORT_BOOL_TYPE | FUNC_PARAM_SUPPORT_TIMESTAMP_TYPE | FUNC_PARAM_SUPPORT_VARCHAR_TYPE | FUNC_PARAM_SUPPORT_NCHAR_TYPE,
.validNodeType = FUNC_PARAM_SUPPORT_EXPR_NODE,
.paramAttribute = FUNC_PARAM_NO_SPECIFIC_ATTRIBUTE,
.valueRangeFlag = FUNC_PARAM_NO_SPECIFIC_VALUE,},
.outputParaInfo = {.validDataType = FUNC_PARAM_SUPPORT_ALL_TYPE}},
.translateFunc = translateGreatestleast,
.getEnvFunc = NULL,
.initFunc = NULL,
@ -5733,6 +5740,17 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.name = "least",
.type = FUNCTION_TYPE_LEAST,
.classification = FUNC_MGT_SCALAR_FUNC,
.parameters = {.minParamNum = 2,
.maxParamNum = -1,
.paramInfoPattern = 1,
.inputParaInfo[0][0] = {.isLastParam = true,
.startParam = 1,
.endParam = -1,
.validDataType = FUNC_PARAM_SUPPORT_NUMERIC_TYPE | FUNC_PARAM_SUPPORT_NULL_TYPE | FUNC_PARAM_SUPPORT_BOOL_TYPE | FUNC_PARAM_SUPPORT_TIMESTAMP_TYPE | FUNC_PARAM_SUPPORT_VARCHAR_TYPE | FUNC_PARAM_SUPPORT_NCHAR_TYPE,
.validNodeType = FUNC_PARAM_SUPPORT_EXPR_NODE,
.paramAttribute = FUNC_PARAM_NO_SPECIFIC_ATTRIBUTE,
.valueRangeFlag = FUNC_PARAM_NO_SPECIFIC_VALUE,},
.outputParaInfo = {.validDataType = FUNC_PARAM_SUPPORT_ALL_TYPE}},
.translateFunc = translateGreatestleast,
.getEnvFunc = NULL,
.initFunc = NULL,

View File

@ -352,6 +352,7 @@ class TDTestCase(TBase):
tdSql.query("select GREATEST(now, 1.0);")
tdSql.query("select GREATEST(now, '1');")
tdSql.error("select GREATEST(1)")
tdSql.error("select GREATEST(cast('a' as varbinary), cast('b' as varbinary), 'c', 'd');")
tdSql.error("select GREATEST(6, cast('f' as varbinary), cast('b' as varbinary), 'c', 'd');")