001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.MaxMinAggregateDefinition
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
025: import org.apache.derby.iapi.services.sanity.SanityManager;
026:
027: import org.apache.derby.impl.sql.execute.MaxMinAggregator;
028:
029: import org.apache.derby.catalog.TypeDescriptor;
030: import org.apache.derby.iapi.types.TypeId;
031: import org.apache.derby.iapi.types.DataTypeDescriptor;
032: import org.apache.derby.iapi.types.NumberDataValue;
033:
034: import org.apache.derby.iapi.error.StandardException;
035:
036: import org.apache.derby.iapi.services.context.ContextService;
037: import org.apache.derby.iapi.reference.ClassName;
038:
039: /**
040: * Defintion for the MAX()/MIN() aggregates.
041: *
042: * @author jamie
043: */
044: public class MaxMinAggregateDefinition implements AggregateDefinition {
045: private boolean isMax;
046:
047: /**
048: * Niladic constructor. Does nothing. For ease
049: * Of use, only.
050: */
051: public MaxMinAggregateDefinition() {
052: super ();
053: }
054:
055: /**
056: * Determines the result datatype. Accept NumberDataValues
057: * only.
058: * <P>
059: * <I>Note</I>: In the future you should be able to do
060: * a sum user data types. One option would be to run
061: * sum on anything that implements divide().
062: *
063: * @param inputType the input type, either a user type or a java.lang object
064: *
065: * @return the output Class (null if cannot operate on
066: * value expression of this type.
067: */
068: public final TypeDescriptor getAggregator(TypeDescriptor inputType,
069: StringBuffer aggregatorClass) {
070: LanguageConnectionContext lcc = (LanguageConnectionContext) ContextService
071: .getContext(LanguageConnectionContext.CONTEXT_ID);
072:
073: /*
074: ** MIN and MAX may return null
075: */
076: DataTypeDescriptor dts = new DataTypeDescriptor(
077: (DataTypeDescriptor) inputType, true);
078: TypeId compType = dts.getTypeId();
079:
080: /*
081: ** If the class implements NumberDataValue, then we
082: ** are in business. Return type is same as input
083: ** type.
084: */
085: if (compType.orderable(lcc.getLanguageConnectionFactory()
086: .getClassFactory())) {
087: aggregatorClass.append(ClassName.MaxMinAggregator);
088:
089: return dts;
090: }
091: return null;
092: }
093:
094: /**
095: * This is set by the parser.
096: */
097: public final void setMaxOrMin(boolean isMax) {
098: this .isMax = isMax;
099: }
100:
101: /**
102: * Return if the aggregator class is for min/max.
103: *
104: * @return boolean true/false
105: */
106: public final boolean isMax() {
107: return (isMax);
108: }
109: }
|