Skip to content

Commit

Permalink
add case
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangstar333 committed Aug 4, 2023
1 parent 04c46bb commit 66461e5
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1537,16 +1537,17 @@ && collectChildReturnTypes()[0].isDecimalV3()) {
}
}
if (fnName.getFunction().equalsIgnoreCase("date_trunc")) {
if ((children.size() != 2) || (getChild(1).isConstant() == false) || !(getChild(1) instanceof StringLiteral)) {
if ((children.size() != 2) || (getChild(1).isConstant() == false)
|| !(getChild(1) instanceof StringLiteral)) {
throw new AnalysisException(
fnName.getFunction() + " needs two params, and the second is must be a string constant: " + this
.toSql());
fnName.getFunction() + " needs two params, and the second is must be a string constant: "
+ this.toSql());
}
final String constParam = ((StringLiteral) getChild(1)).getValue().toLowerCase();
if (!Lists.newArrayList("year", "quarter", "month", "week", "day", "hour", "minute", "second")
.contains(constParam)) {
throw new AnalysisException("date_trunc function second param only support argument is " +
"year|quarter|month|week|day|hour|minute|second");
throw new AnalysisException("date_trunc function second param only support argument is "
+ "year|quarter|month|week|day|hour|minute|second");
}
}
if (fnName.getFunction().equalsIgnoreCase("char")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.util.List;

/**
Expand Down Expand Up @@ -62,8 +63,8 @@ public void checkLegalityBeforeTypeCoercion() {
final String constParam = ((VarcharLiteral) getArgument(1)).getStringValue().toLowerCase();
if (!Lists.newArrayList("year", "quarter", "month", "week", "day", "hour", "minute", "second")
.contains(constParam)) {
throw new AnalysisException("date_trunc function second param only support argument is" +
"year|quarter|month|week|day|hour|minute|second");
throw new AnalysisException("date_trunc function second param only support argument is"
+ "year|quarter|month|week|day|hour|minute|second");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_date_trunc_second --
1989-03-21T13:00
1989-03-21T13:00
2015-03-13T10:30
2015-03-13T12:36:38
2015-04-02T00:00
2000-01-01T00:00
1901-01-01T00:00
9999-11-11T12:12
1989-03-21T13:11
1989-03-21T13:11
2015-04-02T00:00
2015-04-02T00:00
2015-03-13T12:36:38
\N
2013-04-02T15:16:52
2013-04-02T15:16:52

-- !select_date_trunc_minute --
\N
2015-03-13T12:36
2000-01-01T00:00
1901-01-01T00:00
9999-11-11T12:12
1989-03-21T13:11
1989-03-21T13:11
2015-04-02T00:00
2015-04-02T00:00
1989-03-21T13:00
1989-03-21T13:00
2015-03-13T10:30
2015-03-13T12:36
2015-04-02T00:00
2013-04-02T15:16
2013-04-02T15:16

-- !select_date_trunc_hour --
\N
2000-01-01T00:00
1901-01-01T00:00
9999-11-11T12:00
1989-03-21T13:00
1989-03-21T13:00
2015-04-02T00:00
2015-04-02T00:00
2013-04-02T15:00
2013-04-02T15:00
2015-03-13T12:00
1989-03-21T13:00
1989-03-21T13:00
2015-03-13T10:00
2015-03-13T12:00
2015-04-02T00:00

-- !select_date_trunc_day --
2013-04-02T00:00
2013-04-02T00:00
2015-03-13T00:00
1989-03-21T00:00
1989-03-21T00:00
2015-03-13T00:00
2015-03-13T00:00
2015-04-02T00:00
\N
2000-01-01T00:00
1901-01-01T00:00
9999-11-11T00:00
1989-03-21T00:00
1989-03-21T00:00
2015-04-02T00:00
2015-04-02T00:00

-- !select_date_trunc_week --
2015-03-09T00:00
\N
2013-04-01T00:00
2013-04-01T00:00
1999-12-27T00:00
1900-12-31T00:00
9999-11-08T00:00
1989-03-20T00:00
1989-03-20T00:00
2015-03-30T00:00
2015-03-30T00:00
1989-03-20T00:00
1989-03-20T00:00
2015-03-09T00:00
2015-03-09T00:00
2015-03-30T00:00

-- !select_date_trunc_month --
2000-01-01T00:00
1901-01-01T00:00
9999-11-01T00:00
1989-03-01T00:00
1989-03-01T00:00
2015-04-01T00:00
2015-04-01T00:00
\N
2013-04-01T00:00
2013-04-01T00:00
2015-03-01T00:00
1989-03-01T00:00
1989-03-01T00:00
2015-03-01T00:00
2015-03-01T00:00
2015-04-01T00:00

-- !select_date_trunc_quarter --
\N
2015-01-01T00:00
2013-04-01T00:00
2013-04-01T00:00
1989-01-01T00:00
1989-01-01T00:00
2015-01-01T00:00
2015-01-01T00:00
2015-04-01T00:00
2000-01-01T00:00
1901-01-01T00:00
9999-10-01T00:00
1989-01-01T00:00
1989-01-01T00:00
2015-04-01T00:00
2015-04-01T00:00

-- !select_date_trunc_year --
2013-01-01T00:00
2013-01-01T00:00
1989-01-01T00:00
1989-01-01T00:00
2015-01-01T00:00
2015-01-01T00:00
2015-01-01T00:00
2015-01-01T00:00
\N
2000-01-01T00:00
1901-01-01T00:00
9999-01-01T00:00
1989-01-01T00:00
1989-01-01T00:00
2015-01-01T00:00
2015-01-01T00:00

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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_date_trunc") {

def dbName = "test_date_trunc"
sql "DROP DATABASE IF EXISTS ${dbName}"
sql "CREATE DATABASE ${dbName}"
sql "USE $dbName"

sql """
CREATE TABLE IF NOT EXISTS `baseall` (
`k0` boolean null comment "",
`k1` tinyint(4) null comment "",
`k2` smallint(6) null comment "",
`k3` int(11) null comment "",
`k4` bigint(20) null comment "",
`k5` decimal(9, 3) null comment "",
`k6` char(5) null comment "",
`k10` date null comment "",
`k11` datetime null comment "",
`k7` varchar(20) null comment "",
`k8` double max null comment "",
`k9` float sum null comment "",
`k12` string replace null comment "",
`k13` largeint(40) replace null comment ""
) engine=olap
DISTRIBUTED BY HASH(`k1`) BUCKETS 5 properties("replication_num" = "1")
"""

streamLoad {
table "baseall"
db dbName
set 'column_separator', ','
file "../../baseall.txt"
}

sql "sync"

qt_select_date_trunc_second """ SELECT date_trunc(k11,'SECoND') FROM baseall """
qt_select_date_trunc_minute """ SELECT date_trunc(k11,'MINutE') FROM baseall """
qt_select_date_trunc_hour """ SELECT date_trunc(k11,'Hour') FROM baseall """
qt_select_date_trunc_day """ SELECT date_trunc(k11,'DAY') FROM baseall """
qt_select_date_trunc_week """ SELECT date_trunc(k11,'Week') FROM baseall """
qt_select_date_trunc_month """ SELECT date_trunc(k11,'MONTH') FROM baseall """
qt_select_date_trunc_quarter """ SELECT date_trunc(k11,'quarter') FROM baseall """
qt_select_date_trunc_year """ SELECT date_trunc(k11,'YeaR') FROM baseall """

try {
sql """ SELECT date_trunc(k11,k7) FROM baseall ; """
} catch (Exception e) {
assertTrue(e.getMessage().contains("must be a string constant"), e.getMessage())
}

try {
sql """ SELECT date_trunc(k11,'AAAA') FROM baseall ; """
} catch (Exception e) {
assertTrue(e.getMessage().contains("param only support argument"), e.getMessage())
}
}

0 comments on commit 66461e5

Please sign in to comment.