Package org.apache.tajo.engine.planner.logical

Source Code of org.apache.tajo.engine.planner.logical.SortNode

/**
* 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.tajo.engine.planner.logical;

import com.google.common.base.Preconditions;
import com.google.gson.annotations.Expose;
import org.apache.tajo.catalog.Schema;
import org.apache.tajo.catalog.SortSpec;
import org.apache.tajo.engine.planner.PlanString;
import org.apache.tajo.util.TUtil;

public final class SortNode extends UnaryNode implements Cloneable {
  @Expose private SortSpec [] sortKeys;

  public SortNode(int pid, SortSpec[] sortKeys) {
    super(pid, NodeType.SORT);
    Preconditions.checkArgument(sortKeys.length > 0,
        "At least one sort key must be specified");
    this.sortKeys = sortKeys;
  }

  public SortNode(int pid, SortSpec[] sortKeys, Schema inSchema, Schema outSchema) {
    this(pid, sortKeys);
    this.setInSchema(inSchema);
    this.setOutSchema(outSchema);
  }
 
  public SortSpec[] getSortKeys() {
    return this.sortKeys;
  }
 
  @Override
  public boolean equals(Object obj) {
    if (obj instanceof SortNode) {
      SortNode other = (SortNode) obj;
      boolean eq = super.equals(other);
      eq = eq && TUtil.checkEquals(sortKeys, other.sortKeys);
      eq = eq && child.equals(other.child);
      return eq;
    } else {
      return false;
    }
  }
 
  @Override
  public Object clone() throws CloneNotSupportedException {
    SortNode sort = (SortNode) super.clone();
    sort.sortKeys = sortKeys.clone();
   
    return sort;
  }

  @Override
  public PlanString getPlanString() {
    PlanString planStr = new PlanString("Sort");
    StringBuilder sb = new StringBuilder("Sort Keys: ");
    for (int i = 0; i < sortKeys.length; i++) {
      sb.append(sortKeys[i].getSortKey().getColumnName()).append(" ")
          .append(sortKeys[i].isAscending() ? "asc" : "desc");
      if( i < sortKeys.length - 1) {
        sb.append(",");
      }
    }
    planStr.addExplan(sb.toString());
    return planStr;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder("Sort [key= ");
    for (int i = 0; i < sortKeys.length; i++) {   
      sb.append(sortKeys[i].getSortKey().getQualifiedName()).append(" ")
          .append(sortKeys[i].isAscending() ? "asc" : "desc");
      if(i < sortKeys.length - 1) {
        sb.append(",");
      }
    }
    sb.append("]");

    sb.append("\n\"out schema: " + getOutSchema()
        + "\n\"in schema: " + getInSchema());
    return sb.toString()+"\n"
        + getChild().toString();
  }
}
TOP

Related Classes of org.apache.tajo.engine.planner.logical.SortNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.