Skip to content

Commit c993fd7

Browse files
committed
adding config to control Varchar behavior
1 parent 3051d19 commit c993fd7

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

datafusion/common/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ config_namespace! {
204204
/// MySQL, PostgreSQL, Hive, SQLite, Snowflake, Redshift, MsSQL, ClickHouse, BigQuery, and Ansi.
205205
pub dialect: String, default = "generic".to_string()
206206

207+
/// If set to true, the system will return an error when encountering a `Varchar`
208+
/// type with a specified length. This can be useful for enforcing certain schema
209+
/// constraints or maintaining compatibility with systems that do not support
210+
/// length-specified `Varchar` types.
211+
pub error_on_varchar_with_length: bool, default = false
212+
207213
}
208214
}
209215

@@ -303,6 +309,7 @@ config_namespace! {
303309
/// statistics into the same file groups.
304310
/// Currently experimental
305311
pub split_file_groups_by_statistics: bool, default = false
312+
306313
}
307314
}
308315

datafusion/core/src/execution/session_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ impl SessionState {
563563
ParserOptions {
564564
parse_float_as_decimal: sql_parser_options.parse_float_as_decimal,
565565
enable_ident_normalization: sql_parser_options.enable_ident_normalization,
566+
error_on_varchar_with_length: sql_parser_options.error_on_varchar_with_length,
566567
}
567568
}
568569

datafusion/sql/src/planner.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ pub trait ContextProvider {
9797
pub struct ParserOptions {
9898
pub parse_float_as_decimal: bool,
9999
pub enable_ident_normalization: bool,
100+
pub error_on_varchar_with_length: bool,
100101
}
101102

102103
impl Default for ParserOptions {
103104
fn default() -> Self {
104105
Self {
105106
parse_float_as_decimal: false,
106107
enable_ident_normalization: true,
108+
error_on_varchar_with_length: false,
107109
}
108110
}
109111
}
@@ -398,12 +400,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
398400
SQLDataType::UnsignedInt(_) | SQLDataType::UnsignedInteger(_) | SQLDataType::UnsignedInt4(_) => {
399401
Ok(DataType::UInt32)
400402
}
403+
SQLDataType::Varchar(length) => {
404+
match (length, self.options.error_on_varchar_with_length) {
405+
(Some(_), true) => plan_err!("does not support Varchar with length, please set corresponding parameter"),
406+
_ => Ok(DataType::Utf8),
407+
}
408+
}
401409
SQLDataType::UnsignedBigInt(_) | SQLDataType::UnsignedInt8(_) => Ok(DataType::UInt64),
402410
SQLDataType::Float(_) => Ok(DataType::Float32),
403411
SQLDataType::Real | SQLDataType::Float4 => Ok(DataType::Float32),
404412
SQLDataType::Double | SQLDataType::DoublePrecision | SQLDataType::Float8 => Ok(DataType::Float64),
405413
SQLDataType::Char(_)
406-
| SQLDataType::Varchar(_)
407414
| SQLDataType::Text
408415
| SQLDataType::String(_) => Ok(DataType::Utf8),
409416
SQLDataType::Timestamp(None, tz_info) => {

datafusion/sqllogictest/test_files/strings.slt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,42 @@ e1
7878
p2
7979
p2e1
8080
p2m1e1
81+
82+
statement ok
83+
set datafusion.sql_parser.error_on_varchar_with_length = true;
84+
85+
# could not process due to config setting
86+
query error
87+
SELECT '12345'::VARCHAR(2);
88+
89+
query error
90+
SELECT s::VARCHAR(2) FROM (VALUES ('12345')) t(s);
91+
92+
statement ok
93+
create table vals(s char) as values('abc'), ('def');
94+
95+
query error
96+
SELECT s::VARCHAR(2) FROM vals
97+
98+
statement ok
99+
set datafusion.sql_parser.error_on_varchar_with_length = false;
100+
101+
# could be done when we diable this setting
102+
query T
103+
SELECT '12345'::VARCHAR(2)
104+
----
105+
12345
106+
107+
query T
108+
SELECT s::VARCHAR(2) FROM (VALUES ('12345')) t(s)
109+
----
110+
12345
111+
112+
query T
113+
SELECT s::VARCHAR(2) FROM vals
114+
----
115+
abc
116+
def
117+
118+
statement ok
119+
drop table vals;

0 commit comments

Comments
 (0)