Skip to content

Fixes for safe_divide with vectorize and datatypes#15839

Merged
abhishekagarwal87 merged 3 commits intoapache:masterfrom
somu-imply:safe_divide_vectotize
Feb 8, 2024
Merged

Fixes for safe_divide with vectorize and datatypes#15839
abhishekagarwal87 merged 3 commits intoapache:masterfrom
somu-imply:safe_divide_vectotize

Conversation

@somu-imply
Copy link
Copy Markdown
Contributor

@somu-imply somu-imply commented Feb 6, 2024

Queries like

select count(*) c from foo where ((floor(safe_divide(cast(cast(m1 as char) as bigint), 2))) = 0)

used to fail previously with the error Cannot vectorize. The reason was that vectorized() was not implemented for the safe_divide function.

Also SAFE_DIVIDE(1,0) when used without a table would throw an exception and SAFE_DIVIDE(0,0) was not handled correctly for sqlCompatible mode. This PR fixes these issues

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@somu-imply somu-imply changed the title Fix for save_divide with vectorize Fix for safe_divide with vectorize Feb 6, 2024
@somu-imply somu-imply changed the title Fix for safe_divide with vectorize Fixes for safe_divide with vectorize and datatypes Feb 6, 2024
@somu-imply somu-imply force-pushed the safe_divide_vectotize branch from dfe97a2 to 6346075 Compare February 6, 2024 16:47
@somu-imply somu-imply force-pushed the safe_divide_vectotize branch from 6346075 to efaf8ef Compare February 6, 2024 18:37
new Object[]{1.0F, 1L, 1.0, 3253230.0F},
new Object[]{0.0F, 0L, 0.0, 0.0F},
new Object[]{1.0F, 1L, 1.0D, 3253230.0F},
new Object[]{0.0F, null, 0.0D, 0.0F},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is due to change in eval handling SQL compatibility mode correctly

{
private static final SqlFunction SQL_FUNCTION = OperatorConversions
.operatorBuilder(StringUtils.toUpperCase(Function.SafeDivide.NAME))
.operandTypeChecker(OperandTypes.ANY_NUMERIC)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this slightly and added the proper return type inference. Quotient depended on if the numerator and denominators are null and would throw an exception of failing datatype EXPR$0: INTEGER NOT NULL -> INTEGER

Comment on lines +1186 to +1189
if (x != 0) {
return ExprEval.ofLong(null);
}
return ExprEval.ofLong(0);
return ExprEval.ofLong(NullHandling.defaultLongValue());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to distinguish case of numerator being non-zero? in sql compatible mode this results in null in both cases.

Also, i think this should just be set to ExprEval.ofLong(null) in both cases since after #13809 the coercion of null to default values happens when stuff gets out of the expression layer instead of inside it

@somu-imply somu-imply force-pushed the safe_divide_vectotize branch from 0c22617 to c86d0e3 Compare February 8, 2024 04:43
@abhishekagarwal87 abhishekagarwal87 merged commit f3996b9 into apache:master Feb 8, 2024
@adarshsanjeev adarshsanjeev added this to the 30.0.0 milestone May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants