[Fix](planner) fix to_date failed in create table as select (#23613)

Problem:
when create table as select using to_date function, it would failed

Example:
create table test_to_date properties('replication_num' = '1') as select to_date('20230816') as datev2;

Reason:
after release version 2.0, datev1 is disabled, but to_date function signature does not upgrade, so it failed when checking return type of to_date

Solved:
when getfunction, forbidden to_date with return type date_v1, datetime v1 also changed to datetime v2 and decimal v2 changed to decimal v3
This commit is contained in:
LiBinfeng
2023-09-01 17:28:40 +08:00
committed by GitHub
parent b5232ce0d7
commit e3bbba82cf
2 changed files with 28 additions and 0 deletions

View File

@ -17,6 +17,9 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.Config;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
@ -82,6 +85,23 @@ public class CreateTableAsSelectStmt extends DdlStmt {
queryStmt.getResultExprs().get(i).getSrcSlotRef().getColumn()
.setIsAllowNull(outputs.get(i).isNullable());
}
if (Config.enable_date_conversion) {
if (queryStmt.getResultExprs().get(i).getType() == Type.DATE) {
Expr castExpr = queryStmt.getResultExprs().get(i).castTo(Type.DATEV2);
queryStmt.getResultExprs().set(i, castExpr);
}
if (queryStmt.getResultExprs().get(i).getType() == Type.DATETIME) {
Expr castExpr = queryStmt.getResultExprs().get(i).castTo(Type.DATETIMEV2);
queryStmt.getResultExprs().set(i, castExpr);
}
}
if (Config.enable_decimal_conversion && queryStmt.getResultExprs().get(i).getType().isDecimalV2()) {
int precision = queryStmt.getResultExprs().get(i).getType().getPrecision();
int scalar = queryStmt.getResultExprs().get(i).getType().getDecimalDigits();
Expr castExpr = queryStmt.getResultExprs().get(i)
.castTo(ScalarType.createDecimalV3Type(precision, scalar));
queryStmt.getResultExprs().set(i, castExpr);
}
}
ArrayList<Expr> resultExprs = getQueryStmt().getResultExprs();
if (columnNames != null && columnNames.size() != resultExprs.size()) {

View File

@ -234,10 +234,18 @@ suite("test_ctas") {
String desc = sql 'desc c'
assertTrue(desc.contains('Yes'))
sql '''create table test_date_v2
properties (
"replication_num"="1"
) as select to_date('20250829');
'''
desc = sql 'desc test_date_v2'
assertTrue(desc.contains('Yes'))
} finally {
sql 'drop table a'
sql 'drop table b'
sql 'drop table c'
sql 'drop table test_date_v2'
}
}