Skip to content

Commit

Permalink
Merge pull request #8154 from sundy-li/timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Oct 12, 2022
2 parents ad02037 + 04cc8a3 commit 62bf8a2
Show file tree
Hide file tree
Showing 57 changed files with 348 additions and 432 deletions.
108 changes: 54 additions & 54 deletions docs/doc/30-reference/10-data-types/20-data-type-time-date-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ See [Date & Time Functions](/doc/reference/functions/datetime-functions).
## Example

```sql
CREATE TABLE test_dt
(
date DATE,
ts TIMESTAMP
);
CREATE TABLE test_dt
(
date DATE,
ts TIMESTAMP
);

DESC test_dt;
+-------+--------------+------+---------+-------+
| Field | Type | Null | Default | Extra |
+-------+--------------+------+---------+-------+
| date | DATE | NO | 0 | |
| ts | TIMESTAMP(6) | NO | 0 | |
| ts | TIMESTAMP | NO | 0 | |
+-------+--------------+------+---------+-------+

-- A TIMESTAMP value can optionally include a trailing fractional seconds part in up to microseconds (6 digits) precision.

INSERT INTO test_dt
VALUES ('2022-04-07',
'2022-04-07 01:01:01.123456'),
('2022-04-08',
'2022-04-08 01:01:01');
INSERT INTO test_dt
VALUES ('2022-04-07',
'2022-04-07 01:01:01.123456'),
('2022-04-08',
'2022-04-08 01:01:01');

SELECT *
FROM test_dt;
SELECT *
FROM test_dt;
+------------+----------------------------+
| date | ts |
+------------+----------------------------+
Expand All @@ -50,24 +50,24 @@ FROM test_dt;

-- Databend recognizes TIMESTAMP values in several formats.

CREATE TABLE test_formats
(
id INT,
a TIMESTAMP
);

INSERT INTO test_formats
VALUES (1,
'2022-01-01 02:00:11'),
(2,
'2022-01-02T02:00:22'),
(3,
'2022-02-02T04:00:03+00:00'),
(4,
'2022-02-03');

SELECT *
FROM test_formats;
CREATE TABLE test_formats
(
id INT,
a TIMESTAMP
);

INSERT INTO test_formats
VALUES (1,
'2022-01-01 02:00:11'),
(2,
'2022-01-02T02:00:22'),
(3,
'2022-02-02T04:00:03+00:00'),
(4,
'2022-02-03');

SELECT *
FROM test_formats;

----
1 2022-01-01 02:00:11.000000
Expand All @@ -77,30 +77,30 @@ FROM test_formats;

-- Databend automatically adjusts and shows TIMESTAMP values based on your current timezone.

CREATE TABLE test_tz
(
id INT,
t TIMESTAMP
);
CREATE TABLE test_tz
(
id INT,
t TIMESTAMP
);

SET timezone='UTC';

INSERT INTO test_tz
VALUES (1,
'2022-02-03T03:00:00'),
(2,
'2022-02-03T03:00:00+08:00'),
(3,
'2022-02-03T03:00:00-08:00'),
(4,
'2022-02-03'),
(5,
'2022-02-03T03:00:00+09:00'),
(6,
'2022-02-03T03:00:00+06:00');

SELECT *
FROM test_tz;
INSERT INTO test_tz
VALUES (1,
'2022-02-03T03:00:00'),
(2,
'2022-02-03T03:00:00+08:00'),
(3,
'2022-02-03T03:00:00-08:00'),
(4,
'2022-02-03'),
(5,
'2022-02-03T03:00:00+09:00'),
(6,
'2022-02-03T03:00:00+06:00');

SELECT *
FROM test_tz;

----
1 2022-02-03 03:00:00.000000
Expand All @@ -112,8 +112,8 @@ FROM test_tz;

SET timezone='Asia/Shanghai';

SELECT *
FROM test_tz;
SELECT *
FROM test_tz;

----
1 2022-02-03 11:00:00.000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Type Conversion
---

Type conversion to target type.
Type conversion to target type.

## Syntax

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SELECT * FROM system.columns WHERE database='system';
| query_kind | system | query_log | VARCHAR | | | 0 | |
| query_text | system | query_log | VARCHAR | | | 0 | |
| event_date | system | query_log | DATE | | | 0 | |
| event_time | system | query_log | TIMESTAMP(3) | | | 0 | |
| event_time | system | query_log | TIMESTAMP | | | 0 | |
| current_database | system | query_log | VARCHAR | | | 0 | |
| databases | system | query_log | VARCHAR | | | 0 | |
| tables | system | query_log | VARCHAR | | | 0 | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE TABLE `query_log` (
`query_kind` VARCHAR,
`query_text` VARCHAR,
`event_date` DATE,
`event_time` TIMESTAMP(3),
`event_time` TIMESTAMP,
`current_database` VARCHAR,
`databases` VARCHAR,
`tables` VARCHAR,
Expand Down Expand Up @@ -85,8 +85,8 @@ written_io_bytes_cost_ms: 0
, skip_header=0, sql_dialect=PostgreSQL, storage_read_buffer_size=1048576, timezone=UTC, unquoted_ident_case_sensitive=0, wait_for_async_insert=1, wait_for_async_insert_timeout=100, scope: SESSION
extra:
*************************** 5. row ***************************
log_type: 2
handler_type: MySQL
Expand Down
5 changes: 1 addition & 4 deletions src/meta/proto-conv/src/data_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,13 @@ impl FromToProto for dv::TimestampType {
fn from_pb(p: pb::Timestamp) -> Result<Self, Incompatible>
where Self: Sized {
check_ver(p.ver, p.min_compatible)?;
let v = dv::TimestampType::create(p.precision as usize);
Ok(v)
Ok(dv::TimestampType::default())
}

fn to_pb(&self) -> Result<pb::Timestamp, Incompatible> {
let p = pb::Timestamp {
ver: VER,
min_compatible: MIN_COMPATIBLE_VER,
precision: self.precision() as u64,
// tz: self.tz().cloned(),
};

Ok(p)
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const META_CHANGE_LOG: &[(u64, &str)] = &[
14,
"2022-10-11: Add: role_arn and external_id in config.proto/OssStorageConfig, Remove role_arn and oidc_token from config.proto/OssStorageConfig",
),
(15, "2022-10-12: Remove: precision in TimestampType"),
];

pub const VER: u64 = META_CHANGE_LOG.last().unwrap().0;
Expand Down
39 changes: 39 additions & 0 deletions src/meta/proto-conv/tests/it/datatype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed 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.

//! Test UserStageInfo

use common_datavalues::DataTypeImpl;
use common_datavalues::TimestampType;

use crate::common;

#[test]
fn test_datatype_latest() -> anyhow::Result<()> {
common::test_pb_from_to("datatype", test_datatype())?;
Ok(())
}

#[test]
fn test_datatype_v15() -> anyhow::Result<()> {
// It is generated with common::test_pb_from_to.
let datatype_v15 = vec![114, 6, 160, 6, 15, 168, 6, 1, 160, 6, 15, 168, 6, 1];
let want = TimestampType::new_impl();
common::test_load_old(func_name!(), datatype_v15.as_slice(), want)?;
Ok(())
}

fn test_datatype() -> DataTypeImpl {
TimestampType::new_impl()
}
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#[macro_use]
pub(crate) mod common;
mod datatype;
mod proto_conv;
mod user_proto_conv;
mod user_stage;
6 changes: 3 additions & 3 deletions src/meta/proto-conv/tests/it/proto_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn new_table_meta() -> mt::TableMeta {
dv::DataField::new("float32", dv::Float32Type::default().into()),
dv::DataField::new("float64", dv::Float64Type::default().into()),
dv::DataField::new("date", dv::DateType::default().into()),
dv::DataField::new("timestamp", dv::TimestampType::create(5).into()),
dv::DataField::new("timestamp", dv::TimestampType::new_impl()),
dv::DataField::new("string", dv::StringType::default().into()),
dv::DataField::new(
"struct",
Expand Down Expand Up @@ -232,7 +232,7 @@ fn new_table_meta_v10() -> mt::TableMeta {
dv::DataField::new("float32", dv::Float32Type::default().into()),
dv::DataField::new("float64", dv::Float64Type::default().into()),
dv::DataField::new("date", dv::DateType::default().into()),
dv::DataField::new("timestamp", dv::TimestampType::create(5).into()),
dv::DataField::new("timestamp", dv::TimestampType::new_impl()),
dv::DataField::new("string", dv::StringType::default().into()),
dv::DataField::new(
"struct",
Expand Down Expand Up @@ -298,7 +298,7 @@ fn new_table_meta_v12() -> mt::TableMeta {
dv::DataField::new("float32", dv::Float32Type::default().into()),
dv::DataField::new("float64", dv::Float64Type::default().into()),
dv::DataField::new("date", dv::DateType::default().into()),
dv::DataField::new("timestamp", dv::TimestampType::create(5).into()),
dv::DataField::new("timestamp", dv::TimestampType::new_impl()),
dv::DataField::new("string", dv::StringType::default().into()),
dv::DataField::new(
"struct",
Expand Down
7 changes: 3 additions & 4 deletions src/meta/protos/proto/datatype.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ message Timestamp {
uint64 ver = 100;
uint64 min_compatible = 101;

/// The time resolution is determined by the precision parameter, range from 0
/// to 9 Typically are used - 0 (seconds) 3 (milliseconds), 6 (microseconds),
/// 9 (nanoseconds).
uint64 precision = 1;
/// It's deprecated
/// uint64 precision = 1;
reserved 1;
}

// Struct is similar to a `map` with fixed keys.
Expand Down
11 changes: 3 additions & 8 deletions src/query/ast/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ pub enum TypeName {
Float32,
Float64,
Date,
Timestamp {
precision: Option<u64>,
},
Timestamp,
String,
Array {
item_type: Option<Box<TypeName>>,
Expand Down Expand Up @@ -512,11 +510,8 @@ impl Display for TypeName {
TypeName::Date => {
write!(f, "DATE")?;
}
TypeName::Timestamp { precision } => {
write!(f, "Timestamp")?;
if let Some(precision) = precision {
write!(f, "({})", *precision)?;
}
TypeName::Timestamp => {
write!(f, "TIMESTAMP")?;
}
TypeName::String => {
write!(f, "STRING")?;
Expand Down
4 changes: 1 addition & 3 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,9 +1126,7 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
let ty_date = value(TypeName::Date, rule! { DATE });
let ty_datetime = map(
rule! { (DATETIME | TIMESTAMP) ~ ( "(" ~ #literal_u64 ~ ")" )? },
|(_, opt_precision)| TypeName::Timestamp {
precision: opt_precision.map(|(_, precision, _)| precision),
},
|(_, _)| TypeName::Timestamp,
);
let ty_string = value(
TypeName::String,
Expand Down
12 changes: 3 additions & 9 deletions src/query/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ DropDatabase(
---------- Input ----------
create table c(a DateTime null, b DateTime(3));
---------- Output ---------
CREATE TABLE c (a Timestamp NULL, b Timestamp(3) NOT NULL)
CREATE TABLE c (a TIMESTAMP NULL, b TIMESTAMP NOT NULL)
---------- AST ------------
CreateTable(
CreateTableStmt {
Expand All @@ -909,9 +909,7 @@ CreateTable(
span: Ident(15..16),
},
data_type: Nullable(
Timestamp {
precision: None,
},
Timestamp,
),
default_expr: None,
comment: None,
Expand All @@ -922,11 +920,7 @@ CreateTable(
quote: None,
span: Ident(32..33),
},
data_type: Timestamp {
precision: Some(
3,
),
},
data_type: Timestamp,
default_expr: None,
comment: None,
},
Expand Down
4 changes: 2 additions & 2 deletions src/query/datablocks/tests/it/data_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fn test_data_block() -> Result<()> {
fn test_data_block_convert() -> Result<()> {
let schema = DataSchemaRefExt::create(vec![
DataField::new("a", DateType::new_impl()),
DataField::new("b", TimestampType::new_impl(0)),
DataField::new("b", TimestampType::new_impl(3)),
DataField::new("b", TimestampType::new_impl()),
DataField::new("b", TimestampType::new_impl()),
]);

let block = DataBlock::create(schema.clone(), vec![
Expand Down
Loading

1 comment on commit 62bf8a2

@vercel
Copy link

@vercel vercel bot commented on 62bf8a2 Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-databend.vercel.app
databend.rs
databend-git-main-databend.vercel.app
databend.vercel.app

Please sign in to comment.