Conditional creation of property in R2RML - semantic-web

I'm playing around with R2RML and I was wondering if I can create a property depending on the content of a RDB table cell.
The D2RQ mapping language has d2rq:condition that can handle that.
e.g.:
if value in column/table cell 'name' is 'abc' create property 'abc'
rr:predicateObjectMap [
rr:predicate ex:abc
rr:objectMap [
rr:column "name";
rr:datatype xsd:string;
# equivalent for d2rq:condition "name='abc'"
];
]
if value in column/table cell 'name' is 'xyz' create property 'xyz'
rr:predicateObjectMap [
rr:predicate ex:xyz
rr:objectMap [
rr:column "name";
rr:datatype xsd:decimal;
# equivalent for d2rq:condition "name='xyz'"
];
];
I couldn't find any suggestion in W3C's R2RML Recommendation.
Any ideas? :-)
Update:
I had the idea of using rr:sqlQuery
e.g.
rr:SQLQuery """
select (case TABLENAME.COLUMNNAME
when 'this' then 'propertyOne'
when 'that' then 'propertyTwo'
end) as VARIABLE_PREDICATE
from TABLENAME """;
and apply it to a rr:predicate or rr:predicateMap with
rr:predicateObjectMap [
rr:predicateMap [ rr:template "ex:{VARIABLE_PREDICATE}" ];
rr:objectMap [ rr:column "COLUMNNAME"; ];
];
But that didn't work. I guess predicateMaps can be rr:constants only and not rr:templates :( . At least the W3C Recommendation just shows constants within predicateMap.
Still searching for a solution... :/
P.S. I'm disappointed that a proprietary language like d2rq seems to be more powerful (at this point).

R2RML doesn't have conditional properties (like in D2RQ). The design was done on purpose in order not to complicate the language. Any type of "complex" mapping requires SQL.
A solution is the following:
#prefix rr: <http://www.w3.org/ns/r2rml#>.
<#Mapping> a rr:TriplesMap;
rr:logicalTable [ rr:SQLQuery """
select id, COLUMNNAME, (case TABLENAME.COLUMNNAME
when 'this' then 'http://ex.com/propertyOne'
when 'that' then 'http://ex.com/propertyTwo'
end) as VARIABLE_PREDICATE
from TABLENAME """; ];
rr:subjectMap [
rr:template "http://ex.com/foo/{id}";
];
rr:predicateObjectMap [
rr:predicateMap [ rr:column "VARIABLE_PREDICATE" ];
rr:objectMap [ rr:column "COLUMNNAME" ];
].

We routinely do that in mapping the Getty vocabs, for props that depend on key values (flags). Eg
<#ContribTermRelPreferred>
a rr:TriplesMap;
rr:logicalTable [ rr:sqlQuery """
SELECT ...
UDF_LOD_LOOKUP_PROPERTY('contrib_rels_term','preferred',CRT.PREFERRED) CONTRIBPREF
""" ];
rr:predicateObjectMap [
rr:predicateMap [ rr:column "CONTRIBPREF" ];
rr:objectMap [ rr:template "http://vocab.getty.edu/aat/contrib/{CONTRIB_ID}" ];
].

Related

Is it possible to use prefixes for template definition in RML/SPARQL mapping?

I'm trying to map tabular data into RDF using RML mapping.
I've figured out how to define and use prefixes for rr:constant definitions as indicated with the <<--- arrows in the code below. I've also figured out how to map column values using rr:template, as indicated with the <<<<<< arrow in the same code.
#prefix rr: <http://www.w3.org/ns/r2rml#>.
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
#prefix ex: <http://example.org/>.
[...]
:map_001 rr:predicateObjectMap [
rr:predicate rdf:type; # <<---
rr:objectMap [
rr:constant ex:MyClass; # <<---
rr:termType rr:IRI
]
];
rr:predicateObjectMap [
rr:predicate ex:myPredicate; # <<---
rr:objectMap [
rr:template "http://example.org/{some_column}" # <<<<<<
]
].
My question is: is it possible to somehow use prefixes in rr:template definitions, in order to not have to explicitly write the complete base URI? For example, I'd like to do something like what is shown below, although this clearly doesn't work:
:map_001 rr:predicateObjectMap [
rr:predicate ex:myPredicate;
rr:objectMap [
rr:template ex:"{some_column}" # <<--- DOESN'T WORK!
]
].
Is there any syntax for this, or is it simply not possible?

Get return type of SQL Server function

In SQL Server, a function is defined as follows:
-- Transact-SQL Scalar Function Syntax
CREATE [ OR ALTER ] FUNCTION [ schema_name. ] function_name
( [ { #parameter_name [ AS ][ type_schema_name. ] parameter_data_type
[ = default ] [ READONLY ] }
[ ,...n ]
]
)
RETURNS return_data_type
[ WITH <function_option> [ ,...n ] ]
[ AS ]
BEGIN
function_body
RETURN scalar_expression
END
[ ; ]
Where return_data_type can be text, a table (with a slightly different syntax), or almost any other data type.
Is it possible to retrieve the return data type without running the query?
I know it's possible to do using sp_describe_first_result_set, but this executes the query and looks at the response. Edit: I was wrong. It is done through static analysis, but has a number of limitations associated with it.
As mentioned in comments, you can use sp_describe_first_result_set.
Or you can use the query from the linked duplicate and extend it with INFORMATION_SCHEMA.ROUTINE_COLUMNS:
SELECT r.ROUTINE_NAME AS FunctionName,
r.DATA_TYPE AS FunctionReturnType,
rc.COLUMN_NAME AS ColumnName,
rc.DATA_TYPE AS ColumnType
FROM INFORMATION_SCHEMA.ROUTINES r
LEFT JOIN INFORMATION_SCHEMA.ROUTINE_COLUMNS rc ON rc.TABLE_NAME = r.ROUTINE_NAME
WHERE ROUTINE_TYPE = 'FUNCTION'
ORDER BY r.ROUTINE_NAME, rc.ORDINAL_POSITION;
That will give you the return information for both scalar-value functions and table-value functions, including schema information for the TVF result set.

R2RML Blank Node within rr:objectMap

I was wondering how I could model the following with r2rml:
Person -> has an address --> Blank Node [ Street Name, Postal Code, City ]
I'm note sure how I can add street name, postal code and city to the blank node within the rr:objectMap
(Additional question: Is a rr:class mandatory for a rr:BlankNode?)
Any suggestions? :)
<#Person>
rr:logicalTable [ rr:tableName "PERSON" ];
rr:subjectMap [
rr:template "http://ex.com/data/PersonClass/{ID}";
rr:class ex:PersonClass;
];
rr:preciateObjectMap [
rr:predicate ex:hasAddress;
rr:objectMap [
rr:termType rr:BlankNode;
## How/where can a add the street name, postal code, city? ##
];
].
Should be not too difficult. First you take the R2RML mapping you have created and create a blank node for the address. Give it a unique name by using a template, but do not give it an IRI so it is interpreted as a blank node.
<#Person>
rr:logicalTable [ rr:tableName "PERSON" ];
rr:subjectMap [
rr:template "http://ex.com/data/PersonClass/{ID}";
rr:class ex:PersonClass;
];
rr:preciateObjectMap [
rr:predicate ex:hasAddress;
rr:objectMap [
rr:template "BlankAddressNode{ADDRESSID}";
rr:termType rr:BlankNode;
];
].
Note: ADDRESSID in table PERSON is considered a foreign to the primary key of ADDRESS table.
Next you create the blank node with rdf:type and everything else.
<#Address>
rr:logicalTable [ rr:tableName "ADDRESS" ];
rr:subjectMap [
rr:template "BlankAddressNode{ID}";
rr:termType rr:BlankNode;
rr:class ex:AddressClass;
];
rr:predicateObjectMap [
rr:predicate ex:street;
rr:objectMap [ rr:column "Street"]
];
.
Note: ID is primary key of ADDRESS table.
You can do this with a join too... but I think from a tutorial point of view this is clearer.

How to properly read SQL Server syntax?

In the MSDN Library or the Technet website, Microsoft tend to use a pseudo syntax in explaining how to use T-SQL statements with all available options. Here is a sample taking from the Technet page on UPDATE STATISTICS :
UPDATE STATISTICS table_or_indexed_view_name
[
{
{ index_or_statistics__name }
| ( { index_or_statistics_name } [ ,...n ] )
}
]
[ WITH
[
FULLSCAN
| SAMPLE number { PERCENT | ROWS }
| RESAMPLE
| <update_stats_stream_option> [ ,...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
] ;
<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_contant ]
How to properly read such description and quickly figure out what is required and what is optional and a clean way to write your query?
You should refer to this Transact-SQL Syntax Conventions
The first table in that article explains pretty much everything.
In your example we can see the following:
UPDATE STATISTICS table_or_indexed_view_name
UPDATE STATISTICS is the keyword used
table_or_indexed_view_name is the name of the table or the view to update statistics for
[
{
{ index_or_statistics__name }
| ( { index_or_statistics_name } [ ,...n ] )
}
]
This is optional [], but if supplied, you have to put a statistic name {index_or_statistics__name}, or | a list of statistic names separated by commas { index_or_statistics_name } [ ,...n ]
[ WITH
[
FULLSCAN
| SAMPLE number { PERCENT | ROWS }
| RESAMPLE
| <update_stats_stream_option> [ ,...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
] ;
This is optional too []. If used then you must begin with a WITH and you have 4 options that you must choose from.
Your options are
FULLSCAN
SAMPLE number { PERCENT | ROWS }, where you have to define the number and you must choose from PERCENT or | ROWS
RESAMPLE
` [ ,...n ]' which is a list separated by commas
Then you have to choose either ALL, COLUMNS or INDEX and preside that with a comma if you have used the WITH.
Lastly you have another option to use the NORECOMPUTE and put a comma before it if you have used any other option before it.
<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_contant ]
These are the list of predefined options you may use where <update_stats_stream_option> is used before (in 4).
Any thing between Square Brackets [...] are Optional
Any thing seperated by the pipe | symbol is a one or the other option.
In your above example, you could read it as
UPDATE STATISTICS table_or_indexed_view_name
[ optionally specify an index as well]
[ optionally specify options using **WITH**
If you use WITH then you can follow it with one of the following keywords
FULLSCAN
OR SAMPLE number { PERCENT | ROWS }
OR RESAMPLE
].. and so on

Searching for a precise term for a SQL phrase

I'm trying to document some SQL and wanted to get the right terminology. If you write SQL like so;
select child.ID, parent.ID
from hierarchy child
inner join hierarchy parent
on child.parentId = parent.ID
Then you have one actual table ('hierarchy') which you are giving two names ('parent' and 'child') My question is about how you refer to the logical entity of a table with a name.
What would you write in the blank here for the name?
"This query uses one table (hierarchy) but two _ (child and parent)"
[edit] left a previous draft in the question. now corrected.
I believe this is called a SELF JOIN. A and B (or "child" and "parent", I think you have a typo in your question) are called ALIASes or TABLE ALIASes.
The concept is a self join. However, the a is a syntax error. The table is hierarchy, the alias is child.
I would call each part of a self join an instance.
In the SQL Server docs, the term is table_source :
Specifies a table, view, or derived table source, with or without an alias, to use in the Transact-SQL statement
In the BNF grammar, it's:
<table_source> ::=
{
table_or_view_name [ [ AS ] table_alias ] [ <tablesample_clause> ]
[ WITH ( < table_hint > [ [ , ]...n ] ) ]
| rowset_function [ [ AS ] table_alias ]
[ ( bulk_column_alias [ ,...n ] ) ]
| user_defined_function [ [ AS ] table_alias ] [ (column_alias [ ,...n ] ) ]
| OPENXML <openxml_clause>
| derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ]
| <joined_table>
| <pivoted_table>
| <unpivoted_table>
| #variable [ [ AS ] table_alias ]
| #variable.function_call ( expression [ ,...n ] ) [ [ AS ] table_alias ] [ (column_alias [ ,...n ] ) ]
'child', 'parent'
The term used in the SQL-92 Standard spec is "correlation name", being a type of "identifier".
'hierarchy'
The term used in the SQL-92 Standard spec is "table".
Hence the answer to your (edited) question is:
This query uses one table (hierarchy)
but two correlation names (child and
parent).