risingwave_frontend/expr/table_function.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::{Arc, LazyLock};
use anyhow::Context;
use itertools::Itertools;
use mysql_async::consts::ColumnType as MySqlColumnType;
use mysql_async::prelude::*;
use risingwave_common::array::arrow::IcebergArrowConvert;
use risingwave_common::types::{DataType, ScalarImpl, StructType};
use risingwave_connector::source::iceberg::{
get_parquet_fields, list_s3_directory, new_s3_operator,
};
pub use risingwave_pb::expr::table_function::PbType as TableFunctionType;
use risingwave_pb::expr::PbTableFunction;
use thiserror_ext::AsReport;
use tokio::runtime::Runtime;
use tokio_postgres::types::Type as TokioPgType;
use {mysql_async, tokio_postgres};
use super::{infer_type, Expr, ExprImpl, ExprRewriter, Literal, RwResult};
use crate::catalog::function_catalog::{FunctionCatalog, FunctionKind};
use crate::error::ErrorCode::BindError;
static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
.thread_name("rw-binder-ext-query")
.enable_all()
.build()
.expect("failed to build external system querying runtime")
});
/// A table function takes a row as input and returns a table. It is also known as Set-Returning
/// Function.
///
/// See also [`TableFunction`](risingwave_expr::table_function::TableFunction) trait in expr crate
/// and [`ProjectSetSelectItem`](risingwave_pb::expr::ProjectSetSelectItem).
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct TableFunction {
pub args: Vec<ExprImpl>,
pub return_type: DataType,
pub function_type: TableFunctionType,
/// Catalog of user defined table function.
pub user_defined: Option<Arc<FunctionCatalog>>,
}
impl TableFunction {
/// Create a `TableFunction` expr with the return type inferred from `func_type` and types of
/// `inputs`.
pub fn new(func_type: TableFunctionType, mut args: Vec<ExprImpl>) -> RwResult<Self> {
let return_type = infer_type(func_type.into(), &mut args)?;
Ok(TableFunction {
args,
return_type,
function_type: func_type,
user_defined: None,
})
}
/// Create a user-defined `TableFunction`.
pub fn new_user_defined(catalog: Arc<FunctionCatalog>, args: Vec<ExprImpl>) -> Self {
let FunctionKind::Table = &catalog.kind else {
panic!("not a table function");
};
TableFunction {
args,
return_type: catalog.return_type.clone(),
function_type: TableFunctionType::UserDefined,
user_defined: Some(catalog),
}
}
/// A special table function which would be transformed into `LogicalFileScan` by `TableFunctionToFileScanRule` in the optimizer.
/// select * from `file_scan`('parquet', 's3', region, ak, sk, location)
pub fn new_file_scan(mut args: Vec<ExprImpl>) -> RwResult<Self> {
let return_type = {
// arguments:
// file format e.g. parquet
// storage type e.g. s3
// s3 region
// s3 access key
// s3 secret key
// file location
if args.len() != 6 {
return Err(BindError("file_scan function only accepts 6 arguments: file_scan('parquet', 's3', s3 region, s3 access key, s3 secret key, file location)".to_string()).into());
}
let mut eval_args: Vec<String> = vec![];
for arg in &args {
if arg.return_type() != DataType::Varchar {
return Err(BindError(
"file_scan function only accepts string arguments".to_string(),
)
.into());
}
match arg.try_fold_const() {
Some(Ok(value)) => {
if value.is_none() {
return Err(BindError(
"file_scan function does not accept null arguments".to_string(),
)
.into());
}
match value {
Some(ScalarImpl::Utf8(s)) => {
eval_args.push(s.to_string());
}
_ => {
return Err(BindError(
"file_scan function only accepts string arguments".to_string(),
)
.into())
}
}
}
Some(Err(err)) => {
return Err(err);
}
None => {
return Err(BindError(
"file_scan function only accepts constant arguments".to_string(),
)
.into());
}
}
}
if !"parquet".eq_ignore_ascii_case(&eval_args[0]) {
return Err(BindError(
"file_scan function only accepts 'parquet' as file format".to_string(),
)
.into());
}
if !"s3".eq_ignore_ascii_case(&eval_args[1]) {
return Err(BindError(
"file_scan function only accepts 's3' as storage type".to_string(),
)
.into());
}
#[cfg(madsim)]
return Err(crate::error::ErrorCode::BindError(
"file_scan can't be used in the madsim mode".to_string(),
)
.into());
#[cfg(not(madsim))]
{
let files = if eval_args[5].ends_with('/') {
let files = tokio::task::block_in_place(|| {
RUNTIME.block_on(async {
let files = list_s3_directory(
eval_args[2].clone(),
eval_args[3].clone(),
eval_args[4].clone(),
eval_args[5].clone(),
)
.await?;
Ok::<Vec<String>, anyhow::Error>(files)
})
})?;
if files.is_empty() {
return Err(BindError(
"file_scan function only accepts non-empty directory".to_string(),
)
.into());
}
Some(files)
} else {
None
};
let schema = tokio::task::block_in_place(|| {
RUNTIME.block_on(async {
let op = new_s3_operator(
eval_args[2].clone(),
eval_args[3].clone(),
eval_args[4].clone(),
match files.as_ref() {
Some(files) => files[0].clone(),
None => eval_args[5].clone(),
},
)?;
let fields = get_parquet_fields(
op,
match files.as_ref() {
Some(files) => files[0].clone(),
None => eval_args[5].clone(),
},
)
.await?;
let mut rw_types = vec![];
for field in &fields {
rw_types.push((
field.name().to_string(),
IcebergArrowConvert.type_from_field(field)?,
));
}
Ok::<risingwave_common::types::DataType, anyhow::Error>(DataType::Struct(
StructType::new(rw_types),
))
})
})?;
if let Some(files) = files {
// if the file location is a directory, we need to remove the last argument and add all files in the directory as arguments
args.remove(5);
for file in files {
args.push(ExprImpl::Literal(Box::new(Literal::new(
Some(ScalarImpl::Utf8(file.into())),
DataType::Varchar,
))));
}
}
schema
}
};
Ok(TableFunction {
args,
return_type,
function_type: TableFunctionType::FileScan,
user_defined: None,
})
}
pub fn new_postgres_query(args: Vec<ExprImpl>) -> RwResult<Self> {
let args = {
if args.len() != 6 {
return Err(BindError("postgres_query function only accepts 6 arguments: postgres_query(hostname varchar, port varchar, username varchar, password varchar, database_name varchar, postgres_query varchar)".to_string()).into());
}
let mut cast_args = Vec::with_capacity(6);
for arg in args {
let arg = arg.cast_implicit(DataType::Varchar)?;
cast_args.push(arg);
}
cast_args
};
let evaled_args = {
let mut evaled_args: Vec<String> = Vec::with_capacity(6);
for arg in &args {
match arg.try_fold_const() {
Some(Ok(value)) => {
let Some(scalar) = value else {
return Err(BindError(
"postgres_query function does not accept null arguments"
.to_string(),
)
.into());
};
evaled_args.push(scalar.into_utf8().into());
}
Some(Err(err)) => {
return Err(err);
}
None => {
return Err(BindError(
"postgres_query function only accepts constant arguments".to_string(),
)
.into());
}
}
}
evaled_args
};
#[cfg(madsim)]
{
return Err(crate::error::ErrorCode::BindError(
"postgres_query can't be used in the madsim mode".to_string(),
)
.into());
}
#[cfg(not(madsim))]
{
let schema = tokio::task::block_in_place(|| {
RUNTIME.block_on(async {
let (client, connection) = tokio_postgres::connect(
format!(
"host={} port={} user={} password={} dbname={}",
evaled_args[0],
evaled_args[1],
evaled_args[2],
evaled_args[3],
evaled_args[4]
)
.as_str(),
tokio_postgres::NoTls,
)
.await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
tracing::error!(
"mysql_query_executor: connection error: {:?}",
e.as_report()
);
}
});
let statement = client.prepare(evaled_args[5].as_str()).await?;
let mut rw_types = vec![];
for column in statement.columns() {
let name = column.name().to_string();
let data_type = match *column.type_() {
TokioPgType::BOOL => DataType::Boolean,
TokioPgType::INT2 => DataType::Int16,
TokioPgType::INT4 => DataType::Int32,
TokioPgType::INT8 => DataType::Int64,
TokioPgType::FLOAT4 => DataType::Float32,
TokioPgType::FLOAT8 => DataType::Float64,
TokioPgType::NUMERIC => DataType::Decimal,
TokioPgType::DATE => DataType::Date,
TokioPgType::TIME => DataType::Time,
TokioPgType::TIMESTAMP => DataType::Timestamp,
TokioPgType::TIMESTAMPTZ => DataType::Timestamptz,
TokioPgType::TEXT | TokioPgType::VARCHAR => DataType::Varchar,
TokioPgType::INTERVAL => DataType::Interval,
TokioPgType::JSONB => DataType::Jsonb,
TokioPgType::BYTEA => DataType::Bytea,
_ => {
return Err(crate::error::ErrorCode::BindError(
format!("unsupported column type: {}", column.type_())
.to_string(),
)
.into());
}
};
rw_types.push((name, data_type));
}
Ok::<risingwave_common::types::DataType, anyhow::Error>(DataType::Struct(
StructType::new(rw_types),
))
})
})?;
Ok(TableFunction {
args,
return_type: schema,
function_type: TableFunctionType::PostgresQuery,
user_defined: None,
})
}
}
pub fn new_mysql_query(args: Vec<ExprImpl>) -> RwResult<Self> {
static MYSQL_ARGS_LEN: usize = 6;
let args = {
if args.len() != MYSQL_ARGS_LEN {
return Err(BindError("mysql_query function only accepts 6 arguments: mysql_query(hostname varchar, port varchar, username varchar, password varchar, database_name varchar, mysql_query varchar)".to_string()).into());
}
let mut cast_args = Vec::with_capacity(MYSQL_ARGS_LEN);
for arg in args {
let arg = arg.cast_implicit(DataType::Varchar)?;
cast_args.push(arg);
}
cast_args
};
let evaled_args = {
let mut evaled_args: Vec<String> = Vec::with_capacity(MYSQL_ARGS_LEN);
for arg in &args {
match arg.try_fold_const() {
Some(Ok(value)) => {
let Some(scalar) = value else {
return Err(BindError(
"mysql_query function does not accept null arguments".to_string(),
)
.into());
};
evaled_args.push(scalar.into_utf8().into());
}
Some(Err(err)) => {
return Err(err);
}
None => {
return Err(BindError(
"mysql_query function only accepts constant arguments".to_string(),
)
.into());
}
}
}
evaled_args
};
#[cfg(madsim)]
{
return Err(crate::error::ErrorCode::BindError(
"postgres_query can't be used in the madsim mode".to_string(),
)
.into());
}
#[cfg(not(madsim))]
{
let schema = tokio::task::block_in_place(|| {
RUNTIME.block_on(async {
let database_opts: mysql_async::Opts = {
let port = evaled_args[1]
.parse::<u16>()
.context("failed to parse port")?;
mysql_async::OptsBuilder::default()
.ip_or_hostname(evaled_args[0].clone())
.tcp_port(port)
.user(Some(evaled_args[2].clone()))
.pass(Some(evaled_args[3].clone()))
.db_name(Some(evaled_args[4].clone()))
.into()
};
let pool = mysql_async::Pool::new(database_opts);
let mut conn = pool
.get_conn()
.await
.context("failed to connect to mysql in binder")?;
let query = evaled_args[5].clone();
let statement = conn
.prep(query)
.await
.context("failed to prepare mysql_query in binder")?;
let mut rw_types = vec![];
#[allow(clippy::never_loop)]
for column in statement.columns() {
let name = column.name_str().to_string();
let data_type = match column.column_type() {
// Boolean types
MySqlColumnType::MYSQL_TYPE_BIT if column.column_length() == 1 => {
DataType::Boolean
}
// Numeric types
// NOTE(kwannoel): Although `bool/boolean` is a synonym of TINY(1) in MySQL,
// we treat it as Int16 here. It is better to be straightforward in our conversion.
MySqlColumnType::MYSQL_TYPE_TINY => DataType::Int16,
MySqlColumnType::MYSQL_TYPE_SHORT => DataType::Int16,
MySqlColumnType::MYSQL_TYPE_INT24 => DataType::Int32,
MySqlColumnType::MYSQL_TYPE_LONG => DataType::Int32,
MySqlColumnType::MYSQL_TYPE_LONGLONG => DataType::Int64,
MySqlColumnType::MYSQL_TYPE_FLOAT => DataType::Float32,
MySqlColumnType::MYSQL_TYPE_DOUBLE => DataType::Float64,
MySqlColumnType::MYSQL_TYPE_NEWDECIMAL => DataType::Decimal,
MySqlColumnType::MYSQL_TYPE_DECIMAL => DataType::Decimal,
// Date time types
MySqlColumnType::MYSQL_TYPE_YEAR => DataType::Int32,
MySqlColumnType::MYSQL_TYPE_DATE => DataType::Date,
MySqlColumnType::MYSQL_TYPE_NEWDATE => DataType::Date,
MySqlColumnType::MYSQL_TYPE_TIME => DataType::Time,
MySqlColumnType::MYSQL_TYPE_TIME2 => DataType::Time,
MySqlColumnType::MYSQL_TYPE_DATETIME => DataType::Timestamp,
MySqlColumnType::MYSQL_TYPE_DATETIME2 => DataType::Timestamp,
MySqlColumnType::MYSQL_TYPE_TIMESTAMP => DataType::Timestamptz,
MySqlColumnType::MYSQL_TYPE_TIMESTAMP2 => DataType::Timestamptz,
// String types
MySqlColumnType::MYSQL_TYPE_VARCHAR
| MySqlColumnType::MYSQL_TYPE_STRING
| MySqlColumnType::MYSQL_TYPE_VAR_STRING => DataType::Varchar,
// JSON types
MySqlColumnType::MYSQL_TYPE_JSON => DataType::Jsonb,
// Binary types
MySqlColumnType::MYSQL_TYPE_BIT
| MySqlColumnType::MYSQL_TYPE_BLOB
| MySqlColumnType::MYSQL_TYPE_TINY_BLOB
| MySqlColumnType::MYSQL_TYPE_MEDIUM_BLOB
| MySqlColumnType::MYSQL_TYPE_LONG_BLOB => DataType::Bytea,
MySqlColumnType::MYSQL_TYPE_UNKNOWN
| MySqlColumnType::MYSQL_TYPE_TYPED_ARRAY
| MySqlColumnType::MYSQL_TYPE_ENUM
| MySqlColumnType::MYSQL_TYPE_SET
| MySqlColumnType::MYSQL_TYPE_GEOMETRY
| MySqlColumnType::MYSQL_TYPE_NULL => {
return Err(crate::error::ErrorCode::BindError(
format!("unsupported column type: {:?}", column.column_type())
.to_string(),
)
.into());
}
};
rw_types.push((name, data_type));
}
Ok::<risingwave_common::types::DataType, anyhow::Error>(DataType::Struct(
StructType::new(rw_types),
))
})
})?;
Ok(TableFunction {
args,
return_type: schema,
function_type: TableFunctionType::MysqlQuery,
user_defined: None,
})
}
}
pub fn to_protobuf(&self) -> PbTableFunction {
PbTableFunction {
function_type: self.function_type as i32,
args: self.args.iter().map(|c| c.to_expr_proto()).collect_vec(),
return_type: Some(self.return_type.to_protobuf()),
udf: self.user_defined.as_ref().map(|c| c.as_ref().into()),
}
}
/// Get the name of the table function.
pub fn name(&self) -> String {
match self.function_type {
TableFunctionType::UserDefined => self.user_defined.as_ref().unwrap().name.clone(),
t => t.as_str_name().to_lowercase(),
}
}
pub fn rewrite(self, rewriter: &mut impl ExprRewriter) -> Self {
Self {
args: self
.args
.into_iter()
.map(|e| rewriter.rewrite_expr(e))
.collect(),
..self
}
}
}
impl std::fmt::Debug for TableFunction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
f.debug_struct("FunctionCall")
.field("function_type", &self.function_type)
.field("return_type", &self.return_type)
.field("args", &self.args)
.finish()
} else {
let func_name = format!("{:?}", self.function_type);
let mut builder = f.debug_tuple(&func_name);
self.args.iter().for_each(|child| {
builder.field(child);
});
builder.finish()
}
}
}
impl Expr for TableFunction {
fn return_type(&self) -> DataType {
self.return_type.clone()
}
fn to_expr_proto(&self) -> risingwave_pb::expr::ExprNode {
unreachable!("Table function should not be converted to ExprNode")
}
}