All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.rex.RexCallBinding Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * 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 com.hazelcast.com.liance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.com.hazelcast.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 com.hazelcast.org.apache.calcite.rex;

import com.hazelcast.org.apache.calcite.rel.RelCollation;
import com.hazelcast.org.apache.calcite.rel.RelFieldCollation;
import com.hazelcast.org.apache.calcite.rel.type.RelDataType;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeFactory;
import com.hazelcast.org.apache.calcite.runtime.CalciteException;
import com.hazelcast.org.apache.calcite.runtime.Resources;
import com.hazelcast.org.apache.calcite.sql.SqlOperator;
import com.hazelcast.org.apache.calcite.sql.SqlOperatorBinding;
import com.hazelcast.org.apache.calcite.sql.SqlUtil;
import com.hazelcast.org.apache.calcite.sql.parser.SqlParserPos;
import com.hazelcast.org.apache.calcite.sql.validate.SqlMonotonicity;
import com.hazelcast.org.apache.calcite.sql.validate.SqlValidatorException;

import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableList;

import java.util.List;

/**
 * RexCallBinding implements {@link SqlOperatorBinding} by
 * referring to an underlying collection of {@link RexNode} operands.
 */
public class RexCallBinding extends SqlOperatorBinding {
  //~ Instance fields --------------------------------------------------------

  private final List operands;

  private final List inputCollations;

  //~ Constructors -----------------------------------------------------------

  public RexCallBinding(
      RelDataTypeFactory typeFactory,
      SqlOperator sqlOperator,
      List operands,
      List inputCollations) {
    super(typeFactory, sqlOperator);
    this.operands = ImmutableList.copyOf(operands);
    this.inputCollations = ImmutableList.copyOf(inputCollations);
  }

  /** Creates a binding of the appropriate type. */
  public static RexCallBinding create(RelDataTypeFactory typeFactory,
      RexCall call,
      List inputCollations) {
    switch (call.getKind()) {
    case CAST:
      return new RexCastCallBinding(typeFactory, call.getOperator(),
          call.getOperands(), call.getType(), inputCollations);
    }
    return new RexCallBinding(typeFactory, call.getOperator(),
        call.getOperands(), inputCollations);
  }

  //~ Methods ----------------------------------------------------------------

  @SuppressWarnings("deprecation")
  @Override public String getStringLiteralOperand(int ordinal) {
    return RexLiteral.stringValue(operands.get(ordinal));
  }

  @SuppressWarnings("deprecation")
  @Override public int getIntLiteralOperand(int ordinal) {
    return RexLiteral.intValue(operands.get(ordinal));
  }

  @Override public  T getOperandLiteralValue(int ordinal, Class clazz) {
    final RexNode node = operands.get(ordinal);
    if (node instanceof RexLiteral) {
      return ((RexLiteral) node).getValueAs(clazz);
    }
    return clazz.cast(RexLiteral.value(node));
  }

  @Override public SqlMonotonicity getOperandMonotonicity(int ordinal) {
    RexNode operand = operands.get(ordinal);

    if (operand instanceof RexInputRef) {
      for (RelCollation ic : inputCollations) {
        if (ic.getFieldCollations().isEmpty()) {
          continue;
        }

        for (RelFieldCollation rfc : ic.getFieldCollations()) {
          if (rfc.getFieldIndex() == ((RexInputRef) operand).getIndex()) {
            return rfc.direction.monotonicity();
            // TODO: Is it possible to have more than one RelFieldCollation for a RexInputRef?
          }
        }
      }
    } else if (operand instanceof RexCall) {
      final RexCallBinding binding =
          RexCallBinding.create(typeFactory, (RexCall) operand, inputCollations);
      return ((RexCall) operand).getOperator().getMonotonicity(binding);
    }

    return SqlMonotonicity.NOT_MONOTONIC;
  }

  @Override public boolean isOperandNull(int ordinal, boolean allowCast) {
    return RexUtil.isNullLiteral(operands.get(ordinal), allowCast);
  }

  @Override public boolean isOperandLiteral(int ordinal, boolean allowCast) {
    return RexUtil.isLiteral(operands.get(ordinal), allowCast);
  }

  public List operands() {
    return operands;
  }

  // implement SqlOperatorBinding
  public int getOperandCount() {
    return operands.size();
  }

  // implement SqlOperatorBinding
  public RelDataType getOperandType(int ordinal) {
    return operands.get(ordinal).getType();
  }

  public CalciteException newError(
      Resources.ExInst e) {
    return SqlUtil.newContextException(SqlParserPos.ZERO, e);
  }

  /** To be com.hazelcast.com.atible with {@code SqlCall}, CAST needs to pretend that it
   * has two arguments, the second of which is the target type. */
  private static class RexCastCallBinding extends RexCallBinding {
    private final RelDataType type;

    RexCastCallBinding(
        RelDataTypeFactory typeFactory,
        SqlOperator sqlOperator, List operands,
        RelDataType type,
        List inputCollations) {
      super(typeFactory, sqlOperator, operands, inputCollations);
      this.type = type;
    }

    @Override public RelDataType getOperandType(int ordinal) {
      if (ordinal == 1) {
        return type;
      }
      return super.getOperandType(ordinal);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy