[Fix](Planner)fix cast date/datev2/datetime to float/double return null. (#20008)
This commit is contained in:
@ -6421,11 +6421,11 @@ non_pred_expr ::=
|
||||
| function_call_expr:e
|
||||
{: RESULT = e; :}
|
||||
| KW_DATE STRING_LITERAL:l
|
||||
{: RESULT = new StringLiteral(l); :}
|
||||
{: RESULT = new CastExpr(Type.DATE, new StringLiteral(l)); :}
|
||||
| KW_DATEV2 STRING_LITERAL:l
|
||||
{: RESULT = new StringLiteral(l); :}
|
||||
{: RESULT = new CastExpr(Type.DATEV2, new StringLiteral(l)); :}
|
||||
| KW_TIMESTAMP STRING_LITERAL:l
|
||||
{: RESULT = new StringLiteral(l); :}
|
||||
{: RESULT = new CastExpr(Type.DATETIME, new StringLiteral(l)); :}
|
||||
| KW_EXTRACT LPAREN function_name:fn_name KW_FROM func_arg_list:exprs RPAREN
|
||||
{: RESULT = new FunctionCallExpr(fn_name, exprs); :}
|
||||
//| function_name:fn_name LPAREN RPAREN
|
||||
|
||||
@ -127,6 +127,16 @@ public class ExprTest {
|
||||
Assert.assertEquals(0, castLiteral.getMinute());
|
||||
Assert.assertEquals(0, castLiteral.getSecond());
|
||||
|
||||
DateLiteral srcDate = new DateLiteral("2020-01-01", Type.DATE);
|
||||
DateLiteral srcDateV2 = new DateLiteral("2020-01-01", Type.DATEV2);
|
||||
DateLiteral srcDateTime = new DateLiteral("2020-01-01 12:34:45", Type.DATETIME);
|
||||
Assert.assertEquals(20200101L, ((FloatLiteral) (new CastExpr(Type.FLOAT, srcDate)
|
||||
.castTo(Type.FLOAT)).getResultValue(false)).getLongValue());
|
||||
Assert.assertEquals(20200101L, ((FloatLiteral) new CastExpr(Type.FLOAT, srcDateV2)
|
||||
.castTo(Type.FLOAT).getResultValue(false)).getLongValue());
|
||||
Assert.assertEquals(20200101123445L, ((FloatLiteral) new CastExpr(Type.FLOAT, srcDateTime)
|
||||
.castTo(Type.FLOAT).getResultValue(false)).getLongValue());
|
||||
|
||||
// float
|
||||
FloatLiteral floatLiteral = new FloatLiteral(0.1, Type.FLOAT);
|
||||
Assert.assertEquals(floatLiteral.getType(), Type.FLOAT);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
-- This file is automatically generated. You should know what you did if you want to edit this
|
||||
-- !dateTimeOperatorsAccessible --
|
||||
2012-08-10T00:00 2012-08-09T06:00 2012-11-30T01:00 2012-08-06T00:00 2012-08-06T20:00 2012-09-30T01:00
|
||||
2012-08-10 2012-08-09T06:00 2012-11-30T01:00 2012-08-06 2012-08-06T20:00 2012-09-30T01:00
|
||||
|
||||
|
||||
34
regression-test/suites/query_p0/cast/test_cast.groovy
Normal file
34
regression-test/suites/query_p0/cast/test_cast.groovy
Normal file
@ -0,0 +1,34 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
suite('test_cast') {
|
||||
def date = "date '2020-01-01'"
|
||||
def datev2 = "datev2 '2020-01-01'"
|
||||
def datetime = "timestamp '2020-01-01 12:34:45'"
|
||||
test {
|
||||
sql "select cast(${date} as int), cast(${date} as bigint), cast(${date} as float), cast(${date} as double)"
|
||||
result([[20200101, 20200101l, ((float) 20200101), ((double) 20200101)]])
|
||||
}
|
||||
test {
|
||||
sql "select cast(${datev2} as int), cast(${datev2} as bigint), cast(${datev2} as float), cast(${datev2} as double)"
|
||||
result([[20200101, 20200101l, ((float) 20200101), ((double) 20200101)]])
|
||||
}
|
||||
test {
|
||||
sql "select cast(${datetime} as int), cast(${datetime} as bigint), cast(${datetime} as float), cast(${datetime} as double)"
|
||||
result([[869930357, 20200101123445l, ((float) 20200101123445l), ((double) 20200101123445l)]])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user