Skip to content

Commit f6560ed

Browse files
committed
Resolve review-changes that dont require refactoring
1 parent 1dfb388 commit f6560ed

File tree

10 files changed

+100
-133
lines changed

10 files changed

+100
-133
lines changed

nemo-physical/src/resource.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This modules defines the [Resource] type, which is part of the public interface of this crate.
22
//!
33
4-
use core::fmt;
4+
use std::fmt;
55

66
/// Resource that can be referenced in source declarations in Nemo programs
77
/// Resources are resolved using `nemo::io::resource_providers::ResourceProviders`

nemo/src/io/format_builder.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,16 @@ pub(crate) trait FormatBuilder: Sized + Into<AnyImportExportBuilder> {
243243
tag: Self::Tag,
244244
parameters: &Parameters<Self>,
245245
direction: Direction,
246-
) -> Result<(Self, Option<ResourceSpec>), ValidationErrorKind>;
246+
) -> Result<Self, ValidationErrorKind>;
247247

248248
fn expected_arity(&self) -> Option<usize>;
249249

250250
fn supports_tag(tag: &str) -> bool {
251251
Self::Tag::from_str(tag).is_ok()
252252
}
253-
253+
fn override_resource_spec(&self, _direction: Direction) -> Option<ResourceSpec> {
254+
None
255+
}
254256
fn build_import(&self, arity: usize) -> Arc<dyn ImportHandler + Send + Sync + 'static>;
255257
fn build_export(&self, arity: usize) -> Arc<dyn ExportHandler + Send + Sync + 'static>;
256258
}
@@ -405,10 +407,6 @@ impl ImportExportBuilder {
405407
Ok(resource) => Some(resource), // Success: return Some(ResourceSpec)
406408
Err(kind) => {
407409
// Report the error and return None
408-
println!(
409-
"throw error, resource not correct: {:?} {:?}",
410-
origin, kind
411-
);
412410
builder.report_error(origin, kind);
413411
None
414412
}
@@ -423,28 +421,22 @@ impl ImportExportBuilder {
423421
})
424422
.unwrap_or_default();
425423

426-
let (inner, new_res) = match B::new(tag, &parameters, direction) {
424+
let inner = match B::new(tag, &parameters, direction) {
427425
Ok(res) => Some(res),
428426
Err(kind) => {
429427
builder.report_error(origin, kind);
430428
None
431429
}
432430
}?;
433431

432+
let new_resource = inner.override_resource_spec(direction);
433+
434434
// Use the [ResourceSpec] from the Builder if available
435-
if new_res.is_some() {
436-
Some(ImportExportBuilder {
437-
inner: inner.into(),
438-
resource: new_res,
439-
compression,
440-
})
441-
} else {
442-
Some(ImportExportBuilder {
443-
inner: inner.into(),
444-
resource,
445-
compression,
446-
})
447-
}
435+
Some(ImportExportBuilder {
436+
inner: inner.into(),
437+
resource: new_resource.or(resource),
438+
compression,
439+
})
448440
}
449441

450442
pub(crate) fn new(

nemo/src/io/formats/dsv.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ use crate::{
3131
syntax::import_export::{attribute, file_format},
3232
};
3333

34-
use super::{
35-
ExportHandler, FileFormatMeta, FormatBuilder, ImportHandler, ResourceSpec, TableWriter,
36-
};
34+
use super::{ExportHandler, FileFormatMeta, FormatBuilder, ImportHandler, TableWriter};
3735

3836
/// Implements [ImportHandler] and [ExportHandler] for delimiter-separated values.
3937
#[derive(Debug, Clone)]
@@ -200,10 +198,10 @@ impl FormatBuilder for DsvBuilder {
200198
tag: Self::Tag,
201199
parameters: &Parameters<DsvBuilder>,
202200
_direction: Direction,
203-
) -> Result<(Self, Option<ResourceSpec>), ValidationErrorKind> {
201+
) -> Result<Self, ValidationErrorKind> {
204202
let value_formats = parameters
205203
.get_optional(DsvParameter::Format)
206-
.map(|value| DsvValueFormats::try_from(value).unwrap());
204+
.map(|value| DsvValueFormats::try_from(value).expect("value formats have already been validated"));
207205

208206
let limit = parameters
209207
.get_optional(DsvParameter::Limit)
@@ -226,15 +224,12 @@ impl FormatBuilder for DsvBuilder {
226224
.map(AnyDataValue::to_boolean_unchecked)
227225
.unwrap_or(false);
228226

229-
Ok((
230-
Self {
231-
delimiter,
232-
value_formats,
233-
limit,
234-
ignore_headers,
235-
},
236-
None,
237-
))
227+
Ok(Self {
228+
delimiter,
229+
value_formats,
230+
limit,
231+
ignore_headers,
232+
})
238233
}
239234

240235
fn expected_arity(&self) -> Option<usize> {

nemo/src/io/formats/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
syntax::import_export::file_format,
1818
};
1919

20-
use super::{FileFormatMeta, ImportHandler, ResourceSpec};
20+
use super::{FileFormatMeta, ImportHandler};
2121

2222
#[derive(Debug, Clone)]
2323
pub(crate) struct JsonHandler;
@@ -58,12 +58,12 @@ impl FormatBuilder for JsonHandler {
5858
_tag: Self::Tag,
5959
_parameters: &Parameters<Self>,
6060
direction: Direction,
61-
) -> Result<(Self, Option<ResourceSpec>), ValidationErrorKind> {
61+
) -> Result<Self, ValidationErrorKind> {
6262
if matches!(direction, Direction::Export) {
6363
return Err(ValidationErrorKind::UnsupportedJsonExport);
6464
}
6565

66-
Ok((Self, None))
66+
Ok(Self)
6767
}
6868

6969
fn expected_arity(&self) -> Option<usize> {

nemo/src/io/formats/rdf.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{
4242
};
4343

4444
use super::FileFormatMeta;
45-
use super::{ExportHandler, FormatBuilder, ImportHandler, ResourceSpec, TableWriter};
45+
use super::{ExportHandler, FormatBuilder, ImportHandler, TableWriter};
4646

4747
/// The different supported variants of the RDF format.
4848
#[derive(Assoc, Debug, Clone, Copy, PartialEq, Eq, VariantArray)]
@@ -198,7 +198,7 @@ impl FormatBuilder for RdfHandler {
198198
tag: Self::Tag,
199199
parameters: &Parameters<RdfHandler>,
200200
_direction: Direction,
201-
) -> Result<(Self, Option<ResourceSpec>), ValidationErrorKind> {
201+
) -> Result<Self, ValidationErrorKind> {
202202
let variant = match tag {
203203
RdfTag::Rdf => {
204204
let value = parameters
@@ -243,15 +243,12 @@ impl FormatBuilder for RdfHandler {
243243
.as_ref()
244244
.map(AnyDataValue::to_u64_unchecked);
245245

246-
Ok((
247-
Self {
248-
base,
249-
variant,
250-
value_formats,
251-
limit,
252-
},
253-
None,
254-
))
246+
Ok(Self {
247+
base,
248+
variant,
249+
value_formats,
250+
limit,
251+
})
255252
}
256253

257254
fn expected_arity(&self) -> Option<usize> {

nemo/src/io/formats/sparql.rs

+39-53
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
//! Handler for resources of type Sparql (java script object notation).
1+
//! Handler for resources of type SPARQL (SPARQL query language for RDF)).
22
33
use spargebra::Query;
44
use std::sync::Arc;
55

66
use nemo_physical::datavalues::{AnyDataValue, DataValue};
77
use oxiri::Iri;
88

9-
//use reader::DsvReader;
10-
//use value_format::DsvValueFormats;
11-
//use writer::DsvWriter;
12-
139
use crate::{
1410
io::format_builder::{
1511
format_parameter, format_tag, value_type_matches, AnyImportExportBuilder, FormatParameter,
@@ -39,18 +35,12 @@ format_parameter! {
3935
Base(name = attribute::BASE, supported_types = &[ValueType::Constant]),
4036
Endpoint(name = attribute::ENDPOINT, supported_types = &[ValueType::Constant]),
4137
Query(name = attribute::QUERY, supported_types = &[ValueType::String]),
42-
IgnoreHeaders(name = attribute::IGNORE_HEADERS, supported_types = &[ValueType::Boolean]),
43-
4438
}
4539
}
4640

4741
impl FormatParameter<SparqlTag> for SparqlParameter {
4842
fn required_for(&self, tag: SparqlTag) -> bool {
49-
if matches!(tag, SparqlTag::Sparql) {
50-
matches!(self, SparqlParameter::Endpoint) && matches!(self, SparqlParameter::Query)
51-
} else {
52-
false
53-
}
43+
matches!(tag, SparqlTag::Sparql) && matches!(self, Self::Endpoint | Self::Query)
5444
}
5545

5646
fn is_value_valid(&self, value: AnyDataValue) -> Result<(), ValidationErrorKind> {
@@ -67,10 +57,9 @@ impl FormatParameter<SparqlTag> for SparqlParameter {
6757
SparqlParameter::Base => Ok(()),
6858
SparqlParameter::Format => DsvValueFormats::try_from(value).and(Ok(())).map_err(|_| {
6959
ValidationErrorKind::ImportExportValueFormat {
70-
file_format: "rdf".to_string(),
60+
file_format: "sparql".to_string(),
7161
}
7262
}),
73-
SparqlParameter::IgnoreHeaders => Ok(()),
7463
SparqlParameter::Endpoint => value
7564
.to_iri()
7665
.ok_or(ValidationErrorKind::ImportExportInvalidIri)
@@ -79,27 +68,23 @@ impl FormatParameter<SparqlTag> for SparqlParameter {
7968
.map(|_| ())
8069
.map_err(|_| ValidationErrorKind::ImportExportInvalidIri)
8170
}),
82-
SparqlParameter::Query => value
83-
.to_plain_string()
84-
.ok_or(ValidationErrorKind::ImportExportLimitNegative)
85-
.and_then(|query| {
86-
Query::parse(query.as_str(), None).map(|_| ()).map_err(|e| {
87-
ValidationErrorKind::ImportExportInvalidSparqlQuery {
88-
oxi_error: e.to_string(),
89-
}
71+
SparqlParameter::Query => {
72+
Query::parse(value.to_plain_string_unchecked().as_str(), None)
73+
.map(|_| ())
74+
.map_err(|e| ValidationErrorKind::ImportExportInvalidSparqlQuery {
75+
oxi_error: e.to_string(),
9076
})
91-
}),
77+
}
9278
}
9379
}
9480
}
9581

9682
#[derive(Clone)]
9783
pub(crate) struct SparqlBuilder {
9884
limit: Option<u64>,
99-
delimiter: u8,
10085
value_formats: Option<DsvValueFormats>,
101-
ignore_headers: bool,
102-
parsed_query: Query,
86+
endpoint: Iri<String>,
87+
query: Query,
10388
}
10489

10590
impl From<SparqlBuilder> for AnyImportExportBuilder {
@@ -114,52 +99,53 @@ impl FormatBuilder for SparqlBuilder {
11499
_tag: Self::Tag,
115100
parameters: &Parameters<SparqlBuilder>,
116101
_direction: Direction,
117-
) -> Result<(Self, Option<ResourceSpec>), ValidationErrorKind> {
102+
) -> Result<Self, ValidationErrorKind> {
118103
// Copied from DsvBuilder
119104
let value_formats = parameters
120105
.get_optional(SparqlParameter::Format)
121-
.map(|value| DsvValueFormats::try_from(value).unwrap());
106+
.map(|value| DsvValueFormats::try_from(value).expect("value formats have already been validated"));
122107

123108
let limit = parameters
124109
.get_optional(SparqlParameter::Limit)
125110
.map(|value| value.to_u64_unchecked());
126111

127-
let ignore_headers = parameters
128-
.get_optional(SparqlParameter::IgnoreHeaders)
129-
.as_ref()
130-
.map(AnyDataValue::to_boolean_unchecked)
131-
.unwrap_or(false);
132-
133-
// Sparql-specific fields
112+
// SPARQL-specific fields
134113
let endpoint = Iri::parse_unchecked(
135114
parameters
136-
.get_required(SparqlParameter::Endpoint.into())
115+
.get_required(SparqlParameter::Endpoint)
137116
.to_iri_unchecked(),
138117
);
139118

140119
let query = parameters
141-
.get_required(SparqlParameter::Query.into())
120+
.get_required(SparqlParameter::Query)
142121
.to_plain_string_unchecked();
143122

144-
let parsed_query = Query::parse(query.as_str(), None).unwrap();
123+
let query = Query::parse(query.as_str(), None).expect("query has already been validated");
145124

146125
// Create and return a new ResourceSpec to replace the existing ResourceSpec
147-
let resource = ResourceSpec::from_endpoint(endpoint, query);
148-
Ok((
149-
Self {
150-
delimiter: b'\t',
151-
value_formats,
152-
limit,
153-
ignore_headers,
154-
parsed_query,
155-
},
156-
Some(resource),
157-
))
126+
Ok(Self {
127+
value_formats,
128+
limit,
129+
endpoint,
130+
query,
131+
})
132+
}
133+
134+
// TODO: do we need to parse parameters here again? that would be annoying
135+
fn override_resource_spec(&self, direction: Direction) -> Option<ResourceSpec> {
136+
if matches!(direction, Direction::Export) {
137+
None
138+
} else {
139+
Some(ResourceSpec::from_endpoint(
140+
self.endpoint.clone(),
141+
self.query.to_sse(),
142+
))
143+
}
158144
}
159145

160146
fn expected_arity(&self) -> Option<usize> {
161147
let mut var_count: usize = 0;
162-
match &self.parsed_query {
148+
match &self.query {
163149
Query::Select { pattern, .. }
164150
| Query::Describe { pattern, .. }
165151
| Query::Construct { pattern, .. }
@@ -170,17 +156,17 @@ impl FormatBuilder for SparqlBuilder {
170156

171157
fn build_import(&self, arity: usize) -> Arc<dyn ImportHandler + Send + Sync + 'static> {
172158
Arc::new(DsvHandler {
173-
delimiter: self.delimiter,
159+
delimiter: b'\t',
174160
value_formats: self
175161
.value_formats
176162
.clone()
177163
.unwrap_or(DsvValueFormats::default(arity)),
178164
limit: self.limit,
179-
ignore_headers: self.ignore_headers,
165+
ignore_headers: true,
180166
})
181167
}
182168

183169
fn build_export(&self, _arity: usize) -> Arc<dyn ExportHandler + Send + Sync + 'static> {
184-
unimplemented!("Sparql export is currently not supported")
170+
unimplemented!("SPARQL export is currently not supported")
185171
}
186172
}

0 commit comments

Comments
 (0)