Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends Rudof’s SPARQL SELECT result serialization so results can be emitted in additional tabular/structured formats (CSV, JSON, and Markdown), and improves error reporting for unsupported query types/formats.
Changes:
- Introduces
TableFormat/TableOptions/TableStyleand refactors SELECT table rendering to support ASCII/Markdown tables and CSV output. - Adds JSON/Markdown as supported query result formats across the core/lib/CLI enums and improves “unsupported format/type” errors to list valid options.
- Updates CLI wrappers and the changelog to reflect the new formats.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
rudof_rdf/src/rdf_core/query/query_solutions.rs |
Refactors write_table into format-specific writers and adds CSV output + table options. |
rudof_rdf/src/rdf_core/query/query_result_format.rs |
Adds Json and Markdown result formats and associated MIME types/display names. |
rudof_rdf/src/rdf_core/query/mod.rs |
Adds new table-format/types (TableFormat, TableOptions, TableStyle). |
rudof_rdf/Cargo.toml |
Adds csv dependency and minor formatting cleanup. |
rudof_lib/src/formats/query.rs |
Adds JSON/Markdown variants and improves parsing by centralizing supported aliases. |
rudof_lib/src/errors/rudof_error.rs |
Adds a SELECT-specific “unsupported result format” error variant. |
rudof_lib/src/errors/query_error.rs |
Enhances unsupported-format/type errors to include valid values dynamically. |
rudof_lib/src/api/query/implementations/serialize_query_results.rs |
Implements SELECT serialization for CSV/Markdown/JSON and returns a clearer error for unsupported SELECT formats. |
rudof_cli/src/cli/wrappers/query.rs |
Exposes new CLI result format variants (Json/Markdown/AsciiTable). |
rudof_cli/src/cli/wrappers/mod.rs |
Improves panic message when CLI enum ↔ lib enum conversion fails. |
CHANGELOG.md |
Adds an entry for #655 (but currently duplicates the v0.3.2 section). |
Cargo.lock |
Records the new csv dependency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+109
to
114
| match output_format { | ||
| TableFormat::Ascii | TableFormat::Markdown => write_ascii_table(self, writer, options, self.prefixmap()), | ||
| /*TableFormat::Json => serde_json::to_writer(writer, &self.solutions) | ||
| .map_err(|e| RDFError::WritingTableError { error: format!("{e}") }),*/ | ||
| TableFormat::Csv => write_csv_table(self, writer, self.prefixmap()), | ||
| } |
Comment on lines
+216
to
+226
| let variable_count = first.variables_iter().count(); | ||
| let mut variables = Vec::with_capacity(variable_count + 1); | ||
| variables.push(String::new()); // First column = index | ||
| variables.extend(first.variables_iter().map(|v| v.to_string())); | ||
| builder.push_record(variables); | ||
|
|
||
| // Build data rows | ||
| for (idx, result) in query_solutions.solutions.iter().enumerate() { | ||
| let mut record = Vec::with_capacity(variable_count + 1); | ||
| record.push((idx + 1).to_string()); // First column = index | ||
|
|
Comment on lines
+196
to
+200
| wtr.write_record(record) | ||
| .map_err(|e| RDFError::WritingTableError { error: format!("{e}") })?; | ||
| } | ||
| Ok(()) | ||
| } |
Comment on lines
+35
to
+41
| impl Default for TableOptions { | ||
| fn default() -> Self { | ||
| TableOptions { | ||
| show_index: false, | ||
| style: Some(TableStyle::default()), | ||
| } | ||
| } |
Comment on lines
+103
to
+108
| pub fn write_table( | ||
| &self, | ||
| writer: &mut dyn Write, | ||
| output_format: &TableFormat, | ||
| options: &TableOptions, | ||
| ) -> Result<(), RDFError> { |
Comment on lines
+24
to
+25
| /// Markdown format (for Tabled query results). | ||
| Markdown, |
Comment on lines
11
to
15
| JsonLd, | ||
| /// JSON format (for tabular query results). | ||
| Json, | ||
| /// RDF/XML format. | ||
| RdfXml, |
Comment on lines
+10
to
+14
| ## v0.3.2 | ||
| ### Added | ||
|
|
||
| ### Fixed | ||
| - #655: Support for JSON and CSV formats in SPARQL SELECT Queries |
Comment on lines
23
to
57
| match query_results { | ||
| QueryResult::Select(results) => match result_query_format { | ||
| ResultQueryFormat::Internal => { | ||
| results | ||
| .write_table(writer) | ||
| .map_err(|error| QueryError::FailedSerializingQueryResults { | ||
| error: error.to_string(), | ||
| })? | ||
| ResultQueryFormat::Internal => results | ||
| .write_table(writer, &TableFormat::default(), &TableOptions::default()) | ||
| .map_err(|error| QueryError::FailedSerializingQueryResults { | ||
| error: error.to_string(), | ||
| })?, | ||
| ResultQueryFormat::Csv => results | ||
| .write_table(writer, &TableFormat::Csv, &TableOptions::default()) | ||
| .map_err(|error| QueryError::FailedSerializingQueryResults { | ||
| error: error.to_string(), | ||
| })?, | ||
| ResultQueryFormat::Markdown => results | ||
| .write_table(writer, &TableFormat::Markdown, &TableOptions::default()) | ||
| .map_err(|error| QueryError::FailedSerializingQueryResults { | ||
| error: error.to_string(), | ||
| })?, | ||
| ResultQueryFormat::Json => { | ||
| serde_json::to_writer(writer, results).map_err(|error| QueryError::FailedSerializingQueryResults { | ||
| error: error.to_string(), | ||
| })? | ||
| }, | ||
| _ => todo!("Implement serialization of SELECT query results in formats other than Internal"), | ||
| _ => Err(RudofError::UnsupportedResultQueryFormatSelect { | ||
| format: result_query_format.to_string(), | ||
| formats: [ | ||
| ResultQueryFormat::Internal, | ||
| ResultQueryFormat::Csv, | ||
| ResultQueryFormat::Markdown, | ||
| ResultQueryFormat::Json, | ||
| ] | ||
| .iter() | ||
| .map(|a| a.to_string()) | ||
| .collect(), | ||
| })?, | ||
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solves issue 655 adding support for JSON and CSV result formats in SPARQL select queries