Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore varchar with length #17049

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion e2e_test/ddl/table/table.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ insert into "T2" ("V1") values (1);
statement ok
drop table "T2"

statement error
# The length of varchar will be ignored
statement ok
create table C1 (c1 varchar(5));

statement ok
drop table C1;

statement error
create table t (v1 int not null);

Expand Down
11 changes: 10 additions & 1 deletion src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::binder::Binder;
use crate::error::{ErrorCode, Result, RwError};
use crate::expr::{Expr as _, ExprImpl, ExprType, FunctionCall, InputRef, Parameter, SubqueryKind};
use crate::handler::create_sql_function::SQL_UDF_PATTERN;
use crate::session::current;

mod binary_op;
mod column;
Expand Down Expand Up @@ -985,7 +986,15 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result<DataType> {
AstDataType::Double | AstDataType::Float(Some(25..=53) | None) => DataType::Float64,
AstDataType::Float(Some(0 | 54..)) => unreachable!(),
AstDataType::Decimal(None, None) => DataType::Decimal,
AstDataType::Varchar | AstDataType::Text => DataType::Varchar,
AstDataType::Varchar(size) => {
if size.is_some() {
current::notice_to_user(
"CHARACTER VARYING with length is not supported. The length is ignored.",
);
}
DataType::Varchar
}
AstDataType::Text => DataType::Varchar,
AstDataType::Date => DataType::Date,
AstDataType::Time(false) => DataType::Time,
AstDataType::Timestamp(false) => DataType::Timestamp,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ fn derive_alias(expr: &Expr) -> Option<String> {
fn data_type_to_alias(data_type: &AstDataType) -> Option<String> {
let alias = match data_type {
AstDataType::Char(_) => "bpchar".to_string(),
AstDataType::Varchar => "varchar".to_string(),
AstDataType::Varchar(_) => "varchar".to_string(),
AstDataType::Uuid => "uuid".to_string(),
AstDataType::Decimal(_, _) => "numeric".to_string(),
AstDataType::Real | AstDataType::Float(Some(1..=24)) => "float4".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/handler/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn bind_sql_columns(column_defs: &[ColumnDef]) -> Result<Vec<ColumnCatalog>>
}

match data_type {
AstDataType::Text | AstDataType::Varchar | AstDataType::Char(_) => {}
AstDataType::Text | AstDataType::Varchar(_) | AstDataType::Char(_) => {}
_ => {
return Err(ErrorCode::NotSupported(
format!("{} is not a collatable data type", data_type),
Expand Down
8 changes: 5 additions & 3 deletions src/sqlparser/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub enum DataType {
/// Fixed-length character type e.g. CHAR(10)
Char(Option<u64>),
/// Variable-length character type.
/// We diverge from postgres by disallowing Varchar(n).
Varchar,
/// The length is only for compatibility with PostgreSQL. RisingWave ignores it.
Varchar(Option<u64>),
/// Uuid type
Uuid,
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
Expand Down Expand Up @@ -76,7 +76,9 @@ impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size),
DataType::Varchar => write!(f, "CHARACTER VARYING"),
DataType::Varchar(size) => {
format_type_with_optional_length(f, "CHARACTER VARYING", size)
}
DataType::Uuid => write!(f, "UUID"),
DataType::Decimal(precision, scale) => {
if let Some(scale) = scale {
Expand Down
2 changes: 1 addition & 1 deletion src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,7 @@ impl Parser<'_> {
self.next_token();
}
Keyword::VARCHAR => {
header_inner_expect_type = Some(DataType::Varchar);
header_inner_expect_type = Some(DataType::Varchar(None));
self.next_token();
}
_ => {
Expand Down
4 changes: 2 additions & 2 deletions src/sqlparser/src/parser_v2/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ where
Keyword::SMALLINT => empty.value(DataType::SmallInt),
Keyword::INT | Keyword::INTEGER => empty.value(DataType::Int),
Keyword::BIGINT => empty.value(DataType::BigInt),
Keyword::STRING | Keyword::VARCHAR => empty.value(DataType::Varchar),
Keyword::STRING | Keyword::VARCHAR => opt(precision_in_range(..)).map(DataType::Varchar),
Keyword::CHAR | Keyword::CHARACTER => dispatch! {keyword;
Keyword::VARYING => empty.value(DataType::Varchar),
Keyword::VARYING => opt(precision_in_range(..)).map(DataType::Varchar),
_ => opt(precision_in_range(..)).map(DataType::Char),
},
Keyword::UUID => empty.value(DataType::Uuid),
Expand Down
2 changes: 1 addition & 1 deletion src/sqlparser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ fn parse_create_table() {
vec![
ColumnDef::new(
"name".into(),
DataType::Varchar,
DataType::Varchar(None),
None,
vec![ColumnOptionDef {
name: None,
Expand Down
8 changes: 4 additions & 4 deletions src/sqlparser/tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn parse_create_table_with_defaults() {
),
ColumnDef::new(
"first_name".into(),
DataType::Varchar,
DataType::Varchar(None),
None,
vec![ColumnOptionDef {
name: None,
Expand All @@ -76,14 +76,14 @@ fn parse_create_table_with_defaults() {
),
ColumnDef::new(
"last_name".into(),
DataType::Varchar,
DataType::Varchar(None),
Some(ObjectName(vec![Ident::with_quote_unchecked('"', "es_ES")])),
vec![ColumnOptionDef {
name: None,
option: ColumnOption::NotNull,
}],
),
ColumnDef::new("email".into(), DataType::Varchar, None, vec![],),
ColumnDef::new("email".into(), DataType::Varchar(None), None, vec![],),
ColumnDef::new(
"address_id".into(),
DataType::SmallInt,
Expand Down Expand Up @@ -980,7 +980,7 @@ fn parse_drop_function() {
FunctionDesc {
name: ObjectName(vec![Ident::new_unchecked("test_func2")]),
args: Some(vec![
OperateFunctionArg::with_name("a", DataType::Varchar),
OperateFunctionArg::with_name("a", DataType::Varchar(None)),
OperateFunctionArg {
mode: Some(ArgMode::In),
name: Some("b".into()),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/sqlsmith/src/sql_gen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(super) fn data_type_to_ast_data_type(data_type: &DataType) -> AstDataType {
DataType::Decimal => AstDataType::Decimal(None, None),
DataType::Float32 => AstDataType::Real,
DataType::Float64 => AstDataType::Double,
DataType::Varchar => AstDataType::Varchar,
DataType::Varchar => AstDataType::Varchar(None),
DataType::Bytea => AstDataType::Bytea,
DataType::Date => AstDataType::Date,
DataType::Timestamp => AstDataType::Timestamp(false),
Expand Down
Loading