Skip to content
Permalink
Browse files
[ASTERIXDB-3015][FUN] Fix avg() handling of first non-number
- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Fix incorrect result produced by avg() function
  when its first input value is not a number

Change-Id: I21cc52c56fb79a609aabdf994a7b6e19d570b6ea
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/15305
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
  • Loading branch information
Dmitry Lychagin committed Feb 16, 2022
1 parent 2a983d2 commit 3fe860f40a50cb5ffaed2de33243e4cba2756574
Showing 10 changed files with 167 additions and 5 deletions.
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
* Description : Run avg over a list of mixed types (ASTERIXDB-3015)
*/

drop dataverse test if exists;
create dataverse test;

use test;

create dataset d1(id bigint) open type primary key id;
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
* Description : Run avg over a list of mixed types (ASTERIXDB-3015)
*/

use test;

insert into d1 (
[
{ "id": 1, "i": 1, "v": [ "a", 1, 2, 3 ] },
{ "id": 2, "i": 2, "v": [ "b", 4, 5, 6 ] }
]
);
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
* Description : Run avg over a list of mixed types (ASTERIXDB-3015)
* Expected Res : Failure
*/

-- param max-warnings:json=100

select avg(x) a from ["a", 1, 2, 3] x;
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
* Description : Run avg over a list of mixed types (ASTERIXDB-3015)
* Expected Res : Failure
*/

-- param max-warnings:json=100

use test;

select d1.id, avg(v) a
from d1, d1.v
group by d1.id
order by d1.id;
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*
* Description : Run avg over a list of mixed types (ASTERIXDB-3015)
* Expected Res : Failure
*/

-- param max-warnings:json=100

use test;


select d1.id, avg(v) a
from d1, d1.v
/* +hash */ group by d1.id
order by d1.id;
@@ -0,0 +1 @@
{ "a": 2.0 }
@@ -0,0 +1,2 @@
{ "id": 1, "a": 2.0 }
{ "id": 2, "a": 5.0 }
@@ -0,0 +1,2 @@
{ "id": 1, "a": 2.0 }
{ "id": 2, "a": 5.0 }
@@ -2883,6 +2883,14 @@
<expected-error>ASX1079: Compilation error: arrayagg is a SQL-92 aggregate function. The SQL++ core aggregate function strict_arrayagg could potentially express the intent.</expected-error>
</compilation-unit>
</test-case>
<test-case FilePath="aggregate-sql-sugar" check-warnings="true">
<compilation-unit name="avg_mixed">
<output-dir compare="Text">avg_mixed</output-dir>
<expected-warn>ASX0004: Unsupported type: agg-avg cannot process input type string (in line 26, at column 12)</expected-warn>
<expected-warn>ASX0004: Unsupported type: agg-avg cannot process input type string (in line 28, at column 19)</expected-warn>
<expected-warn>ASX0004: Unsupported type: agg-avg cannot process input type string (in line 29, at column 19)</expected-warn>
</compilation-unit>
</test-case>
<test-case FilePath="aggregate-sql-sugar">
<compilation-unit name="distinct_mixed">
<output-dir compare="Text">distinct_mixed</output-dir>
@@ -136,21 +136,22 @@ protected void processDataValues(IFrameTupleReference tuple) throws HyracksDataE
int offset = inputVal.getStartOffset();

ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(data[offset]);
ATypeTag aggTypeTag = aggType;
if (typeTag == ATypeTag.MISSING || typeTag == ATypeTag.NULL) {
processNull();
return;
} else if (aggType == ATypeTag.SYSTEM_NULL) {
aggType = typeTag;
} else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggType)) {
} else if (aggTypeTag == ATypeTag.SYSTEM_NULL) {
aggTypeTag = typeTag;
} else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggTypeTag)) {
// Issue warning only once and treat current tuple as null
if (!isWarned) {
isWarned = true;
ExceptionUtil.warnUnsupportedType(context, sourceLoc, getIdentifier().getName(), typeTag);
}
processNull();
return;
} else if (ATypeHierarchy.canPromote(aggType, typeTag)) {
aggType = typeTag;
} else if (ATypeHierarchy.canPromote(aggTypeTag, typeTag)) {
aggTypeTag = typeTag;
}

switch (typeTag) {
@@ -195,6 +196,7 @@ protected void processDataValues(IFrameTupleReference tuple) throws HyracksDataE
}
}
count++;
aggType = aggTypeTag;
}

protected void finishPartialResults(IPointable result) throws HyracksDataException {

0 comments on commit 3fe860f

Please sign in to comment.