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(typedsql): support scalar list in @param docs #4984

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
133 changes: 111 additions & 22 deletions schema-engine/connectors/sql-schema-connector/src/sql_doc_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl<'a> Input<'a> {
self.0.strip_suffix(pat).map(Self)
}

fn strip_suffix_str(&self, pat: &str) -> Option<Self> {
self.0.strip_suffix(pat).map(Self)
}

fn starts_with(&self, pat: &str) -> bool {
self.0.starts_with(pat)
}
Expand Down Expand Up @@ -199,35 +203,37 @@ fn parse_typ_opt<'a>(
input: Input<'a>,
enum_names: &'a [String],
) -> ConnectorResult<(Input<'a>, Option<ParsedParamType<'a>>)> {
fn list_accepted_types(enum_names: &[String]) -> String {
format!(
"'Int', 'BigInt', 'Float', 'Boolean', 'String', 'DateTime', 'Json', 'Bytes', 'Decimal'{}",
render_enum_names(enum_names)
)
}

if let Some(start) = input.find(&['{']) {
if let Some(end) = input.find(&['}']) {
let typ = input.move_between(start + 1, end);

if typ.is_empty() {
return Err(build_error(input, "missing type (accepted types are: 'Int', 'BigInt', 'Float', 'Boolean', 'String', 'DateTime', 'Json', 'Bytes', 'Decimal')"));
return Err(build_error(
input,
&format!("missing type (accepted types are: {})", list_accepted_types(enum_names)),
));
}

let parsed_typ = ScalarType::try_from_str(typ.inner(), false)
.map(|st| match st {
ScalarType::Int => ColumnType::Int32,
ScalarType::BigInt => ColumnType::Int64,
ScalarType::Float => ColumnType::Float,
ScalarType::Boolean => ColumnType::Boolean,
ScalarType::String => ColumnType::Text,
ScalarType::DateTime => ColumnType::DateTime,
ScalarType::Json => ColumnType::Json,
ScalarType::Bytes => ColumnType::Bytes,
ScalarType::Decimal => ColumnType::Numeric,
})
.map(ParsedParamType::ColumnType)
.or_else(|| {
enum_names.iter().any(|enum_name| *enum_name == typ.inner())
.then(|| ParsedParamType::Enum(typ.inner()))
})
.ok_or_else(|| build_error(
input,
&format!("invalid type: '{typ}' (accepted types are: 'Int', 'BigInt', 'Float', 'Boolean', 'String', 'DateTime', 'Json', 'Bytes', 'Decimal'{})", render_enum_names(enum_names)),
))?;
let parsed_typ = typ
.strip_suffix_str("[]")
.and_then(|typ| parse_typ_name(typ, true, enum_names))
.or_else(|| parse_typ_name(typ, false, enum_names))
.ok_or_else(|| {
build_error(
input,
&format!(
"invalid type: '{typ}' (accepted types are: {})",
list_accepted_types(enum_names)
),
)
})?;

Ok((input.move_from(end + 1), Some(parsed_typ)))
} else {
Expand All @@ -238,6 +244,38 @@ fn parse_typ_opt<'a>(
}
}

fn parse_typ_name<'a>(typ: Input<'a>, list: bool, enum_names: &[String]) -> Option<ParsedParamType<'a>> {
ScalarType::try_from_str(typ.inner(), false)
.map(|st| match (st, list) {
(ScalarType::Int, false) => ColumnType::Int32,
(ScalarType::BigInt, false) => ColumnType::Int64,
(ScalarType::Float, false) => ColumnType::Float,
(ScalarType::Boolean, false) => ColumnType::Boolean,
(ScalarType::String, false) => ColumnType::Text,
(ScalarType::DateTime, false) => ColumnType::DateTime,
(ScalarType::Json, false) => ColumnType::Json,
(ScalarType::Bytes, false) => ColumnType::Bytes,
(ScalarType::Decimal, false) => ColumnType::Numeric,

(ScalarType::Int, true) => ColumnType::Int32Array,
(ScalarType::BigInt, true) => ColumnType::Int64Array,
(ScalarType::Float, true) => ColumnType::FloatArray,
(ScalarType::Boolean, true) => ColumnType::BooleanArray,
(ScalarType::String, true) => ColumnType::TextArray,
(ScalarType::DateTime, true) => ColumnType::DateTimeArray,
(ScalarType::Json, true) => ColumnType::JsonArray,
(ScalarType::Bytes, true) => ColumnType::BytesArray,
(ScalarType::Decimal, true) => ColumnType::NumericArray,
})
.map(ParsedParamType::ColumnType)
.or_else(|| {
enum_names
.iter()
.any(|enum_name| *enum_name == typ.inner())
.then(|| ParsedParamType::Enum(typ.inner()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this branch needs list handling as well?

})
}

fn parse_position_opt(input: Input<'_>) -> ConnectorResult<(Input<'_>, Option<usize>)> {
if let Some((param_input, param_pos)) = input
.trim_start()
Expand Down Expand Up @@ -926,6 +964,57 @@ mod tests {
expected.assert_debug_eq(&res);
}

#[test]
fn parse_param_22() {
use expect_test::expect;

let res = parse_param(Input("@param {Int[]} $12"), &[]);

let expected = expect![[r#"
Ok(
ParsedParameterDoc {
alias: None,
typ: Some(
ColumnType(
Int32Array,
),
),
nullable: None,
position: Some(
12,
),
documentation: None,
},
)
"#]];

expected.assert_debug_eq(&res);
}

#[test]
fn parse_param_23() {
use expect_test::expect;

let res = parse_param(Input("@param {Unknown[]} $12"), &[]);

let expected = expect![[r#"
Err(
ConnectorErrorImpl {
user_facing_error: None,
message: Some(
"SQL documentation parsing: invalid type: 'Unknown[]' (accepted types are: 'Int', 'BigInt', 'Float', 'Boolean', 'String', 'DateTime', 'Json', 'Bytes', 'Decimal') at '{Unknown[]} $12'.",
),
source: None,
context: SpanTrace [],
}
SQL documentation parsing: invalid type: 'Unknown[]' (accepted types are: 'Int', 'BigInt', 'Float', 'Boolean', 'String', 'DateTime', 'Json', 'Bytes', 'Decimal') at '{Unknown[]} $12'.
,
)
"#]];

expected.assert_debug_eq(&res);
}

#[test]
fn parse_sql_1() {
use expect_test::expect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn parses_doc_enum_name(api: TestApi) {
let expected = expect![[r#"
IntrospectSqlQueryOutput {
name: "test_1",
source: "\n -- @param {MyFancyEnum} $1\n SELECT * FROM model WHERE id = $1;\n ",
source: "\n -- @param {MyFancyEnum} $1\n SELECT 1 as \"col\" WHERE 1 = $1;\n ",
documentation: None,
parameters: [
IntrospectSqlQueryParameterOutput {
Expand All @@ -155,22 +155,50 @@ fn parses_doc_enum_name(api: TestApi) {
],
result_columns: [
IntrospectSqlQueryColumnOutput {
name: "id",
name: "col",
typ: "int",
nullable: true,
},
],
}
"#]];

let sql = r#"
-- @param {MyFancyEnum} $1
SELECT 1 as "col" WHERE 1 = ?;
"#;

api.introspect_sql("test_1", sql).send_sync().expect_result(expected)
}

#[test_connector(tags(Postgres))]
fn parses_doc_array(api: TestApi) {
let expected = expect![[r#"
IntrospectSqlQueryOutput {
name: "test_1",
source: "\n -- @param {Int[]} $1:myIntArray\n SELECT 1 as \"col\" WHERE 1 = $1;\n ",
documentation: None,
parameters: [
IntrospectSqlQueryParameterOutput {
documentation: None,
name: "myIntArray",
typ: "int-array",
nullable: false,
},
],
result_columns: [
IntrospectSqlQueryColumnOutput {
name: "enum",
typ: "MyFancyEnum",
nullable: false,
name: "col",
typ: "int",
nullable: true,
},
],
}
"#]];

let sql = r#"
-- @param {MyFancyEnum} $1
SELECT * FROM model WHERE id = ?;
-- @param {Int[]} $1:myIntArray
SELECT 1 as "col" WHERE 1 = ?;
"#;

api.introspect_sql("test_1", sql).send_sync().expect_result(expected)
Expand Down
Loading