This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 543576227db [Featrue](default value) add pi as default value (#36280)
543576227db is described below

commit 543576227db1521e66c2d32a6fa522c1a7a7aa61
Author: Liu Zhenlong <49094455+dragonliu2...@users.noreply.github.com>
AuthorDate: Wed Jun 26 20:21:34 2024 +0800

    [Featrue](default value) add pi as default value (#36280)
    
    ## Proposed changes
    
    Issue Number: refers https://github.com/apache/doris/issues/34228
    
    <!--Describe your changes.-->
    add pi as default value, such as:
    ```sql
      CREATE TABLE IF NOT EXISTS t1
      (
          k TINYINT,
          v double not null DEFAULT PI
      )
      UNIQUE KEY(k)
      DISTRIBUTED BY HASH(k)
      PROPERTIES("replication_num" = "1");
    ```
---
 .../antlr4/org/apache/doris/nereids/DorisLexer.g4  |   1 +
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |   3 +-
 .../antlr4/org/apache/doris/nereids/PLParser.g4    |   1 +
 .../java/org/apache/doris/analysis/ColumnDef.java  |   9 ++
 .../doris/nereids/parser/LogicalPlanBuilder.java   |   2 +
 .../trees/plans/commands/info/DefaultValue.java    |   5 +
 .../data/correctness_p0/test_default_pi.out        |  28 +++++
 .../correctness_p0/test_default_pi_streamload.csv  |   2 +
 .../suites/correctness_p0/test_default_pi.groovy   | 123 +++++++++++++++++++++
 9 files changed, 173 insertions(+), 1 deletion(-)

diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
index 9971d277ebe..5ed655c212a 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
@@ -415,6 +415,7 @@ PERCENT: 'PERCENT';
 PERIOD: 'PERIOD';
 PERMISSIVE: 'PERMISSIVE';
 PHYSICAL: 'PHYSICAL';
+PI: 'PI';
 PLACEHOLDER: '?';
 PLAN: 'PLAN';
 PRIVILEGES: 'PRIVILEGES';
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
index 68da90f9da1..ee9b91fdf6f 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
@@ -588,7 +588,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 | 
stringValue=STRING_LITERAL
+        (DEFAULT (nullValue=NULL | INTEGER_VALUE | DECIMAL_VALUE | PI | 
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)?
@@ -1237,6 +1237,7 @@ nonReserved
     | PERIOD
     | PERMISSIVE
     | PHYSICAL
+    | PI
     | PLAN
     | PLUGIN
     | PLUGINS
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/PLParser.g4 
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/PLParser.g4
index f8dc6039145..e967da2f955 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/PLParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/PLParser.g4
@@ -884,6 +884,7 @@ non_reserved_words :                      // Tokens that 
are not reserved words
      | PART_LOC
      | PCTFREE
      | PCTUSED
+     | PI
      | PRECISION
      | PRESERVE
      | PRINT
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
index f306bb782fe..e01a4f11793 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java
@@ -99,6 +99,7 @@ public class ColumnDef {
             this.defaultValueExprDef = new DefaultValueExprDef(exprName, 
precision);
         }
 
+        public static String PI = "PI";
         public static String CURRENT_DATE = "CURRENT_DATE";
         // default "CURRENT_TIMESTAMP", only for DATETIME type
         public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
@@ -526,6 +527,14 @@ public class ColumnDef {
                     throw new AnalysisException("Types other than DATE and 
DATEV2 "
                             + "cannot use current_date as the default value");
             }
+        } else if (null != defaultValueExprDef
+                && 
defaultValueExprDef.getExprName().equalsIgnoreCase(DefaultValue.PI)) {
+            switch (primitiveType) {
+                case DOUBLE:
+                    break;
+                default:
+                    throw new AnalysisException("Types other than DOUBLE 
cannot use pi as the default value");
+            }
         }
         switch (primitiveType) {
             case TINYINT:
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index f092701fce6..d9a0a49753d 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -2712,6 +2712,8 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
                 }
             } else if (ctx.CURRENT_DATE() != null) {
                 defaultValue = 
Optional.of(DefaultValue.CURRENT_DATE_DEFAULT_VALUE);
+            } else if (ctx.PI() != null) {
+                defaultValue = Optional.of(DefaultValue.PI_DEFAULT_VALUE);
             }
         }
         if (ctx.UPDATE() != null) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/DefaultValue.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/DefaultValue.java
index 48a22daf7b7..6df1d3d7cf1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/DefaultValue.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/DefaultValue.java
@@ -24,6 +24,7 @@ import org.apache.doris.catalog.ScalarType;
  * default value of a column.
  */
 public class DefaultValue {
+    public static String PI = "PI";
     public static String CURRENT_DATE = "CURRENT_DATE";
     public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
     public static String NOW = "now";
@@ -40,6 +41,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);
 
     private final String value;
     // used for column which defaultValue is an expression.
diff --git a/regression-test/data/correctness_p0/test_default_pi.out 
b/regression-test/data/correctness_p0/test_default_pi.out
new file mode 100644
index 00000000000..4b9a36fa12f
--- /dev/null
+++ b/regression-test/data/correctness_p0/test_default_pi.out
@@ -0,0 +1,28 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !insert_into1 --
+1      3.141592653589793       1
+2      3.141592653589793       2
+3      3.141592653589793       3
+4      3.141592653589793       4
+
+-- !stream_load_csv1 --
+1      3.141592653589793       1
+2      3.141592653589793       2
+3      3.141592653589793       3
+4      3.141592653589793       4
+5      3.141592653589793       5
+6      3.141592653589793       6
+
+-- !select_1 --
+1      3.141592653589793       1
+2      3.141592653589793       2
+3      3.141592653589793       3
+4      3.141592653589793       4
+
+-- !stream_load_csv1 --
+1      3.141592653589793       1
+2      3.141592653589793       2
+3      3.141592653589793       3
+4      3.141592653589793       4
+5      3.141592653589793       5
+6      3.141592653589793       6
diff --git a/regression-test/data/correctness_p0/test_default_pi_streamload.csv 
b/regression-test/data/correctness_p0/test_default_pi_streamload.csv
new file mode 100644
index 00000000000..a4f37673aa0
--- /dev/null
+++ b/regression-test/data/correctness_p0/test_default_pi_streamload.csv
@@ -0,0 +1,2 @@
+5,5
+6,6
diff --git a/regression-test/suites/correctness_p0/test_default_pi.groovy 
b/regression-test/suites/correctness_p0/test_default_pi.groovy
new file mode 100644
index 00000000000..47333b6a3ad
--- /dev/null
+++ b/regression-test/suites/correctness_p0/test_default_pi.groovy
@@ -0,0 +1,123 @@
+// 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_pi") {
+    def tableName = "test_default_pi"
+
+    sql """ DROP TABLE IF EXISTS ${tableName} """
+    sql """
+        CREATE TABLE IF NOT EXISTS ${tableName}
+        (
+            k TINYINT,
+            v1 DOUBLE NOT NULL DEFAULT PI,
+            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; """
+
+    // test csv stream load.
+    streamLoad {
+        table "${tableName}"
+
+        set 'column_separator', ','
+        set 'columns', 'k, v1=pi(), v2'
+
+        file 'test_default_pi_streamload.csv'
+
+        time 10000 // limit inflight 10s
+    }
+
+    sql "sync"
+
+    qt_stream_load_csv1 """ select * from ${tableName} order by k; """
+
+    // test partial update
+    sql """ DROP TABLE IF EXISTS ${tableName} """
+    sql """
+        CREATE TABLE IF NOT EXISTS ${tableName}
+        (
+            k TINYINT,
+            v1 DOUBLE NOT NULL DEFAULT PI,
+            v2 INT
+        )
+        UNIQUE KEY(K)
+        DISTRIBUTED BY HASH(k)
+        PROPERTIES("replication_num" = "1");
+    """
+
+    sql "set enable_unique_key_partial_update=true;"
+    sql "set enable_insert_strict=false;"
+
+    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_select_1 "select * from ${tableName} order by k;"
+
+    streamLoad {
+        table "${tableName}"
+
+        set 'partial_columns', 'true'
+        set 'column_separator', ','
+        set 'columns', 'k, v1=pi(), v2'
+
+        file 'test_default_pi_streamload.csv'
+
+        time 10000 // limit inflight 10s
+    }
+
+    sql "sync"
+
+    qt_stream_load_csv1 """ select * from ${tableName} order by k; """
+
+    // test varchar with default pi
+    sql "DROP TABLE IF EXISTS test_varchar_default"
+    test {
+        sql """create table test_varchar_default(a int, b varchar(100) default 
pi)
+        distributed by hash(a) properties('replication_num'="1");"""
+        exception "Types other than DOUBLE cannot use pi as the default value"
+    }
+
+    // test int with default pi
+    sql "DROP TABLE IF EXISTS test_int_default"
+    test {
+        sql """create table test_int_default(a int, b int default pi)
+        distributed by hash(a) properties('replication_num'="1");"""
+        exception "Types other than DOUBLE cannot use pi as the default value"
+    }
+
+    // test float with default pi
+    sql "DROP TABLE IF EXISTS test_double_default"
+    test {
+        sql """create table test_int_default(a int, b float default pi)
+        distributed by hash(a) properties('replication_num'="1");"""
+        exception "Types other than DOUBLE cannot use pi as the default value"
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to