1use core::str::FromStr;
16use std::pin::Pin;
17use std::sync::Arc;
18use std::task::{Context, Poll};
19
20use anyhow::Context as _;
21use bytes::{Bytes, BytesMut};
22use futures::Stream;
23use itertools::Itertools;
24use pgwire::pg_field_descriptor::PgFieldDescriptor;
25use pgwire::pg_response::RowSetResult;
26use pgwire::pg_server::BoxedError;
27use pgwire::types::{Format, FormatIterator, Row};
28use pin_project_lite::pin_project;
29use risingwave_common::array::DataChunk;
30use risingwave_common::catalog::Field;
31use risingwave_common::row::Row as _;
32use risingwave_common::types::{
33 DataType, Interval, ScalarRefImpl, Timestamptz, write_date_time_tz,
34};
35use risingwave_common::util::epoch::Epoch;
36use risingwave_common::util::iter_util::ZipEqFast;
37use risingwave_connector::sink::elasticsearch_opensearch::elasticsearch::ES_SINK;
38use risingwave_connector::source::KAFKA_CONNECTOR;
39use risingwave_connector::source::iceberg::ICEBERG_CONNECTOR;
40use risingwave_pb::catalog::connection_params::PbConnectionType;
41use risingwave_sqlparser::ast::{
42 CompatibleFormatEncode, FormatEncodeOptions, ObjectName, Query, Select, SelectItem, SetExpr,
43 TableFactor, TableWithJoins,
44};
45use thiserror_ext::AsReport;
46use tokio::select;
47use tokio::time::{Duration, sleep};
48
49use crate::catalog::root_catalog::SchemaPath;
50use crate::error::ErrorCode::ProtocolError;
51use crate::error::{ErrorCode, Result as RwResult, RwError};
52use crate::session::{SessionImpl, current};
53use crate::{Binder, HashSet, TableCatalog};
54
55pin_project! {
56 pub struct DataChunkToRowSetAdapter<VS>
63 where
64 VS: Stream<Item = Result<DataChunk, BoxedError>>,
65 {
66 #[pin]
67 chunk_stream: VS,
68 column_types: Vec<DataType>,
69 pub formats: Vec<Format>,
70 session_data: StaticSessionData,
71 }
72}
73
74pub struct StaticSessionData {
76 pub timezone: String,
77}
78
79impl<VS> DataChunkToRowSetAdapter<VS>
80where
81 VS: Stream<Item = Result<DataChunk, BoxedError>>,
82{
83 pub fn new(
84 chunk_stream: VS,
85 column_types: Vec<DataType>,
86 formats: Vec<Format>,
87 session: Arc<SessionImpl>,
88 ) -> Self {
89 let session_data = StaticSessionData {
90 timezone: session.config().timezone(),
91 };
92 Self {
93 chunk_stream,
94 column_types,
95 formats,
96 session_data,
97 }
98 }
99}
100
101impl<VS> Stream for DataChunkToRowSetAdapter<VS>
102where
103 VS: Stream<Item = Result<DataChunk, BoxedError>>,
104{
105 type Item = RowSetResult;
106
107 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
108 let mut this = self.project();
109 match this.chunk_stream.as_mut().poll_next(cx) {
110 Poll::Pending => Poll::Pending,
111 Poll::Ready(chunk) => match chunk {
112 Some(chunk_result) => match chunk_result {
113 Ok(chunk) => Poll::Ready(Some(
114 to_pg_rows(this.column_types, chunk, this.formats, this.session_data)
115 .map_err(|err| err.into()),
116 )),
117 Err(err) => Poll::Ready(Some(Err(err))),
118 },
119 None => Poll::Ready(None),
120 },
121 }
122 }
123}
124
125pub fn pg_value_format(
127 data_type: &DataType,
128 d: ScalarRefImpl<'_>,
129 format: Format,
130 session_data: &StaticSessionData,
131) -> RwResult<Bytes> {
132 match format {
135 Format::Text => {
136 if *data_type == DataType::Timestamptz {
137 Ok(timestamptz_to_string_with_session_data(d, session_data))
138 } else {
139 Ok(d.text_format(data_type).into())
140 }
141 }
142 Format::Binary => Ok(d
143 .binary_format(data_type)
144 .context("failed to format binary value")?),
145 }
146}
147
148fn timestamptz_to_string_with_session_data(
149 d: ScalarRefImpl<'_>,
150 session_data: &StaticSessionData,
151) -> Bytes {
152 let tz = d.into_timestamptz();
153 let time_zone = Timestamptz::lookup_time_zone(&session_data.timezone).unwrap();
154 let instant_local = tz.to_datetime_in_zone(time_zone);
155 let mut result_string = BytesMut::new();
156 write_date_time_tz(instant_local, &mut result_string).unwrap();
157 result_string.into()
158}
159
160fn to_pg_rows(
161 column_types: &[DataType],
162 chunk: DataChunk,
163 formats: &[Format],
164 session_data: &StaticSessionData,
165) -> RwResult<Vec<Row>> {
166 assert_eq!(chunk.dimension(), column_types.len());
167 if cfg!(debug_assertions) {
168 let chunk_data_types = chunk.data_types();
169 for (ty1, ty2) in chunk_data_types.iter().zip_eq_fast(column_types) {
170 debug_assert!(
171 ty1.equals_datatype(ty2),
172 "chunk_data_types: {chunk_data_types:?}, column_types: {column_types:?}"
173 )
174 }
175 }
176
177 chunk
178 .rows()
179 .map(|r| {
180 let format_iter = FormatIterator::new(formats, chunk.dimension())
181 .map_err(ErrorCode::InternalError)?;
182 let row = r
183 .iter()
184 .zip_eq_fast(column_types)
185 .zip_eq_fast(format_iter)
186 .map(|((data, t), format)| match data {
187 Some(data) => Some(pg_value_format(t, data, format, session_data)).transpose(),
188 None => Ok(None),
189 })
190 .try_collect()?;
191 Ok(Row::new(row))
192 })
193 .try_collect()
194}
195
196pub fn to_pg_field(f: &Field) -> PgFieldDescriptor {
198 PgFieldDescriptor::new(
199 f.name.clone(),
200 f.data_type().to_oid(),
201 f.data_type().type_len(),
202 )
203}
204
205#[easy_ext::ext(SourceSchemaCompatExt)]
206impl CompatibleFormatEncode {
207 pub fn into_v2_with_warning(self) -> FormatEncodeOptions {
209 match self {
210 CompatibleFormatEncode::RowFormat(inner) => {
211 current::notice_to_user(
213 "RisingWave will stop supporting the syntax \"ROW FORMAT\" in future versions, which will be changed to \"FORMAT ... ENCODE ...\" syntax.",
214 );
215 inner.into_format_encode_v2()
216 }
217 CompatibleFormatEncode::V2(inner) => inner,
218 }
219 }
220}
221
222pub fn gen_query_from_table_name(from_name: ObjectName) -> Query {
223 let table_factor = TableFactor::Table {
224 name: from_name,
225 alias: None,
226 as_of: None,
227 };
228 let from = vec![TableWithJoins {
229 relation: table_factor,
230 joins: vec![],
231 }];
232 let select = Select {
233 from,
234 projection: vec![SelectItem::Wildcard(None)],
235 ..Default::default()
236 };
237 let body = SetExpr::Select(Box::new(select));
238 Query {
239 with: None,
240 body,
241 order_by: vec![],
242 limit: None,
243 offset: None,
244 fetch: None,
245 }
246}
247
248pub fn convert_unix_millis_to_logstore_u64(unix_millis: u64) -> u64 {
249 Epoch::from_unix_millis(unix_millis).0
250}
251
252pub fn convert_logstore_u64_to_unix_millis(logstore_u64: u64) -> u64 {
253 Epoch::from(logstore_u64).as_unix_millis()
254}
255
256pub fn convert_interval_to_u64_seconds(interval: &String) -> RwResult<u64> {
257 let seconds = (Interval::from_str(interval)
258 .map_err(|err| {
259 ErrorCode::InternalError(format!(
260 "Convert interval to u64 error, please check format, error: {:?}",
261 err.to_report_string()
262 ))
263 })?
264 .epoch_in_micros()
265 / 1000000) as u64;
266 Ok(seconds)
267}
268
269pub fn ensure_connection_type_allowed(
270 connection_type: PbConnectionType,
271 allowed_types: &HashSet<PbConnectionType>,
272) -> RwResult<()> {
273 if !allowed_types.contains(&connection_type) {
274 return Err(RwError::from(ProtocolError(format!(
275 "connection type {:?} is not allowed, allowed types: {:?}",
276 connection_type, allowed_types
277 ))));
278 }
279 Ok(())
280}
281
282fn connection_type_to_connector(connection_type: &PbConnectionType) -> &str {
283 match connection_type {
284 PbConnectionType::Kafka => KAFKA_CONNECTOR,
285 PbConnectionType::Iceberg => ICEBERG_CONNECTOR,
286 PbConnectionType::Elasticsearch => ES_SINK,
287 _ => unreachable!(),
288 }
289}
290
291pub fn check_connector_match_connection_type(
292 connector: &str,
293 connection_type: &PbConnectionType,
294) -> RwResult<()> {
295 if !connector.eq(connection_type_to_connector(connection_type)) {
296 return Err(RwError::from(ProtocolError(format!(
297 "connector {} and connection type {:?} are not compatible",
298 connector, connection_type
299 ))));
300 }
301 Ok(())
302}
303
304pub fn get_table_catalog_by_table_name(
305 session: &SessionImpl,
306 table_name: &ObjectName,
307) -> RwResult<(Arc<TableCatalog>, String)> {
308 let db_name = &session.database();
309 let (schema_name, real_table_name) =
310 Binder::resolve_schema_qualified_name(db_name, table_name)?;
311 let search_path = session.config().search_path();
312 let user_name = &session.user_name();
313
314 let schema_path = SchemaPath::new(schema_name.as_deref(), &search_path, user_name);
315 let reader = session.env().catalog_reader().read_guard();
316 let (table, schema_name) =
317 reader.get_created_table_by_name(db_name, schema_path, &real_table_name)?;
318
319 Ok((table.clone(), schema_name.to_owned()))
320}
321
322#[derive(Clone, Copy)]
341pub enum LongRunningNotificationAction {
342 SuggestRecover,
343 DiagnoseBarrierLatency,
344 MonitorBackfillJob,
345}
346
347impl LongRunningNotificationAction {
348 fn build_message(self, operation_name: &str, notify_timeout_secs: u32) -> String {
349 match self {
350 LongRunningNotificationAction::SuggestRecover => format!(
351 "{} has taken more than {} secs, likely due to high barrier latency.\n\
352 You may trigger cluster recovery to let {} take effect immediately.\n\
353 Run RECOVER in a separate session to trigger recovery.\n\
354 See: https://docs.risingwave.com/sql/commands/sql-recover#recover",
355 operation_name, notify_timeout_secs, operation_name
356 ),
357 LongRunningNotificationAction::DiagnoseBarrierLatency => format!(
358 "{} has taken more than {} secs, likely due to high barrier latency.\n\
359 See: https://docs.risingwave.com/performance/metrics#barrier-monitoring for steps to diagnose high barrier latency.",
360 operation_name, notify_timeout_secs
361 ),
362 LongRunningNotificationAction::MonitorBackfillJob => format!(
363 "{} has taken more than {} secs, barrier latency might be high. Please check barrier latency metrics to confirm.\n\
364 You can also run SHOW JOBS to track the progress of the job.\n\
365 See: https://docs.risingwave.com/performance/metrics#barrier-monitoring and https://docs.risingwave.com/sql/commands/sql-show-jobs",
366 operation_name, notify_timeout_secs
367 ),
368 }
369 }
370}
371
372pub async fn execute_with_long_running_notification<F, T>(
373 operation_fut: F,
374 session: &SessionImpl,
375 operation_name: &str,
376 action: LongRunningNotificationAction,
377) -> RwResult<T>
378where
379 F: std::future::Future<Output = RwResult<T>>,
380{
381 let notify_timeout_secs = session.config().slow_ddl_notification_secs();
382
383 if notify_timeout_secs == 0 {
385 return operation_fut.await;
386 }
387
388 let notify_fut = sleep(Duration::from_secs(notify_timeout_secs as u64));
389 tokio::pin!(operation_fut);
390
391 select! {
392 _ = notify_fut => {
393 session.notice_to_user(action.build_message(operation_name, notify_timeout_secs));
394 operation_fut.await
395 }
396 result = &mut operation_fut => {
397 result
398 }
399 }
400}
401
402#[cfg(test)]
403mod tests {
404 use postgres_types::{ToSql, Type};
405 use risingwave_common::array::*;
406
407 use super::*;
408
409 #[test]
410 fn test_to_pg_field() {
411 let field = Field::with_name(DataType::Int32, "v1");
412 let pg_field = to_pg_field(&field);
413 assert_eq!(pg_field.get_name(), "v1");
414 assert_eq!(pg_field.get_type_oid(), DataType::Int32.to_oid());
415 }
416
417 #[test]
418 fn test_to_pg_rows() {
419 let chunk = DataChunk::from_pretty(
420 "i I f T
421 1 6 6.01 aaa
422 2 . . .
423 3 7 7.01 vvv
424 4 . . . ",
425 );
426 let static_session = StaticSessionData {
427 timezone: "UTC".into(),
428 };
429 let rows = to_pg_rows(
430 &[
431 DataType::Int32,
432 DataType::Int64,
433 DataType::Float32,
434 DataType::Varchar,
435 ],
436 chunk,
437 &[],
438 &static_session,
439 );
440 let expected: Vec<Vec<Option<Bytes>>> = vec![
441 vec![
442 Some("1".into()),
443 Some("6".into()),
444 Some("6.01".into()),
445 Some("aaa".into()),
446 ],
447 vec![Some("2".into()), None, None, None],
448 vec![
449 Some("3".into()),
450 Some("7".into()),
451 Some("7.01".into()),
452 Some("vvv".into()),
453 ],
454 vec![Some("4".into()), None, None, None],
455 ];
456 let vec = rows
457 .unwrap()
458 .into_iter()
459 .map(|r| r.values().iter().cloned().collect_vec())
460 .collect_vec();
461
462 assert_eq!(vec, expected);
463 }
464
465 #[test]
466 fn test_to_pg_rows_mix_format() {
467 let chunk = DataChunk::from_pretty(
468 "i I f T
469 1 6 6.01 aaa
470 ",
471 );
472 let static_session = StaticSessionData {
473 timezone: "UTC".into(),
474 };
475 let rows = to_pg_rows(
476 &[
477 DataType::Int32,
478 DataType::Int64,
479 DataType::Float32,
480 DataType::Varchar,
481 ],
482 chunk,
483 &[Format::Binary, Format::Binary, Format::Binary, Format::Text],
484 &static_session,
485 );
486 let mut raw_params = vec![BytesMut::new(); 3];
487 1_i32.to_sql(&Type::ANY, &mut raw_params[0]).unwrap();
488 6_i64.to_sql(&Type::ANY, &mut raw_params[1]).unwrap();
489 6.01_f32.to_sql(&Type::ANY, &mut raw_params[2]).unwrap();
490 let raw_params = raw_params
491 .into_iter()
492 .map(|b| b.freeze())
493 .collect::<Vec<_>>();
494 let expected: Vec<Vec<Option<Bytes>>> = vec![vec![
495 Some(raw_params[0].clone()),
496 Some(raw_params[1].clone()),
497 Some(raw_params[2].clone()),
498 Some("aaa".into()),
499 ]];
500 let vec = rows
501 .unwrap()
502 .into_iter()
503 .map(|r| r.values().iter().cloned().collect_vec())
504 .collect_vec();
505
506 assert_eq!(vec, expected);
507 }
508
509 #[test]
510 fn test_value_format() {
511 use {DataType as T, ScalarRefImpl as S};
512 let static_session = StaticSessionData {
513 timezone: "UTC".into(),
514 };
515
516 let f = |t, d, f| pg_value_format(t, d, f, &static_session).unwrap();
517 assert_eq!(&f(&T::Float32, S::Float32(1_f32.into()), Format::Text), "1");
518 assert_eq!(
519 &f(&T::Float32, S::Float32(f32::NAN.into()), Format::Text),
520 "NaN"
521 );
522 assert_eq!(
523 &f(&T::Float64, S::Float64(f64::NAN.into()), Format::Text),
524 "NaN"
525 );
526 assert_eq!(
527 &f(&T::Float32, S::Float32(f32::INFINITY.into()), Format::Text),
528 "Infinity"
529 );
530 assert_eq!(
531 &f(
532 &T::Float32,
533 S::Float32(f32::NEG_INFINITY.into()),
534 Format::Text
535 ),
536 "-Infinity"
537 );
538 assert_eq!(
539 &f(&T::Float64, S::Float64(f64::INFINITY.into()), Format::Text),
540 "Infinity"
541 );
542 assert_eq!(
543 &f(
544 &T::Float64,
545 S::Float64(f64::NEG_INFINITY.into()),
546 Format::Text
547 ),
548 "-Infinity"
549 );
550 assert_eq!(&f(&T::Boolean, S::Bool(true), Format::Text), "t");
551 assert_eq!(&f(&T::Boolean, S::Bool(false), Format::Text), "f");
552 assert_eq!(
553 &f(
554 &T::Timestamptz,
555 S::Timestamptz(Timestamptz::from_micros(-1)),
556 Format::Text
557 ),
558 "1969-12-31 23:59:59.999999+00:00"
559 );
560 }
561}