SQL OperatorConversions: Introduce.aggregatorBuilder, allow CAST-as-literal.#14249
SQL OperatorConversions: Introduce.aggregatorBuilder, allow CAST-as-literal.#14249gianm merged 3 commits intoapache:masterfrom
Conversation
…iteral. Four main changes: 1) Provide aggregatorBuilder, a more consistent way of defining the SqlAggFunction we need for all of our SQL aggregators. The mechanism is analogous to the one we already use for SQL functions (OperatorConversions.operatorBuilder). 2) Allow CASTs of constants to be considered as "literalOperands". This fixes an issue where various of our operators are defined with OperandTypes.LITERAL as part of their checkers, which doesn't allow casts. However, in these cases we generally _do_ want to allow casts. The important piece is that the value must be reducible to a constant, not that the SQL text is literally a literal. 3) Update DataSketches SQL aggregators to use the new aggregatorBuilder functionality. The main user-visible effect here is [2]: the aggregators would now accept, for example, "CAST(0.99 AS DOUBLE)" as a literal argument. Other aggregators could be updated in a future patch. 4) Rename "requiredOperands" to "requiredOperandCount", because the old name was confusing. (It rhymes with "literalOperands" but the arguments mean different things.)
| private static final SqlAggFunction FUNCTION_INSTANCE = new HllSketchApproxCountDistinctSqlAggFunction(); | ||
| private static final SqlAggFunction FUNCTION_INSTANCE = | ||
| OperatorConversions.aggregatorBuilder(NAME) | ||
| .operandTypes(SqlTypeFamily.ANY, SqlTypeFamily.NUMERIC, SqlTypeFamily.STRING) |
There was a problem hiding this comment.
not a big deal, but does this lose the nice signature that provides the parameter names in the validation error? similar comment on other changes that drop the OperandTypes.sequence.
I personally find the more verbose OperandTypes.or form of using operandTypeChecker to be a bit easier to understand the shape of the function at a glance than trying to piece it together from looking at all of operandTypes, literalOperands and requiredOperandCount, but i guess it isn't a big deal and is also consistent with what is allowed when using the builder to spit out SqlFunction.
There was a problem hiding this comment.
In the latest patch, I added operandNames which are used to generate the signature for validation errors.
About the syntax, yeah, I was wanting to match it to SqlFunction. Feel free to introduce a new syntax in a future patch 🙂. IMO it would make sense to have a builder for DefaultOperandTypeChecker that builds up the operand list one at a time. That would enable an alternate syntax like:
OperatorConversions
.aggregatorBuilder(NAME)
.operandTypeChecker(
OperandConversions
.checkerBuilder()
.operand("column", SqlTypeFamily.ANY)
.operand("lgK", SqlTypeFamily.NUMERIC).optional().literal()
.operand("tgtHllType", SqlTypeFamily.STRING).optional().literal()
.build()
)
.returnTypeNonNull(SqlTypeName.BIGINT)
.functionCategory(SqlFunctionCategory.NUMERIC)
.build();
…iteral. (apache#14249) * SQL OperatorConversions: Introduce.aggregatorBuilder, allow CAST-as-literal. Four main changes: 1) Provide aggregatorBuilder, a more consistent way of defining the SqlAggFunction we need for all of our SQL aggregators. The mechanism is analogous to the one we already use for SQL functions (OperatorConversions.operatorBuilder). 2) Allow CASTs of constants to be considered as "literalOperands". This fixes an issue where various of our operators are defined with OperandTypes.LITERAL as part of their checkers, which doesn't allow casts. However, in these cases we generally _do_ want to allow casts. The important piece is that the value must be reducible to a constant, not that the SQL text is literally a literal. 3) Update DataSketches SQL aggregators to use the new aggregatorBuilder functionality. The main user-visible effect here is [2]: the aggregators would now accept, for example, "CAST(0.99 AS DOUBLE)" as a literal argument. Other aggregators could be updated in a future patch. 4) Rename "requiredOperands" to "requiredOperandCount", because the old name was confusing. (It rhymes with "literalOperands" but the arguments mean different things.) * Adjust method calls.
Four main changes:
Provide aggregatorBuilder, a more consistent way of defining the
SqlAggFunction we need for all of our SQL aggregators. The mechanism
is analogous to the one we already use for SQL functions
(OperatorConversions.operatorBuilder).
Allow CASTs of constants to be considered as "literalOperands". This
fixes an issue where various of our operators are defined with
OperandTypes.LITERAL as part of their checkers, which doesn't allow
casts. However, in these cases we generally do want to allow casts.
The important piece is that the value must be reducible to a constant,
not that the SQL text is literally a literal.
Update DataSketches SQL aggregators to use the new aggregatorBuilder
functionality. The main user-visible effect here is [2]: the aggregators
would now accept, for example, "CAST(0.99 AS DOUBLE)" as a literal
argument. Other aggregators could be updated in a future patch.
Rename "requiredOperands" to "requiredOperandCount", because the
old name was confusing. (It rhymes with "literalOperands" but the
arguments mean different things.)