org.apache.flink.table.runtime.aggregate.RelFieldCollations.scala Maven / Gradle / Ivy
/*
* 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.flink.table.runtime.aggregate
import org.apache.calcite.config.NullCollation
import org.apache.calcite.rel.RelFieldCollation
import org.apache.calcite.rel.RelFieldCollation.{Direction, NullDirection}
import org.apache.flink.table.calcite.FlinkPlannerImpl
/**
* Utility to create [[RelFieldCollation]] instance.
*/
object RelFieldCollations {
/**
* Returns the null direction if not specified.
*
* @param direction Direction that a field is ordered in.
* @return default null direction
*/
def defaultNullDirection(direction: Direction): NullDirection =
FlinkPlannerImpl.defaultNullCollation match {
case NullCollation.FIRST => NullDirection.FIRST
case NullCollation.LAST => NullDirection.LAST
case NullCollation.LOW =>
direction match {
case Direction.ASCENDING | Direction.STRICTLY_ASCENDING => NullDirection.FIRST
case Direction.DESCENDING | Direction.STRICTLY_DESCENDING => NullDirection.LAST
case _ => NullDirection.UNSPECIFIED
}
case NullCollation.HIGH =>
direction match {
case Direction.ASCENDING | Direction.STRICTLY_ASCENDING => NullDirection.LAST
case Direction.DESCENDING | Direction.STRICTLY_DESCENDING => NullDirection.FIRST
case _ => NullDirection.UNSPECIFIED
}
}
/**
* Creates a field collation with default direction.
*
* @param fieldIndex 0-based index of field being sorted
* @return the field collation with default direction and given field index.
*/
def of(fieldIndex: Int): RelFieldCollation =
new RelFieldCollation(
fieldIndex,
FlinkPlannerImpl.defaultCollationDirection,
defaultNullDirection(FlinkPlannerImpl.defaultCollationDirection))
/**
* Creates a field collation.
*
* @param fieldIndex 0-based index of field being sorted
* @param direction Direction of sorting
* @param nullDirection Direction of sorting of nulls
* @return the field collation.
*/
def of(
fieldIndex: Int,
direction: RelFieldCollation.Direction,
nullDirection: RelFieldCollation.NullDirection): RelFieldCollation =
new RelFieldCollation(fieldIndex, direction, nullDirection)
}