geotools filter CQLException: Encountered "t" - cql3

I am querying a simple feature type schema:
r:Long:index=join,*g:Point:srid=4326,di:Integer:index=join,al:Float,s:Float,b:Float,an:Float,he:Float,ve:Float,t:Float,m:Boolean,i:Boolean,ts:Long;geomesa.table.sharing='true',geomesa.indices='attr:4:3,records:2:3,z2:3:3',geomesa.table.sharing.prefix='\\u0001'
with the query expression: r = 31 AND di = 5 AND BBOX(g, -38.857822, -76.111145, -74.64091, -38.61907) AND al <= 39.407307 AND s <= 1.6442835 AND b <= 83.14717 AND an <= 87.0774 AND he <= 40.89476 AND ve <= 88.761566 AND t <= 44.786507 AND m = true AND i = true.
but it throws an exception saying Encountered "t" at line 1, column 195.
Here is my exception log detail:
org.geotools.filter.text.cql2.CQLException: Encountered "t" at line 1, column 195.
Was expecting one of:
<NOT> ...
<IDENTIFIER> ...
"include" ...
"exclude" ...
"(" ...
"[" ...
Parsing : r = 31 AND di = 5 AND BBOX(g, -38.857822, -76.111145, -74.64091, -38.61907) AND al <= 39.407307 AND s <= 1.6442835 AND b <= 83.14717 AND an <= 87.0774 AND he <= 40.89476 AND ve <= 88.761566 AND t <= 44.786507 AND m = true AND i = true.
at org.geotools.filter.text.cql2.CQLCompiler.compileFilter(CQLCompiler.java:106)
at org.geotools.filter.text.commons.CompilerUtil.parseFilter(CompilerUtil.java:196)
at org.geotools.filter.text.cql2.CQL.toFilter(CQL.java:134)
at org.geotools.filter.text.cql2.CQL.toFilter(CQL.java:113)
at com.hps.GeomesaClient.query(GeomesaClient.java:134)
at com.hps.Reader.run(Reader.java:69)
at java.lang.Thread.run(Thread.java:745)
I am not able to determine, why it's throwing an exception on querying with the attribute named t. Whereas if I remove attribute t from the query, it works as expected. Is t a reserved key? or I am missing something.

Ok, this is a limitation in the ECQL query parser. The letter 't' by itself (ignoring case) is the UTC token.
https://github.com/geotools/geotools/blob/master/modules/library/cql/src/main/jjtree/ECQLGrammar.jjt#L180-L187
The options are to work with the GeoTools team to fix this corner case or pick a different attribute name. Nice find!

Related

Drools rule with not condition with multiple condition causing error

When i use below condition with 'not' I am getting an error.
not(Obj1(value == 0) && Obj2(value <= 3))
However if i replace above condition as below I am not getting any casting exception
Obj1(value != 0) or Obj2(value > 3)
The rule looks like this:
rule "test_6"
salience 10
when
not(Obj1(value == 0) && Obj2(value <= 3))
then
.....
end
And this is the error I'm getting:
throwing error Error Message: org.drools.core.rule.GroupElement cannot be cast to org.drools.core.rule.Pattern
The && and || operators can only be used inside a single pattern. For example: Obj1( value > 3 && value < 10 || value == 0). According to the documentation, to separate Patterns, you have to use the and and or operators.
So, in your case, your rule should be:
rule "test_6"
salience 10
when
not(Obj1(value == 0) and Obj2(value <= 3))
then
.....
end
Note that it was not failing when you were using or because that was the right operator to use instead of ||.
Hope it helps,

Pyspark join doesn't take 5 positional arguments?

I'm implementing LEFT JOIN on 5 columns in Pyspark. But it's throwing an error as shown below
TypeError: join() takes from 2 to 4 positional arguments but 5 were given
Code implemented :
Tgt_df_time_in_zone_detail = Tgt_df_view_time_in_zone_detail_dtaas.join(Tgt_df_individual_in_shift_tiz
,Tgt_df_view_time_in_zone_detail_dtaas.id_individual == Tgt_df_individual_in_shift_tiz.id_individual,
(Tgt_df_view_time_in_zone_detail_dtaas.timestamp_start >= Tgt_df_individual_in_shift_tiz.swipein)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_start <= Tgt_df_individual_in_shift_tiz.swipeout)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_end >= Tgt_df_individual_in_shift_tiz.swipein)
&(Tgt_df_view_time_in_zone_detail_dtaas.timestamp_end <= Tgt_df_individual_in_shift_tiz.swipeout)
, "left_outer")
Why Pyspark doesn't take join on 5 columns? What's the better way to do it then!?
Guess, you missed & in between your 1st and 2nd condition. Try this, if it works.
Tgt_df_time_in_zone_detail = Tgt_df_view_time_in_zone_detail_dtaas.join(Tgt_df_individual_in_shift_tiz,
(Tgt_df_view_time_in_zone_detail_dtaas.id_individual == Tgt_df_individual_in_shift_tiz.id_individual)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_start >= Tgt_df_individual_in_shift_tiz.swipein)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_start <= Tgt_df_individual_in_shift_tiz.swipeout)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_end >= Tgt_df_individual_in_shift_tiz.swipein)
& (Tgt_df_view_time_in_zone_detail_dtaas.timestamp_end <= Tgt_df_individual_in_shift_tiz.swipeout)
, "left_outer")

Drools: how to use abbreviated condition notation together with further conditions?

Using Drools 6.5.0.Final
I want to use abbreviated combined relation condition (e.g. Person( age > 30 && < 40 )) in combination with additional conditions.
I tried it, but the resulting rules are executed more than once.
I have a small example, where temperature deviation from a setpoint is checked and the allowed deviations depend on the setpoint. The allowed deviations are configured with Param facts, see below.
If I execute the example (fire-all-rules):
rule 1 fires two times (bug?)
rule 2 fires once as expected (without abbreviated notation).
Is my usage of the abbreviated notation wrong or is this a bug?
Example rules:
declare Param
from : float
to : float
low : float
high : float
end
declare TemperatureEvent
#role( event )
id : String
setpoint : float
t : float
end
rule "Init abbreviated conditions test"
when
then
insert(new Param(0, 10, 1, 1));
insert(new Param(10,20, 2, 3));
insert(new TemperatureEvent("id1", 13.7f,11.5f));
// rule 1 and rule 2 should fire exactly once
end
rule "rule 1"
when
$p: Param()
$x : TemperatureEvent($p.from <= setpoint && < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
System.out.println("rule 1: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end
rule "rule 2"
when
$p: Param()
$x : TemperatureEvent($p.from <= setpoint, setpoint < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
System.out.println("rule 2: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end
The abbreviated restriction
$p.from <= setpoint && < $p.to
is equivalent to
$p.from <= setpoint && $p.from < $p.to
What you want is
setpoint >= $p.from && < $p.to

unexpected Error in reduce

While finding max value with reduce in pyspark i am getting the below unexpected result.
agg.reduce(lambda a,b : a if a > b else b )
and my sample data is
(u'2013-10-17', 80325.0)
(u'2014-01-01', 68521.0)
(u'2013-11-10', 83691.0)
(u'2013-11-14', 149289.0)
(u'2013-11-18', 94756.0)
(u'2014-01-30', 126171.0)
and result is
(u'2014-07-24', 97088.0)
It should gave result more than 94756
Thanks
sPradeep
You should compare the second value in tuple, like this:
agg.reduce(lambda a,b : a if a[1] > b[1] else b )
Just use max with key:
rdd.max(key=lambda x: x[1])

Query using "CASE WHEN" statement in WHERE causes QuerySyntaxException: unexpected AST

I'm trying to make a query using Spring Data, but I cannot make it work:
#Query(SELECT t FROM Thing t WHERE name LIKE :name AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END AND CASE WHEN (:maxVal <= 0) THEN TRUE ELSE (val >= :maxVal) END)
Page<Thing> getThings(#Param("name") String name, #Param("maxVal") int maxVal, #Param("minVal") minVal);
StackTrace:
Caused by: java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST
node: CASE near line 1, column 49 [SELECT t FROM Thing t WHERE name
LIKE :name AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <=
:minVal) END AND CASE WHEN (:maxVal <= 0) THEN TRUE ELSE (val >=
:maxVal) END] at
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at
org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) at
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:334)
at com.sun.proxy.$Proxy83.createQuery(Unknown Source) at
org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:78)
... 207 more
Caused by:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST
node: CASE near line 1, column 49 [SELECT t FROM Thing t WHERE name
LIKE :name AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <=
:minVal) END AND CASE WHEN (:maxVal <= 0) THEN TRUE ELSE (val >=
:maxVal) END] at
org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at
org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)
at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at
org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:126)
at
org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:88)
at
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at
org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
I'm using this because I want to make a longer query using five filters at least, and I want to simplify the effort of doing the filter combinations making differents querys.
Don't know if there is a different (and better) way to do what I want, glad to ear it if it is.
Thank you.
EDIT: Using native query works fine, but isn't compatible with pagination yet...
It looks like Hibernate cannot evaluate the result of a CASE expression when it returns a boolean literal directly. A workaround is to make the CASE expression part of another expression, e.g. by comparing it to another boolean literal.
So instead of:
... AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END
Try:
... AND (CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END) = TRUE
But looking at that expression, wouldn't it be simpler to just do:
... AND (:minVal <= 0 OR val <= :minVal)
Is it not equivalent?