Skip to content

Commit

Permalink
[Feat](nereids) support column default value current_date (apache#32268)
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei authored Mar 15, 2024
1 parent 52eebc3 commit f359124
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ columnDef
(aggType=aggTypeDef)?
((NOT)? NULL)?
(AUTO_INCREMENT (LEFT_PAREN autoIncInitValue=number RIGHT_PAREN)?)?
(DEFAULT (nullValue=NULL | INTEGER_VALUE | stringValue=STRING_LITERAL
| CURRENT_TIMESTAMP (LEFT_PAREN defaultValuePrecision=number RIGHT_PAREN)?))?
(DEFAULT (nullValue=NULL | INTEGER_VALUE | stringValue=STRING_LITERAL| CURRENT_DATE
| defaultTimestamp=CURRENT_TIMESTAMP (LEFT_PAREN defaultValuePrecision=number RIGHT_PAREN)?))?
(ON UPDATE CURRENT_TIMESTAMP (LEFT_PAREN onUpdateValuePrecision=number RIGHT_PAREN)?)?
(COMMENT comment=STRING_LITERAL)?
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public DefaultValue(boolean isSet, String value, String exprName, Long precision
this.defaultValueExprDef = new DefaultValueExprDef(exprName, precision);
}

public static String CURRENT_DATE = "CURRENT_DATE";
// default "CURRENT_TIMESTAMP", only for DATETIME type
public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
public static String NOW = "now";
Expand Down Expand Up @@ -466,7 +467,15 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV
break;
case DATE:
case DATEV2:
new DateLiteral(defaultValue, scalarType);
if (defaultValueExprDef == null) {
new DateLiteral(defaultValue, scalarType);
} else {
if (defaultValueExprDef.getExprName().equalsIgnoreCase(DefaultValue.CURRENT_DATE)) {
break;
} else {
throw new AnalysisException("date literal [" + defaultValue + "] is invalid");
}
}
break;
case DATETIME:
case DATETIMEV2:
Expand Down Expand Up @@ -520,6 +529,16 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV
throw new AnalysisException("Types other than DATETIME and DATETIMEV2 "
+ "cannot use current_timestamp as the default value");
}
} else if (null != defaultValueExprDef
&& defaultValueExprDef.getExprName().equals(DefaultValue.CURRENT_DATE.toLowerCase())) {
switch (primitiveType) {
case DATE:
case DATEV2:
break;
default:
throw new AnalysisException("Types other than DATE and DATEV2 "
+ "cannot use current_date as the default value");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2550,14 +2550,16 @@ public ColumnDefinition visitColumnDef(ColumnDefContext ctx) {
defaultValue = Optional.of(new DefaultValue(toStringValue(ctx.stringValue.getText())));
} else if (ctx.nullValue != null) {
defaultValue = Optional.of(DefaultValue.NULL_DEFAULT_VALUE);
} else if (ctx.CURRENT_TIMESTAMP() != null) {
} else if (ctx.defaultTimestamp != null) {
if (ctx.defaultValuePrecision == null) {
defaultValue = Optional.of(DefaultValue.CURRENT_TIMESTAMP_DEFAULT_VALUE);
} else {
defaultValue = Optional.of(DefaultValue
.currentTimeStampDefaultValueWithPrecision(
Long.valueOf(ctx.defaultValuePrecision.getText())));
}
} else if (ctx.CURRENT_DATE() != null) {
defaultValue = Optional.of(DefaultValue.CURRENT_DATE_DEFAULT_VALUE);
}
}
if (ctx.UPDATE() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
* default value of a column.
*/
public class DefaultValue {
public static String CURRENT_DATE = "CURRENT_DATE";
public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
public static String NOW = "now";
public static DefaultValue CURRENT_DATE_DEFAULT_VALUE = new DefaultValue(CURRENT_DATE, CURRENT_DATE.toLowerCase());
public static DefaultValue CURRENT_TIMESTAMP_DEFAULT_VALUE = new DefaultValue(CURRENT_TIMESTAMP, NOW);
// default null
public static DefaultValue NULL_DEFAULT_VALUE = new DefaultValue(null);
Expand Down
13 changes: 13 additions & 0 deletions regression-test/data/correctness_p0/test_current_date.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !insert_into1 --
4

-- !insert_into2 --
4

-- !stream_load_csv1 --
5

-- !stream_load_csv2 --
5

75 changes: 75 additions & 0 deletions regression-test/suites/correctness_p0/test_current_date.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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_current_date") {
sql "SET enable_nereids_planner=true"
sql "SET enable_fallback_to_original_planner=false"
def tableName = "test_current_date"

sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName}
(
id TINYINT,
name CHAR(10) NOT NULL DEFAULT "zs",
dt_0 DATE,
dt_2 DATEV2,
dt_1 DATE DEFAULT current_date,
dt_3 DATEV2 DEFAULT current_date,
)
COMMENT "test current_date table"
DISTRIBUTED BY HASH(id)
PROPERTIES("replication_num" = "1");
"""

// test insert into.
sql " insert into ${tableName} (id,name,dt_0,dt_2) values (1,'aa',current_date(),current_date()); "
sql " insert into ${tableName} (id,name,dt_0,dt_2) values (2,'bb',current_date(),current_date()); "
sql " insert into ${tableName} (id,name,dt_0,dt_2) values (3,'cc',current_date(),current_date()); "
sql " insert into ${tableName} (id,name,dt_0,dt_2) values (4,'dd',current_date(),current_date()); "
sql "sync"
qt_insert_into1 """ select count(*) from ${tableName} where dt_0 = dt_1; """
qt_insert_into2 """ select count(*) from ${tableName} where dt_2 = dt_3; """

sql """select now()"""

// test csv stream load.
streamLoad {
table "${tableName}"

set 'column_separator', ','
set 'columns', 'id, name, dt_0 = current_date(), dt_2 = current_date()'

file 'test_current_timestamp_streamload.csv'

time 10000 // limit inflight 10s
}

sql "sync"

qt_stream_load_csv1 """ select count(*) from ${tableName} where id > 4 and dt_0 = dt_1; """
qt_stream_load_csv2 """ select count(*) from ${tableName} where id > 4 and dt_2 = dt_3; """

sql "DROP TABLE IF EXISTS test_default10"
test {
sql """create table test_default10(a int, b varchar(100) default current_date)
distributed by hash(a) properties('replication_num'="1");"""
exception "Types other than DATE and DATEV2 cannot use current_date as the default value"
}

}

0 comments on commit f359124

Please sign in to comment.