Return-Path: X-Original-To: apmail-marmotta-commits-archive@minotaur.apache.org Delivered-To: apmail-marmotta-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 80B3411AF6 for ; Wed, 17 Sep 2014 11:53:15 +0000 (UTC) Received: (qmail 97966 invoked by uid 500); 17 Sep 2014 11:53:15 -0000 Delivered-To: apmail-marmotta-commits-archive@marmotta.apache.org Received: (qmail 97923 invoked by uid 500); 17 Sep 2014 11:53:15 -0000 Mailing-List: contact commits-help@marmotta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@marmotta.apache.org Delivered-To: mailing list commits@marmotta.apache.org Received: (qmail 97861 invoked by uid 99); 17 Sep 2014 11:53:15 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Sep 2014 11:53:15 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id F35D1A182F6; Wed, 17 Sep 2014 11:53:14 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sschaffert@apache.org To: commits@marmotta.apache.org Date: Wed, 17 Sep 2014 11:53:15 -0000 Message-Id: <5106267a2d8e4f898ec7feae046aba5a@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/3] git commit: function cleanups (proper description of function parameters and return types) function cleanups (proper description of function parameters and return types) Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/64b504f0 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/64b504f0 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/64b504f0 Branch: refs/heads/develop Commit: 64b504f053bd9979035edbfdd90dab7695bc7eea Parents: 6e306bb Author: Sebastian Schaffert Authored: Wed Sep 17 12:55:50 2014 +0200 Committer: Sebastian Schaffert Committed: Wed Sep 17 12:55:50 2014 +0200 ---------------------------------------------------------------------- .../kiwi/sparql/builder/FunctionDescriptor.java | 85 +++++++++++ .../kiwi/sparql/builder/SQLBuilder.java | 144 +++++++------------ 2 files changed, 139 insertions(+), 90 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/64b504f0/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/FunctionDescriptor.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/FunctionDescriptor.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/FunctionDescriptor.java new file mode 100644 index 0000000..8f81019 --- /dev/null +++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/FunctionDescriptor.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.marmotta.kiwi.sparql.builder; + +import org.openrdf.model.URI; + +/** + * Description of a SPARQL function to give details about various characteristics that we need when translating to + * SQL, most importantly the parameter and return types. + * + * @author Sebastian Schaffert (sschaffert@apache.org) + */ +public class FunctionDescriptor { + + public static enum Arity { ZERO, UNARY, BINARY, NARY }; + + private URI uri; + + private OPTypes parameterType, returnType; + + private Arity arity; + + public FunctionDescriptor(URI uri, OPTypes parameterType, OPTypes returnType, Arity arity) { + this.uri = uri; + this.parameterType = parameterType; + this.returnType = returnType; + this.arity = arity; + } + + + public URI getUri() { + return uri; + } + + public OPTypes getParameterType() { + return parameterType; + } + + public OPTypes getReturnType() { + return returnType; + } + + public Arity getArity() { + return arity; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FunctionDescriptor that = (FunctionDescriptor) o; + + if (arity != that.arity) return false; + if (parameterType != that.parameterType) return false; + if (returnType != that.returnType) return false; + if (!uri.equals(that.uri)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = uri.hashCode(); + result = 31 * result + parameterType.hashCode(); + result = 31 * result + returnType.hashCode(); + result = 31 * result + arity.hashCode(); + return result; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/64b504f0/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/SQLBuilder.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/SQLBuilder.java b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/SQLBuilder.java index 35b4d8f..480cf0b 100644 --- a/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/SQLBuilder.java +++ b/libraries/kiwi/kiwi-sparql/src/main/java/org/apache/marmotta/kiwi/sparql/builder/SQLBuilder.java @@ -62,93 +62,62 @@ public class SQLBuilder { */ private static final DateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - /** - * Type coercion for function parameters, defines for each function the type used by its parameters - */ - private static Map functionParameterTypes = new HashMap<>(); - static { - functionParameterTypes.put(FN.CONCAT, OPTypes.STRING); - functionParameterTypes.put(FN.CONTAINS, OPTypes.STRING); - functionParameterTypes.put(FN.LOWER_CASE, OPTypes.STRING); - functionParameterTypes.put(FN.UPPER_CASE, OPTypes.STRING); - functionParameterTypes.put(FN.REPLACE, OPTypes.STRING); - functionParameterTypes.put(FN.SUBSTRING_AFTER, OPTypes.STRING); - functionParameterTypes.put(FN.SUBSTRING_BEFORE, OPTypes.STRING); - functionParameterTypes.put(FN.STARTS_WITH, OPTypes.STRING); - functionParameterTypes.put(FN.ENDS_WITH, OPTypes.STRING); - functionParameterTypes.put(FN.STRING_LENGTH, OPTypes.STRING); - functionParameterTypes.put(FN.SUBSTRING, OPTypes.STRING); - - functionParameterTypes.put(FN.NUMERIC_ABS, OPTypes.DOUBLE); - functionParameterTypes.put(FN.NUMERIC_CEIL, OPTypes.DOUBLE); - functionParameterTypes.put(FN.NUMERIC_FLOOR, OPTypes.DOUBLE); - functionParameterTypes.put(FN.NUMERIC_ROUND, OPTypes.DOUBLE); - - - functionParameterTypes.put(FN_MARMOTTA.YEAR, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.MONTH, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.DAY, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.HOURS, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.MINUTES, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.SECONDS, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.TIMEZONE, OPTypes.DATE); - functionParameterTypes.put(FN_MARMOTTA.TZ, OPTypes.DATE); - - functionParameterTypes.put(FN_MARMOTTA.MD5, OPTypes.STRING); - functionParameterTypes.put(FN_MARMOTTA.SHA1, OPTypes.STRING); - functionParameterTypes.put(FN_MARMOTTA.SHA256, OPTypes.STRING); - functionParameterTypes.put(FN_MARMOTTA.SHA384, OPTypes.STRING); - functionParameterTypes.put(FN_MARMOTTA.SHA512, OPTypes.STRING); - - functionParameterTypes.put(FN_MARMOTTA.STDDEV, OPTypes.DOUBLE); - functionParameterTypes.put(FN_MARMOTTA.VARIANCE, OPTypes.DOUBLE); - } /** - * Type coercion for function return values, defines for each function the type of its return value. + * Type coercion for function parameters and return values, defines for each function the type used by its parameters */ - private static Map functionReturnTypes = new HashMap<>(); - static { - functionReturnTypes.put(FN.CONCAT, OPTypes.STRING); - functionReturnTypes.put(FN.CONTAINS, OPTypes.BOOL); - functionReturnTypes.put(FN.LOWER_CASE, OPTypes.STRING); - functionReturnTypes.put(FN.UPPER_CASE, OPTypes.STRING); - functionReturnTypes.put(FN.REPLACE, OPTypes.STRING); - functionReturnTypes.put(FN.SUBSTRING_AFTER, OPTypes.STRING); - functionReturnTypes.put(FN.SUBSTRING_BEFORE, OPTypes.STRING); - functionReturnTypes.put(FN.STARTS_WITH, OPTypes.BOOL); - functionReturnTypes.put(FN.ENDS_WITH, OPTypes.BOOL); - functionReturnTypes.put(FN.STRING_LENGTH, OPTypes.INT); - functionReturnTypes.put(FN.SUBSTRING, OPTypes.STRING); - - functionReturnTypes.put(FN.NUMERIC_ABS, OPTypes.DOUBLE); - functionReturnTypes.put(FN.NUMERIC_CEIL, OPTypes.INT); - functionReturnTypes.put(FN.NUMERIC_FLOOR, OPTypes.INT); - functionReturnTypes.put(FN.NUMERIC_ROUND, OPTypes.INT); - - functionReturnTypes.put(FN_MARMOTTA.UUID, OPTypes.URI); - functionReturnTypes.put(FN_MARMOTTA.STRUUID, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.RAND, OPTypes.DOUBLE); - functionReturnTypes.put(FN_MARMOTTA.NOW, OPTypes.DATE); - functionReturnTypes.put(FN_MARMOTTA.YEAR, OPTypes.INT); - functionReturnTypes.put(FN_MARMOTTA.MONTH, OPTypes.INT); - functionReturnTypes.put(FN_MARMOTTA.DAY, OPTypes.INT); - functionReturnTypes.put(FN_MARMOTTA.HOURS, OPTypes.INT); - functionReturnTypes.put(FN_MARMOTTA.MINUTES, OPTypes.INT); - functionReturnTypes.put(FN_MARMOTTA.SECONDS, OPTypes.DOUBLE); - functionReturnTypes.put(FN_MARMOTTA.TIMEZONE, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.TZ, OPTypes.STRING); - - functionReturnTypes.put(FN_MARMOTTA.MD5, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.SHA1, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.SHA256, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.SHA384, OPTypes.STRING); - functionReturnTypes.put(FN_MARMOTTA.SHA512, OPTypes.STRING); - - functionReturnTypes.put(FN_MARMOTTA.STDDEV, OPTypes.DOUBLE); - functionReturnTypes.put(FN_MARMOTTA.VARIANCE, OPTypes.DOUBLE); + private static Map functions = new HashMap<>(); + + private static void addFunction(URI uri, OPTypes paramType, OPTypes returnType, FunctionDescriptor.Arity arity) { + functions.put(uri, new FunctionDescriptor(uri,paramType,returnType,arity)); } + + static { + addFunction(FN.CONCAT, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.NARY); + addFunction(FN.CONTAINS, OPTypes.STRING, OPTypes.BOOL, FunctionDescriptor.Arity.BINARY); + addFunction(FN.LOWER_CASE, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN.UPPER_CASE, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN.REPLACE, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.BINARY); + addFunction(FN.SUBSTRING_AFTER, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.BINARY); + addFunction(FN.SUBSTRING_BEFORE, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.BINARY); + addFunction(FN.STARTS_WITH, OPTypes.STRING, OPTypes.BOOL, FunctionDescriptor.Arity.BINARY); + addFunction(FN.ENDS_WITH, OPTypes.STRING, OPTypes.BOOL, FunctionDescriptor.Arity.BINARY); + addFunction(FN.STRING_LENGTH, OPTypes.STRING, OPTypes.INT, FunctionDescriptor.Arity.BINARY); + addFunction(FN.SUBSTRING, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.BINARY); + + addFunction(FN.NUMERIC_ABS, OPTypes.DOUBLE, OPTypes.DOUBLE, FunctionDescriptor.Arity.UNARY); + addFunction(FN.NUMERIC_CEIL, OPTypes.DOUBLE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN.NUMERIC_FLOOR, OPTypes.DOUBLE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN.NUMERIC_ROUND, OPTypes.DOUBLE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + + + addFunction(FN_MARMOTTA.YEAR, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.MONTH, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.DAY, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.HOURS, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.MINUTES, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.SECONDS, OPTypes.DATE, OPTypes.INT, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.TIMEZONE, OPTypes.DATE, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.TZ, OPTypes.DATE, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + + addFunction(FN_MARMOTTA.MD5, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.SHA1, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.SHA256, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.SHA384, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.SHA512, OPTypes.STRING, OPTypes.STRING, FunctionDescriptor.Arity.UNARY); + + addFunction(FN_MARMOTTA.UUID, OPTypes.ANY, OPTypes.URI, FunctionDescriptor.Arity.ZERO); + addFunction(FN_MARMOTTA.STRUUID, OPTypes.ANY, OPTypes.STRING, FunctionDescriptor.Arity.ZERO); + addFunction(FN_MARMOTTA.RAND, OPTypes.ANY, OPTypes.DOUBLE, FunctionDescriptor.Arity.ZERO); + + addFunction(FN_MARMOTTA.STDDEV, OPTypes.DOUBLE, OPTypes.DOUBLE, FunctionDescriptor.Arity.UNARY); + addFunction(FN_MARMOTTA.VARIANCE, OPTypes.DOUBLE, OPTypes.DOUBLE, FunctionDescriptor.Arity.UNARY); + + addFunction(FN_MARMOTTA.QUERY_FULLTEXT, OPTypes.STRING, OPTypes.BOOL, FunctionDescriptor.Arity.NARY); + addFunction(FN_MARMOTTA.SEARCH_FULLTEXT, OPTypes.STRING, OPTypes.BOOL, FunctionDescriptor.Arity.NARY); + } + /** * Query results should be DISTINCT. @@ -168,7 +137,6 @@ public class SQLBuilder { private List orderby; private List extensions; - private List groupExpressions; private Set groupLabels; /** @@ -317,7 +285,6 @@ public class SQLBuilder { // find the grouping GroupFinder gf = new GroupFinder(query); - groupExpressions = gf.elements; groupLabels = gf.bindings; // find extensions (BIND) @@ -926,16 +893,13 @@ public class SQLBuilder { String[] args = new String[fc.getArgs().size()]; - OPTypes fOpType = functionParameterTypes.get(fnUri); - if(fOpType == null) { - fOpType = OPTypes.STRING; - } + OPTypes fOpType = functions.containsKey(fnUri) ? functions.get(fnUri).getParameterType() : OPTypes.STRING; for(int i=0; i