vmware / differential-datalog

DDlog is a programming language for incremental computation. It is well suited for writing programs that continuously update their output in response to input changes. A DDlog programmer does not write incremental algorithms; instead they specify the desired input-output mapping in a declarative manner.
MIT License
1.38k stars 119 forks source link

SQL to DDlog compiler cannot compile when queries have windows, joins, aliases #1126

Closed amytai closed 2 years ago

amytai commented 2 years ago

Suppose you have the following tables:

create table t1(column1 integer, column2 boolean)
create table t5(column1 integer, column2 integer)

Then the following view fails to compile:

create view v0 as SELECT DISTINCT alias1.column1 as new_column
                AVG(alias2.column2) OVER (PARTITION BY alias1.column1) as problem 
                from t1 as alias1 
                JOIN t5 as alias2 ON alias1.column1 = alias2.column1

with the following exception:

com.vmware.ddlog.translator.TranslationException: Could not find relation `Table{over}'
Table{over} 

After some digging, it looks like in TranslationContext::popAlias, called from TranslationContext::freshRelationName, sometimes the relationAlias list is non-empty, which causes a fresh relation to get assigned an incorrect alias, in this case the new relation Rover, which is an intermediate view created by the compiler for dealing with windows, is assigned a bogus alias.

Adding the following lines at the beginning of TranslationContext::popAlias seems to solve the problem, but not sure if this is the correct fix:

if (suggested.startsWith("tmp") || suggested.startsWith("over"))
    return suggested;

where tmp and over are prefixes of intermediate views generated by the compiler.