2626import org .apache .calcite .plan .RelOptSchema ;
2727import org .apache .calcite .plan .RelOptTable ;
2828import org .apache .calcite .plan .RelOptUtil ;
29+ import org .apache .calcite .plan .RelTraitSet ;
2930import org .apache .calcite .plan .ViewExpanders ;
3031import org .apache .calcite .prepare .RelOptTableImpl ;
3132import org .apache .calcite .rel .RelCollation ;
@@ -3050,15 +3051,25 @@ private static boolean allNull(@Nullable Object[] values, int column, int column
30503051 * create a relation expression that retains the input, just to read its
30513052 * schema.
30523053 */
3053- public RelBuilder empty () {
3054+ public RelBuilder empty (RelTraitSet sortTraits ) {
30543055 final Frame frame = stack .pop ();
3055- final RelNode values =
3056+ RelNode values =
30563057 struct .valuesFactory .createValues (cluster , frame .rel .getRowType (),
30573058 ImmutableList .of ());
3059+ if (null != sortTraits ) {
3060+ if (values .getConvention () != null ) {
3061+ sortTraits = sortTraits .replace (values .getConvention ());
3062+ }
3063+ values = values .copy (sortTraits , values .getInputs ());
3064+ }
30583065 stack .push (new Frame (values , frame .fields ));
30593066 return this ;
30603067 }
30613068
3069+ public RelBuilder empty () {
3070+ return empty (null );
3071+ }
3072+
30623073 /** Creates a {@link Values} with a specified row type.
30633074 *
30643075 * <p>This method can handle cases that {@link #values(String[], Object...)}
@@ -3190,11 +3201,12 @@ public RelBuilder sort(RelCollation collation) {
31903201
31913202 /** Creates a {@link Sort} by a list of expressions, with limit and offset.
31923203 *
3204+ * @param sortTraits traits of Sort
31933205 * @param offset Number of rows to skip; non-positive means don't skip any
31943206 * @param fetch Maximum number of rows to fetch; negative means no limit
31953207 * @param nodes Sort expressions
31963208 */
3197- public RelBuilder sortLimit (int offset , int fetch ,
3209+ public RelBuilder sortLimit (RelTraitSet sortTraits , int offset , int fetch ,
31983210 Iterable <? extends RexNode > nodes ) {
31993211 final Registrar registrar = new Registrar (fields (), ImmutableList .of ());
32003212 final List <RelFieldCollation > fieldCollations =
@@ -3203,7 +3215,7 @@ public RelBuilder sortLimit(int offset, int fetch,
32033215 final RexNode offsetNode = offset <= 0 ? null : literal (offset );
32043216 final RexNode fetchNode = fetch < 0 ? null : literal (fetch );
32053217 if (offsetNode == null && fetch == 0 && config .simplifyLimit ()) {
3206- return empty ();
3218+ return empty (sortTraits );
32073219 }
32083220 if (offsetNode == null && fetchNode == null && fieldCollations .isEmpty ()) {
32093221 return this ; // sort is trivial
@@ -3254,6 +3266,11 @@ public RelBuilder sortLimit(int offset, int fetch,
32543266 return this ;
32553267 }
32563268
3269+ public RelBuilder sortLimit (int offset , int fetch ,
3270+ Iterable <? extends RexNode > nodes ) {
3271+ return sortLimit (null , offset , fetch , nodes );
3272+ }
3273+
32573274 private static RelFieldCollation collation (RexNode node ,
32583275 RelFieldCollation .Direction direction ,
32593276 RelFieldCollation .@ Nullable NullDirection nullDirection ,
0 commit comments