Skip to content

Commit

Permalink
[fix](Nereids) fix fold constant of time acquired functions (#47288)
Browse files Browse the repository at this point in the history
Problem Summary:
explain select substr(current_date, 1, 10);
when logicalPlanBuilder build ast from original sql of date acquired
functions like current_date, it would add an alias above. Which would
stop folding constant when fold constant rule traversing expression tree
So remove alias when translate to ast
  • Loading branch information
LiBinfeng-01 authored Jan 22, 2025
1 parent 2e61451 commit c9cac8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2387,37 +2387,37 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) {

@Override
public Expression visitCurrentDate(DorisParser.CurrentDateContext ctx) {
return new CurrentDate().alias("CURRENT_DATE");
return new CurrentDate();
}

@Override
public Expression visitCurrentTime(DorisParser.CurrentTimeContext ctx) {
return new CurrentTime().alias("CURRENT_TIME");
return new CurrentTime();
}

@Override
public Expression visitCurrentTimestamp(DorisParser.CurrentTimestampContext ctx) {
return new Now().alias("CURRENT_TIMESTAMP");
return new Now();
}

@Override
public Expression visitLocalTime(DorisParser.LocalTimeContext ctx) {
return new CurrentTime().alias("LOCALTIME");
return new CurrentTime();
}

@Override
public Expression visitLocalTimestamp(DorisParser.LocalTimestampContext ctx) {
return new Now().alias("LOCALTIMESTAMP");
return new Now();
}

@Override
public Expression visitCurrentUser(DorisParser.CurrentUserContext ctx) {
return new CurrentUser().alias("CURRENT_USER");
return new CurrentUser();
}

@Override
public Expression visitSessionUser(DorisParser.SessionUserContext ctx) {
return new SessionUser().alias("SESSION_USER");
return new SessionUser();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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("fold_constant_date_arithmatic") {
def db = "fold_constant_date_arithmatic"
sql "create database if not exists ${db}"

sql "set enable_nereids_planner=true"
sql "set enable_fallback_to_original_planner=false"
sql "set enable_fold_constant_by_be=false"

testFoldConst("select substr(now(), 1, 10);")
testFoldConst("select substr(now(3), 1, 10);")
testFoldConst("select substr(curdate(), 1, 10);")
testFoldConst("select substr(current_date(), 1, 10);")
testFoldConst("select substr(current_timestamp(), 1, 10);")
testFoldConst("select substr(current_timestamp(3), 1, 10);")
}

0 comments on commit c9cac8f

Please sign in to comment.