Skip to content

Check the types of given parameters matches the types of SQL function declared parameters #18010

Closed

Description

@FrankChen021

Affected Version

ALL

Description

For APPROX_QUANTILE and related functions, the first parameter MUST be numeric column. however, currently the SQL planner does not check if the given expression is type of number, even a STRING is accepted.
If users accidentally use single quote instead of double quote for a column, an hard to understand exception is thrown as follows>

SELECT channel,
APPROX_QUANTILE('r1', 0.990000, 100) AS "p99"
FROM "wikipedia"
GROUP BY channel
Error: RUNTIME_FAILURE (OPERATOR)
java.util.concurrent.ExecutionException: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
java.lang.RuntimeException

This makes people think that there's an internal error at the engine side not at the SQL side.
 
So, for these functions, a type check at SQL layer is needed to give a clear information instead of throwing an exception which makes people think there's internal error from the engine.

Activity

  1. CoodingPenguin commented on May 26, 2025

    @CoodingPenguin

    @FrankChen021
    Hi! I'm new to contributing to open source, and this would be my very first contribution.

    I'd really like to take on this issue. I understand the goal is to add type checking for APPROX_QUANTILE and related functions.

    Just to make sure I’m on the right track — could you please clarify which functions are considered "related functions" in this context?

    Thanks a lot in advance!

  2. FrankChen021 commented on May 26, 2025

    @FrankChen021
    MemberAuthor

    @CoodingPenguin

    the related functions here, I mean, we should check all SQL functions if they check the types properly. And I think most of functions are lack of such type check.

    Looking forward to your contribution.

  3. CuteChuanChuan commented on Jun 8, 2025

    @CuteChuanChuan

    Hi @CoodingPenguin ,

    just wanted to follow up and see if you're still actively working on this issue.
    If not, I'm happy to contribute and help move it forward.
    Thank you.

  4. CoodingPenguin commented on Jun 9, 2025

    @CoodingPenguin

    @CuteChuanChuan
    Hi, thanks a lot for following up!

    I haven’t been able to make progress on this recently, and realistically I won’t be able to pick it back up any time soon.
    Please feel free to take over — I’d be really happy to see it move forward.

  5. havefunatcode commented on Oct 28, 2026

    @havefunatcode
    Contributor

    Hi @FrankChen021, I'd like to work on this if nobody else is (cc @CuteChuanChuan, are you still on it?).

    Doing every SQL function at once would be a big PR, so I'd start with the case in the description. With 'm1' instead of "m1", APPROX_QUANTILE and APPROX_QUANTILE_DS return NaN on master and APPROX_QUANTILE_FIXED_BUCKETS fails at runtime. I'd make those three throw InvalidSqlInput at planning time like #18949 (STRING columns would still work, since they're implicitly cast) and do the other functions in separate PRs. Is that OK with you?

  6. FrankChen021 commented on Oct 28, 2026

    @FrankChen021
    MemberAuthor

    @havefunatcode

    Currently if a string column is used on a numeric aggregator, druid implicitly convert it into numer. Simply rejecting a string column on nuemric aggregator would change existing behaviour that people may depeond on it.

    We can apply strict type check and reject implicit type conversion globally and require explicit type conversion from users, but this is broader change and may need wide discussion.

    Currently the problem is that string literal is not check whether they're numbers at planning time. I think we can put a check in Aggregations.toDruidExpressionForNumericAggregator, next to the existing implicit conversion for STRING columns.
    A string literal is converted the same way, but since its value is known at planning time, a literal that can’t be parsed as a number should throw InvalidSqlInput there. This covers all numeric aggregators, not just the quantile ones.

  7. havefunatcode commented on Oct 28, 2026

    @havefunatcode
    Contributor

    @FrankChen021 Sorry, I wasn't clear. I only meant to reject string literals, STRING columns would keep the implicit cast. Putting the check in Aggregations.toDruidExpressionForNumericAggregator for literals that can't be parsed as numbers makes sense, I'll open a PR for that. One thing I ran into: SUM/MIN/MAX/AVG/COUNT/SINGLE_VALUE reach that conversion through getArgumentsForSimpleAggregator, and checking there breaks things like COUNT(DISTINCT 'abc'), so I'd leave that path out and explain it in the PR.

  8. FrankChen021 commented on Oct 28, 2026

    @FrankChen021
    MemberAuthor

    I'm not sure if we have test coverage for SUM/MIN/MAX/AVG/COUNT/COUNT DISTINCT on string column and string literal, you can check and add them to fill the gap if needed.

  9. havefunatcode commented on Oct 28, 2026

    @havefunatcode
    Contributor

    Thanks, I checked. COUNT, COUNT DISTINCT and MAX on a STRING column are covered, SUM/MIN/AVG aren't, and none of them are tested with string literals. I'll add a quidem test that runs all six on a STRING column, a numeric literal ('1.5') and a non-numeric one ('abc'), and records what they return today.

    The test also shows that SUM('abc') and AVG('abc') return null instead of failing. Calcite already turns 'abc' into null:DECIMAL(38, 19) in the logical plan, so the new check never sees the literal. I'd leave that for a follow-up unless you want it in this PR.

  10. FrankChen021 commented on Oct 28, 2026

    @FrankChen021
    MemberAuthor

    The test also shows that SUM('abc') and AVG('abc') return null instead of failing.

    I checked the impl to see how SUM/AVG plans the literal abc into null:DECIMAL(38, 19) while MAX/MIN/quantile aggregator does not.

    SUM/AVG declares the operand type as NUMERIC(see DRUID_SUM in SumSqlAggregator) , MIN/MAX declares the operand type as COMPARABLE_ORDERED and quantile declares the operand type to be ANY. This allows the quantile functions accept any type, not only string, but also array because DefaultOperandTypeChecker does nothing for ANY type.

    So fixing the string literal only fixes part of the problem. To fix this completely, we may need to:

    1. update the QuantileSqlAggregator to define the 1st operand type to be numeric or any
    2. update DefaultOperandTypeChecker so that it checks:
      2.1. if the operand is ComplexSqlType, check against if it's defined in the operand allowed complex type
      2.2. If the operand is a character literal that won’t parse, reject it with the friendly InvalidSqlInput.
      2.3. if the operand is coerciable, try coercion

    This is a little complicated than a simple change in toDruidExpressionForNumericAggregator.

    For null:DECIMAL(38, 19) problem of SUM/AVG, we may need to update DruidTypeCoercion#builtinFunctionCoercion to check if the literal is parsable. This can be done in a separate PR.

  11. havefunatcode commented on Oct 28, 2026

    @havefunatcode
    Contributor

    Thanks, that makes sense. I'll go with that in this PR and leave SUM/AVG for a follow-up. Roughly:

    • APPROX_QUANTILE, APPROX_QUANTILE_FIXED_BUCKETS and APPROX_QUANTILE_DS (plus DS_QUANTILES_SKETCH and the TDIGEST ones, which have the same ANY first operand) declare it as NUMERIC and list the complex types they accept.
    • DefaultOperandTypeChecker gets opt-in builder options for allowed complex types and for coercing an operand, so the other functions using it don't change. Non-numeric string literals are rejected before coercion, otherwise 'm1' would just turn into a null.

    One more thing: the checker rejects a NULL literal for a required operand, so APPROX_QUANTILE(NULL, 0.5) would start failing with "Illegal use of 'NULL'" once the first operand isn't ANY. I think that's fine. Other functions using the checker already behave that way (BITWISE_AND(NULL, 1), SAFE_DIVIDE(NULL, 1), TRUNCATE(NULL, 0)), and I couldn't find a quantile test that relies on NULL being accepted. Keeping it would need a "required but nullable" option, which doesn't really fit the type check, so I'd rather not add one. That also means leaving the Spectator functions on OperandTypes.ANY for now, since SpectatorHistogramSqlAggregatorTest expects SPECTATOR_COUNT(null) and SPECTATOR_PERCENTILE(null, 99.9) to return null. Does that sound OK to you?

  12. FrankChen021 commented on Oct 29, 2026

    @FrankChen021
    MemberAuthor

    sounds good to me

  13. added a commit that references this issue on Nov 4, 2026
    edcbfe7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions