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

org.apache.calcite.rex.RexInputRef Maven / Gradle / Ivy

There is a newer version: 1.17.0-flink-r3
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 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.apache.calcite.rex;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.util.Pair;

import java.util.List;

/**
 * Variable which references a field of an input relational expression.
 *
 * 

Fields of the input are 0-based. If there is more than one input, they are * numbered consecutively. For example, if the inputs to a join are

* *
    *
  • Input #0: EMP(EMPNO, ENAME, DEPTNO) and
  • *
  • Input #1: DEPT(DEPTNO AS DEPTNO2, DNAME)
  • *
* *

then the fields are:

* *
    *
  • Field #0: EMPNO
  • *
  • Field #1: ENAME
  • *
  • Field #2: DEPTNO (from EMP)
  • *
  • Field #3: DEPTNO2 (from DEPT)
  • *
  • Field #4: DNAME
  • *
* *

So RexInputRef(3, Integer) is the correct reference for the * field DEPTNO2.

*/ public class RexInputRef extends RexSlot { //~ Static fields/initializers --------------------------------------------- // list of common names, to reduce memory allocations @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") private static final List NAMES = new SelfPopulatingList("$", 30); //~ Constructors ----------------------------------------------------------- /** * Creates an input variable. * * @param index Index of the field in the underlying row-type * @param type Type of the column */ public RexInputRef(int index, RelDataType type) { super(createName(index), index, type); } //~ Methods ---------------------------------------------------------------- @Override public boolean equals(Object obj) { return this == obj || obj instanceof RexInputRef && index == ((RexInputRef) obj).index; } @Override public int hashCode() { return index; } /** * Creates a reference to a given field in a row type. */ public static RexInputRef of(int index, RelDataType rowType) { return of(index, rowType.getFieldList()); } /** * Creates a reference to a given field in a list of fields. */ public static RexInputRef of(int index, List fields) { return new RexInputRef(index, fields.get(index).getType()); } /** * Creates a reference to a given field in a list of fields. */ public static Pair of2( int index, List fields) { final RelDataTypeField field = fields.get(index); return Pair.of( (RexNode) new RexInputRef(index, field.getType()), field.getName()); } @Override public SqlKind getKind() { return SqlKind.INPUT_REF; } public R accept(RexVisitor visitor) { return visitor.visitInputRef(this); } public R accept(RexBiVisitor visitor, P arg) { return visitor.visitInputRef(this, arg); } /** * Creates a name for an input reference, of the form "$index". If the index * is low, uses a cache of common names, to reduce gc. */ public static String createName(int index) { return NAMES.get(index); } } // End RexInputRef.java




© 2015 - 2024 Weber Informatics LLC | Privacy Policy