servers/postgres/
fixtures.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
// Copyright 2023 Greptime Team
//
// 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::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;

use futures::stream;
use once_cell::sync::Lazy;
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag};
use pgwire::api::Type;
use pgwire::error::PgWireResult;
use pgwire::messages::data::DataRow;
use regex::Regex;
use session::context::QueryContextRef;

fn build_string_data_rows(
    schema: Arc<Vec<FieldInfo>>,
    rows: Vec<Vec<String>>,
) -> Vec<PgWireResult<DataRow>> {
    rows.iter()
        .map(|row| {
            let mut encoder = DataRowEncoder::new(schema.clone());
            for value in row {
                encoder.encode_field(&Some(value))?;
            }
            encoder.finish()
        })
        .collect()
}

static VAR_VALUES: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
    HashMap::from([
        ("default_transaction_isolation", "read committed"),
        ("transaction isolation level", "read committed"),
        ("standard_conforming_strings", "on"),
        ("client_encoding", "UTF8"),
    ])
});

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 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 {
    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>>> {
    // Transaction directives:
    if START_TRANSACTION_PATTERN.is_match(query) {
        set_transaction_warning(query_ctx);
        if query.to_lowercase().starts_with("begin") {
            Some(vec![Response::TransactionStart(Tag::new("BEGIN"))])
        } else {
            Some(vec![Response::TransactionStart(Tag::new(
                "START TRANSACTION",
            ))])
        }
    } else if ABORT_TRANSACTION_PATTERN.is_match(query) {
        Some(vec![Response::TransactionEnd(Tag::new("ROLLBACK"))])
    } else if COMMIT_TRANSACTION_PATTERN.is_match(query) {
        Some(vec![Response::TransactionEnd(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()) {
            let f1 = FieldInfo::new(
                show_var.clone(),
                None,
                None,
                Type::VARCHAR,
                FieldFormat::Text,
            );
            let schema = Arc::new(vec![f1]);
            let data = stream::iter(build_string_data_rows(
                schema.clone(),
                vec![vec![value.to_string()]],
            ));

            Some(vec![Response::Query(QueryResponse::new(schema, data))])
        } else {
            None
        }
    } else if SET_TRANSACTION_PATTERN.is_match(query) {
        Some(vec![Response::Execution(Tag::new("SET"))])
    } else {
        None
    }
}

static LIMIT_CAST_PATTERN: Lazy<Regex> =
    Lazy::new(|| Regex::new("(?i)(LIMIT\\s+\\d+)::bigint").unwrap());
pub(crate) fn rewrite_sql(query: &str) -> Cow<'_, str> {
    //TODO(sunng87): remove this when we upgraded datafusion to 43 or newer
    LIMIT_CAST_PATTERN.replace_all(query, "$1")
}

#[cfg(test)]
mod test {
    use session::context::{QueryContext, QueryContextRef};

    use super::*;

    fn assert_tag(q: &str, t: &str, query_context: QueryContextRef) {
        if let Response::Execution(tag)
        | Response::TransactionStart(tag)
        | Response::TransactionEnd(tag) = process(q, query_context.clone())
            .unwrap_or_else(|| panic!("fail to match {}", q))
            .remove(0)
        {
            assert_eq!(Tag::new(t), tag);
        } else {
            panic!("Invalid response");
        }
    }

    fn get_data<'a>(q: &str, query_context: QueryContextRef) -> QueryResponse<'a> {
        if let Response::Query(resp) = process(q, query_context.clone())
            .unwrap_or_else(|| panic!("fail to match {}", q))
            .remove(0)
        {
            resp
        } else {
            panic!("Invalid response");
        }
    }

    #[test]
    fn test_process() {
        let query_context = QueryContext::arc();

        assert_tag("BEGIN", "BEGIN", query_context.clone());
        assert_tag("BEGIN;", "BEGIN", query_context.clone());
        assert_tag("begin;", "BEGIN", query_context.clone());
        assert_tag("ROLLBACK", "ROLLBACK", query_context.clone());
        assert_tag("ROLLBACK;", "ROLLBACK", query_context.clone());
        assert_tag("rollback;", "ROLLBACK", query_context.clone());
        assert_tag("COMMIT", "COMMIT", query_context.clone());
        assert_tag("COMMIT;", "COMMIT", query_context.clone());
        assert_tag("commit;", "COMMIT", query_context.clone());
        assert_tag(
            "SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
            "SET",
            query_context.clone(),
        );
        assert_tag(
            "SET TRANSACTION ISOLATION LEVEL READ COMMITTED;",
            "SET",
            query_context.clone(),
        );
        assert_tag(
            "SET transaction isolation level READ COMMITTED;",
            "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());
        let resp = get_data("show client_encoding;", query_context.clone());
        assert_eq!(1, resp.row_schema().len());
        let resp = get_data("show standard_conforming_strings;", query_context.clone());
        assert_eq!(1, resp.row_schema().len());
        let resp = get_data("show default_transaction_isolation", query_context.clone());
        assert_eq!(1, resp.row_schema().len());

        assert!(process("SELECT 1", query_context.clone()).is_none());
        assert!(process("SHOW TABLES ", query_context.clone()).is_none());
        assert!(process("SET TIME_ZONE=utc ", query_context.clone()).is_none());
    }

    #[test]
    fn test_rewrite() {
        let sql = "SELECT * FROM number LIMIT 1::bigint";
        let sql2 = "SELECT * FROM number limit      1::BIGINT";

        assert_eq!("SELECT * FROM number LIMIT 1", rewrite_sql(sql));
        assert_eq!("SELECT * FROM number limit      1", rewrite_sql(sql2));
    }
}