Skip to content

Commit

Permalink
Scalar type representation (#165)
Browse files Browse the repository at this point in the history
<!-- The PR description should answer 2 (maybe 3) important questions:
-->

### What
Return proper representation for scalar types. Still need to verify
and/or fix serialization of types.

<!-- What is this PR trying to accomplish (and why, if it's not
obvious)? -->

### How

<!-- How is it trying to accomplish it (what are the implementation
steps)? -->

---------

Co-authored-by: Daniel Harvey <[email protected]>
  • Loading branch information
codedmart and danieljharvey authored Dec 12, 2024
1 parent 915b1a6 commit 932d0eb
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 31 deletions.
30 changes: 30 additions & 0 deletions crates/configuration/src/introspection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Configuration and state for our connector.
use ndc_models::TypeRepresentation;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -64,3 +65,32 @@ pub struct IntrospectionForeignKeyColumn {
pub struct IntrospectionSchema {
pub name: String,
}

pub fn map_type_representation(sql_type: &str) -> Option<TypeRepresentation> {
match sql_type.to_lowercase().as_str() {
"bigint" => Some(TypeRepresentation::Int64),
"bit" => Some(TypeRepresentation::Boolean),
"decimal" | "numeric" | "money" | "smallmoney" => Some(TypeRepresentation::BigDecimal),
"int" => Some(TypeRepresentation::Int32),
"smallint" => Some(TypeRepresentation::Int16),
"tinyint" => Some(TypeRepresentation::Int16),
"float" => Some(TypeRepresentation::Float64),
"real" => Some(TypeRepresentation::Float32),
"date" => Some(TypeRepresentation::Date),
"datetime" | "datetime2" | "smalldatetime" => Some(TypeRepresentation::Timestamp),
"datetimeoffset" => Some(TypeRepresentation::TimestampTZ),
"time" | "timestamp" => Some(TypeRepresentation::String),
"char" | "varchar" | "text" | "nchar" | "nvarchar" | "ntext" => {
Some(TypeRepresentation::String)
}
"binary" | "varbinary" | "image" => Some(TypeRepresentation::String),
"uniqueidentifier" => Some(TypeRepresentation::UUID),
"xml" => Some(TypeRepresentation::String),
"json" => Some(TypeRepresentation::JSON),
// TODO: Add support for hierarchyid and sql_variant
// "geometry" => Some(TypeRepresentation::Geometry),
// "geography" => Some(TypeRepresentation::Geography),
// "hierarchyid" | "sql_variant" => XXX,
_ => None,
}
}
8 changes: 4 additions & 4 deletions crates/ndc-sqlserver/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use ndc_sdk::models::ObjectTypeName;
use ndc_sdk::models::ProcedureName;
use ndc_sdk::models::ScalarTypeName;
use ndc_sdk::models::TypeRepresentation;
use query_engine_metadata::metadata;

use ndc_sqlserver_configuration as configuration;
use query_engine_metadata::metadata;
use query_engine_metadata::metadata::stored_procedures::{
StoredProcedureArgumentInfo, StoredProcedures,
};
Expand Down Expand Up @@ -308,8 +307,9 @@ pub fn get_schema(
(
scalar_type.0.clone().into(),
models::ScalarType {
// TODO(PY): Add representation for beta
representation: None,
representation: configuration::introspection::map_type_representation(
scalar_type.0.as_str(),
),
aggregate_functions: metadata
.aggregate_functions
.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ expression: result
"rows": [
{
"InvoiceId": 98,
"Total": 3.98
"Total": "3.98"
},
{
"InvoiceId": 121,
"Total": 3.96
"Total": "3.96"
},
{
"InvoiceId": 143,
"Total": 5.94
"Total": "5.94"
},
{
"InvoiceId": 195,
"Total": 0.99
"Total": "0.99"
},
{
"InvoiceId": 316,
"Total": 1.98
"Total": "1.98"
},
{
"InvoiceId": 327,
"Total": 13.86
"Total": "13.86"
},
{
"InvoiceId": 382,
"Total": 8.91
"Total": "8.91"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ expression: result
{
"rows": [
{
"bigint": 1,
"bigint": "1",
"int": 245,
"float": 24.555,
"numeric": 24.555,
"numeric": "24.555",
"char": "s",
"text": "textual text",
"date": "2021-12-21",
Expand Down
Loading

0 comments on commit 932d0eb

Please sign in to comment.