Skip to content

Commit 5c645ec

Browse files
authored
Merge pull request #358 from EvgSkv/ti2023
Time type.
2 parents 8b571ea + 7477f9d commit 5c645ec

6 files changed

Lines changed: 69 additions & 7 deletions

File tree

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,49 @@ $ logica primes.l run Prime
174174
+-------+
175175
```
176176

177+
### Cities with largest beer variety
178+
179+
Let's use beer variety dataset from [plotly](https://github.com/plotly/datasets/blob/master/beers.csv).
180+
181+
Let us find top 5 states with largest variety of beers. In each state we will pick city with the largest
182+
variety in the state.
183+
184+
Program `beer.l`:
185+
186+
```
187+
@Engine("duckdb");
188+
189+
@Ground(Beer);
190+
Beer(..r) :-
191+
`('https://github.com/plotly/datasets/blob/master/beers.csv?raw=true')`(..r);
192+
193+
BeersInState(state) += 1 :- Beer(state:);
194+
BeersInCity(state, city) += 1 :- Beer(state:, city:);
195+
196+
ArgMax5(x) = ArgMaxK(x, 5);
197+
BestCityForBeer(state:, city:,
198+
city_beers: BeersInCity(state, city),
199+
state_beers: BeersInState(state)) :-
200+
state in ArgMax5{s -> BeersInState(s)},
201+
city = ArgMax{c -> BeersInCity(state, c)};
202+
```
203+
204+
Running `beer.l`:
205+
206+
```
207+
# logica beer.l run BestCityForBeer
208+
+-------+--------------+------------+-------------+
209+
| state | city | city_beers | state_beers |
210+
+-------+--------------+------------+-------------+
211+
| IN | Indianapolis | 43 | 139 |
212+
| CO | Boulder | 41 | 265 |
213+
| CA | San Diego | 42 | 183 |
214+
| TX | Austin | 25 | 130 |
215+
| MI | Grand Rapids | 66 | 162 |
216+
+-------+--------------+------------+-------------+
217+
```
218+
219+
<!--
177220
### News mentions
178221
179222
Who was mentioned in the news in 2020 the most?
@@ -211,7 +254,7 @@ $ logica mentions.l run Mentions
211254
212255
Note that cities of Los Angeles and Las Vegas are mentioned in this table due to known
213256
missclasification issue in the GDELT data analysis.
214-
257+
-->
215258
## Feedback
216259

217260
Feel free to create [github issues](https://github.com/EvgSkv/logica/issues)

compiler/dialect_libraries/duckdb_library.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
Num(a) = a;
5555
Str(a) = a;
5656
57+
Epoch(a) = epoch :-
58+
epoch = SqlExpr("epoch_ns({a})", {a:}) / 1000000000,
59+
a ~ Time,
60+
epoch ~ Num;
61+
TimeDiffSeconds(a, b) = Epoch(SqlExpr("{a} - {b}", {a:, b:}));
62+
ToTime(a) = SqlExpr("cast({a} as timestamp)", {a:});
63+
5764
NaturalHash(x) = ToInt64(SqlExpr("hash(cast({x} as string)) // cast(2 as ubigint)", {x:}));
5865
5966
# This is unsafe to use because due to the way Logica compiles this number

compiler/dialects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ def BuiltInFunctions(self):
416416
'Greatest': 'GREATEST(%s)',
417417
'ToString': 'CAST(%s AS TEXT)',
418418
'DateAddDay': "DATE({0}, {1} || ' days')",
419-
'DateDiffDay': "CAST(JULIANDAY({0}) - JULIANDAY({1}) AS INT64)"
419+
'DateDiffDay': "CAST(JULIANDAY({0}) - JULIANDAY({1}) AS INT64)",
420+
'CurrentTimestamp': 'GET_CURRENT_TIMESTAMP()',
421+
'TimeAdd': '{0} + to_microseconds(cast(1000000 * {1} as int64))'
420422
}
421423

422424
def DecorateCombineRule(self, rule, var):

type_inference/research/infer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def ActMindingRecordLiterals(self, node):
416416
def ActMindingTypingPredicateLiterals(self, node):
417417
if 'type' in node and 'literal' in node and 'the_predicate' in node['literal']:
418418
predicate_name = node['literal']['the_predicate']['predicate_name']
419-
if predicate_name in ['Str', 'Num', 'Bool']:
419+
if predicate_name in ['Str', 'Num', 'Bool', 'Time']:
420420
reference_algebra.Unify(node['type']['the_type'],
421421
reference_algebra.TypeReference(predicate_name))
422422

@@ -700,6 +700,8 @@ def PsqlType(self, t):
700700
return 'numeric'
701701
if t == 'Bool':
702702
return 'bool'
703+
if t == 'Time':
704+
return 'timestamp'
703705
if isinstance(t, dict):
704706
return RecordTypeName(reference_algebra.RenderType(t))
705707
if isinstance(t, list):

type_inference/research/reference_algebra.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,14 @@ def Rank(x):
210210
return 4
211211
if x == 'Bool':
212212
return 5
213-
if isinstance(x, list):
213+
if x == 'Time':
214214
return 6
215-
if isinstance(x, OpenRecord):
215+
if isinstance(x, list):
216216
return 7
217-
if isinstance(x, ClosedRecord):
217+
if isinstance(x, OpenRecord):
218218
return 8
219+
if isinstance(x, ClosedRecord):
220+
return 9
219221
assert False, 'Bad type: %s' % x
220222

221223

@@ -276,7 +278,7 @@ def Unify(a, b):
276278
Incompatible(b.target, a.target))
277279
return
278280

279-
if concrete_a in ('Num', 'Str', 'Bool'):
281+
if concrete_a in ('Num', 'Str', 'Bool', 'Time'):
280282
if concrete_a == concrete_b:
281283
return # It's all fine.
282284
# Type error: a is incompatible with b.

type_inference/research/types_of_builtins.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def TypesOfBultins():
7777
0: 'Str',
7878
'logica_value': 'Str'
7979
},
80+
'Time': {
81+
'logica_value': 'Time'
82+
},
8083
'Agg+': {
8184
0: 'Num',
8285
'logica_value': 'Num'
@@ -248,6 +251,9 @@ def TypesOfBultins():
248251
0: x,
249252
1: x,
250253
'logica_value': x
254+
},
255+
'CurrentTimestamp': {
256+
'logica_value': 'Time'
251257
}
252258
}
253259
types_of_predicate['<'] = types_of_predicate['<='] = types_of_predicate['>='] = types_of_predicate['>']

0 commit comments

Comments
 (0)