Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more transaction related statement for postgres interface #5081

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/servers/src/postgres/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,40 @@ static VAR_VALUES: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
static SHOW_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new("(?i)^SHOW (.*?);?$").unwrap());
static SET_TRANSACTION_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)^SET TRANSACTION (.*?);?$").unwrap());
static TRANSACTION_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)^(BEGIN|ROLLBACK|COMMIT);?").unwrap());
static START_TRANSACTION_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)^(START TRANSACTION.*|BEGIN);?").unwrap());
static COMMIT_TRANSACTION_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)^(COMMIT TRANSACTION|COMMIT);?").unwrap());
static ABORT_TRANSACTION_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)^(ABORT TRANSACTION|ROLLBACK);?").unwrap());

/// Test if given query statement matches the patterns
pub(crate) fn matches(query: &str) -> bool {
TRANSACTION_PATTERN.captures(query).is_some()
START_TRANSACTION_PATTERN.is_match(query)
|| COMMIT_TRANSACTION_PATTERN.is_match(query)
|| ABORT_TRANSACTION_PATTERN.is_match(query)
|| SHOW_PATTERN.captures(query).is_some()
|| SET_TRANSACTION_PATTERN.is_match(query)
}

fn set_transaction_warning(query_ctx: QueryContextRef) {
query_ctx.set_warning("Please note transaction is not supported in GreptimeDB.".to_string());
}

/// Process unsupported SQL and return fixed result as a compatibility solution
pub(crate) fn process<'a>(query: &str, _query_ctx: QueryContextRef) -> Option<Vec<Response<'a>>> {
pub(crate) fn process<'a>(query: &str, query_ctx: QueryContextRef) -> Option<Vec<Response<'a>>> {
// Transaction directives:
if let Some(tx) = TRANSACTION_PATTERN.captures(query) {
let tx_tag = &tx[1];
Some(vec![Response::Execution(Tag::new(&tx_tag.to_uppercase()))])
if START_TRANSACTION_PATTERN.is_match(query) {
set_transaction_warning(query_ctx);
if query.to_lowercase().starts_with("begin") {
Some(vec![Response::Execution(Tag::new("BEGIN"))])
} else {
Some(vec![Response::Execution(Tag::new("START TRANSACTION"))])
}
} else if ABORT_TRANSACTION_PATTERN.is_match(query) {
Some(vec![Response::Execution(Tag::new("ROLLBACK"))])
} else if COMMIT_TRANSACTION_PATTERN.is_match(query) {
Some(vec![Response::Execution(Tag::new("COMMIT"))])
} else if let Some(show_var) = SHOW_PATTERN.captures(query) {
let show_var = show_var[1].to_lowercase();
if let Some(value) = VAR_VALUES.get(&show_var.as_ref()) {
Expand Down Expand Up @@ -150,6 +168,19 @@ mod test {
"SET",
query_context.clone(),
);
assert_tag(
"START TRANSACTION isolation level READ COMMITTED;",
"START TRANSACTION",
query_context.clone(),
);
assert_tag(
"start transaction isolation level READ COMMITTED;",
"START TRANSACTION",
query_context.clone(),
);
assert_tag("abort transaction;", "ROLLBACK", query_context.clone());
assert_tag("commit transaction;", "COMMIT", query_context.clone());
assert_tag("COMMIT transaction;", "COMMIT", query_context.clone());

let resp = get_data("SHOW transaction isolation level", query_context.clone());
assert_eq!(1, resp.row_schema().len());
Expand Down
Loading