Skip to content

Commit

Permalink
fix(cubesql): cast strings to timestamp (#5331)
Browse files Browse the repository at this point in the history
  • Loading branch information
gandronchik committed Sep 30, 2022
1 parent 334af8c commit a706258
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/cubejs-backend-native/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl NodeConfig {
) -> CubeServices {
let injector = self.config.injector();

self.config.configure_injector().await;
self.config.configure().await;

injector
.register_typed::<dyn TransportService, _, _, _>(async move |_| transport)
Expand Down
7 changes: 7 additions & 0 deletions packages/cubejs-backend-native/test/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ describe('SQLInterface', () => {
}
}

{
const [result] = await connection.query('select CAST(\'2020-12-25 22:48:48.000\' AS timestamp)');
console.log(result);

expect(result).toEqual([{"CAST(Utf8(\"2020-12-25 22:48:48.000\") AS Timestamp(Nanosecond, None))": "2020-12-25T22:48:48.000"}]);
}

// Increment it in case you throw Error
setTimeout(_ => {
expect(logger.mock.calls.length).toEqual(1);
Expand Down
3 changes: 2 additions & 1 deletion rust/cubesql/cubesql/e2e/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl MySqlIntegrationTestSuite {
c
});

let services = config.configure().await;
config.configure().await;
let services = config.cube_services().await;
services.wait_processing_loops().await.unwrap();
});

Expand Down
3 changes: 2 additions & 1 deletion rust/cubesql/cubesql/e2e/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ impl PostgresIntegrationTestSuite {
c
});

let services = config.configure().await;
config.configure().await;
let services = config.cube_services().await;
services.wait_processing_loops().await.unwrap();
});

Expand Down
3 changes: 2 additions & 1 deletion rust/cubesql/cubesql/src/bin/cubesqld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ fn main() {

let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
runtime.block_on(async move {
let services = config.configure().await;
config.configure().await;
let services = config.cube_services().await;
log::debug!("Cube SQL Start");
stop_on_ctrl_c(&services).await;
services.wait_processing_loops().await.unwrap();
Expand Down
29 changes: 29 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,8 @@ mod tests {
}

async fn convert_select_to_query_plan(query: String, db: DatabaseProtocol) -> QueryPlan {
env::set_var("TZ", "UTC");

let query =
convert_sql_to_cube_query(&query, get_test_tenant_ctx(), get_test_session(db).await)
.await;
Expand Down Expand Up @@ -4654,6 +4656,8 @@ ORDER BY \"COUNT(count)\" DESC"
queries: Vec<String>,
db: DatabaseProtocol,
) -> Result<(String, StatusFlags), CubeError> {
env::set_var("TZ", "UTC");

let meta = get_test_tenant_ctx();
let session = get_test_session(db).await;

Expand Down Expand Up @@ -12440,4 +12444,29 @@ ORDER BY \"COUNT(count)\" DESC"
true
);
}

#[tokio::test]
async fn test_cast_to_timestamp_timezone_utc() -> Result<(), CubeError> {
init_logger();

insta::assert_snapshot!(
"test_cast_to_timestamp_timezone_utc_1",
execute_query(
"select CAST ('2020-12-25 22:48:48.000' AS timestamptz)".to_string(),
DatabaseProtocol::PostgreSQL
)
.await?
);

insta::assert_snapshot!(
"test_cast_to_timestamp_timezone_utc_2",
execute_query(
"select CAST ('2020-12-25 22:48:48.000' AS timestamp)".to_string(),
DatabaseProtocol::PostgreSQL
)
.await?
);

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: cubesql/src/compile/mod.rs
expression: "execute_query(\"select CAST ('2020-12-25 22:48:48.000' AS timestamptz)\".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
---
+----------------------------------------------------------------------+
| CAST(Utf8("2020-12-25 22:48:48.000") AS Timestamp(Nanosecond, None)) |
+----------------------------------------------------------------------+
| 2020-12-25T22:48:48.000 |
+----------------------------------------------------------------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: cubesql/src/compile/mod.rs
expression: "execute_query(\"select CAST ('2020-12-25 22:48:48.000' AS timestamp)\".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
---
+----------------------------------------------------------------------+
| CAST(Utf8("2020-12-25 22:48:48.000") AS Timestamp(Nanosecond, None)) |
+----------------------------------------------------------------------+
| 2020-12-25T22:48:48.000 |
+----------------------------------------------------------------------+
12 changes: 9 additions & 3 deletions rust/cubesql/cubesql/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub struct ConfigObjImpl {
pub postgres_bind_address: Option<String>,
pub nonce: Option<Vec<u8>>,
pub query_timeout: u64,
pub timezone: Option<String>,
}

crate::di_service!(ConfigObjImpl, [ConfigObj]);
Expand Down Expand Up @@ -172,19 +173,22 @@ impl Config {
.map(|port| format!("0.0.0.0:{}", port.parse::<u16>().unwrap())),
nonce: None,
query_timeout,
timezone: Some("UTC".to_string()),
}),
}
}

pub fn test(_name: &str) -> Config {
let query_timeout = 15;
let timezone = Some("UTC".to_string());
Config {
injector: Injector::new(),
config_obj: Arc::new(ConfigObjImpl {
bind_address: None,
postgres_bind_address: None,
nonce: None,
query_timeout,
timezone,
}),
}
}
Expand Down Expand Up @@ -274,10 +278,12 @@ impl Config {
}
}

pub async fn configure(&self) -> CubeServices {
self.configure_injector().await;
pub async fn configure(&self) {
if let Some(timezone) = &self.config_obj.timezone {
env::set_var("TZ", timezone.as_str());
}

self.cube_services().await
self.configure_injector().await;
}
}

Expand Down

0 comments on commit a706258

Please sign in to comment.