Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct Datatypes for StringExpressions #1615

Closed
wants to merge 0 commits into from

Conversation

DuDaAG
Copy link
Contributor

@DuDaAG DuDaAG commented Nov 13, 2024

No description provided.

@DuDaAG DuDaAG marked this pull request as ready for review November 13, 2024 11:03
Copy link
Member

@joka921 joka921 left a comment

Choose a reason for hiding this comment

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

Some initial comments on the parts of the code written so far.

Comment on lines 232 to 269
switch (id.getDatatype()) {
case Undefined:
return std::nullopt;
case Double:
// We use the immediately invoked lambda here because putting this block
// in braces confuses the test coverage tool.
return [id]() -> std::optional<LiteralOrIri> {
// Format as integer if fractional part is zero, let C++ decide
// otherwise.
std::stringstream ss;
double d = id.getDouble();
double dIntPart;
if (std::modf(d, &dIntPart) == 0.0) {
ss << std::fixed << std::setprecision(0) << id.getDouble();
} else {
ss << d;
}
return LiteralOrIri::literalWithoutQuotes(
std::move(ss).str(),
TripleComponent::Iri::fromIrirefWithoutBrackets(XSD_DOUBLE_TYPE));
}();
case Bool:
return id.getBool() ? LiteralOrIri::literalWithoutQuotes(
"true", fromIri(XSD_BOOLEAN_TYPE))
: LiteralOrIri::literalWithoutQuotes(
"false", fromIri(XSD_BOOLEAN_TYPE));
case Int:
return LiteralOrIri::literalWithoutQuotes(std::to_string(id.getInt()),
fromIri(XSD_INT_TYPE));
case Date:
return LiteralOrIri::literalWithoutQuotes(
id.getDate().toStringAndType().first, fromIri(XSD_DATE_TYPE));
case GeoPoint:
return LiteralOrIri::literalWithoutQuotes(
id.getGeoPoint().toStringAndType().first, fromIri(GEO_WKT_LITERAL));
case BlankNodeIndex:
return LiteralOrIri::literalWithoutQuotes(
absl::StrCat("_:bn", id.getBlankNodeIndex().get()));
Copy link
Member

Choose a reason for hiding this comment

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

Try to share as much code as possible with the `idToStringAndTypeForEncodedValue (maybe just call this function, and then just construct the result by wrapping everything in the correct types).

};
switch (id.getDatatype()) {
case WordVocabIndex:
// TODO
Copy link
Member

Choose a reason for hiding this comment

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

I think you can do exactly the same thing as the other function, and also use literalWithoutQuotest.

@@ -287,6 +336,31 @@ ExportQueryExecutionTrees::idToStringAndType(const Index& index, Id id,
return idToStringAndTypeForEncodedValue(id);
}
}

// _____________________________________________________________________________
std::optional<LiteralOrIri> ExportQueryExecutionTrees::idToLiteralOrIri(
Copy link
Member

Choose a reason for hiding this comment

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

Maybe (for the performance) add the argument onlyReturnLiterals that immediately returns nullopt for everything that is not a literal).

Comment on lines 96 to 104
auto optionalLiteralOrIriAndType =
ExportQueryExecutionTrees::idToLiteralOrIri(context->_qec.getIndex(), id,
context->_localVocab);
if (optionalLiteralOrIriAndType.has_value()) {
return std::move(optionalLiteralOrIriAndType.value());
} else {
return std::nullopt;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto optionalLiteralOrIriAndType =
ExportQueryExecutionTrees::idToLiteralOrIri(context->_qec.getIndex(), id,
context->_localVocab);
if (optionalLiteralOrIriAndType.has_value()) {
return std::move(optionalLiteralOrIriAndType.value());
} else {
return std::nullopt;
}
}
return optionalLiteralOrIriAndType =
ExportQueryExecutionTrees::idToLiteralOrIri(context->_qec.getIndex(), id,
context->_localVocab);
}

// Same as the previous function, but only handles the datatypes for which the
// value is encoded directly in the ID. For other datatypes an exception is
// thrown.
static std::optional<LiteralOrIri> idToLiteralOrIriForEncodedValue(Id id);
Copy link
Member

Choose a reason for hiding this comment

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

Consider an overload/parameter with the following semantics:

Mode a) Only return Literals (no IRIs) with no datatype or datatype XSD:string, for all other cases (IRIs + literals with datatypes) return nullopt.
Mode b) remove all datatypes from literals if they are not xsd:string, so for example 42 becomes "42" not "42^^xsd::integer.

@sparql-conformance
Copy link

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.

2 participants