Skip to content

Issue655 - #696

Merged
algarfer merged 2 commits into
masterfrom
issue655
Jun 5, 2026
Merged

algarfer merged 2 commits into
masterfrom
issue655

Conversation

@labra

@labra labra commented Jun 5, 2026

Copy link
Copy Markdown
Member

Solves issue 655 adding support for JSON and CSV result formats in SPARQL select queries

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 / TableStyle and 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 thread CHANGELOG.md
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(),
})?,
},

@samuel-bustamante samuel-bustamante left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@algarfer algarfer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@algarfer
algarfer merged commit a6cb3b1 into master Jun 5, 2026
25 checks passed
@algarfer
algarfer deleted the issue655 branch June 5, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for other output formats for SPARQL queries appart from Internal

4 participants