Search in sources :

Example 41 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class TestDefaultWhereClauseGenerator method checkWhereConditions.

private void checkWhereConditions(QueryNode node, String... expected) {
    List<QueryNode> alternative = new ArrayList<>();
    alternative.add(node);
    Set<String> actual = generator.whereConditions(queryData, alternative, "");
    for (String item : expected) {
        assertThat(actual, hasItem(item));
    }
    assertThat(actual, is(size(expected.length)));
}
Also used : QueryNode(annis.model.QueryNode) ArrayList(java.util.ArrayList) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) SqlConstraints.sqlString(annis.sqlgen.SqlConstraints.sqlString)

Example 42 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class SolutionSqlGenerator method selectClause.

@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    int maxWidth = queryData.getMaxWidth();
    Validate.isTrue(alternative.size() <= maxWidth, "BUG: nodes.size() > maxWidth");
    List<String> cols = new ArrayList<>();
    int i = 0;
    boolean needsDistinct = isDistinctNeeded(alternative);
    for (QueryNode node : alternative) {
        ++i;
        TableAccessStrategy tblAccessStr = tables(node);
        cols.add(tblAccessStr.aliasedColumn(NODE_TABLE, "id") + " AS id" + i);
        if (annoCondition != null) {
            if (node.getNodeAnnotations().isEmpty()) {
                // If a query node is not using annotations, fallback to NULL as the value.
                // This is important for the DISTINCT clause, since we don't want to match
                // the annotation itself but the node.
                cols.add("NULL::int AS cat" + i);
            } else {
                cols.add(tblAccessStr.aliasedColumn("node_annotation", "category") + " AS cat" + i);
            }
        }
        if (outputNodeName) {
            if (needsDistinct) {
                cols.add("min(" + tblAccessStr.aliasedColumn(NODE_TABLE, "salt_id") + ") AS salt_id" + i);
            } else {
                cols.add(tblAccessStr.aliasedColumn(NODE_TABLE, "salt_id") + " AS salt_id" + i);
            }
        }
    }
    // add additional empty columns in or clauses with different node sizes
    for (i = alternative.size() + 1; i <= maxWidth; ++i) {
        cols.add("NULL::bigint AS id" + i);
        cols.add("NULL::integer AS cat" + i);
        if (outputNodeName) {
            cols.add("NULL::varchar AS salt_id" + i);
        }
    }
    if (!alternative.isEmpty() && outputNodeName) {
        TableAccessStrategy tblAccessStr = tables(alternative.get(0));
        String corpusRefAlias = tblAccessStr.aliasedColumn(NODE_TABLE, "corpus_ref");
        if (needsDistinct) {
            cols.add("(SELECT c.path_name FROM corpus AS c WHERE c.id = min(" + corpusRefAlias + ") LIMIT 1) AS path_name");
        } else {
            cols.add("(SELECT c.path_name FROM corpus AS c WHERE c.id = " + corpusRefAlias + " LIMIT 1) AS path_name");
        }
    }
    if (outputToplevelCorpus) {
        if (needsDistinct) {
            cols.add("min(" + tables(alternative.get(0)).aliasedColumn(NODE_TABLE, "toplevel_corpus") + ") AS toplevel_corpus");
        } else {
            cols.add(tables(alternative.get(0)).aliasedColumn(NODE_TABLE, "toplevel_corpus") + " AS toplevel_corpus");
        }
    }
    if (needsDistinct) {
        cols.add("min(" + tables(alternative.get(0)).aliasedColumn(NODE_TABLE, "corpus_ref") + ") AS corpus_ref");
    } else {
        cols.add(tables(alternative.get(0)).aliasedColumn(NODE_TABLE, "corpus_ref") + " AS corpus_ref");
    }
    if (queryData.getCorpusList().size() > 1) {
        if (needsDistinct) {
            cols.add("min(" + tables(alternative.get(0)).aliasedColumn(TableAccessStrategy.FACTS_TABLE, "sourceIdx") + ") AS sourceIdx");
        } else {
            cols.add(tables(alternative.get(0)).aliasedColumn(TableAccessStrategy.FACTS_TABLE, "sourceIdx") + " AS sourceIdx");
        }
    }
    String colIndent = indent + TABSTOP + TABSTOP;
    return "\n" + colIndent + StringUtils.join(cols, ",\n" + colIndent);
}
Also used : QueryNode(annis.model.QueryNode) ArrayList(java.util.ArrayList)

Example 43 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class QueryServiceImpl method parseNodes.

/**
 * Return the list of the query nodes if this is a valid query
 * or throw exception when invalid
 *
 * @param query Query to get the query nodes for
 * @param rawCorpusNames
 * @return
 */
@GET
@Path("parse/nodes")
@Produces("application/xml")
public Response parseNodes(@QueryParam("q") String query, @DefaultValue("") @QueryParam("corpora") String rawCorpusNames) {
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:parse:" + c);
    }
    Collections.sort(corpusNames);
    List<Long> corpusIDs = queryDao.mapCorpusNamesToIds(corpusNames);
    QueryData data = queryDao.parseAQL(query, corpusIDs);
    List<QueryNode> nodes = new LinkedList<>();
    int i = 0;
    for (List<QueryNode> alternative : data.getAlternatives()) {
        for (QueryNode n : alternative) {
            n.setAlternativeNumber(i);
            nodes.add(n);
        }
        i++;
    }
    return Response.ok(new GenericEntity<List<QueryNode>>(nodes) {
    }).build();
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) QueryNode(annis.model.QueryNode) GenericEntity(javax.ws.rs.core.GenericEntity) Subject(org.apache.shiro.subject.Subject) LinkedList(java.util.LinkedList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 44 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class AbstractWhereClauseGenerator method whereConditions.

@Override
public Set<String> whereConditions(QueryData queryData, List<QueryNode> alternative, String indent) {
    List<String> conditions = new ArrayList<>();
    for (QueryNode node : alternative) {
        // node constraints
        if (node.getSpannedText() != null) {
            addSpanConditions(conditions, queryData, node);
        }
        if (node.isToken()) {
            addIsTokenConditions(conditions, queryData, node);
        }
        if (node.isRoot()) {
            addIsRootConditions(conditions, queryData, node);
        }
        if (node.getArity() != null) {
            addNodeArityConditions(conditions, queryData, node);
        }
        if (node.getTokenArity() != null) {
            addTokenArityConditions(conditions, queryData, node);
        }
        // node joins
        for (Join join : node.getOutgoingJoins()) {
            QueryNode target = join.getTarget();
            if (join instanceof SameSpan) {
                addSameSpanConditions(conditions, node, target, (SameSpan) join, queryData);
            } else if (join instanceof Identical) {
                addIdenticalConditions(conditions, node, target, (Identical) join, queryData);
            } else if (join instanceof LeftAlignment) {
                addLeftAlignmentConditions(conditions, node, target, (LeftAlignment) join, queryData);
            } else if (join instanceof RightAlignment) {
                addRightAlignmentConditions(conditions, node, target, (RightAlignment) join, queryData);
            } else if (join instanceof Inclusion) {
                addInclusionConditions(conditions, node, target, (Inclusion) join, queryData);
            } else if (join instanceof Overlap) {
                addOverlapConditions(conditions, node, target, (Overlap) join, queryData);
            } else if (join instanceof LeftOverlap) {
                addLeftOverlapConditions(conditions, target, node, (LeftOverlap) join, queryData);
            } else if (join instanceof RightOverlap) {
                addRightOverlapConditions(conditions, target, node, (RightOverlap) join, queryData);
            } else if (join instanceof Precedence) {
                addPrecedenceConditions(conditions, node, target, (Precedence) join, queryData);
            } else if (join instanceof Near) {
                addNearConditions(conditions, node, target, (Near) join, queryData);
            } else if (join instanceof Sibling) {
                addSiblingConditions(conditions, node, target, (Sibling) join, queryData);
            } else if (join instanceof CommonAncestor) {
                addCommonAncestorConditions(conditions, node, target, (CommonAncestor) join, queryData);
            } else if (join instanceof LeftDominance) {
                addLeftDominanceConditions(conditions, node, target, (LeftDominance) join, queryData);
            } else if (join instanceof RightDominance) {
                addRightDominanceConditions(conditions, node, target, (RightDominance) join, queryData);
            } else if (join instanceof Dominance) {
                addDominanceConditions(conditions, node, target, (Dominance) join, queryData);
            } else if (join instanceof PointingRelation) {
                addPointingRelationConditions(conditions, node, target, (PointingRelation) join, queryData);
            } else if (join instanceof EqualValue) {
                addEqualValueConditions(conditions, node, target, (EqualValue) join, queryData);
            } else if (join instanceof NotEqualValue) {
                addNotEqualValueConditions(conditions, node, target, (NotEqualValue) join, queryData);
            }
        }
        // node annotations
        int i = 0;
        for (QueryAnnotation annotation : node.getNodeAnnotations()) {
            ++i;
            addAnnotationConditions(conditions, node, i, annotation, NODE_ANNOTATION_TABLE, queryData);
        }
        // relation annotations
        int j = 0;
        for (QueryAnnotation annotation : node.getEdgeAnnotations()) {
            ++j;
            addAnnotationConditions(conditions, node, j, annotation, EDGE_ANNOTATION_TABLE, queryData);
        }
    }
    return new HashSet<>(conditions);
}
Also used : Identical(annis.sqlgen.model.Identical) Inclusion(annis.sqlgen.model.Inclusion) SameSpan(annis.sqlgen.model.SameSpan) RightAlignment(annis.sqlgen.model.RightAlignment) ArrayList(java.util.ArrayList) LeftAlignment(annis.sqlgen.model.LeftAlignment) RightOverlap(annis.sqlgen.model.RightOverlap) Overlap(annis.sqlgen.model.Overlap) LeftOverlap(annis.sqlgen.model.LeftOverlap) Sibling(annis.sqlgen.model.Sibling) CommonAncestor(annis.sqlgen.model.CommonAncestor) NotEqualValue(annis.sqlgen.model.NotEqualValue) HashSet(java.util.HashSet) PointingRelation(annis.sqlgen.model.PointingRelation) EqualValue(annis.sqlgen.model.EqualValue) NotEqualValue(annis.sqlgen.model.NotEqualValue) QueryAnnotation(annis.model.QueryAnnotation) RightOverlap(annis.sqlgen.model.RightOverlap) Join(annis.model.Join) RightDominance(annis.sqlgen.model.RightDominance) Dominance(annis.sqlgen.model.Dominance) LeftDominance(annis.sqlgen.model.LeftDominance) RightDominance(annis.sqlgen.model.RightDominance) Near(annis.sqlgen.model.Near) LeftDominance(annis.sqlgen.model.LeftDominance) QueryNode(annis.model.QueryNode) LeftOverlap(annis.sqlgen.model.LeftOverlap) Precedence(annis.sqlgen.model.Precedence)

Example 45 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class AnnotateInnerQuerySqlGenerator method selectClause.

@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    int maxWidth = queryData.getMaxWidth();
    Validate.isTrue(alternative.size() <= maxWidth, "BUG: nodes.size() > maxWidth");
    List<AnnotateQueryData> extensions = queryData.getExtensions(AnnotateQueryData.class);
    AnnotateQueryData annotateQueryData = null;
    if (extensions.isEmpty()) {
        annotateQueryData = new AnnotateQueryData(5, 5);
    } else {
        annotateQueryData = extensions.get(0);
    }
    List<String> selectClauseForNode = new ArrayList<>();
    int i = 0;
    for (QueryNode node : alternative) {
        i++;
        TableAccessStrategy tables = tables(node);
        List<String> fields = new ArrayList<>();
        fields.addAll(solutionKey.generateInnerQueryColumns(tables, i));
        fields.add(tables.aliasedColumn(NODE_TABLE, "text_ref") + " AS text" + i);
        // token but not any of the other segmentation layers
        if (annotateQueryData.getSegmentationLayer() == null) {
            fields.add(tables.aliasedColumn(NODE_TABLE, "left_token") + " - " + annotateQueryData.getLeft() + " AS min" + i);
            fields.add(tables.aliasedColumn(NODE_TABLE, "right_token") + " + " + annotateQueryData.getRight() + " AS max" + i);
        } else {
            fields.add(tables.aliasedColumn(NODE_TABLE, "left_token") + " AS min" + i);
            fields.add(tables.aliasedColumn(NODE_TABLE, "right_token") + " AS max" + i);
        }
        fields.add(tables.aliasedColumn(NODE_TABLE, "corpus_ref") + " AS corpus" + i);
        fields.add(tables.aliasedColumn(NODE_TABLE, "name") + " AS name" + i);
        selectClauseForNode.add("\n" + indent + TABSTOP + StringUtils.join(fields, ", "));
    }
    for (i = alternative.size() + 1; i <= maxWidth; ++i) {
        selectClauseForNode.add("NULL::bigint AS id" + i);
        selectClauseForNode.add("NULL::bigint AS text" + i);
        selectClauseForNode.add("NULL::int AS min" + i);
        selectClauseForNode.add("NULL::int AS max" + i);
        selectClauseForNode.add("NULL::bigint AS corpus" + i);
        selectClauseForNode.add("NULL::varchar AS name" + i);
    }
    return "DISTINCT" + StringUtils.join(selectClauseForNode, ", ");
}
Also used : QueryNode(annis.model.QueryNode) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) ArrayList(java.util.ArrayList)

Aggregations

QueryNode (annis.model.QueryNode)67 Join (annis.model.Join)12 LinkedList (java.util.LinkedList)11 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)10 ArrayList (java.util.ArrayList)8 QueryAnnotation (annis.model.QueryAnnotation)7 Precedence (annis.sqlgen.model.Precedence)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Dominance (annis.sqlgen.model.Dominance)5 PointingRelation (annis.sqlgen.model.PointingRelation)5 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 TreeSet (java.util.TreeSet)5 Test (org.junit.Test)5 Identical (annis.sqlgen.model.Identical)4 LeftDominance (annis.sqlgen.model.LeftDominance)4 Near (annis.sqlgen.model.Near)4 RightDominance (annis.sqlgen.model.RightDominance)4 TestUtils.uniqueLong (annis.test.TestUtils.uniqueLong)4 QueryData (annis.ql.parser.QueryData)3