use super::SourcePlan;
use crate::{RowSchema, SQLError};
#[cfg(test)]
mod tests;
impl SourcePlan {
pub fn is_explicitly_lateral(&self) -> bool {
matches!(
self,
Self::Values { lateral: true, .. } | Self::Subquery { lateral: true, .. }
)
}
pub fn internal_values_schema(&self) -> Result<Option<RowSchema>, SQLError> {
self.internal_values_schema_with_inference(None)
}
pub fn internal_values_schema_with_inference(
&self,
inferred: Option<&RowSchema>,
) -> Result<Option<RowSchema>, SQLError> {
self.internal_values_schema_with_control(
inferred,
&uqa_core::memory::ProductionControl::uncontrolled(),
)
}
pub fn internal_values_schema_with_control(
&self,
inferred: Option<&RowSchema>,
control: &uqa_core::memory::ProductionControl<'_>,
) -> Result<Option<RowSchema>, SQLError> {
control.check()?;
let Self::Values {
internal_relation: Some(relation),
internal_column_types: types,
internal_column_collations: collations,
..
} = self
else {
return Ok(None);
};
if !collations.is_empty() && collations.len() != types.len() {
return Err(SQLError::Internal(
"internal VALUES collation width does not match its declared attributes".into(),
));
}
let metadata = types.iter().enumerate().map(|(position, declared)| {
let ty = declared
.as_ref()
.or_else(|| inferred.and_then(|schema| schema.column_type(position)));
let collation = collations.get(position).copied().flatten().or_else(|| {
declared
.is_none()
.then(|| inferred.and_then(|schema| schema.column_collation(position)))
.flatten()
});
(ty, collation)
});
RowSchema::with_internal_relation_metadata_with_control(*relation, metadata, control)
.map(Some)
}
}