Odd error message in DataFrame in a linq where clause - linq

When I try to filter a DataFrame by a Date the following way:
#linq df |> #where(:a .> (myDate + Dates.Month(18)))
I get the error message:
LoadError: MethodError: `convert` has no method matching convert(::Type{DataFramesMeta.SymbolParameter{T}}, ::Expr)
This may have arisen from a call to the constructor DataFramesMeta.SymbolParameter{T}(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, !Matched::T)
DataFramesMeta.SymbolParameter(!Matched::Symbol)
while loading In[79], in expression starting on line 2
while if I define a variable t it works:
t = (myDate + Dates.Month(18))
#linq df |> #where(:a .> t)
Is the first case a fundamentally wrong way of using the #where clause? If so, why? (myDate + Dates.Month(18)) should be just a Date just as t is, so why is this different behavior; also how to make the two cases behave the same way?

Related

how to use F expression to first cast a string to int, then add 1 to it then cast to string and update

I have a DB column which is generic type for some stats(qualitative and quantitative info).
Some values are string - type A and some values are numbers stored as string - type B.
What i want to do is cast the B types to number then add one to them and cast back to string and store.
Metadata.objects.filter(key='EVENT', type='COUNT').update(value=CAST(F(CAST('value', IntegerField()) + 1), CharField())
What i want to do is avoid race conditions using F expression and
update in DB.
https://docs.djangoproject.com/en/4.0/ref/models/expressions/#avoiding-race-conditions-using-f
It says in below post that casting and updating in db is possible for mysql
Mysql Type Casting in Update Query
I also know we can do arithmetic very easily on F expressions as it supports it and we can override functionality of add as well. How to do arthmetic on Django 'F' types?
How can i achieve Cast -> update -> cast -> store in Django queryset?
Try using annotation as follows:
Metadata.objects
.filter(key='EVENT', type='COUNT')
.annotate(int_value=CAST('value', IntegerField()))
.update(value=CAST(F('int_value') + 1, CharField())
Or maybe switching F and CAST works?
Metadata.objects
.filter(key='EVENT', type='COUNT')
.update(value=CAST( # cast the whole expression below
CAST( # cast a value
F('value'), # of field "value"
IntegerField() # to integer
) + 1, # then add 1
CharField() # to char.
)
I've added indentation, it helps sometimes to find the errors.
Also, doc says, CAST accepts field name, not an F-object. Maybe it works without F-object at all?
UPD: switched back to first example, it actually works :)
I believe the answer from #som-1 was informative but not substantiated with info or debugged data. I believe assuming is not always right.
I debugged the mysql queries formed in these two cases -
1 - Metadata.objects.update(value=Cast(Cast(F('value'), output_field=IntegerField()) + 1, output_field=CharField()))
2 - Metadata.objects.update(value=Cast(Cast('value', IntegerField()) + 1, CharField())) and
both give the same output as expected.
UPDATE Metadata SET value = CAST((CAST(value AS signed integer) + 1) AS char) WHERE ( key = 'EVENT' AND type = 'COUNT' )
Please find the link to add mysqld options to my.cnf and debug your queries. Location of my.cnf file on macOS
enabling queries - https://tableplus.com/blog/2018/10/how-to-show-queries-log-in-mysql.html

Relational Algebra: Problems with division operator

I am writing a query, where I am using the division operator. For some reason I can't get it to work properly, and I can't see why.
pol = pi allergen (sigma allergy_type = 'pollen' (allergies))
tmp = (patient_allergies/pol)
tmp
The above is my query. In pol I am retrieving all allergens, who has the allergy type pollen. This gives me a 1 column, two rows table, which content is the two allergens who has allergy_type pollen.
tmp:
Patient_allergies is a 2 column, 23 row table. The first column is allergens, second column is ssn for the people with those allergens.
What I am trying to do, is to get everyone in patient_allergies, who has the two allergens I found in pol. I'm pretty sure I need to division operator for this, but it returns an empty list, which is incorrect for what I am trying to do.
EDIT: I am using this relational algebra calc, provided by our university: dbis-uibk.github.io/relax/calc/local/uibk/local/0 There is a division operator with another symbol, but yields the same result

Can pyparsing spit out the text which causes a ParseException?

Using pyparsing i try to parse some text with a compound expression like
a = pp.Word(pp.alphas).setResultsName('A')
b = pp.Word(pp.nums).setResultsName('B')
c = pp.Word(pp.alphas).setResultsName('C')
expr = a + b + c
and parseString fails with the Exception
ParseException: Expected W:(0123...) (at char 7), (line:1, col:8)
So far so good. However, to understand better what's going on, is it possible to ask pyparsing/parseString to tell me directly what character from the input string didn't match? (I can, of course, calculate this myself from the information in the Exception text.)
Additionally is it possible to see in which sub-expression (a,b or c) the exception was raised?
Pyparsing exceptions include a method markInputline() that will print the last line of the input string and a marker where the exception occurred:
import pyparsing as pp
a = pp.Word(pp.alphas).setResultsName('A')
b = pp.Word(pp.nums).setResultsName('B')
c = pp.Word(pp.alphas).setResultsName('C')
expr = a + b + c
try:
expr.parseString("lskdjf lskdjf sdlkfj")
except ParseException as pe:
print(pe.markInputline())
lskdjf >!<lskdjf sdlkfj
(You can specify a different marker if you don't like '>!<'.)
Here is another method I've used that makes use of the col and line attributes of the ParseException:
alphaword = pp.Word(pp.alphas).setName('alphaword')
numword = pp.Word(pp.nums).setName('numword')
expr = alphaword('A') + numword('B') + alphaword('C')
try:
expr.parseString('sldkj slkdj sldkj')
except ParseException as pe:
print(pe.line)
print(' '*(pe.col-1) + '^')
print(pe)
sldkj slkdj sldkj
^
Expected numword (at char 6), (line:1, col:7)
A couple of other points:
I've used setName() to give the expressions themselves names, so that the exception messages are a little more readable. Please note the distinction between setName and setResultsName.
I've used the call syntax for defining results names. In practice (or just out of laziness) I found the '.setResultsName' method call to really detract from the grammar definition portion of the code. So in place of expr.setResultsName('xyz'), you can just write expr('xyz').

How do I project-range in a nested tuple via dereferencing using Apache Pig?

I have tuple like so:
(a: (b:int, c:int, d:int, e:int))
I would like to call a UDF and pass a range of the nested tuple. This is what I would expect the command to be:
FOREACH alias GENERATE myUDF(a.(c .. e));
but this gives me an error like
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <line 12, column 133> mismatched input '(' expecting SEMI_COLON
What is the proper way to perform this operation?
Thanks!
Uri
You have a couple of options. If your tuple is not too long, it hopefully won't be too cumbersome to use the TOTUPLE built-in UDF and list each member individually:
FOREACH alias GENERATE myUDF(TOTUPLE(a.c, a.d, a.e));
But you are probably asking this question because it will be cumbersome for your application. In that case you could start by FLATTENing the tuple and then doing a range:
FOREACH (FOREACH alias GENERATE FLATTEN(a)) GENERATE myUDF(TOTUPLE(a::c .. a::e));
I'm not sure exactly what you expect to be passing to the UDF -- a smaller tuple? Or a collection of elements from the original tuple? Depending on what your UDF does, the above TOTUPLEs may be superfluous.

Type inference failed in the call to 'SelectMany'

I have this LINQ query:
var businessAffiliates = from b in context.Businesses
from ba in b.BusinessOfficers
from p in ba.Person
select b;
but I am getting this error:
An expression of type
'myproj.Models.Person' is not allowed
in a subsequent from clause in a query
expression with source type
'System.Linq.IQueryable'.
Type inference failed in the call to
'SelectMany'.
It looks like ba.Person it a single object, but the from clause expects a sequence of objects.
if you replace that line with let p = ba.Person then it would work. But i wonder why you need those additional from clauses.

Resources