use std::sync::Arc;
use hamelin_legacy::Compiler;
use hamelin_lib::{
catalog::{CatalogBuilder, CatalogProvider, DataSetBuilder},
types::{struct_type::Struct, INT, STRING},
};
use rstest::rstest;
use crate::shared;
fn compiler() -> Arc<Compiler> {
let catalog = CatalogBuilder::default()
.with_data_set(
DataSetBuilder::new("table".into())
.with("t1", INT)
.with("t2", INT)
.with("t3", STRING)
.with(
"nested",
Struct::default()
.with("a".parse().unwrap(), INT)
.with("b".parse().unwrap(), STRING)
.with(
"c".parse().unwrap(),
Struct::default()
.with("d".parse().unwrap(), INT)
.with("e".parse().unwrap(), STRING)
.into(),
)
.into(),
),
)
.build();
let mut compiler = Compiler::new();
compiler.set_environment_provider(Arc::new(CatalogProvider::try_from(catalog).unwrap()));
Arc::new(compiler)
}
#[rstest]
#[case::select_nested(
"FROM table | SELECT nested.c.d",
r#"
SELECT
CAST(
ROW(ROW("nested"."c"."d")) AS ROW("c" ROW("d" BIGINT))
) AS "nested"
FROM
"table"
"#
)]
#[case::select_two_nested(
"FROM table | SELECT nested.b, nested.c.d",
r#"
SELECT
CAST(
ROW("nested"."b", ROW("nested"."c"."d")) AS ROW("b" VARCHAR, "c" ROW("d" BIGINT))
) AS "nested"
FROM
"table"
"#
)]
#[case::drop_nested(
"FROM table | DROP nested.c.d",
r#"
SELECT
"t1"
, "t2"
, "t3"
, CAST(
ROW(
"nested"."a"
, "nested"."b"
, ROW("nested"."c"."e")
)
AS
ROW(
"a" BIGINT
, "b" VARCHAR
, "c" ROW("e" VARCHAR)
)
) AS "nested"
FROM
"table"
"#
)]
#[case::let_nested(
"FROM table | LET nested.b = 'new value'",
r#"
SELECT
CAST(
ROW(
"nested"."a"
, 'new value'
, ROW("nested"."c"."d", "nested"."c"."e")
) AS ROW("a" BIGINT, "b" VARCHAR, "c" ROW("d" BIGINT, "e" VARCHAR))
) AS "nested"
, "t1"
, "t2"
, "t3"
FROM
"table"
"#
)]
#[case::let_then_select_nested(
"FROM table | LET nested.c.f = 1 | SELECT nested.c.d, nested.c.f",
r#"
SELECT
CAST(
ROW(ROW("nested"."c"."d", "nested"."c"."f")) AS ROW("c" ROW("d" BIGINT, "f" BIGINT))
) AS "nested"
FROM (
SELECT
CAST(
ROW(
"nested"."a"
, "nested"."b"
, ROW("nested"."c"."d", "nested"."c"."e", 1)
) AS ROW("a" BIGINT, "b" VARCHAR, "c" ROW("d" BIGINT, "e" VARCHAR, "f" BIGINT))
) AS "nested"
, "t1"
, "t2"
, "t3"
FROM
"table"
)
"#
)]
pub fn test_dotted_select(#[case] hamelin: String, #[case] expected: String) -> anyhow::Result<()> {
shared::compare(compiler(), hamelin, expected)
}