Skip to content

Commit

Permalink
Making DuckDB to be the default engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgSkv committed Jan 11, 2025
1 parent 89e737c commit 102581e
Show file tree
Hide file tree
Showing 55 changed files with 135 additions and 1 deletion.
5 changes: 5 additions & 0 deletions common/logica_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def RunQuery(sql,
'--file=/dev/stdin'] +
['--output-format=ALIGNED'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
elif engine == 'duckdb':
import duckdb
connection = duckdb.connect()
df = connection.sql(sql).df()
return sqlite3_logica.DataframeAsArtisticTable(df)
else:
assert False, 'Unknown engine: %s' % engine
o, _ = p.communicate(sql.encode())
Expand Down
5 changes: 5 additions & 0 deletions common/sqlite3_logica.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def WriteFile(filename, content):
return 'OK'


def DataframeAsArtisticTable(df):
return ArtisticTable(df.columns,
list(df.itertuples(index=False, name=None)))


def ArtisticTable(header, rows):
"""ASCII art table for query output."""
width = [0] * len(header)
Expand Down
2 changes: 1 addition & 1 deletion compiler/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(self, rules, user_flags):
if 'logica_default_engine' in user_flags:
self.default_engine = user_flags['logica_default_engine']
else:
self.default_engine = 'bigquery'
self.default_engine = 'duckdb'

self.annotations = self.ExtractAnnotations(
rules, restrict_to=['@DefineFlag', '@ResetFlagValue'])
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/analytic_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing analytic functions.

@Engine("bigquery");

Ten() = x :- x in Range(10);

Data("one_five", x, y) :-
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/arg_min_max_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing ArgMax, ArgMin, ArgMaxK and ArgMinK builtins.

@Engine("bigquery");

Data(v: 10, payload: "a");
Data(v: 5, payload: "b");
Data(v: 20, payload: "c");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/array_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing array creation.

@Engine("bigquery");

FibonacciArray() = f :-
f Array= (n -> ToInt64(Round((1 + 5 ^ 0.5) ^ n / (2 ^ n * 5 ^ 0.5))) :-
n in Range(10));
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/bulk_functions.l
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

@Engine("bigquery");

@OrderBy(Test, "col0");
Test("Tan") = Round(Tan(3.14159265 / 4.0), 3);
Test("Least") = Round(Least(Sin(0.5), Sin(0.7), Sin(0.001)), 5);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/cast_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

# Testing Cast function.

@Engine("bigquery");

T(true);
T(Cast(1, "BOOL"));
2 changes: 2 additions & 0 deletions integration_tests/chain.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# An example of a chain broken due to lack of files.
# See chain_test on how to test it.

@Engine("bigquery");

A("data");

B(x) :- A(x);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/chain_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import integration_tests.chain.C; # To connect.
import integration_tests.chain.D; # To connect.
import integration_tests.chain.F; # To test.

@Engine("bigquery");

# Mocking data.
MockA("mock_data");

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/closure_test.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import lib.closure.TransitiveClosure;

@Engine("bigquery");

Friend("Alice", "Bob");
Friend("Bob", "Carrol");
Friend("Bob", "Diana");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/composite_functor_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Test for composite functor.

@Engine("bigquery");

Traffic(campaign: 1, cookie: 100, country: "US");
Traffic(campaign: 2, cookie: 200, country: "US");
Traffic(campaign: 3, cookie: 300, country: "US");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/define_aggregation.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing custom aggregation functions.

@Engine("bigquery");

SampledVisit(campaign:"1", person:123);
SampledVisit(campaign:"1", person:125);
SampledVisit(campaign:"2", person:155);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/disjunction_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# What day of the week was it?
# Source: http://brainden.com/logic-problems.htm

@Engine("bigquery");

# Note that we can not factor out the logic of (x == "T" | ....) into a helper
# function because disjunction is supported only for all-out predicates.
@OrderBy(Options, "col0");
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/duckdb_is_default.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Only duckdb has UniqueNumber function.
Test(fourty_two? Count= UniqueNumber()) distinct :-
i in Range(42);
5 changes: 5 additions & 0 deletions integration_tests/duckdb_is_default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
+------------+
| fourty_two |
+------------+
| 42 |
+------------+
2 changes: 2 additions & 0 deletions integration_tests/equals_true_test.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Testing equality of equality to true.

@Engine("bigquery");

Is5(x) = (x == 5);

Test(x) :- Is5(x) == true, x in Range(10);
1 change: 1 addition & 0 deletions integration_tests/factorial_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

@Engine("bigquery");

Prod(x) = Exp(Sum(Log(x)));

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/flags_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Example of running:
# yodaql --f=a=my_value flags_test_manual.ydg testrun FlagValues

@Engine("bigquery");

@DefineFlag("a", "default_value_for_a");
@DefineFlag("b", "default_value_for_b");
@DefineFlag("c", "default_value_for_c");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/functor_annotations_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Predicates are meaningless. Simply creating a deep call stack and calling the
# functor.

@Engine("bigquery");

@Ground(L0);
L0() = 0;
L1() = L0() + 1;
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/functor_chain_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Verifying functor call for a chain.

@Engine("bigquery");

A(0);
B(x) :- A(x);
C(x) :- B(x);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/ground_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing default ground and grounding a predicate to another.

@Engine("bigquery");

@Ground(A);
A("a");

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/if_then.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing implication and OrderBy annotation.

@Engine("bigquery");

N(n1 + n2 * 5 + n3 * 25) :- l == [0,1,2,3,4], n1 in l, n2 in l, n3 in l;

Qualify(n) = (
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/import_tests/canada_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import integration_tests.import_tests.canada.Consume;
import integration_tests.import_tests.canada.ImportCost;
import integration_tests.import_tests.canada.DomesticCost;

@Engine("bigquery");

TestImportFraction() =
Format("%.3f", import_cost / (import_cost + domestic_cost)) :-
import_cost == ImportCost(), domestic_cost == DomesticCost();
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/import_tests/functor_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

import integration_tests.import_tests.new_canada.NewCanadaConsume;

@Engine("bigquery");

@OrderBy(Test, "col0");
Test(..r) :- NewCanadaConsume(..r);
2 changes: 2 additions & 0 deletions integration_tests/import_tests/modification_inside.l
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Main predicate: BetterCountry, which is equal to Country, except for anyone
# who wanter have got a new washer and doesn't want a new washer any more.

@Engine("bigquery");

BetterCountry(better_country) :-
Country(country),
better_country == MakeCountryBetter(country);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/in_expr_test.l
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Testing 'in' as a classic function.

@Engine("bigquery");

Test(x, x in [1,2,3]) :- x in Range(10);
2 changes: 2 additions & 0 deletions integration_tests/long_functor_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing a long sequence of Make statements.

@Engine("bigquery");

S("success");

R(x) :- T(x);
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/modification_inside.l
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Main predicate: BetterCountry, which is equal to Country, except for anyone
# who wanted have got a new washer and doesn't want a new washer any more.

@Engine("bigquery");

@OrderBy(BetterCountry, "col0.name");
BetterCountry(better_country) :-
Country(country),
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/multi_body_aggregation.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Tests for multi-body-aggregation.

@Engine("bigquery");

A() += 1;
A() += 2;

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/nested_combines_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Tests that unified combines don't confuse variables.

@Engine("bigquery");

A(x, y) :- x == (combine List= t * 2 :- t in y), B(y);
B(z) :- z == (combine List= t + 1 :- t in [1,2,3]);

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/no_from_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Testing that predicates with no 'FROM' clause, but with constraints are
# compiled correctly.

@Engine("bigquery");

A() += 1;

Test(x) :- x == "a", x == "b";
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/noinject_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# @NoInject annotation test.

@Engine("bigquery");

@NoInject(A);
A(x + x * x) :- x in [1,2,3];

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/nontrivial_restof_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Testing non-trivial rest-of field.

@Engine("bigquery");

T(a: 1, b: 2, c: 3, d: "a");
T(a: 4, b: 5, c: 6, d: "b");
T(a: 7, b: 8, c: 9, d: "c");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/operation_order_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

@Engine("bigquery");

Test() = 4 / 4 / 4;
2 changes: 2 additions & 0 deletions integration_tests/outer_join.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Outer join via mult-body-aggregation rules.

@Engine("bigquery");

Phones(person: "Peter", phone: "4251112222");
Phones(person: "John", phone: "4251113333");
Emails(person: "John", email: "[email protected]");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/outer_join_combine.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Outer join via combine.

@Engine("bigquery");

Phones(person: "Peter", phone: "4251112222");
Phones(person: "John", phone: "4251113333");
Emails(person: "John", email: "[email protected]");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/outer_join_disjunction.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Outer join via disjunction.

@Engine("bigquery");

Phones(person: "Peter", phone: "4251112222");
Phones(person: "John", phone: "4251113333");
Emails(person: "John", email: "[email protected]");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/outer_join_some_value.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Outer join via mult-body-aggregation rules.

@Engine("bigquery");

Phones(person: "Peter", phone: "4251112222");
Phones(person: "John", phone: "4251113333");
Emails(person: "John", email: "[email protected]");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/outer_join_verbose.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Outer join without mult-body-aggregation.

@Engine("bigquery");

Phones(person: "Peter", phone: "4251112222");
Phones(person: "John", phone: "4251113333");
Emails(person: "John", email: "[email protected]");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/ppq_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Test for ++? operator.

@Engine("bigquery");

Character(full_name: "Toby the dog");
Character(full_name: "Pete the cat");
Character(full_name: "Sharik the dog");
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/quote_escape_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# This is to test escaping of quotes in command line flags.

@Engine("bigquery");

@DefineFlag("name", "");

Q(FlagValue("name"));
2 changes: 2 additions & 0 deletions integration_tests/reachability_test.l
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import lib.reachability.GraphPath;
import lib.reachability.GP3;
import lib.reachability.GraphDistance;

@Engine("bigquery");

D(a, a + 1) :- a in Range(100);

@OrderBy(P, "col0", "col1");
Expand Down
Loading

0 comments on commit 102581e

Please sign in to comment.