Skip to content

Commit fa455c4

Browse files
MichaelScofieldpaomian
authored andcommitted
fix: describe distribute table (GreptimeTeam#988)
* fix: describe distribute table
1 parent d064d5c commit fa455c4

File tree

9 files changed

+63
-151
lines changed

9 files changed

+63
-151
lines changed

src/datanode/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ pub enum Error {
192192
#[snafu(display("Specified timestamp key or primary key column not found: {}", name))]
193193
KeyColumnNotFound { name: String, backtrace: Backtrace },
194194

195-
#[snafu(display("Invalid primary key: {}", msg))]
196-
InvalidPrimaryKey { msg: String, backtrace: Backtrace },
195+
#[snafu(display("Illegal primary keys definition: {}", msg))]
196+
IllegalPrimaryKeysDef { msg: String, backtrace: Backtrace },
197197

198198
#[snafu(display(
199199
"Constraint in CREATE TABLE statement is not supported yet: {}",
@@ -378,7 +378,7 @@ impl ErrorExt for Error {
378378
| Error::InvalidSql { .. }
379379
| Error::NotSupportSql { .. }
380380
| Error::KeyColumnNotFound { .. }
381-
| Error::InvalidPrimaryKey { .. }
381+
| Error::IllegalPrimaryKeysDef { .. }
382382
| Error::MissingTimestampColumn { .. }
383383
| Error::CatalogNotFound { .. }
384384
| Error::SchemaNotFound { .. }

src/datanode/src/sql/create.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use table::requests::*;
3232

3333
use crate::error::{
3434
self, CatalogNotFoundSnafu, CatalogSnafu, ConstraintNotSupportedSnafu, CreateSchemaSnafu,
35-
CreateTableSnafu, InsertSystemCatalogSnafu, InvalidPrimaryKeySnafu, KeyColumnNotFoundSnafu,
35+
CreateTableSnafu, IllegalPrimaryKeysDefSnafu, InsertSystemCatalogSnafu, KeyColumnNotFoundSnafu,
3636
RegisterSchemaSnafu, Result, SchemaExistsSnafu, SchemaNotFoundSnafu,
3737
};
3838
use crate::sql::SqlHandler;
@@ -159,8 +159,8 @@ impl SqlHandler {
159159

160160
ensure!(
161161
pk_map.len() < 2,
162-
InvalidPrimaryKeySnafu {
163-
msg: "Multiple definitions of primary key found"
162+
IllegalPrimaryKeysDefSnafu {
163+
msg: "not allowed to inline multiple primary keys in columns options"
164164
}
165165
);
166166

@@ -191,8 +191,8 @@ impl SqlHandler {
191191
}
192192
} else if is_primary {
193193
if !primary_keys.is_empty() {
194-
return InvalidPrimaryKeySnafu {
195-
msg: "Multiple definitions of primary key found",
194+
return IllegalPrimaryKeysDefSnafu {
195+
msg: "found definitions of primary keys in multiple places",
196196
}
197197
.fail();
198198
}
@@ -223,7 +223,7 @@ impl SqlHandler {
223223

224224
ensure!(
225225
!primary_keys.iter().any(|index| *index == ts_index),
226-
InvalidPrimaryKeySnafu {
226+
IllegalPrimaryKeysDefSnafu {
227227
msg: "time index column can't be included in primary key"
228228
}
229229
);
@@ -337,7 +337,7 @@ mod tests {
337337
let error = handler
338338
.create_to_request(42, parsed_stmt, &TableReference::bare("demo_table"))
339339
.unwrap_err();
340-
assert_matches!(error, Error::InvalidPrimaryKey { .. });
340+
assert_matches!(error, Error::IllegalPrimaryKeysDef { .. });
341341
}
342342

343343
#[tokio::test]
@@ -352,7 +352,7 @@ mod tests {
352352
let error = handler
353353
.create_to_request(42, parsed_stmt, &TableReference::bare("demo_table"))
354354
.unwrap_err();
355-
assert_matches!(error, Error::InvalidPrimaryKey { .. });
355+
assert_matches!(error, Error::IllegalPrimaryKeysDef { .. });
356356
}
357357

358358
#[tokio::test]
@@ -410,7 +410,7 @@ mod tests {
410410
let error = handler
411411
.create_to_request(42, create_table, &TableReference::full("c", "s", "demo"))
412412
.unwrap_err();
413-
assert_matches!(error, Error::InvalidPrimaryKey { .. });
413+
assert_matches!(error, Error::IllegalPrimaryKeysDef { .. });
414414
}
415415

416416
#[tokio::test]

src/frontend/src/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ pub enum Error {
342342
#[snafu(backtrace)]
343343
source: query::error::Error,
344344
},
345+
346+
#[snafu(display("Illegal primary keys definition: {}", msg))]
347+
IllegalPrimaryKeysDef { msg: String, backtrace: Backtrace },
345348
}
346349

347350
pub type Result<T> = std::result::Result<T, Error>;
@@ -352,7 +355,8 @@ impl ErrorExt for Error {
352355
Error::ParseAddr { .. }
353356
| Error::InvalidSql { .. }
354357
| Error::InvalidInsertRequest { .. }
355-
| Error::ColumnValuesNumberMismatch { .. } => StatusCode::InvalidArguments,
358+
| Error::ColumnValuesNumberMismatch { .. }
359+
| Error::IllegalPrimaryKeysDef { .. } => StatusCode::InvalidArguments,
356360

357361
Error::NotSupported { .. } => StatusCode::Unsupported,
358362

src/frontend/src/expr_factory.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ use datanode::instance::sql::table_idents_to_full_name;
2222
use datatypes::schema::ColumnSchema;
2323
use session::context::QueryContextRef;
2424
use snafu::{ensure, ResultExt};
25-
use sql::ast::{ColumnDef, TableConstraint};
25+
use sql::ast::{ColumnDef, ColumnOption, TableConstraint};
2626
use sql::statements::column_def_to_schema;
2727
use sql::statements::create::{CreateTable, TIME_INDEX};
2828

2929
use crate::error::{
3030
self, BuildCreateExprOnInsertionSnafu, ColumnDataTypeSnafu,
31-
ConvertColumnDefaultConstraintSnafu, InvalidSqlSnafu, ParseSqlSnafu, Result,
31+
ConvertColumnDefaultConstraintSnafu, IllegalPrimaryKeysDefSnafu, InvalidSqlSnafu,
32+
ParseSqlSnafu, Result,
3233
};
3334

3435
pub type CreateExprFactoryRef = Arc<dyn CreateExprFactory + Send + Sync>;
@@ -88,7 +89,7 @@ pub(crate) fn create_to_expr(
8889
desc: "".to_string(),
8990
column_defs: columns_to_expr(&create.columns, &time_index)?,
9091
time_index,
91-
primary_keys: find_primary_keys(&create.constraints)?,
92+
primary_keys: find_primary_keys(&create.columns, &create.constraints)?,
9293
create_if_not_exists: create.if_not_exists,
9394
// TODO(LFC): Fill in other table options.
9495
table_options: HashMap::from([("engine".to_string(), create.engine.clone())]),
@@ -98,8 +99,32 @@ pub(crate) fn create_to_expr(
9899
Ok(expr)
99100
}
100101

101-
fn find_primary_keys(constraints: &[TableConstraint]) -> Result<Vec<String>> {
102-
let primary_keys = constraints
102+
fn find_primary_keys(
103+
columns: &[ColumnDef],
104+
constraints: &[TableConstraint],
105+
) -> Result<Vec<String>> {
106+
let columns_pk = columns
107+
.iter()
108+
.filter_map(|x| {
109+
if x.options
110+
.iter()
111+
.any(|o| matches!(o.option, ColumnOption::Unique { is_primary: true }))
112+
{
113+
Some(x.name.value.clone())
114+
} else {
115+
None
116+
}
117+
})
118+
.collect::<Vec<String>>();
119+
120+
ensure!(
121+
columns_pk.len() <= 1,
122+
IllegalPrimaryKeysDefSnafu {
123+
msg: "not allowed to inline multiple primary keys in columns options"
124+
}
125+
);
126+
127+
let constraints_pk = constraints
103128
.iter()
104129
.filter_map(|constraint| match constraint {
105130
TableConstraint::Unique {
@@ -111,6 +136,17 @@ fn find_primary_keys(constraints: &[TableConstraint]) -> Result<Vec<String>> {
111136
})
112137
.flatten()
113138
.collect::<Vec<String>>();
139+
140+
ensure!(
141+
columns_pk.is_empty() || constraints_pk.is_empty(),
142+
IllegalPrimaryKeysDefSnafu {
143+
msg: "found definitions of primary keys in multiple places"
144+
}
145+
);
146+
147+
let mut primary_keys = Vec::with_capacity(columns_pk.len() + constraints_pk.len());
148+
primary_keys.extend(columns_pk);
149+
primary_keys.extend(constraints_pk);
114150
Ok(primary_keys)
115151
}
116152

tests/cases/distributed/create/create.result

Lines changed: 0 additions & 90 deletions
This file was deleted.

tests/cases/distributed/create/create.sql

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/cases/standalone/create/create.result renamed to tests/cases/standalone/common/create/create.result

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ Affected Rows: 1
108108

109109
CREATE TABLE test_multiple_pk_definitions (timestamp BIGINT TIME INDEX, host STRING PRIMARY KEY, value DOUBLE, PRIMARY KEY(host));
110110

111-
Error: 1004(InvalidArguments), Invalid primary key: Multiple definitions of primary key found
111+
Error: 1004(InvalidArguments), Illegal primary keys definition: found definitions of primary keys in multiple places
112112

113113
CREATE TABLE test_multiple_pk_definitions (timestamp BIGINT TIME INDEX, host STRING PRIMARY KEY, value DOUBLE, PRIMARY KEY(host), PRIMARY KEY(host));
114114

115-
Error: 1004(InvalidArguments), Invalid primary key: Multiple definitions of primary key found
115+
Error: 1004(InvalidArguments), Illegal primary keys definition: found definitions of primary keys in multiple places
116116

117117
CREATE TABLE test_multiple_inline_pk_definitions (timestamp BIGINT TIME INDEX, host STRING PRIMARY KEY, value DOUBLE PRIMARY KEY);
118118

119-
Error: 1004(InvalidArguments), Invalid primary key: Multiple definitions of primary key found
119+
Error: 1004(InvalidArguments), Illegal primary keys definition: not allowed to inline multiple primary keys in columns options
120120

File renamed without changes.

tests/cases/standalone/order/order_variable_size_payload.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,5 @@ DROP table test7;
123123
DROP table test8;
124124

125125
DROP TABLE DirectReports;
126+
127+
-- TODO(LFC): Seems creating distributed table has some column schema related issues, look into "order_variable_size_payload" test case.

0 commit comments

Comments
 (0)