risingwave_frontend/optimizer/plan_node/batch_mssql_query.rs
1// Copyright 2026 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use pretty_xmlish::XmlNode;
16use risingwave_pb::batch_plan::MssqlQueryNode;
17use risingwave_pb::batch_plan::plan_node::NodeBody;
18
19use super::batch::prelude::*;
20use super::utils::{Distill, childless_record, column_names_pretty};
21use super::{
22 BatchPlanRef as PlanRef, ExprRewritable, PlanBase, ToBatchPb, ToDistributedBatch, ToLocalBatch,
23 generic,
24};
25use crate::error::Result;
26use crate::optimizer::plan_node::expr_visitable::ExprVisitable;
27use crate::optimizer::property::{Distribution, Order};
28
29/// Batch plan node for the `mssql_query` table function. The actual
30/// SQL Server round-trip is performed by `MssqlQueryExecutor`; this node
31/// only carries the connection parameters, the pre-discovered column
32/// schema (from `describe_mssql_query`), and the user query.
33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct BatchMssqlQuery {
35 pub base: PlanBase<Batch>,
36 pub core: generic::MssqlQuery,
37}
38
39impl BatchMssqlQuery {
40 /// Build a new `BatchMssqlQuery` plan node. The plan runs on a
41 /// single fragment (`Distribution::Single`) with no specific row
42 /// order; the executor returns whatever order SQL Server produces.
43 pub fn new(core: generic::MssqlQuery) -> Self {
44 let base = PlanBase::new_batch_with_core(&core, Distribution::Single, Order::any());
45
46 Self { base, core }
47 }
48
49 /// Names of the result columns as discovered at bind time. For
50 /// unnamed columns like `SELECT COUNT(*) FROM t` these are the
51 /// synthetic `column_N` names produced by `describe_mssql_query`.
52 pub fn column_names(&self) -> Vec<&str> {
53 self.schema().names_str()
54 }
55
56 /// Return a clone of this node with `Distribution::Single`. Used
57 /// by `ToLocalBatch` / `ToDistributedBatch` because the underlying
58 /// `mssql_query` always runs on a single fragment.
59 pub fn clone_with_dist(&self) -> Self {
60 let base = self.base.clone_with_new_distribution(Distribution::Single);
61 Self {
62 base,
63 core: self.core.clone(),
64 }
65 }
66}
67
68impl_plan_tree_node_for_leaf! { Batch, BatchMssqlQuery }
69
70impl Distill for BatchMssqlQuery {
71 /// Pretty-print the node as `BatchMssqlQuery { columns: [...] }` for
72 /// `EXPLAIN` output.
73 fn distill<'a>(&self) -> XmlNode<'a> {
74 let fields = vec![("columns", column_names_pretty(self.schema()))];
75 childless_record("BatchMssqlQuery", fields)
76 }
77}
78
79impl ToLocalBatch for BatchMssqlQuery {
80 /// `mssql_query` always runs on a single fragment; the local plan
81 /// is a clone with the single-node distribution.
82 fn to_local(&self) -> Result<PlanRef> {
83 Ok(self.clone_with_dist().into())
84 }
85}
86
87impl ToDistributedBatch for BatchMssqlQuery {
88 /// `mssql_query` always runs on a single fragment; the distributed
89 /// plan is a clone with the single-node distribution.
90 fn to_distributed(&self) -> Result<PlanRef> {
91 Ok(self.clone_with_dist().into())
92 }
93}
94
95impl ToBatchPb for BatchMssqlQuery {
96 /// Serialize the node into a `NodeBody::MssqlQuery` proto for the
97 /// batch task. `encrypt` / `trust_cert` have already been parsed and
98 /// validated by the binder and are carried as booleans in the plan.
99 fn to_batch_prost_body(&self) -> NodeBody {
100 let connection = &self.core.connection;
101 NodeBody::MssqlQuery(MssqlQueryNode {
102 columns: self
103 .core
104 .columns()
105 .iter()
106 .map(|c| c.to_protobuf())
107 .collect(),
108 hostname: connection.hostname.clone(),
109 port: connection.port.clone(),
110 username: connection.username.clone(),
111 password: connection.password.clone(),
112 database: connection.database.clone(),
113 query: self.core.query.clone(),
114 encrypt: connection.encrypt,
115 trust_cert: connection.trust_cert,
116 })
117 }
118}
119
120impl ExprRewritable<Batch> for BatchMssqlQuery {}
121
122impl ExprVisitable for BatchMssqlQuery {}