Skip to main content

uqa_execution/query/block/
mod.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Physical SELECT query blocks over local, foreign, and composed sources.
8
9pub mod context;
10mod execution;
11mod facets;
12pub mod foreign;
13mod table;
14pub mod where_filter;
15use crate::query::privileges::ensure_select_privileges_for_query_block;
16use crate::query::{
17    binding::{
18        binding_context, validate_query_block_expression_types, validate_query_block_references,
19    },
20    consumer::QueryOutputMode,
21    output::{QueryOutput, QueryRows},
22    projection::{expand_from_star_columns, physical_work_mem_bytes},
23    relational::output::execute_query_block_operator_output,
24    scored_input::{ScoredDocumentSource, ScoredInput},
25    sources::SourceContext,
26    CteScope,
27};
28use facets::{
29    build_facet_output, facet_projection_fields, post_retrieval_score_top_k,
30    score_limited_text_filter, score_order_top_k, FacetExecution,
31};
32use foreign::run_single_foreign_select_output;
33use table::run_single_table_select_output;
34use uqa_core::{ScoredEntry, Value};
35use uqa_sql::{
36    binding::{overlay_outer_schema, with_query_table_pseudo_columns},
37    plan::{
38        source_projection::SourceProjection, AccessPathPlan, ComputePlan, ProjectionPlan,
39        QueryBlockPlan, SourcePlan,
40    },
41    semantics::sets::validation::{
42        validate_query_set_contexts, validate_source_set_contexts_before_build,
43    },
44    semantics::{
45        expect_column_name, flatten_and_filter_parts, projection_columns,
46        source_filters::combine_filters as combine_filter_parts, SCORE_COLUMN, TABLE_OID_COLUMN,
47    },
48    SQLError, SQLParam, ScalarExpr,
49};
50use where_filter::execute_mixed_where;
51
52pub struct SingleRelation<'a> {
53    pub reference_name: &'a str,
54    pub relation_name: &'a str,
55    pub qualifier: &'a str,
56}
57
58pub struct BoundSingleRelation<'a> {
59    pub relation: SingleRelation<'a>,
60    pub schema: &'a crate::RowSchema,
61}
62
63pub use execution::execute_query_block_output;
64
65fn run_select_without_from_output<'a, S: Clone + 'static>(
66    context: &SourceContext<'a, S>,
67    original: &'a QueryBlockPlan,
68    statement: &'a QueryBlockPlan,
69    params: &'a [SQLParam],
70    ctes: &'a CteScope<S>,
71    output: QueryOutputMode<'a>,
72) -> Result<QueryOutput, SQLError> {
73    let columns = projection_columns(&statement.projections);
74    let operator = Box::new(crate::TableScan::from_physical_rows(
75        crate::RowSchema::default(),
76        vec![crate::PhysicalRow::default()],
77    ));
78    execute_query_block_operator_output(
79        context.relational,
80        operator,
81        statement.r#where.clone(),
82        statement,
83        original,
84        params,
85        ctes,
86        columns,
87        output,
88    )
89}
90
91fn expr_contains_jsonpath_fts_match(expr: &ScalarExpr) -> bool {
92    let mut found = false;
93    expr.visit(&mut |part| {
94        if expr_is_jsonpath_fts_match(part) {
95            found = true;
96        }
97    });
98    found
99}
100fn expr_is_jsonpath_fts_match(expr: &ScalarExpr) -> bool {
101    matches!(expr, ScalarExpr::Func { name, args, .. } if name.eq_ignore_ascii_case("fts_match")
102        && matches!(args.get(1), Some(ScalarExpr::Literal(Value::Str(path))) if path.trim_start().starts_with('$')))
103}