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

Fix MySQL errors #180

Merged
merged 13 commits into from
Nov 22, 2024
4 changes: 2 additions & 2 deletions src/sql/db_connection_pool/dbconnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub enum Error {
#[snafu(display("Unable to downcast connection"))]
UnableToDowncastConnection {},

#[snafu(display("Unable to get schema: {source}"))]
#[snafu(display("{source}"))]
UnableToGetSchema { source: GenericError },

#[snafu(display("The field '{field_name}' has an unsupported data type: {data_type}"))]
#[snafu(display("The field '{field_name}' has an unsupported data type: {data_type}.\n Please file an issue https://github.com/datafusion-contrib/datafusion-table-providers"))]
Sevenannn marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "duckdb")]
UnsupportedDataType {
data_type: DataType,
Expand Down
27 changes: 19 additions & 8 deletions src/sql/db_connection_pool/dbconnection/mysqlconn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ pub enum Error {
#[snafu(display("Unable to get MySQL query result stream"))]
QueryResultStreamError {},

#[snafu(display("Unsupported column data type: {data_type}"))]
UnsupportedDataTypeError { data_type: String },
#[snafu(display("The field '{column_name}' contains unsupported data type: {data_type}. \n Please file an issue https://github.com/datafusion-contrib/datafusion-table-providers"))]
Sevenannn marked this conversation as resolved.
Show resolved Hide resolved
UnsupportedDataTypeError {
column_name: String,
data_type: String,
},

#[snafu(display("Unable to extract precision and scale from type: {data_type}"))]
#[snafu(display("Unable to extract precision and scale from type: {data_type}. \n This was likely caused by a bug in DataFusion Table Providers code, please file an issue https://github.com/datafusion-contrib/datafusion-table-providers"))]
Sevenannn marked this conversation as resolved.
Show resolved Hide resolved
UnableToGetDecimalPrecisionAndScale { data_type: String },

#[snafu(display("Field '{field}' is missing"))]
#[snafu(display("Field '{field}' is missing. \n This was likely caused by a bug in DataFusion Table Providers code, please file an issue https://github.com/datafusion-contrib/datafusion-table-providers"))]
Sevenannn marked this conversation as resolved.
Show resolved Hide resolved
MissingField { field: String },
}

Expand Down Expand Up @@ -142,6 +145,7 @@ impl<'a> AsyncDbConnection<Conn, &'a (dyn ToValue + Sync)> for MySQLConnection {
) -> Result<SendableRecordBatchStream> {
let params_vec: Vec<_> = params.iter().map(|&p| p.to_value()).collect();
let sql = sql.replace('"', "");

let conn = Arc::clone(&self.conn);

let mut stream = Box::pin(stream! {
Expand Down Expand Up @@ -214,7 +218,7 @@ fn columns_meta_to_schema(columns_meta: Vec<Row>) -> Result<SchemaRef> {
field: "Type".to_string(),
})?;

let column_type = map_str_type_to_column_type(&data_type)?;
let column_type = map_str_type_to_column_type(&column_name, &data_type)?;
let column_is_binary = map_str_type_to_is_binary(&data_type);
let column_is_enum = map_str_type_to_is_enum(&data_type);
let column_use_large_str_or_blob = map_str_type_to_use_large_str_or_blob(&data_type);
Expand All @@ -236,14 +240,17 @@ fn columns_meta_to_schema(columns_meta: Vec<Row>) -> Result<SchemaRef> {
precision,
scale,
)
.context(UnsupportedDataTypeSnafu { data_type })?;
.context(UnsupportedDataTypeSnafu {
column_name: column_name.clone(),
data_type,
})?;

fields.push(Field::new(&column_name, arrow_data_type, true));
}
Ok(Arc::new(Schema::new(fields)))
}

fn map_str_type_to_column_type(data_type: &str) -> Result<ColumnType> {
fn map_str_type_to_column_type(column_name: &str, data_type: &str) -> Result<ColumnType> {
let data_type = data_type.to_lowercase();
let column_type = match data_type.as_str() {
_ if data_type.starts_with("decimal") || data_type.starts_with("numeric") => {
Expand Down Expand Up @@ -284,7 +291,11 @@ fn map_str_type_to_column_type(data_type: &str) -> Result<ColumnType> {
_ if data_type.starts_with("char") => ColumnType::MYSQL_TYPE_STRING,
_ if data_type.starts_with("binary") => ColumnType::MYSQL_TYPE_STRING,
_ if data_type.starts_with("geometry") => ColumnType::MYSQL_TYPE_GEOMETRY,
_ => UnsupportedDataTypeSnafu { data_type }.fail()?,
_ => UnsupportedDataTypeSnafu {
column_name,
data_type,
}
.fail()?,
};

Ok(column_type)
Expand Down
Loading