Search in sources :

Example 36 with QueryNode

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

the class TestAbstractFromClauseGenerator method shouldUsePartitions.

@Test
public void shouldUsePartitions() {
    // given
    long id = uniqueLong();
    QueryNode node = new QueryNode(id);
    String table = uniqueString();
    String tableAlias = uniqueString();
    int count = 1;
    long corpusID = uniqueInt(0, Integer.MAX_VALUE);
    List<Long> corpora = Lists.newArrayList(corpusID);
    Map<String, String> tableAliases = setupTableAliases(node, table, tableAlias, count);
    Map<String, Boolean> tablePartioned = new HashMap<>();
    tablePartioned.put(table, Boolean.TRUE);
    TableAccessStrategy tas = new TableAccessStrategy(node);
    tas.setTableAliases(tableAliases);
    tas.setTablePartitioned(tablePartioned);
    // when
    String alias = AbstractFromClauseGenerator.tableAliasDefinition(tas, node, table, count, corpora);
    // then
    String expected = tableAlias + "_" + corpusID + " AS " + tableAlias + id;
    assertThat(alias, is(expected));
}
Also used : HashMap(java.util.HashMap) QueryNode(annis.model.QueryNode) TestUtils.uniqueLong(annis.test.TestUtils.uniqueLong) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Test(org.junit.Test)

Example 37 with QueryNode

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

the class TestAbstractFromClauseGenerator method shouldCreateTableAliasWithoutCount.

/**
 * Create a table alias definition for a table that is only used once.
 */
@Test
public void shouldCreateTableAliasWithoutCount() {
    // given
    long id = uniqueLong();
    QueryNode node = new QueryNode(id);
    String table = uniqueString();
    String tableAlias = uniqueString();
    int count = 1;
    Map<String, String> tableAliases = setupTableAliases(node, table, tableAlias, count);
    TableAccessStrategy tas = new TableAccessStrategy(node);
    tas.setTableAliases(tableAliases);
    // when
    String alias = AbstractFromClauseGenerator.tableAliasDefinition(tas, node, table, count, new LinkedList<Long>());
    // then
    String expected = tableAlias + " AS " + tableAlias + id;
    assertThat(alias, is(expected));
}
Also used : QueryNode(annis.model.QueryNode) TestUtils.uniqueLong(annis.test.TestUtils.uniqueLong) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Test(org.junit.Test)

Example 38 with QueryNode

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

the class TestAbstractFromClauseGenerator method shouldCreateTableAliasWithCount.

/**
 * Create a table alias definition for a table that is used multiple times.
 */
@Test
public void shouldCreateTableAliasWithCount() {
    // given
    long id = uniqueLong();
    QueryNode node = new QueryNode(id);
    String table = uniqueString();
    String tableAlias = uniqueString();
    int count = uniqueInt(2, 100);
    Map<String, String> tableAliases = setupTableAliases(node, table, tableAlias, count);
    TableAccessStrategy tas = new TableAccessStrategy(node);
    tas.setTableAliases(tableAliases);
    // when
    String alias = AbstractFromClauseGenerator.tableAliasDefinition(tas, node, table, count, new LinkedList<Long>());
    // then
    String expected = tableAlias + " AS " + tableAlias + id + "_" + count;
    assertThat(alias, is(expected));
}
Also used : QueryNode(annis.model.QueryNode) TestUtils.uniqueLong(annis.test.TestUtils.uniqueLong) TestUtils.uniqueString(annis.test.TestUtils.uniqueString) Test(org.junit.Test)

Example 39 with QueryNode

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

the class TestAnnotateInnerQuerySqlGenerator method shouldGenerateSelectClause.

/**
 * The SELECT clause consists of the column text_ref, left, right and the
 * key columns for each node in the query alternative. Left and right are
 * modified by the requested annotation context.
 */
@Test
public void shouldGenerateSelectClause() {
    // given
    alternative = Collections.nCopies(2, new QueryNode());
    int left = uniqueInt(10);
    int right = uniqueInt(20);
    List<AnnotateQueryData> extensions = new ArrayList<>();
    extensions.add(annotateQueryData);
    given(annotateQueryData.getLeft()).willReturn(left);
    given(annotateQueryData.getRight()).willReturn(right);
    given(queryData.getExtensions(AnnotateQueryData.class)).willReturn(extensions);
    given(queryData.getMaxWidth()).willReturn(alternative.size());
    String key1Column1 = uniqueAlphaString();
    String key1Column2 = uniqueAlphaString();
    String key2Column1 = uniqueAlphaString();
    String key2Column2 = uniqueAlphaString();
    given(key.generateInnerQueryColumns(tableAccessStrategy, 1)).willReturn(asList(key1Column1, key1Column2));
    given(key.generateInnerQueryColumns(tableAccessStrategy, 2)).willReturn(asList(key2Column1, key2Column2));
    String textRefAlias1 = uniqueAlphaString();
    String leftTokenAlias1 = uniqueAlphaString();
    String rightTokenAlias1 = uniqueAlphaString();
    String textRefAlias2 = uniqueAlphaString();
    String leftTokenAlias2 = uniqueAlphaString();
    String rightTokenAlias2 = uniqueAlphaString();
    String corpusRefAlias1 = uniqueAlphaString();
    String corpusRefAlias2 = uniqueAlphaString();
    String nodeNameAlias1 = uniqueAlphaString();
    String nodeNameAlias2 = uniqueAlphaString();
    given(tableAccessStrategy.aliasedColumn(NODE_TABLE, "text_ref")).willReturn(textRefAlias1, textRefAlias2);
    given(tableAccessStrategy.aliasedColumn(NODE_TABLE, "left_token")).willReturn(leftTokenAlias1, leftTokenAlias2);
    given(tableAccessStrategy.aliasedColumn(NODE_TABLE, "right_token")).willReturn(rightTokenAlias1, rightTokenAlias2);
    given(tableAccessStrategy.aliasedColumn(NODE_TABLE, "corpus_ref")).willReturn(corpusRefAlias1, corpusRefAlias2);
    given(tableAccessStrategy.aliasedColumn(NODE_TABLE, "name")).willReturn(nodeNameAlias1, nodeNameAlias2);
    // when
    String actual = generator.selectClause(queryData, alternative, INDENT);
    // then
    String expected = "DISTINCT" + "\n" + INDENT + TABSTOP + key1Column1 + ", " + key1Column2 + ", " + textRefAlias1 + " AS " + "text" + 1 + ", " + leftTokenAlias1 + " - " + left + " AS " + "min" + 1 + ", " + rightTokenAlias1 + " + " + right + " AS " + "max" + 1 + ", " + corpusRefAlias1 + " AS corpus1, " + nodeNameAlias1 + " AS name1, " + "\n" + INDENT + TABSTOP + key2Column1 + ", " + key2Column2 + ", " + textRefAlias2 + " AS " + "text" + 2 + ", " + leftTokenAlias2 + " - " + left + " AS " + "min" + 2 + ", " + rightTokenAlias2 + " + " + right + " AS " + "max" + 2 + ", " + corpusRefAlias2 + " AS corpus2, " + nodeNameAlias2 + " AS name2";
    assertThat(actual, is(expected));
}
Also used : QueryNode(annis.model.QueryNode) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) ArrayList(java.util.ArrayList) TestUtils.uniqueAlphaString(annis.test.TestUtils.uniqueAlphaString) Test(org.junit.Test)

Example 40 with QueryNode

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

the class MetaDataAndCorpusWhereClauseGenerator method whereConditions.

@Override
public Set<String> whereConditions(QueryData queryData, List<QueryNode> alternative, String indent) {
    Set<String> conditions = new HashSet<>();
    List<Long> corpusList = queryData.getCorpusList();
    List<Long> documents = queryData.getDocuments();
    if (documents == null && corpusList == null) {
        return new HashSet<>();
    }
    for (QueryNode node : alternative) {
        // conditions.add("-- select documents by metadata and toplevel corpus");
        if (documents != null) {
            List<String> emptyList = new LinkedList<>();
            emptyList.add("NULL");
            conditions.add(in(tables(node).aliasedColumn(NODE_TABLE, "corpus_ref"), documents.isEmpty() ? emptyList : documents));
        }
        if (corpusList != null && !corpusList.isEmpty()) {
            conditions.add(in(tables(node).aliasedColumn(NODE_TABLE, "toplevel_corpus"), corpusList));
            if (tables(node).usesRankTable() && !tables(node).isMaterialized(RANK_TABLE, NODE_TABLE)) {
                conditions.add(in(tables(node).aliasedColumn(RANK_TABLE, "toplevel_corpus"), corpusList));
            }
        }
    }
    return conditions;
}
Also used : QueryNode(annis.model.QueryNode) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

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