org.apache.calcite.util.BuiltInMethod Java Examples

The following examples show how to use org.apache.calcite.util.BuiltInMethod. 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: HiveRexExecutorImpl.java    From marble with Apache License 2.0 6 votes vote down vote up
public Expression field(BlockBuilder list, int index, Type storageType) {
  MethodCallExpression recFromCtx = Expressions.call(
      DataContext.ROOT,
      BuiltInMethod.DATA_CONTEXT_GET.method,
      Expressions.constant("inputRecord"));
  Expression recFromCtxCasted =
      RexToLixTranslator.convert(recFromCtx, Object[].class);
  IndexExpression recordAccess = Expressions.arrayIndex(recFromCtxCasted,
      Expressions.constant(index));
  if (storageType == null) {
    final RelDataType fieldType =
        rowType.getFieldList().get(index).getType();
    storageType = ((JavaTypeFactory) typeFactory).getJavaClass(fieldType);
  }
  return RexToLixTranslator.convert(recordAccess, storageType);
}
 
Example #2
Source File: JdbcToEnumerableConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Method getMethod(SqlTypeName sqlTypeName, boolean nullable,
    boolean offset) {
  switch (sqlTypeName) {
  case DATE:
    return (nullable
        ? BuiltInMethod.DATE_TO_INT_OPTIONAL
        : BuiltInMethod.DATE_TO_INT).method;
  case TIME:
    return (nullable
        ? BuiltInMethod.TIME_TO_INT_OPTIONAL
        : BuiltInMethod.TIME_TO_INT).method;
  case TIMESTAMP:
    return (nullable
        ? (offset
        ? BuiltInMethod.TIMESTAMP_TO_LONG_OPTIONAL_OFFSET
        : BuiltInMethod.TIMESTAMP_TO_LONG_OPTIONAL)
        : (offset
            ? BuiltInMethod.TIMESTAMP_TO_LONG_OFFSET
            : BuiltInMethod.TIMESTAMP_TO_LONG)).method;
  default:
    throw new AssertionError(sqlTypeName + ":" + nullable);
  }
}
 
Example #3
Source File: EnumUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Expression toInternal(Expression operand,
    Type fromType, Type targetType) {
  if (fromType == java.sql.Date.class) {
    if (targetType == int.class) {
      return Expressions.call(BuiltInMethod.DATE_TO_INT.method, operand);
    } else if (targetType == Integer.class) {
      return Expressions.call(BuiltInMethod.DATE_TO_INT_OPTIONAL.method, operand);
    }
  } else if (fromType == java.sql.Time.class) {
    if (targetType == int.class) {
      return Expressions.call(BuiltInMethod.TIME_TO_INT.method, operand);
    } else if (targetType == Integer.class) {
      return Expressions.call(BuiltInMethod.TIME_TO_INT_OPTIONAL.method, operand);
    }
  } else if (fromType == java.sql.Timestamp.class) {
    if (targetType == long.class) {
      return Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG.method, operand);
    } else if (targetType == Long.class) {
      return Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG_OPTIONAL.method, operand);
    }
  }
  return operand;
}
 
Example #4
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public Result visitDynamicParam(RexDynamicParam dynamicParam) {
  final Pair<RexNode, Type> key = Pair.of(dynamicParam, currentStorageType);
  if (rexWithStorageTypeResultMap.containsKey(key)) {
    return rexWithStorageTypeResultMap.get(key);
  }
  final Type storageType = currentStorageType != null
      ? currentStorageType : typeFactory.getJavaClass(dynamicParam.getType());
  final Expression valueExpression = EnumUtils.convert(
      Expressions.call(root, BuiltInMethod.DATA_CONTEXT_GET.method,
          Expressions.constant("?" + dynamicParam.getIndex())),
      storageType);
  final ParameterExpression valueVariable =
      Expressions.parameter(valueExpression.getType(), list.newName("value_dynamic_param"));
  list.add(Expressions.declare(Modifier.FINAL, valueVariable, valueExpression));
  final ParameterExpression isNullVariable =
      Expressions.parameter(Boolean.TYPE, list.newName("isNull_dynamic_param"));
  list.add(Expressions.declare(Modifier.FINAL, isNullVariable, checkNull(valueVariable)));
  final Result result = new Result(isNullVariable, valueVariable);
  rexWithStorageTypeResultMap.put(key, result);
  return result;
}
 
Example #5
Source File: EnumerableInterpreter.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  final JavaTypeFactory typeFactory = implementor.getTypeFactory();
  final BlockBuilder builder = new BlockBuilder();
  final PhysType physType =
      PhysTypeImpl.of(typeFactory, getRowType(), JavaRowFormat.ARRAY);
  final Expression interpreter_ = builder.append("interpreter",
      Expressions.new_(Interpreter.class,
          implementor.getRootExpression(),
          implementor.stash(getInput(), RelNode.class)));
  final Expression sliced_ =
      getRowType().getFieldCount() == 1
          ? Expressions.call(BuiltInMethod.SLICE0.method, interpreter_)
          : interpreter_;
  builder.add(sliced_);
  return implementor.result(physType, builder.toBlock());
}
 
Example #6
Source File: RexImpTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public Expression implement(RexToLixTranslator translator,
    Expression inputEnumerable, RexCall call, PhysType inputPhysType, PhysType outputPhysType) {
  RexCall timestampDescriptor = (RexCall) call.getOperands().get(0);
  RexCall keyDescriptor = (RexCall) call.getOperands().get(1);
  Expression gapInterval = translator.translate(call.getOperands().get(2));

  List<Expression> translatedOperands = new ArrayList<>();
  Expression wmColIndexExpr =
      Expressions.constant(((RexInputRef) timestampDescriptor.getOperands().get(0)).getIndex());
  Expression keyColIndexExpr =
      Expressions.constant(((RexInputRef) keyDescriptor.getOperands().get(0)).getIndex());
  translatedOperands.add(wmColIndexExpr);
  translatedOperands.add(keyColIndexExpr);
  translatedOperands.add(gapInterval);

  return Expressions.call(BuiltInMethod.SESSIONIZATION.method,
      Expressions.list(
          Expressions.call(inputEnumerable, BuiltInMethod.ENUMERABLE_ENUMERATOR.method),
          translatedOperands.get(0),
          translatedOperands.get(1),
          translatedOperands.get(2)));
}
 
Example #7
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static CallImplementor createImplementor(final Method method) {
  return RexImpTable.createImplementor(
      new ReflectiveCallNotNullImplementor(method) {
        public Expression implement(RexToLixTranslator translator,
            RexCall call, List<Expression> translatedOperands) {
          Expression expr = super.implement(translator, call,
              translatedOperands);
          final Class<?> returnType = method.getReturnType();
          if (QueryableTable.class.isAssignableFrom(returnType)) {
            Expression queryable = Expressions.call(
                Expressions.convert_(expr, QueryableTable.class),
                BuiltInMethod.QUERYABLE_TABLE_AS_QUERYABLE.method,
                Expressions.call(DataContext.ROOT,
                    BuiltInMethod.DATA_CONTEXT_GET_QUERY_PROVIDER.method),
                Expressions.constant(null, SchemaPlus.class),
                Expressions.constant(call.getOperator().getName(), String.class));
            expr = Expressions.call(queryable,
                BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
          } else {
            expr = Expressions.call(expr,
                BuiltInMethod.SCANNABLE_TABLE_SCAN.method, DataContext.ROOT);
          }
          return expr;
        }
      }, NullPolicy.NONE, false);
}
 
Example #8
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression toEnumerable(Expression expression) {
  final Type type = expression.getType();
  if (Types.isArray(type)) {
    if (Types.toClass(type).getComponentType().isPrimitive()) {
      expression =
          Expressions.call(BuiltInMethod.AS_LIST.method, expression);
    }
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, expression);
  } else if (Types.isAssignableFrom(Iterable.class, type)
      && !Types.isAssignableFrom(Enumerable.class, type)) {
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method,
        expression);
  } else if (Types.isAssignableFrom(Queryable.class, type)) {
    // Queryable extends Enumerable, but it's too "clever", so we call
    // Queryable.asEnumerable so that operations such as take(int) will be
    // evaluated directly.
    return Expressions.call(expression,
        BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
  }
  return expression;
}
 
Example #9
Source File: RexExecutorImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Expression field(BlockBuilder list, int index, Type storageType) {
  MethodCallExpression recFromCtx = Expressions.call(
      DataContext.ROOT,
      BuiltInMethod.DATA_CONTEXT_GET.method,
      Expressions.constant("inputRecord"));
  Expression recFromCtxCasted =
      EnumUtils.convert(recFromCtx, Object[].class);
  IndexExpression recordAccess = Expressions.arrayIndex(recFromCtxCasted,
      Expressions.constant(index));
  if (storageType == null) {
    final RelDataType fieldType =
        rowType.getFieldList().get(index).getType();
    storageType = ((JavaTypeFactory) typeFactory).getJavaClass(fieldType);
  }
  return EnumUtils.convert(recordAccess, storageType);
}
 
Example #10
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression toRows(PhysType physType, Expression expression) {
  if (physType.getFormat() == JavaRowFormat.SCALAR
      && Object[].class.isAssignableFrom(elementType)
      && getRowType().getFieldCount() == 1
      && (table.unwrap(ScannableTable.class) != null
          || table.unwrap(FilterableTable.class) != null
          || table.unwrap(ProjectableFilterableTable.class) != null)) {
    return Expressions.call(BuiltInMethod.SLICE0.method, expression);
  }
  JavaRowFormat oldFormat = format();
  if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) {
    return expression;
  }
  final ParameterExpression row_ =
      Expressions.parameter(elementType, "row");
  final int fieldCount = table.getRowType().getFieldCount();
  List<Expression> expressionList = new ArrayList<>(fieldCount);
  for (int i = 0; i < fieldCount; i++) {
    expressionList.add(fieldExpression(row_, i, physType, oldFormat));
  }
  return Expressions.call(expression,
      BuiltInMethod.SELECT.method,
      Expressions.lambda(Function1.class, physType.record(expressionList),
          row_));
}
 
Example #11
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Expression generateSelector(
    ParameterExpression parameter,
    List<Integer> fields,
    JavaRowFormat targetFormat) {
  // Optimize target format
  switch (fields.size()) {
  case 0:
    targetFormat = JavaRowFormat.LIST;
    break;
  case 1:
    targetFormat = JavaRowFormat.SCALAR;
    break;
  }
  final PhysType targetPhysType =
      project(fields, targetFormat);
  switch (format) {
  case SCALAR:
    return Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
  default:
    return Expressions.lambda(Function1.class,
        targetPhysType.record(fieldReferences(parameter, fields)), parameter);
  }
}
 
Example #12
Source File: Schemas.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the expression to access a table within a schema. */
public static Expression tableExpression(SchemaPlus schema, Type elementType,
    String tableName, Class clazz) {
  final MethodCallExpression expression;
  if (Table.class.isAssignableFrom(clazz)) {
    expression = Expressions.call(
        expression(schema),
        BuiltInMethod.SCHEMA_GET_TABLE.method,
        Expressions.constant(tableName));
    if (ScannableTable.class.isAssignableFrom(clazz)) {
      return Expressions.call(
          BuiltInMethod.SCHEMAS_ENUMERABLE_SCANNABLE.method,
          Expressions.convert_(expression, ScannableTable.class),
          DataContext.ROOT);
    }
    if (FilterableTable.class.isAssignableFrom(clazz)) {
      return Expressions.call(
          BuiltInMethod.SCHEMAS_ENUMERABLE_FILTERABLE.method,
          Expressions.convert_(expression, FilterableTable.class),
          DataContext.ROOT);
    }
    if (ProjectableFilterableTable.class.isAssignableFrom(clazz)) {
      return Expressions.call(
          BuiltInMethod.SCHEMAS_ENUMERABLE_PROJECTABLE_FILTERABLE.method,
          Expressions.convert_(expression, ProjectableFilterableTable.class),
          DataContext.ROOT);
    }
  } else {
    expression = Expressions.call(
        BuiltInMethod.SCHEMAS_QUERYABLE.method,
        DataContext.ROOT,
        expression(schema),
        Expressions.constant(elementType),
        Expressions.constant(tableName));
  }
  return EnumUtils.convert(expression, clazz);
}
 
Example #13
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonLengthFailed(JsonFunctions.JsonValueContext input,
    Matcher<? super Throwable> matcher) {
  assertFailed(
      invocationDesc(BuiltInMethod.JSON_LENGTH.getMethodName(), input),
      () -> JsonFunctions.jsonLength(input),
      matcher);
}
 
Example #14
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonValueAnyFailed(JsonFunctions.JsonPathContext input,
    SqlJsonValueEmptyOrErrorBehavior emptyBehavior,
    Object defaultValueOnEmpty,
    SqlJsonValueEmptyOrErrorBehavior errorBehavior,
    Object defaultValueOnError,
    Matcher<? super Throwable> matcher) {
  assertFailed(
      invocationDesc(BuiltInMethod.JSON_VALUE.getMethodName(), input, emptyBehavior,
          defaultValueOnEmpty, errorBehavior, defaultValueOnError),
      () -> JsonFunctions.jsonValue(input, emptyBehavior,
          defaultValueOnEmpty, errorBehavior, defaultValueOnError),
      matcher);
}
 
Example #15
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Expression implement(RexToLixTranslator translator, RexCall call,
    ParameterExpression row, ParameterExpression rows,
    ParameterExpression symbols, ParameterExpression i) {
  return EnumUtils.convert(
      Expressions.call(symbols, BuiltInMethod.LIST_GET.method, i),
      String.class);
}
 
Example #16
Source File: HiveRexExecutorImpl.java    From marble with Apache License 2.0 5 votes vote down vote up
private String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.HIVE;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example #17
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonObjectAggAdd(Map map, String k, Object v,
    SqlJsonConstructorNullClause nullClause,
    Matcher<? super Map> matcher) {
  JsonFunctions.jsonObjectAggAdd(map, k, v, nullClause);
  assertThat(
      invocationDesc(BuiltInMethod.JSON_ARRAYAGG_ADD.getMethodName(), map, k, v, nullClause),
      map, matcher);
}
 
Example #18
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implementNotNullAdd(AggContext info,
    AggAddContext add) {
  Expression acc = add.accumulator().get(0);
  Expression arg = add.arguments().get(0);
  SqlAggFunction aggregation = info.aggregation();

  final BuiltInMethod builtInMethod;
  switch (aggregation.kind) {
  case BIT_AND:
    builtInMethod = BuiltInMethod.BIT_AND;
    break;
  case BIT_OR:
    builtInMethod = BuiltInMethod.BIT_OR;
    break;
  case BIT_XOR:
    builtInMethod = BuiltInMethod.BIT_XOR;
    break;
  default:
    throw new IllegalArgumentException("Unknown " + aggregation.getName()
        + ". Only support bit_and, bit_or and bit_xor for bit aggregation function");
  }
  final Method method = builtInMethod.method;
  Expression next = Expressions.call(
      method.getDeclaringClass(),
      method.getName(),
      acc,
      Expressions.unbox(arg));
  accAdvance(add, acc, next);
}
 
Example #19
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implementNotNullAdd(AggContext info,
    AggAddContext add) {
  final Expression accValue = add.accumulator().get(0);
  final Expression arg0 = add.arguments().get(0);
  final Expression arg1 = add.arguments().size() == 2
      ? add.arguments().get(1) : COMMA_EXPR;
  final Expression result = Expressions.condition(
      Expressions.equal(NULL_EXPR, accValue),
      arg0,
      Expressions.call(BuiltInMethod.STRING_CONCAT.method, accValue,
          Expressions.call(BuiltInMethod.STRING_CONCAT.method, arg1, arg0)));

  add.currentBlock().add(Expressions.statement(Expressions.assign(accValue, result)));
}
 
Example #20
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonType(Matcher<? super String> matcher,
    String input) {
  assertThat(
      invocationDesc(BuiltInMethod.JSON_TYPE.getMethodName(), input),
      JsonFunctions.jsonType(input),
      matcher);
}
 
Example #21
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private MethodImplementor getImplementor(SqlTypeName sqlTypeName) {
  switch (sqlTypeName) {
  case ARRAY:
    return new MethodImplementor(BuiltInMethod.ARRAY_ITEM.method, nullPolicy, false);
  case MAP:
    return new MethodImplementor(BuiltInMethod.MAP_ITEM.method, nullPolicy, false);
  default:
    return new MethodImplementor(BuiltInMethod.ANY_ITEM.method, nullPolicy, false);
  }
}
 
Example #22
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonQueryFailed(JsonFunctions.JsonPathContext input,
    SqlJsonQueryWrapperBehavior wrapperBehavior,
    SqlJsonQueryEmptyOrErrorBehavior emptyBehavior,
    SqlJsonQueryEmptyOrErrorBehavior errorBehavior,
    Matcher<? super Throwable> matcher) {
  assertFailed(
      invocationDesc(BuiltInMethod.JSON_QUERY.getMethodName(), input, wrapperBehavior,
          emptyBehavior, errorBehavior),
      () -> JsonFunctions.jsonQuery(input, wrapperBehavior, emptyBehavior,
          errorBehavior),
      matcher);
}
 
Example #23
Source File: EnumerableNestedLoopJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  final BlockBuilder builder = new BlockBuilder();
  final Result leftResult =
      implementor.visitChild(this, 0, (EnumerableRel) left, pref);
  Expression leftExpression =
      builder.append("left", leftResult.block);
  final Result rightResult =
      implementor.visitChild(this, 1, (EnumerableRel) right, pref);
  Expression rightExpression =
      builder.append("right", rightResult.block);
  final PhysType physType =
      PhysTypeImpl.of(implementor.getTypeFactory(),
          getRowType(),
          pref.preferArray());
  final Expression predicate =
      EnumUtils.generatePredicate(implementor, getCluster().getRexBuilder(), left, right,
          leftResult.physType, rightResult.physType, condition);
  return implementor.result(
      physType,
      builder.append(
          Expressions.call(BuiltInMethod.NESTED_LOOP_JOIN.method,
              leftExpression,
              rightExpression,
              predicate,
              EnumUtils.joinSelector(joinType,
                  physType,
                  ImmutableList.of(leftResult.physType,
                      rightResult.physType)),
              Expressions.constant(EnumUtils.toLinq4jJoinType(joinType))))
          .toBlock());
}
 
Example #24
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override Expression implementSafe(final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList) {
  final SqlOperator op = call.getOperator();
  final Expression root = translator.getRoot();
  if (op == CURRENT_USER
      || op == SESSION_USER
      || op == USER) {
    return Expressions.call(BuiltInMethod.USER.method, root);
  } else if (op == SYSTEM_USER) {
    return Expressions.call(BuiltInMethod.SYSTEM_USER.method, root);
  } else if (op == CURRENT_PATH
      || op == CURRENT_ROLE
      || op == CURRENT_CATALOG) {
    // By default, the CURRENT_ROLE and CURRENT_CATALOG functions return the
    // empty string because a role or a catalog has to be set explicitly.
    return Expressions.constant("");
  } else if (op == CURRENT_TIMESTAMP) {
    return Expressions.call(BuiltInMethod.CURRENT_TIMESTAMP.method, root);
  } else if (op == CURRENT_TIME) {
    return Expressions.call(BuiltInMethod.CURRENT_TIME.method, root);
  } else if (op == CURRENT_DATE) {
    return Expressions.call(BuiltInMethod.CURRENT_DATE.method, root);
  } else if (op == LOCALTIMESTAMP) {
    return Expressions.call(BuiltInMethod.LOCAL_TIMESTAMP.method, root);
  } else if (op == LOCALTIME) {
    return Expressions.call(BuiltInMethod.LOCAL_TIME.method, root);
  } else {
    throw new AssertionError("unknown function " + op);
  }
}
 
Example #25
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implementNotNullAdd(AggContext info,
    AggAddContext add) {
  // acc[0].add(arg);
  add.currentBlock().add(
      Expressions.statement(
          Expressions.call(add.accumulator().get(0),
              BuiltInMethod.COLLECTION_ADD.method,
              add.arguments().get(0))));
}
 
Example #26
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implementNotNullAdd(AggContext info,
    AggAddContext add) {
  Expression acc = add.accumulator().get(0);
  Expression arg = add.arguments().get(0);
  SqlAggFunction aggregation = info.aggregation();
  final Method method = (aggregation == MIN
      ? BuiltInMethod.LESSER
      : BuiltInMethod.GREATER).method;
  Expression next = Expressions.call(
      method.getDeclaringClass(),
      method.getName(),
      acc,
      Expressions.unbox(arg));
  accAdvance(add, acc, next);
}
 
Example #27
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected Expression getExpression(RexToLixTranslator translator,
    Expression operand, BuiltInMethod builtInMethod) {
  final MethodCallExpression locale =
      Expressions.call(BuiltInMethod.LOCALE.method, translator.getRoot());
  return Expressions.call(builtInMethod.method.getDeclaringClass(),
      builtInMethod.method.getName(), operand, locale);
}
 
Example #28
Source File: SqlJsonFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void assertJsonKeys(JsonFunctions.JsonPathContext input,
    Matcher<? super String> matcher) {
  assertThat(
      invocationDesc(BuiltInMethod.JSON_KEYS.getMethodName(), input),
      JsonFunctions.jsonKeys(input),
      matcher);
}
 
Example #29
Source File: EnumerableLimit.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Expression getExpression(RexNode offset) {
  if (offset instanceof RexDynamicParam) {
    final RexDynamicParam param = (RexDynamicParam) offset;
    return Expressions.convert_(
        Expressions.call(DataContext.ROOT,
            BuiltInMethod.DATA_CONTEXT_GET.method,
            Expressions.constant("?" + param.getIndex())),
        Integer.class);
  } else {
    return Expressions.constant(RexLiteral.intValue(offset));
  }
}
 
Example #30
Source File: EnumerableLimit.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  final BlockBuilder builder = new BlockBuilder();
  final EnumerableRel child = (EnumerableRel) getInput();
  final Result result = implementor.visitChild(this, 0, child, pref);
  final PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(),
          getRowType(),
          result.format);

  Expression v = builder.append("child", result.block);
  if (offset != null) {
    v = builder.append(
        "offset",
        Expressions.call(
            v,
            BuiltInMethod.SKIP.method,
            getExpression(offset)));
  }
  if (fetch != null) {
    v = builder.append(
        "fetch",
        Expressions.call(
            v,
            BuiltInMethod.TAKE.method,
            getExpression(fetch)));
  }

  builder.add(
      Expressions.return_(
          null,
          v));
  return implementor.result(physType, builder.toBlock());
}