Skip to content

Commit 9469465

Browse files
Populate bayesdb_generator_column correctly.
Provisional, until we eliminate it (Github issue #441).
1 parent 167ddb6 commit 9469465

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/bql.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,49 @@ def map_constraint(((var, _expression), value)):
309309
'Name already defined as generator: %s' %
310310
(repr(phrase.name),))
311311
else:
312+
# Insert a record into bayesdb_generator and get the
313+
# assigned id.
312314
bdb.sql_execute('''
313315
INSERT INTO bayesdb_generator
314316
(name, tabname, population_id, metamodel)
315317
VALUES (?, ?, ?, ?)
316318
''', (phrase.name, table, population_id, metamodel.name()))
317319
generator_id = core.bayesdb_get_generator(bdb, phrase.name)
320+
321+
# Populate bayesdb_generator_column.
322+
#
318323
# XXX Omit needless bayesdb_generator_column table --
319324
# Github issue #441.
320325
bdb.sql_execute('''
321326
INSERT INTO bayesdb_generator_column
322327
(generator_id, colno, stattype)
323-
SELECT ?, colno, stattype
328+
SELECT :generator_id, colno, stattype
324329
FROM bayesdb_variable
325-
WHERE population_id = ?
326-
''', (generator_id, population_id))
330+
WHERE population_id = :population_id
331+
AND generator_id IS NULL
332+
''', {
333+
'generator_id': generator_id,
334+
'population_id': population_id,
335+
})
336+
337+
# Do any metamodel-specific initialization.
327338
metamodel.create_generator(bdb, generator_id, phrase.schema)
328339

340+
# Populate bayesdb_generator_column with any latent
341+
# variables that metamodel.create_generator has added
342+
# with bayesdb_add_latent.
343+
bdb.sql_execute('''
344+
INSERT INTO bayesdb_generator_column
345+
(generator_id, colno, stattype)
346+
SELECT :generator_id, colno, stattype
347+
FROM bayesdb_variable
348+
WHERE population_id = :population_id
349+
AND generator_id = :generator_id
350+
''', {
351+
'generator_id': generator_id,
352+
'population_id': population_id,
353+
})
354+
329355
# All done. Nothing to return.
330356
return empty_cursor(bdb)
331357

tests/test_vscgpm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
from cgpm.utils import general as gu
2828

29+
import bayeslite.core as core
30+
2931
from bayeslite import bayesdb_open
3032
from bayeslite import bayesdb_register_metamodel
3133
from bayeslite.metamodels.cgpm_metamodel import CGPM_Metamodel
@@ -176,6 +178,10 @@ def test_cgpm_extravaganza__ci_slow():
176178
)
177179
'''.format(kepler_source))
178180

181+
generator_id = core.bayesdb_get_generator(bdb, 'g0')
182+
assert core.bayesdb_generator_column_numbers(bdb, generator_id) == \
183+
[-2, -1, 1, 2, 3, 4, 5, 6]
184+
179185
# -- MODEL country_of_operator GIVEN class_of_orbit USING forest;
180186
bdb.execute('INITIALIZE 1 MODELS FOR g0')
181187
bdb.execute('ANALYZE g0 FOR 1 iteration WAIT (;)')

0 commit comments

Comments
 (0)