Skip to content

Commit

Permalink
feat: add a temporary sql rewrite for cast in limit
Browse files Browse the repository at this point in the history
this issue is described in GreptimeTeam#5097
  • Loading branch information
sunng87 committed Dec 5, 2024
1 parent deff6b9 commit 3793d1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/servers/src/postgres/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// 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;

Expand Down Expand Up @@ -94,6 +95,13 @@ pub(crate) fn process<'a>(query: &str, _query_ctx: QueryContextRef) -> Option<Ve
}
}

static LIMIT_CAST_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("(?i)(LIMIT\\s+\\d+)::bigint").unwrap());
pub(crate) fn rewrite_sql<'a>(query: &'a str) -> Cow<'a, 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};
Expand Down Expand Up @@ -164,4 +172,13 @@ mod test {
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));
}
}
6 changes: 6 additions & 0 deletions src/servers/src/postgres/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ impl SimpleQueryHandler for PostgresServerHandlerInner {
return Ok(vec![Response::EmptyQuery]);
}

let query = fixtures::rewrite_sql(query);
let query = query.as_ref();

if let Some(resps) = fixtures::process(query, query_ctx.clone()) {
send_warning_opt(client, query_ctx).await?;
Ok(resps)
Expand Down Expand Up @@ -229,6 +232,9 @@ impl QueryParser for DefaultQueryParser {
});
}

let sql = fixtures::rewrite_sql(sql);
let sql = sql.as_ref();

let mut stmts =
ParserContext::create_with_dialect(sql, &PostgreSqlDialect {}, ParseOptions::default())
.map_err(|e| PgWireError::ApiError(Box::new(e)))?;
Expand Down
4 changes: 2 additions & 2 deletions tests-integration/tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ pub async fn test_declare_fetch_close_cursor(store_type: StorageType) {

client
.execute(
"DECLARE c1 CURSOR FOR SELECT * FROM numbers WHERE number > 2",
"DECLARE c1 CURSOR FOR SELECT * FROM numbers WHERE number > 2 LIMIT 50::bigint",
&[],
)
.await
Expand All @@ -1225,7 +1225,7 @@ pub async fn test_declare_fetch_close_cursor(store_type: StorageType) {
assert_eq!(5, rows.len());

let rows = client.query("FETCH 100 FROM c1", &[]).await.unwrap();
assert_eq!(92, rows.len());
assert_eq!(45, rows.len());

client.execute("CLOSE c1", &[]).await.expect("close cursor");

Expand Down

0 comments on commit 3793d1b

Please sign in to comment.