Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify grounding by removing joins with var ID tables #110

Merged
merged 2 commits into from Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 9 additions & 21 deletions src/main/scala/org/deepdive/ddlog/DeepDiveLogCompiler.scala
Expand Up @@ -116,8 +116,9 @@ class DeepDiveLogCompiler( program : DeepDiveLog.Program, config : DeepDiveLog.C
// odd has happened.
def resolveName( v : Variable ) : String = {
v match { case Variable(v,relName,i) =>
if(attrNameForRelationAndPosition contains (relName,i)) {
attrNameForRelationAndPosition(relName,i)
val realRel = relName.replaceAll("^" + deepdivePrefixForVariablesWithIdsTable, "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with stitching regex like this.. Looks like stripPrefix will do a cleaner job?

if(attrNameForRelationAndPosition contains (realRel,i)) {
attrNameForRelationAndPosition(realRel,i)
} else {
// for views, columns names are in the form of "column_index"
return s"column_${i}"
Expand Down Expand Up @@ -511,7 +512,7 @@ class QueryCompiler(cq : ConjunctiveQuery, hackFrom: List[String] = Nil, hackWhe
case (x: Atom, i) if schemaDeclarationByRelationName get x.name exists (_.isQuery) =>
// TODO maybe TableAlias can be useful here or we can completely get rid of it?
// variable id column
s"""${deepdiveVariableIdColumn}_${headAsBody indexOf x}.${
s"""R${headAsBody indexOf x}.${
deepdiveVariableIdColumn
} AS "${x.name}.R${headAsBody indexOf x}.${deepdiveVariableIdColumn}\"""" :: (
// project variable key columns as well (to reduce unnecssary joins)
Expand All @@ -530,35 +531,22 @@ class QueryCompiler(cq : ConjunctiveQuery, hackFrom: List[String] = Nil, hackWhe
case _ => List.empty
}

val internalVarTables = headAsBody.zipWithIndex flatMap {
case (x: Atom, i) if schemaDeclarationByRelationName get x.name exists (_.isQuery) =>
List(s"""${deepdivePrefixForVariablesIdsTable}${x.name} AS ${deepdiveVariableIdColumn}_${headAsBody indexOf x}""")
case _ => List.empty
val headAsBodyWithIds = headAsBody map {
case(x: Atom) => Atom(s"""${deepdivePrefixForVariablesWithIdsTable}${x.name}""", x.terms)
}

val internalVarJoinConds = headAsBody.zipWithIndex flatMap {
case (x: Atom, i) if schemaDeclarationByRelationName get x.name exists (_.isQuery) =>
List(
// project variable key columns as well (to reduce unnecssary joins)
schemaDeclarationByRelationName get x.name map (_.keyColumns map {
case term => s"""R${headAsBody indexOf x}.${term} = ${deepdiveVariableIdColumn}_${headAsBody indexOf x}.${term}"""
}) get
)
case _ => List.empty
} flatten

var nonCategoryWeightCols = new HashSet[String]()

val inputQueries =
stmt.q.bodies map { case cqBody =>
// Here we need to select from the bodies atoms as well as the head atoms,
// which are the variable relations.
// This is achieved by puting head atoms into the body.
val fakeBody = headAsBody ++ cqBody
val fakeBody = headAsBodyWithIds ++ cqBody
val fakeCQ = stmt.q.copy(bodies = List(fakeBody))

// TODO XXX: Fix the `internal` hack below
val qc = new QueryCompiler(fakeCQ, internalVarTables, internalVarJoinConds)
val qc = new QueryCompiler(fakeCQ)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove the TODO XXX comment since they're gone now?


// weight columns
val weightColumns = stmt.weights.variables.zipWithIndex collect {
Expand Down Expand Up @@ -685,7 +673,7 @@ object DeepDiveLogCompiler extends DeepDiveLogHandler {
val deepdiveVariableIdColumn = "dd_id"
val deepdiveVariableLabelColumn = "dd_label"
val deepdiveVariableLabelTruthinessColumn = "dd_truthiness"
val deepdivePrefixForVariablesIdsTable = "dd_variables_"
val deepdivePrefixForVariablesWithIdsTable = "dd_predicate_"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, could you elaborate a bit on the naming of this table? "predicate" isn't very intuitive IMO. I think it's still a materialized variable, so maybe something straightforward as dd_variable_copy_ or dd_variable_with_id_?


// entry point for compilation
override def run(parsedProgram: DeepDiveLog.Program, config: DeepDiveLog.Config) = {
Expand Down
42 changes: 15 additions & 27 deletions test/expected-output-test/chunking_example/compile.expected
Expand Up @@ -85,27 +85,25 @@ deepdive.inference.factors.inf_istrue_chunk {
weight: """?(dd_weight_column_0, dd_weight_column_1)"""
non_category_weight_cols: [
dd_weight_column_0
dd_weight_column_1
]
input_query: """
SELECT dd_id_0.dd_id AS "chunk.R0.dd_id"
SELECT R0.dd_id AS "chunk.R0.dd_id"
, R0.sent_id AS "chunk.R0.sent_id"
, R0.word_id AS "chunk.R0.word_id"
, R0.tag AS "chunk.R0.tag"
, R3.feature AS "dd_weight_column_0"
, R0.tag AS "dd_weight_column_1"
, (R3.feature)::float AS feature_value
FROM chunk R0
FROM dd_predicate_chunk R0
, words R1
, tags R2
, word_features R3
, dd_variables_chunk AS dd_id_0
WHERE R1.sent_id = R0.sent_id
AND R1.word_id = R0.word_id
AND R2.tag = R0.tag
AND R3.sent_id = R0.sent_id
AND R3.word_id = R0.word_id
AND R0.sent_id = dd_id_0.sent_id
AND R0.word_id = dd_id_0.word_id
"""
input_relations: [
chunk
Expand All @@ -120,28 +118,27 @@ function: """AndCategorical(chunk.R0.dd_label)"""
deepdive.inference.factors.linear_chain {
weight: """?(dd_weight_column_0, dd_weight_column_1)"""
non_category_weight_cols: [

dd_weight_column_0
dd_weight_column_1
]
input_query: """
SELECT dd_id_0.dd_id AS "chunk.R0.dd_id"
SELECT R0.dd_id AS "chunk.R0.dd_id"
, R0.sent_id AS "chunk.R0.sent_id"
, R0.word_id AS "chunk.R0.word_id"
, R0.tag AS "chunk.R0.tag"
, dd_id_1.dd_id AS "chunk.R1.dd_id"
, R1.dd_id AS "chunk.R1.dd_id"
, R1.sent_id AS "chunk.R1.sent_id"
, R1.word_id AS "chunk.R1.word_id"
, R1.tag AS "chunk.R1.tag"
, R0.tag AS "dd_weight_column_0"
, R1.tag AS "dd_weight_column_1"
, (1)::float AS feature_value
FROM chunk R0
, chunk R1
FROM dd_predicate_chunk R0
, dd_predicate_chunk R1
, words R2
, words R3
, tags R4
, tags R5
, dd_variables_chunk AS dd_id_0
, dd_variables_chunk AS dd_id_1
WHERE R1.sent_id = R0.sent_id
AND R2.sent_id = R0.sent_id
AND R2.word_id = R0.word_id
Expand All @@ -150,10 +147,6 @@ WHERE R1.sent_id = R0.sent_id
AND R4.tag = R0.tag
AND R5.tag = R1.tag
AND R1.word_id = (R0.word_id + 1)
AND R0.sent_id = dd_id_0.sent_id
AND R0.word_id = dd_id_0.word_id
AND R1.sent_id = dd_id_1.sent_id
AND R1.word_id = dd_id_1.word_id
"""
input_relations: [
chunk
Expand All @@ -167,28 +160,27 @@ function: """AndCategorical(chunk.R0.dd_label, chunk.R1.dd_label)"""
deepdive.inference.factors.phony_rule {
weight: """?(dd_weight_column_0, dd_weight_column_1)"""
non_category_weight_cols: [

dd_weight_column_0
dd_weight_column_1
]
input_query: """
SELECT dd_id_0.dd_id AS "chunk.R0.dd_id"
SELECT R0.dd_id AS "chunk.R0.dd_id"
, R0.sent_id AS "chunk.R0.sent_id"
, R0.word_id AS "chunk.R0.word_id"
, R0.tag AS "chunk.R0.tag"
, dd_id_1.dd_id AS "chunk.R1.dd_id"
, R1.dd_id AS "chunk.R1.dd_id"
, R1.sent_id AS "chunk.R1.sent_id"
, R1.word_id AS "chunk.R1.word_id"
, R1.tag AS "chunk.R1.tag"
, R0.tag AS "dd_weight_column_0"
, R1.tag AS "dd_weight_column_1"
, (1)::float AS feature_value
FROM chunk R0
, chunk R1
FROM dd_predicate_chunk R0
, dd_predicate_chunk R1
, words R2
, words R3
, tags R4
, tags R5
, dd_variables_chunk AS dd_id_0
, dd_variables_chunk AS dd_id_1
WHERE R1.sent_id = R0.sent_id
AND R2.sent_id = R0.sent_id
AND R2.word_id = R0.word_id
Expand All @@ -198,10 +190,6 @@ WHERE R1.sent_id = R0.sent_id
AND R5.tag = R1.tag
AND R2.tag IS NOT NULL
AND R0.word_id < R1.word_id
AND R0.sent_id = dd_id_0.sent_id
AND R0.word_id = dd_id_0.word_id
AND R1.sent_id = dd_id_1.sent_id
AND R1.word_id = dd_id_1.word_id
"""
input_relations: [
chunk
Expand Down
24 changes: 8 additions & 16 deletions test/expected-output-test/factor_functions/compile.expected
Expand Up @@ -12,21 +12,17 @@ non_category_weight_cols: [
dd_weight_column_0
]
input_query: """
SELECT dd_id_0.dd_id AS "Q.R0.dd_id"
SELECT R0.dd_id AS "Q.R0.dd_id"
, R0.x AS "Q.R0.x"
, dd_id_1.dd_id AS "Q.R1.dd_id"
, R1.dd_id AS "Q.R1.dd_id"
, R1.x AS "Q.R1.x"
, R1.x AS "dd_weight_column_0"
, (1)::float AS feature_value
FROM Q R0
, Q R1
FROM dd_predicate_Q R0
, dd_predicate_Q R1
, R R2
, dd_variables_Q AS dd_id_0
, dd_variables_Q AS dd_id_1
WHERE R2.a = R0.x
AND R2.b = R1.x
AND R0.x = dd_id_0.x
AND R1.x = dd_id_1.x
"""
input_relations: [
Q
Expand All @@ -42,21 +38,17 @@ non_category_weight_cols: [
dd_weight_column_0
]
input_query: """
SELECT dd_id_0.dd_id AS "Q.R0.dd_id"
SELECT R0.dd_id AS "Q.R0.dd_id"
, R0.x AS "Q.R0.x"
, dd_id_1.dd_id AS "Q.R1.dd_id"
, R1.dd_id AS "Q.R1.dd_id"
, R1.x AS "Q.R1.x"
, R1.x AS "dd_weight_column_0"
, (1)::float AS feature_value
FROM Q R0
, Q R1
FROM dd_predicate_Q R0
, dd_predicate_Q R1
, R R2
, dd_variables_Q AS dd_id_0
, dd_variables_Q AS dd_id_1
WHERE R2.a = R0.x
AND R2.b = R1.x
AND R0.x = dd_id_0.x
AND R1.x = dd_id_1.x
"""
input_relations: [
Q
Expand Down
14 changes: 5 additions & 9 deletions test/expected-output-test/logical_rules/compile.expected
Expand Up @@ -39,22 +39,18 @@ materialize: false
deepdive.inference.factors.inf_imply_P_Q {
weight: """1"""
non_category_weight_cols: [

]
input_query: """
SELECT dd_id_0.dd_id AS "P.R0.dd_id"
SELECT R0.dd_id AS "P.R0.dd_id"
, R0.x AS "P.R0.x"
, dd_id_1.dd_id AS "Q.R1.dd_id"
, R1.dd_id AS "Q.R1.dd_id"
, R1.x AS "Q.R1.x"
, (1)::float AS feature_value
FROM P R0
, Q R1
, dd_variables_P AS dd_id_0
, dd_variables_Q AS dd_id_1
FROM dd_predicate_P R0
, dd_predicate_Q R1
WHERE R1.x = R0.x
AND true
AND R0.x = dd_id_0.x
AND R1.x = dd_id_1.x
"""
input_relations: [
P
Expand Down