Skip to content

Commit

Permalink
Merge pull request #358 from EvgSkv/ti2023
Browse files Browse the repository at this point in the history
Time type.
  • Loading branch information
EvgSkv committed Jul 11, 2024
2 parents 8b571ea + 7477f9d commit 5c645ec
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,49 @@ $ logica primes.l run Prime
+-------+
```

### Cities with largest beer variety

Let's use beer variety dataset from [plotly](https://github.com/plotly/datasets/blob/master/beers.csv).

Let us find top 5 states with largest variety of beers. In each state we will pick city with the largest
variety in the state.

Program `beer.l`:

```
@Engine("duckdb");
@Ground(Beer);
Beer(..r) :-
`('https://github.com/plotly/datasets/blob/master/beers.csv?raw=true')`(..r);
BeersInState(state) += 1 :- Beer(state:);
BeersInCity(state, city) += 1 :- Beer(state:, city:);
ArgMax5(x) = ArgMaxK(x, 5);
BestCityForBeer(state:, city:,
city_beers: BeersInCity(state, city),
state_beers: BeersInState(state)) :-
state in ArgMax5{s -> BeersInState(s)},
city = ArgMax{c -> BeersInCity(state, c)};
```

Running `beer.l`:

```
# logica beer.l run BestCityForBeer
+-------+--------------+------------+-------------+
| state | city | city_beers | state_beers |
+-------+--------------+------------+-------------+
| IN | Indianapolis | 43 | 139 |
| CO | Boulder | 41 | 265 |
| CA | San Diego | 42 | 183 |
| TX | Austin | 25 | 130 |
| MI | Grand Rapids | 66 | 162 |
+-------+--------------+------------+-------------+
```

<!--
### News mentions
Who was mentioned in the news in 2020 the most?
Expand Down Expand Up @@ -211,7 +254,7 @@ $ logica mentions.l run Mentions
Note that cities of Los Angeles and Las Vegas are mentioned in this table due to known
missclasification issue in the GDELT data analysis.

-->
## Feedback

Feel free to create [github issues](https://github.com/EvgSkv/logica/issues)
Expand Down
7 changes: 7 additions & 0 deletions compiler/dialect_libraries/duckdb_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
Num(a) = a;
Str(a) = a;
Epoch(a) = epoch :-
epoch = SqlExpr("epoch_ns({a})", {a:}) / 1000000000,
a ~ Time,
epoch ~ Num;
TimeDiffSeconds(a, b) = Epoch(SqlExpr("{a} - {b}", {a:, b:}));
ToTime(a) = SqlExpr("cast({a} as timestamp)", {a:});
NaturalHash(x) = ToInt64(SqlExpr("hash(cast({x} as string)) // cast(2 as ubigint)", {x:}));
# This is unsafe to use because due to the way Logica compiles this number
Expand Down
4 changes: 3 additions & 1 deletion compiler/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def BuiltInFunctions(self):
'Greatest': 'GREATEST(%s)',
'ToString': 'CAST(%s AS TEXT)',
'DateAddDay': "DATE({0}, {1} || ' days')",
'DateDiffDay': "CAST(JULIANDAY({0}) - JULIANDAY({1}) AS INT64)"
'DateDiffDay': "CAST(JULIANDAY({0}) - JULIANDAY({1}) AS INT64)",
'CurrentTimestamp': 'GET_CURRENT_TIMESTAMP()',
'TimeAdd': '{0} + to_microseconds(cast(1000000 * {1} as int64))'
}

def DecorateCombineRule(self, rule, var):
Expand Down
4 changes: 3 additions & 1 deletion type_inference/research/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def ActMindingRecordLiterals(self, node):
def ActMindingTypingPredicateLiterals(self, node):
if 'type' in node and 'literal' in node and 'the_predicate' in node['literal']:
predicate_name = node['literal']['the_predicate']['predicate_name']
if predicate_name in ['Str', 'Num', 'Bool']:
if predicate_name in ['Str', 'Num', 'Bool', 'Time']:
reference_algebra.Unify(node['type']['the_type'],
reference_algebra.TypeReference(predicate_name))

Expand Down Expand Up @@ -700,6 +700,8 @@ def PsqlType(self, t):
return 'numeric'
if t == 'Bool':
return 'bool'
if t == 'Time':
return 'timestamp'
if isinstance(t, dict):
return RecordTypeName(reference_algebra.RenderType(t))
if isinstance(t, list):
Expand Down
10 changes: 6 additions & 4 deletions type_inference/research/reference_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ def Rank(x):
return 4
if x == 'Bool':
return 5
if isinstance(x, list):
if x == 'Time':
return 6
if isinstance(x, OpenRecord):
if isinstance(x, list):
return 7
if isinstance(x, ClosedRecord):
if isinstance(x, OpenRecord):
return 8
if isinstance(x, ClosedRecord):
return 9
assert False, 'Bad type: %s' % x


Expand Down Expand Up @@ -276,7 +278,7 @@ def Unify(a, b):
Incompatible(b.target, a.target))
return

if concrete_a in ('Num', 'Str', 'Bool'):
if concrete_a in ('Num', 'Str', 'Bool', 'Time'):
if concrete_a == concrete_b:
return # It's all fine.
# Type error: a is incompatible with b.
Expand Down
6 changes: 6 additions & 0 deletions type_inference/research/types_of_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def TypesOfBultins():
0: 'Str',
'logica_value': 'Str'
},
'Time': {
'logica_value': 'Time'
},
'Agg+': {
0: 'Num',
'logica_value': 'Num'
Expand Down Expand Up @@ -248,6 +251,9 @@ def TypesOfBultins():
0: x,
1: x,
'logica_value': x
},
'CurrentTimestamp': {
'logica_value': 'Time'
}
}
types_of_predicate['<'] = types_of_predicate['<='] = types_of_predicate['>='] = types_of_predicate['>']
Expand Down

0 comments on commit 5c645ec

Please sign in to comment.