[ 
https://issues.apache.org/jira/browse/HIVE-27157?focusedWorklogId=852051&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-852051
 ]

ASF GitHub Bot logged work on HIVE-27157:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 21/Mar/23 15:59
            Start Date: 21/Mar/23 15:59
    Worklog Time Spent: 10m 
      Work Description: zabetak opened a new pull request, #4135:
URL: https://github.com/apache/hive/pull/4135

   ### What changes were proposed in this pull request?
   Change the implementation of unix_timestamp operators to avoid the 
AssertionError and infer the return type correctly; always BIGINT.
   
   Break the inheritance relation with SqlAbstractTimeFunction and change the 
SqlFunctionCategory from TIMEDATE to NUMERIC; unix_timestamp is not a time 
function since the result is never among DATE, TIME, or TIMESTAMP.
   
   Change the operand type checker to a more truthful implementation; the type 
checker is not really used at the moment but it is better to have something 
realistic there instead of null or something completely wrong.
   
   ### Why are the changes needed?
   Calls to `inferReturnType` method for `unix_timestamp` operators always lead 
to `AssertionError`.
   
   Contrary to operand type checking and operand type inference that are not 
really relevant for Hive (the latter is not using the `SqlValidator` logic), 
the return type inference is important since it may kick in some calls to 
`RelBuilder/RexBuilder` APIs. Such calls exist in older versions of Hive and 
are widely used in Calcite's built-in rules.
   
   ### Does this PR introduce _any_ user-facing change?
   In this version no, but in older versions of Hive it can fix queries failing 
with `AssertionError`.
   
   ### How was this patch tested?
   ```
   mvn test -Dtest=TestSqlOperatorInferReturnType
   ```




Issue Time Tracking
-------------------

            Worklog Id:     (was: 852051)
    Remaining Estimate: 0h
            Time Spent: 10m

> AssertionError when inferring return type for unix_timestamp function
> ---------------------------------------------------------------------
>
>                 Key: HIVE-27157
>                 URL: https://issues.apache.org/jira/browse/HIVE-27157
>             Project: Hive
>          Issue Type: Bug
>          Components: CBO
>    Affects Versions: 4.0.0-alpha-2
>            Reporter: Stamatis Zampetakis
>            Assignee: Stamatis Zampetakis
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> Any attempt to derive the return data type for the {{unix_timestamp}} 
> function results into the following assertion error.
> {noformat}
> java.lang.AssertionError: typeName.allowsPrecScale(true, false): BIGINT
>       at 
> org.apache.calcite.sql.type.BasicSqlType.checkPrecScale(BasicSqlType.java:65)
>       at org.apache.calcite.sql.type.BasicSqlType.<init>(BasicSqlType.java:81)
>       at 
> org.apache.calcite.sql.type.SqlTypeFactoryImpl.createSqlType(SqlTypeFactoryImpl.java:67)
>       at 
> org.apache.calcite.sql.fun.SqlAbstractTimeFunction.inferReturnType(SqlAbstractTimeFunction.java:78)
>       at 
> org.apache.calcite.rex.RexBuilder.deriveReturnType(RexBuilder.java:278)
> {noformat}
> due to a faulty implementation of type inference for the respective operators:
>  * 
> [https://github.com/apache/hive/blob/52360151dc43904217e812efde1069d6225e9570/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveUnixTimestampSqlOperator.java]
>  * 
> [https://github.com/apache/hive/blob/52360151dc43904217e812efde1069d6225e9570/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/reloperators/HiveToUnixTimestampSqlOperator.java]
> Although at this stage in master it is not possible to reproduce the problem 
> with an actual SQL query the buggy implementation must be fixed since slight 
> changes in the code/CBO rules may lead to methods relying on 
> {{{}SqlOperator.inferReturnType{}}}.
> Note that in older versions of Hive it is possible to hit the AssertionError 
> in various ways. For example in Hive 3.1.3 (and older), the error may come 
> from 
> [HiveRelDecorrelator|https://github.com/apache/hive/blob/4df4d75bf1e16fe0af75aad0b4179c34c07fc975/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveRelDecorrelator.java#L1933]
>  in the presence of sub-queries.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to