Skip to content

Commit

Permalink
use a dedicated type for native queries column info
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Mizrahi committed Mar 13, 2024
1 parent eb6867a commit b61cb10
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 18 deletions.
18 changes: 13 additions & 5 deletions crates/configuration/src/version3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,23 @@ pub fn occurring_scalar_types(
tables: &metadata::TablesInfo,
native_queries: &metadata::NativeQueries,
) -> BTreeSet<metadata::ScalarType> {
let tables_column_types = tables.0.values().flat_map(|v| v.columns.values());
let native_queries_column_types = native_queries.0.values().flat_map(|v| v.columns.values());
let native_queries_arguments_types =
native_queries.0.values().flat_map(|v| v.arguments.values());
let tables_column_types = tables
.0
.values()
.flat_map(|v| v.columns.values().map(|c| &c.r#type));
let native_queries_column_types = native_queries
.0
.values()
.flat_map(|v| v.columns.values().map(|c| &c.r#type));
let native_queries_arguments_types = native_queries
.0
.values()
.flat_map(|v| v.arguments.values().map(|c| &c.r#type));

tables_column_types
.chain(native_queries_column_types)
.chain(native_queries_arguments_types)
.filter_map(|c| match c.r#type {
.filter_map(|t| match t {
metadata::Type::ScalarType(ref t) => Some(t.clone()), // only keep scalar types
metadata::Type::ArrayType(_) | metadata::Type::CompositeType(_) => None,
})
Expand Down
21 changes: 16 additions & 5 deletions crates/connectors/ndc-postgres/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ pub async fn get_schema(
arguments: info
.arguments
.iter()
.map(|(name, column_info)| {
.map(|(name, readonly_column_info)| {
(
name.clone(),
models::ArgumentInfo {
description: column_info.description.clone(),
argument_type: column_to_type(column_info),
description: readonly_column_info.description.clone(),
argument_type: readonly_column_to_type(readonly_column_info),
},
)
})
Expand Down Expand Up @@ -208,7 +208,7 @@ pub async fn get_schema(
column.name.clone(),
models::ObjectField {
description: column.description.clone(),
r#type: column_to_type(column),
r#type: readonly_column_to_type(column),
},
)
})),
Expand Down Expand Up @@ -253,7 +253,7 @@ pub async fn get_schema(
name.clone(),
models::ArgumentInfo {
description: column_info.description.clone(),
argument_type: column_to_type(column_info),
argument_type: readonly_column_to_type(column_info),
},
)
})
Expand Down Expand Up @@ -286,6 +286,7 @@ pub async fn get_schema(
})
}

/// Extract the models::Type representation of a column.
fn column_to_type(column: &metadata::ColumnInfo) -> models::Type {
match &column.nullable {
metadata::Nullable::NonNullable => type_to_type(&column.r#type),
Expand All @@ -295,6 +296,16 @@ fn column_to_type(column: &metadata::ColumnInfo) -> models::Type {
}
}

/// Extract the models::Type representation of a readonly column.
fn readonly_column_to_type(column: &metadata::ReadOnlyColumnInfo) -> models::Type {
match &column.nullable {
metadata::Nullable::NonNullable => type_to_type(&column.r#type),
metadata::Nullable::Nullable => models::Type::Nullable {
underlying_type: Box::new(type_to_type(&column.r#type)),
},
}
}

fn type_to_type(typ: &metadata::Type) -> models::Type {
match &typ {
metadata::Type::ArrayType(typ) => models::Type::Array {
Expand Down
16 changes: 14 additions & 2 deletions crates/query-engine/metadata/src/metadata/native_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub struct NativeQueryInfo {
/// such as `SELECT * FROM authors WHERE name = {{author_name}}`
pub sql: NativeQuerySql,
/// Columns returned by the Native Query
pub columns: BTreeMap<String, ColumnInfo>,
pub columns: BTreeMap<String, ReadOnlyColumnInfo>,
#[serde(default)]
/// Names and types of arguments that can be passed to this Native Query
pub arguments: BTreeMap<String, ColumnInfo>,
pub arguments: BTreeMap<String, ReadOnlyColumnInfo>,
#[serde(default)]
pub description: Option<String>,
/// True if this native query mutates the database
Expand All @@ -34,6 +34,18 @@ pub struct NativeQueryInfo {
pub is_procedure: bool,
}

/// Information about a native query column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ReadOnlyColumnInfo {
pub name: String,
pub r#type: Type,
#[serde(default)]
pub nullable: Nullable,
#[serde(default)]
pub description: Option<String>,
}

/// A part of a Native Query text, either raw text or a parameter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NativeQueryPart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,15 @@ expression: schema
"description": "Columns returned by the Native Query",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ColumnInfo"
"$ref": "#/definitions/ReadOnlyColumnInfo"
}
},
"arguments": {
"description": "Names and types of arguments that can be passed to this Native Query",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ColumnInfo"
"$ref": "#/definitions/ReadOnlyColumnInfo"
}
},
"description": {
Expand All @@ -834,6 +834,37 @@ expression: schema
"Native_query_sql": {
"type": "string"
},
"ReadOnlyColumnInfo": {
"description": "Information about a native query column.",
"type": "object",
"required": [
"name",
"type"
],
"properties": {
"name": {
"type": "string"
},
"type": {
"$ref": "#/definitions/Type"
},
"nullable": {
"default": "nullable",
"allOf": [
{
"$ref": "#/definitions/Nullable"
}
]
},
"description": {
"default": null,
"type": [
"string",
"null"
]
}
}
},
"AggregateFunctions": {
"description": "All supported aggregate functions, grouped by type.",
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,15 +795,15 @@ expression: schema
"description": "Columns returned by the Native Query",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ColumnInfo"
"$ref": "#/definitions/ReadOnlyColumnInfo"
}
},
"arguments": {
"description": "Names and types of arguments that can be passed to this Native Query",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ColumnInfo"
"$ref": "#/definitions/ReadOnlyColumnInfo"
}
},
"description": {
Expand All @@ -822,6 +822,37 @@ expression: schema
"Native_query_sql": {
"type": "string"
},
"ReadOnlyColumnInfo": {
"description": "Information about a native query column.",
"type": "object",
"required": [
"name",
"type"
],
"properties": {
"name": {
"type": "string"
},
"type": {
"$ref": "#/definitions/Type"
},
"nullable": {
"default": "nullable",
"allOf": [
{
"$ref": "#/definitions/Nullable"
}
]
},
"description": {
"default": null,
"type": [
"string",
"null"
]
}
}
},
"AggregateFunctions": {
"description": "All supported aggregate functions, grouped by type.",
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,15 @@ expression: generated_schema_json
"description": "Columns returned by the Native Query",
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/ColumnInfo"
"$ref": "#/components/schemas/ReadOnlyColumnInfo"
}
},
"arguments": {
"description": "Names and types of arguments that can be passed to this Native Query",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/ColumnInfo"
"$ref": "#/components/schemas/ReadOnlyColumnInfo"
}
},
"description": {
Expand All @@ -814,6 +814,35 @@ expression: generated_schema_json
"Native_query_sql": {
"type": "string"
},
"ReadOnlyColumnInfo": {
"description": "Information about a native query column.",
"type": "object",
"required": [
"name",
"type"
],
"properties": {
"name": {
"type": "string"
},
"type": {
"$ref": "#/components/schemas/Type"
},
"nullable": {
"default": "nullable",
"allOf": [
{
"$ref": "#/components/schemas/Nullable"
}
]
},
"description": {
"default": null,
"type": "string",
"nullable": true
}
}
},
"AggregateFunctions": {
"description": "All supported aggregate functions, grouped by type.",
"type": "object",
Expand Down

0 comments on commit b61cb10

Please sign in to comment.