Skip to content

Commit 08b1053

Browse files
fix: panic when record not found (#6)
Co-authored-by: CangSheng Shen <67856731+shencangsheng@users.noreply.github.com>
1 parent 641db6f commit 08b1053

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src-tauri/src/commands/query.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,12 @@ pub async fn fetch(
7575
let _ = insert_query_history(&app, &sql, "fail");
7676
err
7777
})?;
78-
let records = collect(&mut context, &new_sql).await.map_err(|err| {
78+
79+
let (header, records) = collect(&mut context, &new_sql).await.map_err(|err| {
7980
let _ = insert_query_history(&app, &sql, "fail");
8081
err
8182
})?;
82-
83-
let row = &records[0];
84-
let width = row.columns().len();
85-
86-
let header: Vec<String> = row
87-
.schema()
88-
.fields()
89-
.iter()
90-
.map(|c| c.name().to_string())
91-
.collect();
83+
let width = header.len();
9284

9385
// Pre-calculate the total number of rows to avoid frequent reallocation
9486
let total_rows: usize = records.iter().map(|r| r.num_rows()).sum();

src-tauri/src/context/context.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use sqlparser::ast::{
1717
TableFunctionArgs, Value,
1818
};
1919
use std::collections::HashMap;
20-
use std::future::Future;
21-
use std::pin::Pin;
2220
use std::sync::Arc;
2321

2422
pub fn get_sql_context() -> SessionContext {
@@ -29,12 +27,23 @@ pub async fn get_data_frame(ctx: &mut SessionContext, sql: &String) -> AppResult
2927
ctx.sql(sql).await.map_err(AppError::from)
3028
}
3129

32-
pub async fn collect(ctx: &mut SessionContext, sql: &String) -> AppResult<Vec<RecordBatch>> {
33-
get_data_frame(ctx, sql)
34-
.await?
35-
.collect()
36-
.await
37-
.map_err(AppError::from)
30+
pub async fn collect(
31+
ctx: &mut SessionContext,
32+
sql: &String,
33+
) -> AppResult<(Vec<String>, Vec<RecordBatch>)> {
34+
let df = get_data_frame(ctx, sql).await?;
35+
36+
// Get header from DataFrame's schema (column information can be retrieved even if there's no data)
37+
let header: Vec<String> = df
38+
.schema()
39+
.fields()
40+
.iter()
41+
.map(|f| f.name().to_string())
42+
.collect();
43+
44+
let records = df.collect().await.map_err(AppError::from)?;
45+
46+
Ok((header, records))
3847
}
3948

4049
pub fn get_csv_read_options(
@@ -296,12 +305,10 @@ pub async fn convert_table_name(
296305
for join in &mut table_with_joins.joins {
297306
match &mut join.relation {
298307
TableFactor::Derived { subquery, .. } => {
299-
table_count =
300-
convert_table_name(ctx, subquery, table_count).await?;
308+
table_count = convert_table_name(ctx, subquery, table_count).await?;
301309
}
302310
relation => {
303-
table_count =
304-
register_table(ctx, &mut join.relation, table_count).await?;
311+
table_count = register_table(ctx, &mut join.relation, table_count).await?;
305312
}
306313
}
307314
}

src/pages/notebook/notebook-middle/notebook-middle-data-result.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,16 @@ function DataResult({ data, isLoading }: DataResultProps) {
145145
</table>
146146
<div className="flex flex-col items-center justify-center gap-2 text-default-400 pt-16">
147147
<FontAwesomeIcon icon={faTable} size="2x" />
148-
<p className="font-medium text-default-500">No data available</p>
149-
<p className="text-sm">Run a query to see results here</p>
148+
{data.header.length > 0 ? (
149+
<p className="font-medium text-default-500">No records found</p>
150+
) : (
151+
<>
152+
<p className="font-medium text-default-500">
153+
No data available
154+
</p>
155+
<p className="text-sm">Run a query to see results here</p>
156+
</>
157+
)}
150158
</div>
151159
</div>
152160
</div>

0 commit comments

Comments
 (0)