org.apache.calcite.rel.type.RelDataTypeFactoryImpl Java Examples

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactoryImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlUserDefinedTableMacro.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static Object coerce(Object o, RelDataType type) {
  if (o == null) {
    return null;
  }
  if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) {
    return null;
  }
  final RelDataTypeFactoryImpl.JavaType javaType =
      (RelDataTypeFactoryImpl.JavaType) type;
  final Class<?> clazz = javaType.getJavaClass();
  //noinspection unchecked
  if (clazz.isAssignableFrom(o.getClass())) {
    return o;
  }
  if (clazz == String.class && o instanceof NlsString) {
    return ((NlsString) o).getValue();
  }
  return null;
}
 
Example #2
Source File: SqlUserDefinedAggFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelDataType toSql(RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example #3
Source File: CalciteCatalogReader.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example #4
Source File: SqlUserDefinedAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelDataType toSql(RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example #5
Source File: DremioCatalogReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private RelDataType toSql(RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return typeFactory.toSql(type);
}
 
Example #6
Source File: SamzaSqlValidator.java    From samza with Apache License 2.0 5 votes vote down vote up
private RelDataType getCalciteSqlFieldType(RelDataType fieldType) {
  RelDataType sqlFieldType;

  // JavaTypes are relevant for Udf argument and return types
  // TODO: Support UDF argument validation. Currently, only return types are validated and argument types are
  //  validated during run-time.
  if (fieldType instanceof RelDataTypeFactoryImpl.JavaType) {
    sqlFieldType = new SamzaSqlJavaTypeFactoryImpl().toSql(fieldType);
  } else {
    sqlFieldType = fieldType;
  }

  return sqlFieldType;
}
 
Example #7
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 5 votes vote down vote up
public Enumerable<MetaColumn> columns(final MetaTable table_) {
  final QuarkMetaTable table = (QuarkMetaTable) table_;
  final RelDataType rowType =
      table.calciteTable.getRowType(getConnection().typeFactory);
  return Linq4j.asEnumerable(rowType.getFieldList())
      .select(
          new Function1<RelDataTypeField, MetaColumn>() {
            public MetaColumn apply(RelDataTypeField field) {
              final int precision =
                  field.getType().getSqlTypeName().allowsPrec()
                      && !(field.getType()
                      instanceof RelDataTypeFactoryImpl.JavaType)
                      ? field.getType().getPrecision()
                      : -1;
              return new MetaColumn(
                  table.tableCat,
                  table.tableSchem,
                  table.tableName,
                  field.getName(),
                  field.getType().getSqlTypeName().getJdbcOrdinal(),
                  field.getType().getFullTypeString(),
                  precision,
                  field.getType().getSqlTypeName().allowsScale()
                      ? field.getType().getScale()
                      : null,
                  10,
                  field.getType().isNullable()
                      ? DatabaseMetaData.columnNullable
                      : DatabaseMetaData.columnNoNulls,
                  precision,
                  field.getIndex() + 1,
                  field.getType().isNullable() ? "YES" : "NO");
            }
          });
}
 
Example #8
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Cast operand at index {@code index} to target type.
 * we do this base on the fact that validate happens before type coercion.
 */
protected boolean coerceOperandType(
    SqlValidatorScope scope,
    SqlCall call,
    int index,
    RelDataType targetType) {
  // Transform the JavaType to SQL type because the SqlDataTypeSpec
  // does not support deriving JavaType yet.
  if (RelDataTypeFactoryImpl.isJavaType(targetType)) {
    targetType = ((JavaTypeFactory) factory).toSql(targetType);
  }

  SqlNode operand = call.getOperandList().get(index);
  if (operand instanceof SqlDynamicParam) {
    // Do not support implicit type coercion for dynamic param.
    return false;
  }
  // Check it early.
  if (!needToCast(scope, operand, targetType)) {
    return false;
  }
  // Fix up nullable attr.
  RelDataType targetType1 = syncAttributes(validator.deriveType(scope, operand), targetType);
  SqlNode desired = castTo(operand, targetType1);
  call.setOperand(index, desired);
  updateInferredType(desired, targetType1);
  return true;
}
 
Example #9
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelDataType nullifyType(JavaTypeFactory typeFactory,
    final RelDataType type, final boolean nullable) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType) {
    final Primitive primitive = Primitive.ofBox(
        ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass());
    if (primitive != null) {
      return typeFactory.createJavaType(primitive.primitiveClass);
    }
  }
  return typeFactory.createTypeWithNullability(type, nullable);
}
 
Example #10
Source File: PlanExecutor.java    From quark with Apache License 2.0 5 votes vote down vote up
private static String getTypeName(RelDataType type) {
  SqlTypeName sqlTypeName = type.getSqlTypeName();
  if (type instanceof RelDataTypeFactoryImpl.JavaType) {
    // We'd rather print "INTEGER" than "JavaType(int)".
    return sqlTypeName.getName();
  }
  switch (sqlTypeName) {
    case INTERVAL_YEAR_MONTH:
      // e.g. "INTERVAL_MONTH" or "INTERVAL_YEAR_MONTH"
      return "INTERVAL_"
          + type.getIntervalQualifier().toString().replace(' ', '_');
    default:
      return type.toString(); // e.g. "VARCHAR(10)", "INTEGER ARRAY"
  }
}
 
Example #11
Source File: PigRelSqlUdfs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the return data type for a given function.
 *
 * @param function ScalarFunction
 * @return returned data type
 */
private static RelDataType getRelDataType(ScalarFunction function) {
  final JavaTypeFactory typeFactory = TYPE_FACTORY;
  final RelDataType type = function.getReturnType(typeFactory);
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return typeFactory.toSql(type);
}
 
Example #12
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<MetaColumn> columns(final MetaTable table_) {
  final CalciteMetaTable table = (CalciteMetaTable) table_;
  final RelDataType rowType =
      table.calciteTable.getRowType(getConnection().typeFactory);
  return Linq4j.asEnumerable(rowType.getFieldList())
      .select(field -> {
        final int precision =
            field.getType().getSqlTypeName().allowsPrec()
                && !(field.getType()
                instanceof RelDataTypeFactoryImpl.JavaType)
                ? field.getType().getPrecision()
                : -1;
        return new MetaColumn(
            table.tableCat,
            table.tableSchem,
            table.tableName,
            field.getName(),
            field.getType().getSqlTypeName().getJdbcOrdinal(),
            field.getType().getFullTypeString(),
            precision,
            field.getType().getSqlTypeName().allowsScale()
                ? field.getType().getScale()
                : null,
            10,
            field.getType().isNullable()
                ? DatabaseMetaData.columnNullable
                : DatabaseMetaData.columnNoNulls,
            precision,
            field.getIndex() + 1,
            field.getType().isNullable() ? "YES" : "NO");
      });
}
 
Example #13
Source File: CalciteCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example #14
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType) {
    final SqlTypeName typeName = type.getSqlTypeName();
    if (typeName != null && typeName != SqlTypeName.OTHER) {
      return typeFactory.createTypeWithNullability(
          typeFactory.createSqlType(typeName),
          type.isNullable());
    }
  }
  return type;
}
 
Example #15
Source File: IntervalSqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Combines two IntervalTypes and returns the result. E.g. the result of
 * combining<br>
 * <code>INTERVAL DAY TO HOUR</code><br>
 * with<br>
 * <code>INTERVAL SECOND</code> is<br>
 * <code>INTERVAL DAY TO SECOND</code>
 */
public IntervalSqlType combine(
    RelDataTypeFactoryImpl typeFactory,
    IntervalSqlType that) {
  assert this.typeName.isYearMonth() == that.typeName.isYearMonth();
  boolean nullable = isNullable || that.isNullable;
  TimeUnit thisStart = Objects.requireNonNull(typeName.getStartUnit());
  TimeUnit thisEnd = typeName.getEndUnit();
  final TimeUnit thatStart =
      Objects.requireNonNull(that.typeName.getStartUnit());
  final TimeUnit thatEnd = that.typeName.getEndUnit();

  int secondPrec =
      this.intervalQualifier.getStartPrecisionPreservingDefault();
  final int fracPrec =
      SqlIntervalQualifier.combineFractionalSecondPrecisionPreservingDefault(
          typeSystem,
          this.intervalQualifier,
          that.intervalQualifier);

  if (thisStart.ordinal() > thatStart.ordinal()) {
    thisEnd = thisStart;
    thisStart = thatStart;
    secondPrec =
        that.intervalQualifier.getStartPrecisionPreservingDefault();
  } else if (thisStart.ordinal() == thatStart.ordinal()) {
    secondPrec =
        SqlIntervalQualifier.combineStartPrecisionPreservingDefault(
            typeFactory.getTypeSystem(),
            this.intervalQualifier,
            that.intervalQualifier);
  } else if (null == thisEnd || thisEnd.ordinal() < thatStart.ordinal()) {
    thisEnd = thatStart;
  }

  if (null != thatEnd) {
    if (null == thisEnd || thisEnd.ordinal() < thatEnd.ordinal()) {
      thisEnd = thatEnd;
    }
  }

  RelDataType intervalType =
      typeFactory.createSqlIntervalType(
          new SqlIntervalQualifier(
              thisStart,
              secondPrec,
              thisEnd,
              fracPrec,
              SqlParserPos.ZERO));
  intervalType =
      typeFactory.createTypeWithNullability(
          intervalType,
          nullable);
  return (IntervalSqlType) intervalType;
}
 
Example #16
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Cast column at index {@code index} to target type.
 *
 * @param scope      Validator scope for the node list
 * @param nodeList   Column node list
 * @param index      Index of column
 * @param targetType Target type to cast to
 */
protected boolean coerceColumnType(
    SqlValidatorScope scope,
    SqlNodeList nodeList,
    int index,
    RelDataType targetType) {
  // Transform the JavaType to SQL type because the SqlDataTypeSpec
  // does not support deriving JavaType yet.
  if (RelDataTypeFactoryImpl.isJavaType(targetType)) {
    targetType = ((JavaTypeFactory) factory).toSql(targetType);
  }

  // This will happen when there is a star/dynamic-star column in the select list,
  // and the source is values expression, i.e. `select * from (values(1, 2, 3))`.
  // There is no need to coerce the column type, only remark
  // the inferred row type has changed, we will then add in type coercion
  // when expanding star/dynamic-star.

  // See SqlToRelConverter#convertSelectList for details.
  if (index >= nodeList.getList().size()) {
    // Can only happen when there is a star(*) in the column,
    // just return true.
    return true;
  }

  final SqlNode node = nodeList.get(index);
  if (node instanceof SqlDynamicParam) {
    // Do not support implicit type coercion for dynamic param.
    return false;
  }
  if (node instanceof SqlIdentifier) {
    // Do not expand a star/dynamic table col.
    SqlIdentifier node1 = (SqlIdentifier) node;
    if (node1.isStar()) {
      return true;
    } else if (DynamicRecordType.isDynamicStarColName(Util.last(node1.names))) {
      // Should support implicit cast for dynamic table.
      return false;
    }
  }

  if (node instanceof SqlCall) {
    SqlCall node2 = (SqlCall) node;
    if (node2.getOperator().kind == SqlKind.AS) {
      final SqlNode operand = node2.operand(0);
      if (!needToCast(scope, operand, targetType)) {
        return false;
      }
      RelDataType targetType2 = syncAttributes(validator.deriveType(scope, operand), targetType);
      final SqlNode casted = castTo(operand, targetType2);
      node2.setOperand(0, casted);
      updateInferredType(casted, targetType2);
      return true;
    }
  }
  if (!needToCast(scope, node, targetType)) {
    return false;
  }
  RelDataType targetType3 = syncAttributes(validator.deriveType(scope, node), targetType);
  final SqlNode node3 = castTo(node, targetType3);
  nodeList.set(index, node3);
  updateInferredType(node3, targetType3);
  return true;
}
 
Example #17
Source File: PlanExecutor.java    From quark with Apache License 2.0 4 votes vote down vote up
private RelDataTypeFactoryImpl.JavaType getStringJavaType() {
  RelDataTypeFactoryImpl relDataTypeFactoryImpl = new JavaTypeFactoryImpl();
  return relDataTypeFactoryImpl.new JavaType(String.class,
      !(String.class.isPrimitive()), Util.getDefaultCharset(), null);
}
 
Example #18
Source File: PlanExecutor.java    From quark with Apache License 2.0 4 votes vote down vote up
private RelDataTypeFactoryImpl.JavaType getIntegerJavaType() {
  RelDataTypeFactoryImpl relDataTypeFactoryImpl = new JavaTypeFactoryImpl();
  return relDataTypeFactoryImpl.new JavaType(Integer.class);
}
 
Example #19
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example #20
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example #21
Source File: IntervalSqlType.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Combines two IntervalTypes and returns the result. E.g. the result of
 * combining<br>
 * <code>INTERVAL DAY TO HOUR</code><br>
 * with<br>
 * <code>INTERVAL SECOND</code> is<br>
 * <code>INTERVAL DAY TO SECOND</code>
 */
public IntervalSqlType combine(
    RelDataTypeFactoryImpl typeFactory,
    IntervalSqlType that) {
  assert this.typeName.isYearMonth() == that.typeName.isYearMonth();
  boolean nullable = isNullable || that.isNullable;
  TimeUnit thisStart = Objects.requireNonNull(typeName.getStartUnit());
  TimeUnit thisEnd = typeName.getEndUnit();
  final TimeUnit thatStart =
      Objects.requireNonNull(that.typeName.getStartUnit());
  final TimeUnit thatEnd = that.typeName.getEndUnit();

  int secondPrec =
      this.intervalQualifier.getStartPrecisionPreservingDefault();
  final int fracPrec =
      SqlIntervalQualifier.combineFractionalSecondPrecisionPreservingDefault(
          typeSystem,
          this.intervalQualifier,
          that.intervalQualifier);

  if (thisStart.ordinal() > thatStart.ordinal()) {
    thisEnd = thisStart;
    thisStart = thatStart;
    secondPrec =
        that.intervalQualifier.getStartPrecisionPreservingDefault();
  } else if (thisStart.ordinal() == thatStart.ordinal()) {
    secondPrec =
        SqlIntervalQualifier.combineStartPrecisionPreservingDefault(
            typeFactory.getTypeSystem(),
            this.intervalQualifier,
            that.intervalQualifier);
  } else if (null == thisEnd || thisEnd.ordinal() < thatStart.ordinal()) {
    thisEnd = thatStart;
  }

  if (null != thatEnd) {
    if (null == thisEnd || thisEnd.ordinal() < thatEnd.ordinal()) {
      thisEnd = thatEnd;
    }
  }

  RelDataType intervalType =
      typeFactory.createSqlIntervalType(
          new SqlIntervalQualifier(
              thisStart,
              secondPrec,
              thisEnd,
              fracPrec,
              SqlParserPos.ZERO));
  intervalType =
      typeFactory.createTypeWithNullability(
          intervalType,
          nullable);
  return (IntervalSqlType) intervalType;
}