  A conjunctive query (CQ) is semantically acyclic if it is equivalent to an
acyclic one. Semantic acyclicity has been studied in the constraint-free case,
and deciding whether a query enjoys this property is NP-complete. However, in
case the database is subject to constraints such as tuple-generating
dependencies (tgds) that can express, e.g., inclusion dependencies, or
equality-generating dependencies (egds) that capture, e.g., functional
dependencies, a CQ may turn out to be semantically acyclic under the
constraints while not semantically acyclic in general. This opens avenues to
new query optimization techniques. In this paper we initiate and develop the
theory of semantic acyclicity under constraints. More precisely, we study the
following natural problem: Given a CQ and a set of constraints, is the query
semantically acyclic under the constraints, or, in other words, is the query
equivalent to an acyclic one over all those databases that satisfy the set of
constraints? We show that, contrary to what one might expect, decidability of
CQ containment is a necessary but not sufficient condition for the decidability
of semantic acyclicity. In particular, we show that semantic acyclicity is
undecidable in the presence of full tgds (i.e., Datalog rules). In view of this
fact, we focus on the main classes of tgds for which CQ containment is
decidable, and do not capture the class of full tgds, namely guarded,
non-recursive and sticky tgds. For these classes we show that semantic
acyclicity is decidable, and its complexity coincides with the complexity of CQ
containment. In the case of egds, we show that for keys over unary and binary
predicates semantic acyclicity is decidable (NP-complete). We finally consider
the problem of evaluating a semantically acyclic query over a database that
satisfies a set of constraints; for guarded tgds and functional dependencies
this problem is tractable.
