Skip to content

Commit

Permalink
[Enhancement] Add E as default value function for doris (apache#40209)
Browse files Browse the repository at this point in the history
## Proposed changes

Issue Number: close apache#34228

add e as default value
  • Loading branch information
Vallishp authored Sep 11, 2024
1 parent d0cb128 commit 4f1d566
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ DROPP: 'DROPP';
DUAL: 'DUAL';
DUPLICATE: 'DUPLICATE';
DYNAMIC: 'DYNAMIC';
E:'E';
ELSE: 'ELSE';
ENABLE: 'ENABLE';
ENCRYPTKEY: 'ENCRYPTKEY';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ columnDef
((GENERATED ALWAYS)? AS LEFT_PAREN generatedExpr=expression RIGHT_PAREN)?
((NOT)? nullable=NULL)?
(AUTO_INCREMENT (LEFT_PAREN autoIncInitValue=number RIGHT_PAREN)?)?
(DEFAULT (nullValue=NULL | INTEGER_VALUE | DECIMAL_VALUE | PI | stringValue=STRING_LITERAL
(DEFAULT (nullValue=NULL | INTEGER_VALUE | DECIMAL_VALUE | PI | E | 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 Expand Up @@ -1866,6 +1866,7 @@ nonReserved
| DORIS_INTERNAL_TABLE_ID
| DUAL
| DYNAMIC
| E
| ENABLE
| ENCRYPTKEY
| ENCRYPTKEYS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ non_reserved_words : // Tokens that are not reserved words
| DIR
| DIRECTORY
| DISTRIBUTE
| E
| ESCAPED
| EXEC
| EXCEPTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public DefaultValue(boolean isSet, String value, String exprName, Long precision
this.defaultValueExprDef = new DefaultValueExprDef(exprName, precision);
}

public static String E_NUM = "E";
public static String PI = "PI";
public static String CURRENT_DATE = "CURRENT_DATE";
// default "CURRENT_TIMESTAMP", only for DATETIME type
Expand Down Expand Up @@ -535,6 +536,14 @@ public static void validateDefaultValue(Type type, String defaultValue, DefaultV
default:
throw new AnalysisException("Types other than DOUBLE cannot use pi as the default value");
}
} else if (null != defaultValueExprDef
&& defaultValueExprDef.getExprName().equalsIgnoreCase(DefaultValue.E_NUM)) {
switch (primitiveType) {
case DOUBLE:
break;
default:
throw new AnalysisException("Types other than DOUBLE cannot use e as the default value");
}
}
switch (primitiveType) {
case TINYINT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,8 @@ public ColumnDefinition visitColumnDef(ColumnDefContext ctx) {
defaultValue = Optional.of(DefaultValue.CURRENT_DATE_DEFAULT_VALUE);
} else if (ctx.PI() != null) {
defaultValue = Optional.of(DefaultValue.PI_DEFAULT_VALUE);
} else if (ctx.E() != null) {
defaultValue = Optional.of(DefaultValue.E_NUM_DEFAULT_VALUE);
}
}
if (ctx.UPDATE() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* default value of a column.
*/
public class DefaultValue {

public static String E_NUM = "E";
public static String PI = "PI";
public static String CURRENT_DATE = "CURRENT_DATE";
public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
Expand All @@ -41,11 +43,10 @@ public class DefaultValue {
public static DefaultValue BITMAP_EMPTY_DEFAULT_VALUE = new DefaultValue(ZERO);
// default "value", "[]" means empty array
public static DefaultValue ARRAY_EMPTY_DEFAULT_VALUE = new DefaultValue("[]");
// default "value", "3.14159265358979323846" means pi, refer to M_PI in <math.h>.
// M_PI is enough for type double because the precision of type double is 15~16 digits.
// but it is not adequate for computation using long double. in this case, use M_PIl in <math.h>.
public static DefaultValue PI_DEFAULT_VALUE = new DefaultValue("3.14159265358979323846", PI);
// Lets use the const value from math pacakge.
public static DefaultValue PI_DEFAULT_VALUE = new DefaultValue(Double.toString(Math.PI), PI);

public static DefaultValue E_NUM_DEFAULT_VALUE = new DefaultValue(Double.toString(Math.E), E_NUM);
private final String value;
// used for column which defaultValue is an expression.
private final DefaultValueExprDef defaultValueExprDef;
Expand Down
13 changes: 13 additions & 0 deletions regression-test/data/correctness_p0/test_default_e.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 --
1 2.718281828459045 1
2 2.718281828459045 2
3 2.718281828459045 3
4 2.718281828459045 4

-- !select_1 --
1 2.718281828459045 1
2 2.718281828459045 2
3 2.718281828459045 3
4 2.718281828459045 4

69 changes: 69 additions & 0 deletions regression-test/suites/correctness_p0/test_default_e.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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_default_e") {
def tableName = "test_default_e"

sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName}
(
k TINYINT,
v1 DOUBLE NOT NULL DEFAULT E,
v2 INT
)
UNIQUE KEY(K)
DISTRIBUTED BY HASH(k)
PROPERTIES("replication_num" = "1");
"""

// test insert into.
sql " insert into ${tableName} (k, v2) values (1, 1); "
sql " insert into ${tableName} (k, v2) values (2, 2); "
sql " insert into ${tableName} (k, v2) values (3, 3); "
sql " insert into ${tableName} (k, v2) values (4, 4); "
sql "sync"
qt_insert_into1 """ select * from ${tableName} order by k; """

qt_select_1 "select * from ${tableName} order by k;"


// test varchar with default e
sql "DROP TABLE IF EXISTS test_varchar_default"
test {
sql """create table test_varchar_default(a int, b varchar(100) default e)
distributed by hash(a) properties('replication_num'="1");"""
exception "Types other than DOUBLE cannot use e as the default value"
}

// test int with default e
sql "DROP TABLE IF EXISTS test_int_default"
test {
sql """create table test_int_default(a int, b int default e)
distributed by hash(a) properties('replication_num'="1");"""
exception "Types other than DOUBLE cannot use e as the default value"
}

// test float with default e
sql "DROP TABLE IF EXISTS test_double_default"
test {
sql """create table test_int_default(a int, b float default e)
distributed by hash(a) properties('replication_num'="1");"""
exception "Types other than DOUBLE cannot use e as the default value"
}

}

0 comments on commit 4f1d566

Please sign in to comment.