Skip to content

Commit

Permalink
TEIID-5459 adding long versions of count and ranking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Oct 2, 2018
1 parent c2b85db commit a55ec44
Show file tree
Hide file tree
Showing 37 changed files with 408 additions and 44 deletions.
1 change: 1 addition & 0 deletions api/src/main/java/org/teiid/language/SQLConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public interface NonReserved {
public static final String MAX = "MAX"; //$NON-NLS-1$
public static final String MIN = "MIN"; //$NON-NLS-1$
public static final String COUNT = "COUNT"; //$NON-NLS-1$
public static final String COUNT_BIG = "COUNT_BIG"; //$NON-NLS-1$
public static final String AVG = "AVG"; //$NON-NLS-1$
public static final String SUM = "SUM"; //$NON-NLS-1$
//texttable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public void visit(AggregateFunction obj) {
.append(Tokens.SPACE);
}

if (obj.getParameters().isEmpty() && SQLConstants.NonReserved.COUNT.equalsIgnoreCase(obj.getName())) {
if (obj.getParameters().isEmpty()
&& (SQLConstants.NonReserved.COUNT.equalsIgnoreCase(obj.getName())
|| SQLConstants.NonReserved.COUNT_BIG.equalsIgnoreCase(obj.getName()))) {
buffer.append(Tokens.ALL_COLS);
} else {
append(obj.getParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,20 @@ public void setSupportsMultipleOpenStatements(
this.supportsMultipleOpenStatements = supportsMultipleOpenStatements;
}

Boolean supportsAggregatesCountBig;
@Override
public boolean supportsAggregatesCountBig() {
if (supportsAggregatesCountBig != null) {
return supportsAggregatesCountBig;
}
return delegate.supportsAggregatesCountBig();
}

public void setSupportsAggregatesCountBig(
boolean supportsAggregatesCountBig) {
this.supportsAggregatesCountBig = supportsAggregatesCountBig;
}

Boolean supportsGeographyType;
@Override
public boolean supportsGeographyType() {
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/org/teiid/translator/ExecutionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,17 @@ public boolean supportsWindowFunctionNthValue() {
public boolean supportsMultipleOpenExecutions() {
return true;
}

/**
* @since 11.2
* @return true if the translator supports a specific count aggregate returning a long value
*
* {@link #supportsAggregatesCount()} will be consulted by the engine, this capability
* only affects the name of the pushed count function if a long value is expected
*/
public boolean supportsAggregatesCountBig() {
return false;
}

/**
* If the geography type is supported by the standard ST_ geospatial functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class TestBaseDelegatingExecutionFactory {
Method[] methods = ExecutionFactory.class.getDeclaredMethods();
Method[] proxyMethods = BaseDelegatingExecutionFactory.class.getDeclaredMethods();
//excluding the setter methods the counts should be equal
assertEquals(methods.length+99, proxyMethods.length);
assertEquals(methods.length+100, proxyMethods.length);
}

@Test public void testExecution() throws TranslatorException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
public class TimestampWithTimezone {

public static final String ISO8601_WEEK_PROP = "org.teiid.iso8601Week"; //$NON-NLS-1$
public static boolean ISO8601_WEEK = PropertiesUtils.getHierarchicalProperty(ISO8601_WEEK_PROP, true, Boolean.class);
public static final boolean ISO8601_WEEK = PropertiesUtils.getHierarchicalProperty(ISO8601_WEEK_PROP, true, Boolean.class);

private static ThreadLocal<Calendar> CALENDAR = new ThreadLocal<Calendar>() {
protected Calendar initialValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,10 @@ public String getTemporaryTableName(String prefix) {
protected boolean supportsBooleanExpressions() {
return false;
}

@Override
public boolean supportsAggregatesCountBig() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@ protected String getUpsertKeyword() {
};
}

@Override
public boolean supportsAggregatesCountBig() {
return getVersion().compareTo(ELEVEN_2) > 0;
}

@Override
public boolean supportsGeographyType() {
return getVersion().compareTo(ELEVEN_2) > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static BasicSourceCapabilities convertCapabilities(ExecutionFactory srcCa
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_MIN, srcCaps.supportsAggregatesMin());
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_MAX, srcCaps.supportsAggregatesMax());
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_COUNT, srcCaps.supportsAggregatesCount());
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_COUNT_BIG, srcCaps.supportsAggregatesCountBig());
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_COUNT_STAR, srcCaps.supportsAggregatesCountStar());
tgtCaps.setCapabilitySupport(Capability.QUERY_AGGREGATES_DISTINCT, srcCaps.supportsAggregatesDistinct());
tgtCaps.setCapabilitySupport(Capability.QUERY_SUBQUERIES_SCALAR, srcCaps.supportsScalarSubqueries());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public static void initLanguageBridgeFactory(LanguageBridgeFactory factory,
CommandContext context, SourceCapabilities capabilities) {
factory.setCommandContext(context);
factory.setSupportsConcat2(capabilities.supportsFunction(SourceSystemFunctions.CONCAT2));
factory.setSupportsCountBig(capabilities.supportsCapability(Capability.QUERY_AGGREGATES_COUNT_BIG));
factory.setMaxInPredicateSize((Integer) capabilities.getSourceProperty(Capability.MAX_IN_CRITERIA_SIZE));
factory.setExcludeWithName((String) capabilities.getSourceProperty(Capability.EXCLUDE_COMMON_TABLE_EXPRESSION_NAME));
factory.setSourceNullOrder((NullOrder) capabilities.getSourceProperty(Capability.QUERY_ORDERBY_DEFAULT_NULL_ORDER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.teiid.query.sql.lang.SetQuery;
import org.teiid.query.sql.lang.Update;
import org.teiid.query.sql.symbol.AggregateSymbol;
import org.teiid.query.sql.symbol.AggregateSymbol.Type;
import org.teiid.query.sql.symbol.AliasSymbol;
import org.teiid.query.sql.symbol.Array;
import org.teiid.query.sql.symbol.Constant;
Expand Down Expand Up @@ -156,6 +157,7 @@ public void remove() {
private boolean convertIn;
private boolean supportsConcat2;
private int maxInCriteriaSize;
private boolean supportsCountBig;

//state to handle with name exclusion
private IdentityHashMap<Object, GroupSymbol> remappedGroups;
Expand Down Expand Up @@ -188,6 +190,10 @@ public void setExcludeWithName(String excludeWithName) {
this.excludeWithName = excludeWithName;
}

public void setSupportsCountBig(boolean supportsCountBig) {
this.supportsCountBig = supportsCountBig;
}

public org.teiid.language.Command translate(Command command) {
try {
if (command == null) {
Expand Down Expand Up @@ -920,6 +926,8 @@ AggregateFunction translate(AggregateSymbol symbol) {
name = Symbol.getShortName(symbol.getFunctionDescriptor().getName());
} else if (symbol.getAggregateFunction() == AggregateSymbol.Type.USER_DEFINED) {
name = symbol.getName();
} else if (symbol.getAggregateFunction() == Type.COUNT_BIG && !supportsCountBig) {
name = Type.COUNT.name();
}

AggregateFunction af = new AggregateFunction(name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public List<FunctionMethod> getBuiltInAggregateFunctions(boolean includeAnalytic
case RANK:
case ROW_NUMBER:
aa.setAllowsDistinct(false);
returnType = DataTypeManager.DefaultDataTypes.INTEGER;
returnType = AggregateSymbol.LONG_RANKS?DataTypeManager.DefaultDataTypes.LONG:DataTypeManager.DefaultDataTypes.INTEGER;
argTypes = new String[] {};
break;
case ANY:
Expand All @@ -588,6 +588,10 @@ public List<FunctionMethod> getBuiltInAggregateFunctions(boolean includeAnalytic
returnType = DataTypeManager.DefaultDataTypes.BOOLEAN;
argTypes = new String[] {DataTypeManager.DefaultDataTypes.BOOLEAN};
break;
case COUNT_BIG:
returnType = DataTypeManager.DefaultDataTypes.LONG;
argTypes = new String[] {DataTypeManager.DefaultDataTypes.OBJECT};
break;
case COUNT:
returnType = DataTypeManager.DefaultDataTypes.INTEGER;
argTypes = new String[] {DataTypeManager.DefaultDataTypes.OBJECT};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags and
* the COPYRIGHT.txt file distributed with this work.
*
* Licensed 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.
*/

package org.teiid.query.function.aggregate;

import java.util.Arrays;
import java.util.List;

import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.query.QueryPlugin;
import org.teiid.query.util.CommandContext;

/**
* Just a simple COUNT() implementation that counts every non-null row it sees.
*/
public class CountBig extends AggregateFunction {

private long count = 0;

public void reset() {
count = 0;
}

@Override
public void addInputDirect(List<?> tuple, CommandContext commandContext)
throws TeiidComponentException, TeiidProcessingException {
if (count == Long.MAX_VALUE) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174));
}
count++;
}

/**
* @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext)
*/
public Object getResult(CommandContext commandContext) {
return count;
}

@Override
public void getState(List<Object> state) {
state.add(count);
}

@Override
public int setState(List<?> state, int index) {
count = (Long) state.get(index);
return index++;
}

@Override
public List<? extends Class<?>> getStateTypes() {
return Arrays.asList(Long.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags and
* the COPYRIGHT.txt file distributed with this work.
*
* Licensed 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.
*/

package org.teiid.query.function.aggregate;

import java.util.List;

import org.teiid.api.exception.query.ExpressionEvaluationException;
import org.teiid.api.exception.query.FunctionExecutionException;
import org.teiid.core.TeiidComponentException;
import org.teiid.query.QueryPlugin;
import org.teiid.query.sql.symbol.AggregateSymbol.Type;
import org.teiid.query.util.CommandContext;

/**
* computes rank/dense_rank
*/
public class RankingFunctionBig extends AggregateFunction {

private long count = 0;
private long lastCount = 0;
private Type type;

public RankingFunctionBig(Type function) {
this.type = function;
}

@Override
public void reset() {
count = 0;
lastCount = 0;
}

@Override
public void addInputDirect(List<?> tuple, CommandContext commandContext)
throws FunctionExecutionException, ExpressionEvaluationException,
TeiidComponentException {
if (type == Type.RANK) {
if (count == Long.MAX_VALUE) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID31289, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31289));
}
count++;
}
}

@Override
public Object getResult(CommandContext commandContext) throws FunctionExecutionException,
ExpressionEvaluationException, TeiidComponentException {
if (type == Type.DENSE_RANK) {
if (count == Long.MAX_VALUE) {
throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID31289, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31289));
}
count++;
return count;
}
long result = ++lastCount;
lastCount = count;
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,9 @@ public boolean isEnvAllowed() {
return true;
}

@Override
public boolean isLongRanks() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,10 @@ public Class<?> getDataTypeClass(String typeName)
public boolean isEnvAllowed() {
return actualMetadata.isEnvAllowed();
}

@Override
public boolean isLongRanks() {
return actualMetadata.isLongRanks();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,6 @@ String getExtensionProperty(Object metadataID, String key,
Class<?> getDataTypeClass(String typeOrDomainName) throws QueryMetadataException;

boolean isEnvAllowed();

boolean isLongRanks();
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public StorageMode getStorageMode() {
private Map<String, Collection<StoredProcedureInfo>> procedureCache = Collections.synchronizedMap(new LRUCache<String, Collection<StoredProcedureInfo>>(200));

private boolean widenComparisonToString = true;

private boolean allowEnv = true;
private boolean longRanks;
/**
* TransformationMetadata constructor
* @param context Object containing the info needed to lookup metadata.
Expand Down Expand Up @@ -1100,5 +1100,14 @@ public boolean isEnvAllowed() {
public void setAllowENV(boolean b) {
this.allowEnv = b;
}

@Override
public boolean isLongRanks() {
return longRanks;
}

public void setLongRanks(boolean longRanks) {
this.longRanks = longRanks;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public enum Capability {
QUERY_WINDOW_FUNCTION_NTH_VALUE,
WINDOW_FUNCTION_FRAME_CLAUSE,
QUERY_AGGREGATES_LIST,
QUERY_AGGREGATES_COUNT_BIG,
GEOGRAPHY_TYPE;

private final String toString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public static boolean supportsAggregateFunction(Object modelID, AggregateSymbol
// Check particular function
Type func = aggregate.getAggregateFunction();
switch (func) {
case COUNT_BIG:
case COUNT:
if(aggregate.getArgs().length == 0) {
if(! caps.supportsCapability(Capability.QUERY_AGGREGATES_COUNT_STAR)) {
Expand Down

0 comments on commit a55ec44

Please sign in to comment.