use super::{LayoutCache, SingleOutput, MAX_LAYOUT_VISITS};
use crate::output_label::scalar::projection_labels;
use sqlparser::{ast::Statement, dialect::PostgreSqlDialect, parser::Parser};
#[test]
fn take_visit_stays_exhausted_at_the_inference_limit() {
let cache = LayoutCache::default();
for _ in 0..MAX_LAYOUT_VISITS {
assert!(cache.take_visit());
}
assert!(!cache.take_visit());
assert!(!cache.take_visit());
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-no-duplicate-keyed-lookups/fixture/review-aggregate-demand/output-label-scope-boundaries.sql"
));
let statements = Parser::parse_sql(&PostgreSqlDialect {}, sql).unwrap();
let Statement::Query(query) = &statements[1] else {
panic!("expected a saved SELECT");
};
let scope = super::super::root_scope(query, None, &cache);
assert_eq!(scope.output("outer_cte"), None);
}
#[test]
fn shared_lateral_layouts_expand_once_per_lexical_scope() {
let sql = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-cases/rules/postgres-no-duplicate-keyed-lookups/fixture/review-aggregate-demand/output-labels-shared-lateral.sql"
));
let statements = Parser::parse_sql(&PostgreSqlDialect {}, sql).unwrap();
let [Statement::Query(query)] = statements.as_slice() else {
panic!("expected a saved SELECT");
};
let calls = std::cell::Cell::new(0);
let supplied = |name: &str| {
assert_eq!(name, "supplied");
calls.set(calls.get() + 1);
Some(SingleOutput::Named("k".into()))
};
let cache = LayoutCache::default();
let scope = super::super::root_scope(query, Some(&supplied), &cache);
assert_eq!(
projection_labels(&query.body, &scope),
Some(vec![Some("k".into())])
);
assert_eq!(calls.get(), 1);
assert_eq!(cache.visits.get(), 37);
for remaining in [0, 1, 2] {
let cache = LayoutCache::default();
cache.visits.set(MAX_LAYOUT_VISITS - remaining);
let scope = super::super::root_scope(query, Some(&supplied), &cache);
assert_eq!(projection_labels(&query.body, &scope), Some(vec![None]));
assert_eq!(cache.visits.get(), MAX_LAYOUT_VISITS);
}
assert_eq!(calls.get(), 1);
}