text:
package com.dremio.exec.planner.common;

import static com.dremio.exec.planner.common.ScanRelBase.getRowTypeFromProjectedColumns;
import static java.lang.Math.max;
import static org.apache.calcite.sql.type.SqlTypeName.INTERVAL_TYPES;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.AbstractList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.calcite.linq4j.Ord;
import org.apache.calcite.plan.Convention;
import org.apache.calcite.plan.RelHintsPropagator;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelOptRuleOperand;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.plan.hep.HepRelVertex;
import org.apache.calcite.plan.volcano.RelSubset;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelShuttleImpl;
import org.apache.calcite.rel.RelWriter;
import org.apache.calcite.rel.SingleRel;
import org.apache.calcite.rel.core.Aggregate;
import org.apache.calcite.rel.core.AggregateCall;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Join;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.RelFactories;
import org.apache.calcite.rel.core.Snapshot;
import org.apache.calcite.rel.core.Sort;
import org.apache.calcite.rel.core.TableFunctionScan;
import org.apache.calcite.rel.logical.LogicalAggregate;
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.rel.rules.MultiJoin;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
import org.apache.calcite.rel.type.RelRecordType;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexCorrelVariable;
import org.apache.calcite.rex.RexDynamicParam;
import org.apache.calcite.rex.RexFieldAccess;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexLocalRef;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexOver;
import org.apache.calcite.rex.RexRangeRef;
import org.apache.calcite.rex.RexShuttle;
import org.apache.calcite.rex.RexSubQuery;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.rex.RexVisitorImpl;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SqlValidatorUtil;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RelBuilderFactory;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.tuple.Triple;

import com.carrotsearch.hppc.IntIntHashMap;
import com.dremio.common.exceptions.UserException;
import com.dremio.common.expression.SchemaPath;
import com.dremio.common.types.TypeProtos;
import com.dremio.common.types.Types;
import com.dremio.exec.planner.RoutingShuttle;
import com.dremio.exec.planner.StatelessRelShuttleImpl;
import com.dremio.exec.planner.physical.PrelUtil;
import com.dremio.exec.record.BatchSchema;
import com.dremio.exec.resolver.TypeCastRules;
import com.dremio.exec.store.NamespaceTable;
import com.dremio.service.Pointer;
import com.dremio.service.namespace.capabilities.SourceCapabilities;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;

import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils;

/**
 * Utility class that is a subset of the RelOptUtil class and is a placeholder for Dremio specific
 * static methods that are needed during either logical or physical planning.
 */
public final class MoreRelOptUtil {
  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MoreRelOptUtil.class);

  private MoreRelOptUtil() {}

  /**
   * Computes the height of the rel tree under the input rel node.
   * @param rel RelNode to compute the minimum height of the tree underneath it
   * @return minimum height of the tree under the input rel node
   */
  public static int getDepth(RelNode rel) {
    if (rel == null) {
      return 0;
    }
    if (rel instanceof RelSubset) {
      RelSubset subset = (RelSubset) rel;
      return getDepth(subset.getBest());
    }

    if (rel.getInputs() == null || rel.getInputs().size() == 0) {
      return 1;
    }

    int minDepth = Integer.MAX_VALUE;
    for (RelNode node : rel.getInputs()) {
      int nodeDepth = getDepth(node);
      if (nodeDepth > 0) {
        minDepth = Math.min(nodeDepth, minDepth);
      }
    }

    if (minDepth == Integer.MAX_VALUE) {
      return 0;
    }

    return minDepth + 1;
  }

  public static int countRelNodes(RelNode rel) {
    int count = 1; // Current node
    for (RelNode child : rel.getInputs()) { // Add children
      count += countRelNodes(child);
    }
    return count;
  }

  public static boolean areRowTypesCompatibleForInsert(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean allowSubstring) {
    return checkRowTypesCompatibility(rowType1, rowType2, compareNames, allowSubstring, true);
  }

  public static boolean areRowTypesCompatible(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean allowSubstring) {
    return checkRowTypesCompatibility(rowType1, rowType2, compareNames, allowSubstring, false);
  }

  public static boolean checkFieldTypesCompatibility(
    RelDataType fieldType1,
    RelDataType fieldType2,
    boolean allowSubstring,
    boolean insertOp) {
    // If one of the types is ANY comparison should succeed
    if (fieldType1.getSqlTypeName() == SqlTypeName.ANY
      || fieldType2.getSqlTypeName() == SqlTypeName.ANY) {
      return true;
    }

    if ((fieldType1.toString().equals(fieldType2.toString()))) {
      return true;
    }

    if (allowSubstring
      && (fieldType1.getSqlTypeName() == SqlTypeName.CHAR && fieldType2.getSqlTypeName() == SqlTypeName.CHAR)
      && (fieldType1.getPrecision() <= fieldType2.getPrecision())) {
      return true;
    }

    // Check if Dremio implicit casting can resolve the incompatibility
    List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
    TypeProtos.MinorType minorType1 = Types.getMinorTypeFromName(fieldType1.getSqlTypeName().getName());
    TypeProtos.MinorType minorType2 = Types.getMinorTypeFromName(fieldType2.getSqlTypeName().getName());
    types.add(minorType1);
    types.add(minorType2);
    if (insertOp) {
      // Insert is more strict than normal select in terms of implicit casts
      // Return false if TypeCastRules do not allow implicit cast
      if (TypeCastRules.isCastable(minorType1, minorType2, true) &&
        TypeCastRules.getLeastRestrictiveTypeForInsert(types) != null) {
        if (TypeCastRules.isCastSafeFromDataTruncation(fieldType1, fieldType2)) {
          return true;
        }
      }
    } else {
      if (TypeCastRules.getLeastRestrictiveType(types) != null) {
        return true;
      }
    }

    return false;
  }

  // Similar to RelOptUtil.areRowTypesEqual() with the additional check for allowSubstring
  private static boolean checkRowTypesCompatibility(
      RelDataType rowType1,
      RelDataType rowType2,
      boolean compareNames,
      boolean allowSubstring,
      boolean insertOp) {
    if (rowType1 == rowType2) {
      return true;
    }
    if (compareNames) {
      // if types are not identity-equal, then either the names or
      // the types must be different
      return false;
    }
    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
      return false;
    }
    final List<RelDataTypeField> f1 = rowType1.getFieldList();
    final List<RelDataTypeField> f2 = rowType2.getFieldList();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
      final RelDataType type1 = pair.left.getType();
      final RelDataType type2 = pair.right.getType();
      if (!checkFieldTypesCompatibility(type1, type2, allowSubstring, insertOp)) {
        return false;
      }
    }
    return true;
  }

  /**
   * Verifies that two row type names match.
   * Does not compare nullability.
   * Differs from RelOptUtil implementation by not defining types as equal if one is of type ANY.
   *
   * @param rowType1           row type for comparison
   * @param rowType2           row type for comparison
   * @param compareNames       boolean for name match
   * @param compareNullability boolean for nullability match
   *
   * @return boolean indicating that row types are equivalent
   */
  public static boolean areRowTypesEqual(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean compareNullability) {
    if (rowType1 == rowType2) {
      return true;
    }

    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
      return false;
    }

    final List<RelDataTypeField> f1 = rowType1.getFieldList();
    final List<RelDataTypeField> f2 = rowType2.getFieldList();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
      final RelDataType type1 = pair.left.getType();
      final RelDataType type2 = pair.right.getType();

      if (!areDataTypesEqual(type1, type2, !compareNullability)) {
        return false;
      }

      if (compareNames) {
        if (!pair.left.getName().equalsIgnoreCase(pair.right.getName())) {
          return false;
        }
      }
    }
    return true;
  }

  /**
   * Verifies that two data types match without requiring any cast
   *
   * @param dataType1 data type for comparison
   * @param dataType2 data type for comparison
   * @return boolean indicating that data types are equivalent
   */
  public static boolean areDataTypesEqualWithoutRequiringCast(
    RelDataType dataType1,
    RelDataType dataType2) {
    return
      // The types are equal
      areDataTypesEqual(dataType1, dataType2, true) ||
      // They fall in the same type bucket without requiring a cast
      (getTypeBucket(dataType1.getSqlTypeName()) == getTypeBucket(dataType2.getSqlTypeName()));
  }

  public static int getTypeBucket(SqlTypeName typeName) {
    // Groups of types which don't require a cast if compared.
    switch (typeName) {
      case BOOLEAN:
        return 1;
      case TINYINT:
      case SMALLINT:
      case INTEGER:
      case BIGINT:
        return 2;
      case DECIMAL:
        return 3;
      case FLOAT:
        return 4;
      case REAL:
        return 5;
      case DOUBLE:
        return 6;
      case DATE:
        return 7;
      case TIME:
      case TIME_WITH_LOCAL_TIME_ZONE:
        return 8;
      case TIMESTAMP:
      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        return 9;
      case INTERVAL_YEAR:
      case INTERVAL_YEAR_MONTH:
      case INTERVAL_MONTH:
      case INTERVAL_DAY:
      case INTERVAL_DAY_HOUR:
      case INTERVAL_DAY_MINUTE:
      case INTERVAL_DAY_SECOND:
      case INTERVAL_HOUR:
      case INTERVAL_HOUR_MINUTE:
      case INTERVAL_HOUR_SECOND:
      case INTERVAL_MINUTE:
      case INTERVAL_MINUTE_SECOND:
      case INTERVAL_SECOND:
        return 10;
      case CHAR:
      case VARCHAR:
        return 11;
      case BINARY:
      case VARBINARY:
        return 12;
      case NULL:
        return 13;
      case ANY:
        return 14;
      default:
        return -1;
    }
  }

  /**
   * Verifies that two data types match.
   * @param dataType1          data type for comparison
   * @param dataType2          data type for comparison
   * @param sqlTypeNameOnly    boolean for only SqlTypeName match
   *
   * @return boolean indicating that data types are equivalent
   */
  public static boolean areDataTypesEqual(
    RelDataType dataType1,
    RelDataType dataType2,
    boolean sqlTypeNameOnly) {
    if (sqlTypeNameOnly) {
      return dataType1.getSqlTypeName().equals(dataType2.getSqlTypeName());
    }
    return dataType1.equals(dataType2);
  }

  /**
   * Creates a projection which casts a rel's output to a desired row type.
   * Differs from RelOptUtil implementation by casting even when type is ANY.
   *
   * @param rel         producer of rows to be converted
   * @param castRowType row type after cast
   *
   * @return conversion rel
   */
  public static RelNode createCastRel(
    final RelNode rel,
    RelDataType castRowType) {
    return createCastRel(
      rel, castRowType, RelFactories.DEFAULT_PROJECT_FACTORY);
  }

  /**
   * Creates a projection which casts a rel's output to a desired row type.
   * Differs from RelOptUtil implementation by casting even when type is ANY.
   *
   * @param rel         producer of rows to be converted
   * @param castRowType row type after cast
   * @param projectFactory Project Factory
   *
   * @return conversion rel
   */
  public static RelNode createCastRel(
    final RelNode rel,
    RelDataType castRowType,
    RelFactories.ProjectFactory projectFactory) {
    assert projectFactory != null;
    RelDataType rowType = rel.getRowType();
    if (areRowTypesEqual(rowType, castRowType, false, true)) {
      // nothing to do
      return rel;
    }
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
    final List<RexNode> castExps =
      RexUtil.generateCastExpressions(rexBuilder, castRowType, rowType);

    final List<RexNode> castExpsWithNullReplacement = new ArrayList<>();
    for (RexNode expr : castExps) {
      if (expr.getKind() == SqlKind.CAST && expr.getType().getSqlTypeName() == SqlTypeName.NULL) {
        // Cast to NULL type is not supported. Insert NULL type literal instead
        castExpsWithNullReplacement.add(rexBuilder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.NULL)));
      } else {
        castExpsWithNullReplacement.add(expr);
      }
    }

    return projectFactory.createProject(rel, ImmutableList.of(), castExpsWithNullReplacement, rowType.getFieldNames());
  }

  /**
   * Returns a relational expression which has the same fields as the
   * underlying expression, but the fields have different names.
   *
   *
   * @param rel        Relational expression
   * @param fieldNames Field names
   * @return Renamed relational expression
   */
  public static RelNode createRename(
      RelNode rel,
      final List<String> fieldNames) {
    final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
    assert fieldNames.size() == fields.size();
    final List<RexNode> refs =
        new AbstractList<RexNode>() {
          @Override
          public int size() {
            return fields.size();
          }

          @Override
          public RexNode get(int index) {
            return RexInputRef.of(index, fields);
          }
        };

    return RelOptUtil.createProject(rel, refs, fieldNames, false);
  }

  public static boolean isSimpleColumnSelection(Project project) {
    Set<Integer> inputRefReferenced = new HashSet<>();
    for (Pair<RexNode, String> proj : project.getNamedProjects()) {
      if (proj.getKey().getKind() != SqlKind.INPUT_REF) {
        return false;
      }
      RexInputRef inputRef = (RexInputRef) proj.getKey();
      // If the input reference is again referenced, then it is not a simple column selection (since it is not a permutation).
      if (inputRefReferenced.contains(inputRef.getIndex())) {
        return false;
      }
      final String nameOfProjectField = proj.getValue();
      final String nameOfInput = project.getInput().getRowType().getFieldNames().get(inputRef.getIndex());
      // Renaming a column is not a simple column selection
      if (nameOfProjectField == null || !nameOfProjectField.equals(nameOfInput)) {
        return false;
      }
      inputRefReferenced.add(inputRef.getIndex());
    }
    return true;
  }

  public static boolean isTrivialProject(Project project) {
    return containIdentity(project.getProjects(),
      project.getRowType(),
      project.getInput().getRowType(),
      String::compareTo);
  }

  public static boolean isTrivialProjectIgnoreNameCasing(Project project) {
    return containIdentity(project.getProjects(),
        project.getRowType(),
        project.getInput().getRowType(),
        String::compareToIgnoreCase);
  }

  /** Returns a rowType having all unique field name.
   *
   * @param rowType : input rowType
   * @param typeFactory : type factory used to create a new row type.
   * @return
   */
  public static RelDataType uniqifyFieldName(final RelDataType rowType, final RelDataTypeFactory typeFactory) {
    return typeFactory.createStructType(RelOptUtil.getFieldTypeList(rowType),
        SqlValidatorUtil.uniquify(rowType.getFieldNames()));
  }

  /**
   * Returns whether the leading edge of a given array of expressions is
   * wholly {@link RexInputRef} objects with types and names corresponding
   * to the underlying row type. */
  public static boolean containIdentity(List<? extends RexNode> exps,
      RelDataType rowType,
      RelDataType childRowType,
      Comparator<String> nameComparator) {
    List<RelDataTypeField> fields = rowType.getFieldList();
    List<RelDataTypeField> childFields = childRowType.getFieldList();
    int fieldCount = childFields.size();
    if (exps.size() != fieldCount) {
      return false;
    }
    for (int i = 0; i < exps.size(); i++) {
      RexNode exp = exps.get(i);
      if (!(exp instanceof RexInputRef)) {
        return false;
      }
      RexInputRef var = (RexInputRef) exp;
      if (var.getIndex() != i) {
        return false;
      }
      if (0 != nameComparator.compare(fields.get(i).getName(), childFields.get(i).getName())) {
        return false;
      }
      if (!fields.get(i).getType().equals(childFields.get(i).getType())) {
        return false;
      }
    }
    return true;
  }

  public static Pair<Integer, RelDataTypeField> findFieldWithIndex(List<RelDataTypeField> fields, String fieldName) {
    for (int i = 0; i < fields.size(); i++) {
      if (fields.get(i).getName().equals(fieldName)) {
        return Pair.of(i, fields.get(i));
      }
    }
    return null;
  }

  public static class VertexRemover extends StatelessRelShuttleImpl {

    @Override
    public RelNode visit(RelNode other) {
      if (other instanceof HepRelVertex) {
        return super.visit(((HepRelVertex) other).getCurrentRel());
      } else {
        return super.visit(other);
      }
    }
  }

  public static class SubsetRemover extends StatelessRelShuttleImpl {

    private final boolean needBest;

    public SubsetRemover() {
      this(true);
    }

    public SubsetRemover(boolean needBest) {
      this.needBest = needBest;
    }

    @Override
    public RelNode visit(RelNode other) {
      if (other instanceof RelSubset) {
        if (((RelSubset) other).getBest() != null) {
          return ((RelSubset) other).getBest().accept(this);
        }
        if (!needBest && ((RelSubset) other).getRelList().size() == 1) {
          return ((RelSubset) other).getRelList().get(0).accept(this);
        }
        throw UserException.unsupportedError().message("SubsetRemover: found null best, parent " + other).build(logger);
      } else {
        return super.visit(other);
      }
    }
  }

  /**
   * Helper class to rewrite the Calcite plan to remove Sort nodes
   * that would be in sub-queries and do not have a fetch or offset.
   *
   * A sort has no effect inside a sub-query on its own.
   */
  public static class OrderByInSubQueryRemover extends StatelessRelShuttleImpl {

    /**
     * The sort at the root of the plan (or underneath a project
     * at the root of the plan). An order by without fetch or limit
     * is valid here, so never remove this node, if it exists.
     */
    private final Sort topLevelSort;

    /**
     * Constructor.
     *
     * @param root The root of the plan.
     */
    public OrderByInSubQueryRemover(RelNode root)
    {
      // Identify if there is either a sort at the plan root, or
      // a Project with a sort at the plan root. These Sorts should not
      // be skipped since ORDER BY is always legal there.
      if (root instanceof Sort) {
        this.topLevelSort = (Sort) root;
      } else if (root instanceof Project &&
        root.getInput(0) instanceof Sort) {
        this.topLevelSort = (Sort) root.getInput(0);
      } else {
        this.topLevelSort = null;
      }
    }

    @Override
    protected RelNode visitChild(RelNode parent, int i, RelNode child) {

      // Ignore the root node.
      if (null == parent) {
        return super.visitChild(parent, i, child);
      }

      // Ignore non-sort child nodes.
      if (!(child instanceof Sort)) {
        return super.visitChild(parent, i, child);
      }

      // Ignore the sort for the top level SELECT. It's valid to use ORDER BY
      // without FETCH / OFFSET here.
      if (child == topLevelSort) {
        return super.visitChild(parent, i, child);
      }

      // If the child Sort has FETCH and LIMIT clauses, do not touch them.
      Sort childAsSort = (Sort) child;
      if (childAsSort.offset == null &&
          childAsSort.fetch == null) {
        parent.replaceInput(i, childAsSort.getInput());
        return super.visitChild(parent, i, childAsSort.getInput());
      }

      return super.visitChild(parent, i, child);
    }
  }

  public static class FlattenRexVisitor extends RexVisitorImpl<Boolean> {

    public FlattenRexVisitor() {
      super(true);
    }

    @Override
    public Boolean visitInputRef(RexInputRef inputRef) {
      return false;
    }

    @Override
    public Boolean visitLocalRef(RexLocalRef localRef) {
      return false;
    }

    @Override
    public Boolean visitLiteral(RexLiteral literal) {
      return false;
    }

    @Override
    public Boolean visitOver(RexOver over) {
      return false;
    }

    @Override
    public Boolean visitCorrelVariable(RexCorrelVariable correlVariable) {
      return false;
    }

    @Override
    public Boolean visitCall(RexCall call) {
      if (call.getOperator().getName().equalsIgnoreCase("flatten")) {
        return true;
      }

      for (RexNode op : call.getOperands()) {
        Boolean opResult = op.accept(this);
        if (opResult != null && opResult.booleanValue()) {
          return true;
        }
      }
      return false;
    }

    @Override
    public Boolean visitDynamicParam(RexDynamicParam dynamicParam) {
      return false;
    }

    @Override
    public Boolean visitRangeRef(RexRangeRef rangeRef) {
      return false;
    }

    @Override
    public Boolean visitFieldAccess(RexFieldAccess fieldAccess) {
      return fieldAccess.getReferenceExpr().accept(this);
    }

    @Override
    public Boolean visitSubQuery(RexSubQuery subQuery) {
      return false;
    }
  }

  public static final class ContainsRexVisitor extends RexVisitorImpl<Boolean> {

    private final boolean checkOrigin;
    private final RelMetadataQuery mq;
    private final RelNode node;
    private final int index;

    public static boolean hasContains(RexNode rexNode) {
      return rexNode.accept(new ContainsRexVisitor(false, null, null, -1));
    }

    public static boolean hasContainsCheckOrigin(RelNode node, RexNode rex, int index) {
      return rex.accept(new ContainsRexVisitor(true, node.getCluster().getMetadataQuery(), node, index));
    }

    private ContainsRexVisitor(boolean checkOrigin, RelMetadataQuery mq, RelNode node, int index) {
      super(true);
      this.checkOrigin = checkOrigin;
      this.mq = mq;
      this.node = node;
      this.index = index;
      Preconditions.checkArgument(!checkOrigin || (mq != null && node != null));
    }

    @Override
    public Boolean visitInputRef(RexInputRef inputRef) {
      return false;
    }

    @Override
    public Boolean visitLocalRef(RexLocalRef localRef) {
      return false;
    }

    @Override
    public Boolean visitLiteral(RexLiteral literal) {
      return false;
    }

    @Override
    public Boolean visitOver(RexOver over) {
      return false;
    }

    @Override
    public Boolean visitCorrelVariable(RexCorrelVariable correlVariable) {
      return false;
    }

    @Override
    public Boolean visitCall(RexCall call) {
      if (call.getOperator().getName().equalsIgnoreCase("contains")) {
        if (!checkOrigin) {
          return true;
        }
        // Check origin
        final Set<RelColumnOrigin> origins;
        if (index >= 0) {
          origins = mq.getColumnOrigins(node, index);
        } else {
          List<RelDataTypeField> fields = ImmutableList.<RelDataTypeField>of(new RelDataTypeFieldImpl("ContainsTemp", 0, call.getType()));
          Project temporary =
              new LogicalProject(node.getCluster(), node.getTraitSet().plus(Convention.NONE), node.getInput(0), ImmutableList.of(call), new RelRecordType(fields));
          origins = mq.getColumnOrigins(temporary, 0);
        }

        boolean supportContains = true;
        if (origins == null) {
          supportContains = false;
        } else {
          for (RelColumnOrigin column : origins) {
            if (column.getOriginTable() == null) {
              supportContains = false;
            } else {
              NamespaceTable namespaceTable2 = column.getOriginTable().unwrap(NamespaceTable.class);
              if (namespaceTable2 != null) {
                if(!namespaceTable2.getStoragePluginId().getCapabilities().getCapability(SourceCapabilities.SUPPORTS_CONTAINS)){
                  supportContains = false;
                }
              } else {
                supportContains = false;
              }
            }
          }
        }
        if (!supportContains) {
          throw UserException.unsupportedError().message("Contains operator is not supported for the table, %s", call).build(logger);
        }
        return true;
      }

      for (RexNode op : call.getOperands()) {
        Boolean opResult = op.accept(this);
        if (opResult != null && opResult.booleanValue()) {
          return true;
        }
      }
      return false;
    }

    @Override
    public Boolean visitDynamicParam(RexDynamicParam dynamicParam) {
      return false;
    }

    @Override
    public Boolean visitRangeRef(RexRangeRef rangeRef) {
      return false;
    }

    @Override
    public Boolean visitFieldAccess(RexFieldAccess fieldAccess) {
      return fieldAccess.getReferenceExpr().accept(this);
    }

    @Override
    public Boolean visitSubQuery(RexSubQuery subQuery) {
      return false;
    }
  }

  public static class NodeRemover extends  RelShuttleImpl{

    private final Predicate<RelNode> predicate;

    public NodeRemover(Predicate<RelNode> predicate) {
      super();
      this.predicate = predicate;
    }

    @Override
    protected RelNode visitChild(RelNode parent, int i, RelNode child) {
      if(!predicate.test(parent)){
        return child.accept(this);
      }

      return super.visitChild(parent, i, child);
    }
  }

  /**
   * Find the path to the node that matches the predicate
   * @param root
   * @param predicate
   * @return
   */
  public static List<Integer> findPathToNode(final RelNode root, final Predicate<RelNode> predicate) {
    final Deque<Integer> stack = new ArrayDeque<>();
    final List<Integer> result = new ArrayList<>();
    final Pointer<Boolean> found = new Pointer<>(false);
    root.accept(new RoutingShuttle() {
      @Override
      public RelNode visitChildren(RelNode rel) {
        for (Ord<RelNode> input : Ord.zip(rel.getInputs())) {
          stack.addLast(input.i);
          rel = visitChild(rel, input.i, input.e);
          stack.removeLast();
          if (found.value) {
            return rel;
          }
        }
        return rel;
      }
      @Override
      public RelNode visit(RelNode other) {
        if (found.value) {
          return other;
        }
        if (predicate.test(other)) {
          result.addAll(stack);
          found.value = true;
          return other;
        }
        return super.visit(other);
      }
    });

    Preconditions.checkState(found.value);

    return result;
  }

  /**
   * Find the node at a given path.  The output from {@link this.findPathToNode} when used as input will return the
   * found node
   * @param root
   * @param path
   * @return
   */
  public static RelNode findNodeAtPath(final RelNode root, final Iterable<Integer> path) {
    final Iterator<Integer> iter = path.iterator();

    RelNode node = root.accept(new RoutingShuttle() {
      @Override
      public RelNode visit(RelNode other) {
        return super.visit(other);
      }

      @Override
      protected RelNode visitChildren(RelNode rel) {
        if (!iter.hasNext()) {
          return rel;
        }

        int child = iter.next();

        return rel.getInput(child).accept(this);
      }
    });

    Preconditions.checkNotNull(node);
    return node;
  }

  /**
   * Removes constant group keys in aggregate and adds a project on top.
   * Returns the original rel if there is a failure handling the rel.
   */
  public static RelNode removeConstantGroupKeys(RelNode rel, RelBuilderFactory factory) {
    return rel.accept(new StatelessRelShuttleImpl() {
      @Override
      public RelNode visit(RelNode other) {
        if (other instanceof Aggregate) {
          Aggregate agg = (Aggregate) super.visitChildren(other);
          RelNode newRel = handleAggregate(agg, factory.create(agg.getCluster(), null));
          if (newRel == null) {
            return agg;
          }
          return newRel;
        }
        return super.visit(other);
      }

      @Override
      public RelNode visit(LogicalAggregate aggregate) {
        Aggregate agg = (Aggregate) super.visitChildren(aggregate);
        RelNode newRel = handleAggregate(agg, factory.create(agg.getCluster(), null));
        if (newRel == null) {
          return agg;
        }
        return newRel;
      }
    });
  }

  private static RelNode handleAggregate(Aggregate agg, RelBuilder relBuilder) {
    final Set<Integer> newGroupKeySet = new HashSet<>();
    relBuilder.push(agg.getInput());

    // 1. Create an array with the number of group by keys in Aggregate
    // 2. Partially fill out the array with constant fields at the original position in the rowType
    // 3. Add a project below aggregate by reordering the fields in a way that non-constant fields come first
    // 3. Build an aggregate for the remaining fields
    // 4. Fill out the rest of the array by getting fields from constructed aggregate
    // 5. Create a Project with removed constants along with original fields from input
    int numGroupKeys = agg.getGroupSet().cardinality();
    if (numGroupKeys == 0) {
      return null;
    }
    final RexNode[] groupProjects = new RexNode[numGroupKeys];
    final Set<Integer> constantInd = new LinkedHashSet<>();
    final Set<Integer> groupInd = new LinkedHashSet<>();
    final Map<Integer, Integer> mapping = new HashMap<>();
    for (int i = 0 ; i < agg.getGroupSet().cardinality() ; i++) {
      RexLiteral literal = projectedLiteral(agg.getInput(), i);
      if(literal != null) {
        groupProjects[i] = literal;
        constantInd.add(i);
      } else {
        groupProjects[i] = null;
        newGroupKeySet.add(groupInd.size());
        groupInd.add(i);
      }
    }

    final List<RexNode> projectBelowAggregate = new ArrayList<>();
    if (constantInd.size() <= 1 || constantInd.size() == numGroupKeys) {
      return null;
    }

    for(int ind : groupInd) {
      mapping.put(ind, projectBelowAggregate.size());
      projectBelowAggregate.add(relBuilder.field(ind));
    }

    for(int ind : constantInd) {
      mapping.put(ind, projectBelowAggregate.size());
      projectBelowAggregate.add(relBuilder.field(ind));
    }

    for(int i = 0 ; i < relBuilder.fields().size() ; i++) {
      if(!constantInd.contains(i) && !groupInd.contains(i)) {
        mapping.put(i, projectBelowAggregate.size());
        projectBelowAggregate.add(relBuilder.field(i));
      }
    }

    final List<AggregateCall> aggregateCalls = new ArrayList<>();
    for (final AggregateCall aggregateCall : agg.getAggCallList()) {
      List<Integer> newArgList = new ArrayList<>();
      for (final int argIndex : aggregateCall.getArgList()) {
        newArgList.add(mapping.get(argIndex));
      }
      aggregateCalls.add(aggregateCall.copy(newArgList, aggregateCall.filterArg));
    }

    ImmutableBitSet newGroupSet = ImmutableBitSet.of(newGroupKeySet);
    relBuilder.project(projectBelowAggregate).aggregate(relBuilder.groupKey(newGroupSet.toArray()), aggregateCalls);

    int count = 0;
    for(int i = 0; i < groupProjects.length; i++) {
      if (groupProjects[i] == null) {
        groupProjects[i] = relBuilder.field(count);
        count++;
      }
    }

    List<RexNode> projects = new ArrayList<>(Arrays.asList(groupProjects));
    for(int i = 0 ; i < agg.getAggCallList().size() ; i++) {
      projects.add(relBuilder.field(i+newGroupKeySet.size()));
    }
    RelNode newRel = relBuilder.project(projects, agg.getRowType().getFieldNames()).build();
    if (!RelOptUtil.areRowTypesEqual(newRel.getRowType(), agg.getRowType(), true)) {
      return null;
    }
    return newRel;
  }
  private static RexLiteral projectedLiteral(RelNode rel, int i) {
    if (rel instanceof Project) {
      Project project = (Project)rel;
      RexNode node = (RexNode)project.getProjects().get(i);
      if (node instanceof RexLiteral) {
        return (RexLiteral)node;
      }
    }

    return null;
  }

  public static List<RexNode> identityProjects(RelDataType type) {
    List<RexNode> projects = new ArrayList<>();
    List<RelDataTypeField> fieldList = type.getFieldList();
    for (int i = 0; i < type.getFieldCount(); i++) {
      RelDataTypeField field = fieldList.get(i);
      projects.add(new RexInputRef(i, field.getType()));
    }
    return projects;
  }

  public static List<RexNode> identityProjects(RelDataType type, ImmutableBitSet selectedColumns) {
    List<RexNode> projects = new ArrayList<>();
    List<RelDataTypeField> fieldList = type.getFieldList();
    for (int i = 0; i < type.getFieldCount(); i++) {
      if(selectedColumns.get(i)) {
        RelDataTypeField field = fieldList.get(i);
        projects.add(new RexInputRef(i, field.getType()));
      }
    }
    return projects;
  }

  public static boolean isNegative(RexLiteral literal) {
    Double d = literal.getValueAs(Double.class);
    return d < 0;
  }

  public static SqlOperator op(SqlKind kind) {
    switch (kind) {
    case IS_FALSE:
      return SqlStdOperatorTable.IS_FALSE;
    case IS_TRUE:
      return SqlStdOperatorTable.IS_TRUE;
    case IS_UNKNOWN:
      return SqlStdOperatorTable.IS_UNKNOWN;
    case IS_NULL:
      return SqlStdOperatorTable.IS_NULL;
    case IS_NOT_FALSE:
      return SqlStdOperatorTable.IS_NOT_FALSE;
    case IS_NOT_TRUE:
      return SqlStdOperatorTable.IS_NOT_TRUE;
    case IS_NOT_NULL:
      return SqlStdOperatorTable.IS_NOT_NULL;
    case EQUALS:
      return SqlStdOperatorTable.EQUALS;
    case NOT_EQUALS:
      return SqlStdOperatorTable.NOT_EQUALS;
    case LESS_THAN:
      return SqlStdOperatorTable.LESS_THAN;
    case GREATER_THAN:
      return SqlStdOperatorTable.GREATER_THAN;
    case LESS_THAN_OR_EQUAL:
      return SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
    case GREATER_THAN_OR_EQUAL:
      return SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
    default:
      throw new AssertionError(kind);
    }
  }

  /**
   * RexVisitor which returns an ImmutableBitSet of the columns referenced by a list of RexNodes
   */
  public static class InputRefFinder extends RexVisitorImpl<Void> {
    private final ImmutableBitSet.Builder setBuilder = ImmutableBitSet.builder();

    public InputRefFinder() {
      super(true);
    }

    @Override
    public Void visitInputRef(RexInputRef inputRef) {
      setBuilder.set(inputRef.getIndex());
      return null;
    }

    public ImmutableBitSet getInputRefs(List<RexNode> rexNodes) {
      for (RexNode rexNode : rexNodes) {
        rexNode.accept(this);
      }
      return setBuilder.build();
    }
  }

  /**
   * RexShuttle which canonicalizes numeric literals in the RexNode to its expanded form.
   */
  public static class RexLiteralCanonicalizer extends RexShuttle {
    private final RexBuilder rexBuilder;

    public RexLiteralCanonicalizer(RexBuilder rexBuilder) {
      this.rexBuilder = rexBuilder;
    }

    @Override
    public RexNode visitLiteral(RexLiteral literal) {
      RexNode newLiteral = literal(literal.getValue(), literal.getType());
      if (newLiteral != null) {
        return newLiteral;
      }
      // Should we try with value2/value3?
      return literal;
    }

    /**
     * Creates a literal (constant expression).
     */
    private RexNode literal(Object value, RelDataType type) {
      if (value instanceof BigDecimal) {
        return rexBuilder.makeExactLiteral(stripTrailingZeros((BigDecimal) value), type);
      } else if (value instanceof Float || value instanceof Double) {
        return rexBuilder.makeApproxLiteral(
          stripTrailingZeros(BigDecimal.valueOf(((Number) value).doubleValue())), type);
      } else if (value instanceof Number) {
        return rexBuilder.makeExactLiteral(
          stripTrailingZeros(BigDecimal.valueOf(((Number) value).longValue())), type);
      } else {
        return null;
      }
    }

    private BigDecimal stripTrailingZeros(BigDecimal value) {
      if (value.scale() <= 0) {
        // Its already an integer value. No need to strip any zeroes.
        return value;
      }
      try {
        BigDecimal newValue = value.stripTrailingZeros();
        int scale = newValue.scale();
        if (scale >= 0 && scale <= rexBuilder.getTypeFactory().getTypeSystem().getMaxNumericScale()) {
          return newValue;
        } else if (scale < 0) {
          // In case we have something like 600.00, where the scale is greater than 0,
          // stripping trailing zeros will end up in scientific notation 6E+2. Set
          // the scale to 0 to get the expanded notation.
          return newValue.setScale(0, BigDecimal.ROUND_UNNECESSARY);
        }
      } catch (Exception ex) {
        // In case any exception happens, log and continue without stripping.
        logger.info(String.format("Caught exception while stripping trailing zeroes from %s", value.toPlainString()), ex);
      }
      return value;
    }
  }

  /**
   * Rewrites structured condition
   * For example,
   * ROW($1,$2) <> ROW(1,2)
   * =>
   * $1 <> 1 and $2 <> 2
   */
  public static final class StructuredConditionRewriter extends StatelessRelShuttleImpl {
    private StructuredConditionRewriter() {
    }

    @Override
    public RelNode visit(LogicalProject project) {
      RelNode newInput = project.getInput().accept(this);
      final ConditionFlattenter flattener = new ConditionFlattenter(project.getCluster().getRexBuilder());
      List<RexNode> newExprs = project.getProjects().stream().map(expr -> expr.accept(flattener)).collect(Collectors.toList());
      return LogicalProject.create(newInput, ImmutableList.of(), newExprs, project.getRowType().getFieldNames());
    }

    @Override
    public RelNode visit(LogicalFilter filter) {
      RelNode newInput = filter.getInput().accept(this);
      final ConditionFlattenter flattener = new ConditionFlattenter(filter.getCluster().getRexBuilder());
      return LogicalFilter.create(newInput, filter.getCondition().accept(flattener));
    }

    public static RelNode rewrite(RelNode rel) {
      return rel.accept(new StructuredConditionRewriter());
    }

    /**
     *
     */
    private static final class ConditionFlattenter extends RexShuttle {
      private RexBuilder builder;

      private ConditionFlattenter(RexBuilder builder) {
        this.builder = builder;
      }

      public RexNode visitCall(RexCall rexCall) {
        if (rexCall.isA(SqlKind.COMPARISON)) {
          if(rexCall.getOperands().get(0).getType().isStruct()) {
            RexNode converted = flattenComparison(builder, rexCall.getOperator(), rexCall.getOperands());
            if (converted != null) {
              return converted;
            }
          }
        }
        return super.visitCall(rexCall);
      }
    }

    private static RexNode flattenComparison(RexBuilder rexBuilder, SqlOperator op, List<RexNode> exprs) {
      final List<Pair<RexNode, String>> flattenedExps = Lists.newArrayList();
      for(RexNode expr : exprs) {
        if (expr instanceof RexCall && expr.isA(SqlKind.ROW)) {
          RexCall call = (RexCall) expr;
          List<RexNode> operands = call.getOperands();
          for (int i = 0 ; i < operands.size() ; i++) {
            flattenedExps.add(new Pair<>(operands.get(i), call.getType().getFieldList().get(i).getName()));
          }
        } else if (expr instanceof RexCall && expr.isA(SqlKind.CAST)) {
          RexCall call = (RexCall) expr;
          if (!call.getOperands().get(0).isA(SqlKind.ROW)) {
            return null;
          }
          List<RexNode> fields =  ((RexCall) call.getOperands().get(0)).getOperands();
          List<RelDataTypeField> types = call.type.getFieldList();
          for (int i = 0 ; i < fields.size() ; i++) {
            RexNode cast = rexBuilder.makeCast(types.get(i).getType(), fields.get(i));
            flattenedExps.add(new Pair<>(cast, types.get(i).getName()));
          }
        } else {
          return null;
        }
      }

      int n = flattenedExps.size() / 2;
      boolean negate = false;
      if (op.getKind() == SqlKind.NOT_EQUALS) {
        negate = true;
        op = SqlStdOperatorTable.EQUALS;
      }

      if (n > 1 && op.getKind() != SqlKind.EQUALS) {
        throw Util.needToImplement("inequality comparison for row types");
      } else {
        RexNode conjunction = null;

        for(int i = 0; i < n; ++i) {
          RexNode comparison = rexBuilder.makeCall(op, (RexNode) flattenedExps.get(i).left, (RexNode) flattenedExps.get(i + n).left);
          if (conjunction == null) {
            conjunction = comparison;
          } else {
            conjunction = rexBuilder.makeCall(SqlStdOperatorTable.AND, conjunction, comparison);
          }
        }

        if (negate) {
          return rexBuilder.makeCall(SqlStdOperatorTable.NOT, conjunction);
        } else {
          return conjunction;
        }
      }
    }
  }

  public static class RexNodeCountVisitor extends RexShuttle {

    public static int count(RexNode node) {
      RexNodeCountVisitor v = new RexNodeCountVisitor();
      node.accept(v);
      return v.count.value;
    }

    private Pointer<Integer> count = new Pointer<>(0);

    public int getRexNodeCount() {
      return count.value;
    }

    @Override
    public RexNode visitCall(RexCall call) {
      count.value++;
      return super.visitCall(call);
    }

    @Override
    public RexNode visitInputRef(RexInputRef input) {
      count.value++;
      return super.visitInputRef(input);
    }

    @Override
    public RexNode visitLiteral(RexLiteral literal) {
      count.value++;
      return super.visitLiteral(literal);
    }
  }

  /**
   * Same as {@link RelOptUtil#shiftFilter(int, int, int, RexBuilder, List, int, List, RexNode)}.
   * Shift filters using inputs ref of joins to filters using input refs of children of joins
   */
  public static RexNode shiftFilter(
    int start,
    int end,
    int offset,
    RexBuilder rexBuilder,
    List<RelDataTypeField> joinFields,
    int nTotalFields,
    List<RelDataTypeField> rightFields,
    RexNode filter) {
    int[] adjustments = new int[nTotalFields];
    for (int i = start; i < end; i++) {
      adjustments[i] = offset;
    }
    return filter.accept(
      new RelOptUtil.RexInputConverter(
        rexBuilder,
        joinFields,
        rightFields,
        adjustments));
  }

  public static PrelUtil.InputRewriter getInputRewriterFromProjectedFields(List<SchemaPath> projection, RelDataType rowType, BatchSchema batchSchema, RelOptCluster cluster) {
    final List<String> newFields = getRowTypeFromProjectedColumns(projection, batchSchema, cluster).getFieldNames();
    final Map<String, Integer> fieldMap = new HashMap<>();
    final IntIntHashMap oldToNewFields = new IntIntHashMap();
    final List<String> oldFields = rowType.getFieldNames();
    IntStream.range(0, newFields.size()).forEach(i -> fieldMap.put(newFields.get(i), i));
    for (int i = 0; i < oldFields.size(); i++) {
      final String field = oldFields.get(i);
      if (fieldMap.containsKey(field)) {
        oldToNewFields.put(i, fieldMap.get(field));
      }
    }
    return new PrelUtil.InputRewriter(oldToNewFields);
  }

  /**
   * Combination of {@link RelOptUtil#conjunctions(RexNode)} and {@link RelOptUtil#disjunctions(RexNode)}.
   * Check {@code RexNode.getKind()} and for AND/OR return unnested elements.
   */
  public static List<RexNode> conDisjunctions(RexNode rexNode) {
    switch (rexNode.getKind()) {
      case AND:
        return RelOptUtil.conjunctions(rexNode);
      case OR:
        return RelOptUtil.disjunctions(rexNode);
      default:
        throw new UnsupportedOperationException();
    }
  }

  /**
   * Combination of {@link RexUtil#composeConjunction(RexBuilder, Iterable, boolean)} and
   * {@link RexUtil#composeDisjunction(RexBuilder, Iterable, boolean)}.
   * Check {@code RexNode.getKind()} and for AND/OR compose expressions using {@code nodes}.
   */
  public static RexNode composeConDisjunction(RexBuilder rexBuilder,
    Iterable<? extends RexNode> nodes, boolean nullOnEmpty, SqlKind sqlKind) {
    switch (sqlKind) {
      case AND:
        return RexUtil.composeConjunction(rexBuilder, nodes, nullOnEmpty);
      case OR:
        return RexUtil.composeDisjunction(rexBuilder, nodes, nullOnEmpty);
      default:
        throw new UnsupportedOperationException();
    }
  }

  /**
   * Returns a list of the child expressions of given relational expression.
   * @param rel Relational expression
   * @return List of the child expressions of given relational expression
   */
  public static List<RexNode> getChildExps(RelNode rel) {
    List<RexNode> rexNodes = ImmutableList.of();
    if (rel instanceof Filter) {
      rexNodes = ImmutableList.of(((Filter)rel).getCondition());
    } else if (rel instanceof Join) {
      rexNodes = ImmutableList.of(((Join)rel).getCondition());
    } else if (rel instanceof MultiJoin) {
      rexNodes = ImmutableList.of(((MultiJoin)rel).getJoinFilter());
    } else if (rel instanceof Project) {
      rexNodes = ((Project)rel).getProjects();
    } else if (rel instanceof Sort) {
      rexNodes = ((Sort)rel).getSortExps();
    } else if (rel instanceof TableFunctionScan) {
      rexNodes = ImmutableList.of(((TableFunctionScan)rel).getCall());
    } else if (rel instanceof Snapshot) {
      rexNodes = ImmutableList.of(((Snapshot)rel).getPeriod());
    }
    return rexNodes;
  }

  /* If the call was to datetime - interval. */
  public static boolean isDatetimeMinusInterval(RexCall call) {
    return ((call.getOperator() == SqlStdOperatorTable.MINUS_DATE)
            && (call.getOperands().size() == 2)
            && (INTERVAL_TYPES.contains(call.getOperands().get(1).getType().getSqlTypeName())));
  }

  /* If the call was to datetime +/- interval. */
  public static boolean isDatetimeIntervalArithmetic(RexCall call) {
    return ((call.getOperator() == SqlStdOperatorTable.DATETIME_PLUS)
            || isDatetimeMinusInterval(call));
  }

  /**
   * TODO we might be able to replace this deepEquals/deepHashcode in CALCITE-4129
   * @param relNode
   * @return
   */
  public static long longHashCode(RelNode relNode) {
    Hasher hasher = Hashing.sha256().newHasher();
    relNode.explain(new RelWriter() {
      @Override public void explain(RelNode rel, List<Pair<String, Object>> valueList) {
        for(Pair<String, Object> pair: valueList) {
          item(pair.left, pair.right);
        }
        done(relNode);
      }

      @Override public SqlExplainLevel getDetailLevel() {
        return SqlExplainLevel.DIGEST_ATTRIBUTES;
      }

      @Override public RelWriter input(String term, RelNode input) {
        hasher.putString(term, StandardCharsets.UTF_8);
        input.explain(this);
        return this;
      }

      @Override public RelWriter item(String term, Object value) {
        if(value instanceof RelNode) {
          input(term, (RelNode) value);
        } else {
          hasher.putString(term, StandardCharsets.UTF_8)
            .putString(value.toString(), StandardCharsets.UTF_8);
        }
        return this;
      }

      @Override public RelWriter itemIf(String term, Object value, boolean condition) {
        if(condition) {
          return item(term, value);
        } else {
          return this;
        }
      }

      @Override public RelWriter done(RelNode node) {
        hasher.putString(node.getClass().toString(), StandardCharsets.UTF_8);
        return this;
      }

      @Override public boolean nest() {
        return true;
      }
    });
    return hasher.hash().asLong();
  }

  public static class TransformCollectingCall extends RelOptRuleCall {
    private final List<RelNode> outcome = new ArrayList<>();

    public TransformCollectingCall(RelOptPlanner planner, RelOptRuleOperand operand, RelNode[] rels,
                                   Map<RelNode, List<RelNode>> nodeInputs) {
      super(planner, operand, rels, nodeInputs);
    }

    @Override
    public void transformTo(RelNode rel, Map<RelNode, RelNode> equiv, RelHintsPropagator handler) {
      outcome.add(rel);
    }

    public boolean isTransformed() {
      return outcome.size() != 0;
    }

    public RelNode getTransformedRel() {
      if (isTransformed()) {
        return outcome.get(0);
      } else {
        throw new RuntimeException("No transformed node found.");
      }
    }
  }

  /** Builds an equi-join condition from a list of (leftKey, rightKey, filterNull). */
  public static RexNode createEquiJoinCondition(
    final RelNode left,
    final RelNode right,
    final List<Triple<Integer, Integer, Boolean>> equiConditions,
    final RexBuilder rexBuilder) {
    final List<RelDataType> leftTypes =
      RelOptUtil.getFieldTypeList(left.getRowType());
    final List<RelDataType> rightTypes =
      RelOptUtil.getFieldTypeList(right.getRowType());
    final List<RexNode> conditions = new ArrayList<>();
    for (Triple<Integer, Integer, Boolean> key : equiConditions) {
      final int leftKey = key.getLeft();
      final int rightKey = key.getMiddle();
      final Boolean filterNull = key.getRight();
      final SqlOperator op = filterNull ? SqlStdOperatorTable.EQUALS : SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
      conditions.add(rexBuilder.makeCall(op,
        rexBuilder.makeInputRef(leftTypes.get(leftKey), leftKey),
        rexBuilder.makeInputRef(rightTypes.get(rightKey),
          leftTypes.size() + rightKey)));
    }
    return RexUtil.composeConjunction(rexBuilder, conditions, false);
  }

  public static boolean containsUnsupportedDistinctCall(Aggregate aggregate) {
    return aggregate.getAggCallList().stream().anyMatch(aggregateCall ->
      aggregateCall.isDistinct() && aggregateCall.getAggregation().getKind() != SqlKind.LISTAGG);
  }

  public static int getNextExprIndexFromFields(List<String> fieldNames) {
    int startIndex = -1;
    for (String name : fieldNames) {
      if (name.startsWith("EXPR$")) {
        String[] split = name.split("EXPR\\$");
        if (split.length == 2 && StringUtils.isNumeric(split[1])) {
          startIndex = max(Integer.parseInt(split[1]), startIndex);
        }
      }
    }
    return startIndex + 1;
  }

  public static class SimpleReflectionFinderVisitor extends StatelessRelShuttleImpl {
    private final List<String> primaryKey;
    private final List<Integer> indices;

    public SimpleReflectionFinderVisitor() {
      this.primaryKey = new ArrayList<>();
      this.indices = new ArrayList<>();
    }

    @Override
    public RelNode visit(LogicalAggregate aggregate) {
      // Will get here if its an agg reflection. We only support PK for raw reflections.
      return aggregate;
    }

    @Override
    public RelNode visit(LogicalProject project) {
      return visit(project.getInput());
    }

    @Override
    public RelNode visit(LogicalFilter filter) {
      return visit(filter.getInput());
    }

    @Override
    public RelNode visit(RelNode other) {
      if (other instanceof ScanRelBase) {
        List<String> toAdd = ((ScanRelBase) other).getTableMetadata().getPrimaryKey();
        if (!CollectionUtils.isEmpty(toAdd)) {
          primaryKey.addAll(toAdd);
          indices.addAll(other.getRowType().getFieldList().stream()
            .filter(f -> toAdd.contains(f.getName().toLowerCase(Locale.ROOT)))
            .map(RelDataTypeField::getIndex)
            .collect(Collectors.toList()));
        }
        return other;
      } else if (other instanceof LogicalProject || other instanceof LogicalFilter) {
        return visit(((SingleRel) other).getInput());
      } else {
        return other;
      }
    }

    public List<String> getPrimaryKey() {
      return primaryKey;
    }

    public List<Integer> getIndices() {
      return indices;
    }
  }
}


text:
using CoreLocation;
using eShopOnContainers.Core.Models.Location;
using eShopOnContainers.Core.Models.Permissions;
using eShopOnContainers.Core.Services.Location;
using eShopOnContainers.Core.Services.Permissions;
using eShopOnContainers.iOS.Services;
using Foundation;
using System;
using System.Threading;
using System.Threading.Tasks;
using UIKit;

[assembly: Xamarin.Forms.Dependency(typeof(LocationServiceImplementation))]
namespace eShopOnContainers.iOS.Services
{
    public class LocationServiceImplementation : ILocationServiceImplementation
    {
        IPermissionsService _permissionsService;

        public LocationServiceImplementation()
        {
            DesiredAccuracy = 100;
            _permissionsService = new PermissionsService();
        }

        #region Internal Implementation

        async Task<bool> CheckPermissions(Permission permission)
        {
            var status = await _permissionsService.CheckPermissionStatusAsync(permission);
            if (status != PermissionStatus.Granted)
            {
                Console.WriteLine("Currently do not have Location permissions, requesting permissions");

                var request = await _permissionsService.RequestPermissionsAsync(permission);
                if (request[permission] != PermissionStatus.Granted)
                {
                    Console.WriteLine("Location permission denied, can not get positions async.");
                    return false;
                }
            }
            return true;
        }

        CLLocationManager GetManager()
        {
            CLLocationManager manager = null;
            new NSObject().InvokeOnMainThread(() => manager = new CLLocationManager());
            return manager;
        }

        #endregion

        #region ILocationServiceImplementation

        public double DesiredAccuracy { get; set; }
        public bool IsGeolocationAvailable => true;
        public bool IsGeolocationEnabled
        {
            get
            {
                var status = CLLocationManager.Status;
                return CLLocationManager.LocationServicesEnabled;
            }
        }

        public async Task<Position> GetPositionAsync(TimeSpan? timeout, CancellationToken? cancelToken = null)
        {
            var permission = Permission.LocationWhenInUse;
            var hasPermission = await CheckPermissions(permission);
            if (!hasPermission)
                throw new GeolocationException(GeolocationError.Unauthorized);

            var timeoutMilliseconds = timeout.HasValue ? (int)timeout.Value.TotalMilliseconds : Timeout.Infinite;
            if (timeoutMilliseconds <= 0 && timeoutMilliseconds != Timeout.Infinite)
                throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout must be positive or Timeout.Infinite");
            if (!cancelToken.HasValue)
                cancelToken = CancellationToken.None;

            TaskCompletionSource<Position> tcs;

            var manager = GetManager();
            manager.DesiredAccuracy = DesiredAccuracy;

            // Always prevent location update pausing since we're only listening for a single update
            if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
                manager.PausesLocationUpdatesAutomatically = false;

            tcs = new TaskCompletionSource<Position>(manager);
            var singleListener = new GeolocationSingleUpdateDelegate(manager, DesiredAccuracy, timeoutMilliseconds, cancelToken.Value);
            manager.Delegate = singleListener;
            manager.StartUpdatingLocation();

            return await singleListener.Task;
        }

        #endregion
    }
}

redpajama_set_name: RedPajamaGithub


text:
MTTestSemaphore
===============

A class to help you create unit tests that test asynchronous methods. You will need this to unit test any class that fetch data from the network, use location, camera, etc.

How to include MTTestSemaphore in your project
------------

Clone this repo from [https://github.com/icanzilb/MTTestSemaphore](https://github.com/icanzilb/MTTestSemaphore) or download it as a zip file.

Include the contents of the **MTTestSemaphore** sub-folder in your project - make sure that the **MTTestSemaphore** class is included in your Test target.

How to use MTTestSemaphore in your unit tests
------------

Use `[[MTTestSemaphore semaphore] waitForKey: @"myAwesomeKey"]` to stop the code execution at a given place and wait (ie wait for your asynchronious code to finish executing)

Use `[[MTTestSemaphore semaphore] lift: @"myAwesomeKey"]` to un-block the program execution at the place where you stopped it for the same key.

Here's a complete example, how to make an async call in a test case and wait for its execution:

```objective-c

-(void)testLocationAsyncNoComments
{
    NSString* sempahoreKey = @"testLocationAsync1";

    CLGeocoder* gc = [[CLGeocoder alloc] init];
    [gc geocodeAddressString:@"Triq Sant' Orsla, Valletta, Malta"
           completionHandler:^(NSArray *placemarks, NSError *error) {

               NSAssert(placemarks.count>0, @"the geocoder returned 0 results");
               [[MTTestSemaphor semaphore] lift: sempahoreKey];

           }];

    [[MTTestSemaphor semaphore] waitForKey: sempahoreKey];
}

```

Download and run Project/Test the project included in this repo, to see the usage of MTTestSemaphore


text:
import os
import subprocess
import socket

name = "gobuildslave"

current_hash = ""
for line in os.popen("md5sum " + name).readlines():
    current_hash = line.split(' ')[0]

# Move the old version over
for line in os.popen('cp ' + name + ' old' + name).readlines():
    print line.strip()

# Rebuild
for line in os.popen('go build').readlines():
    print line.strip()

size_1 = os.path.getsize('./old' + name)
size_2 = os.path.getsize('./' + name)

lines = os.popen('ps -ef | grep ' + name).readlines()
running = False
for line in lines:
    if "./" + name in line:
        running = True

new_hash = ""
for line in os.popen("md5sum " + name).readlines():
    new_hash = line.split(' ')[0]

if size_1 != size_2 or new_hash != current_hash or not running:
    if not running:
        for line in os.popen('go get github.com/brotherlogic/buildserver/buildserver_cli'):
            pass
        for line in os.popen('buildserver_cli crash gobuildslave version out.txt'):
            pass
    else:
        try:
            os.popen('go get github.com/brotherlogic/versionserver/versionserver_cli')
            os.popen('versionserver_cli guard ' + name)
        except IOError:
            os.Exit(0)
        for line in os.popen("cp out.txt oldout.txt").readline():
            pass
        for line in os.popen('echo "" > out.txt').readlines():
            pass
        for line in os.popen('killall ' + name).readlines():
            pass
    if socket.gethostname() == "stationone" or socket.gethostname() == "natframe" or socket.gethostname() == 'framethree' or socket.gethostname() == "printer":
        subprocess.Popen(['./' + name, '--builds=false'])
    else:
        if socket.gethostname() == "argon":
            subprocess.Popen(['./' + name, '--max_jobs=50'])
        else:
            subprocess.Popen(['./' + name])


text:
package org.joverseer.orders.reports;

import org.joverseer.support.Container;

public class TurnReport {
    Container reportItems = new Container(new String[]{"nationNo"});

    public Container getReportItems() {
        return this.reportItems;
    }

    public void setReportItems(Container reportItems) {
        this.reportItems = reportItems;
    }

}


text:
FILENAME="${1:-index}"
pdflatex ${FILENAME}.tex && bibtex ${FILENAME} && \
pdflatex ${FILENAME}.tex && pdflatex ${FILENAME}.tex


text:
package ru.job4j.tracker;

/** Class Класс MenuTracker.
 * @author Arseniy Kulkiov
 * @since 03.07.2017
 * @version 1
 */

public class MenuTracker {
    /**
     * input - ввод.
     */
    private Input input;
    /**
     * tracker - трекер.
     */
    private Tracker tracker;
    /**
     * actions - массив действий меню.
     */
    private UserAction[] actions = new UserAction[7];
    /**
     * Конструктор.
     * @param input - ввод.
     * @param tracker - трекер.
     */
    public MenuTracker(Input input, Tracker tracker) {
        this.input = input;
        this.tracker = tracker;
    }
    /**
     * Метод заполнения массива действий соответствующими методами.
     */
    public void fillAction() {
        this.actions[0] = new AddItem(0, "Add Item.");
        this.actions[1] = new MenuTracker.ShowItems(1, "Show all items.");
        this.actions[2] = this.new EditItem(2, "Edit item.");
        this.actions[3] = this.new DeleteItem(3, "Delete item.");
        this.actions[4] = this.new FindById(4, "Find item by Id.");
        this.actions[5] = this.new FindByName(5, "Find item(s) by name.");
        this.actions[6] = this.new Exit(6, "Exit.");
    }
    /**
     * Метод геттер поля actions.
     * @return - возврат actions.
     */
    public UserAction[] getActions() {
        return this.actions;
    }
    /**
     * Метод ввода выбора "ключа" пункта меню.
     * @param key - "ключ"/номер пункта меню.
     */
    public void select(int key) {
        this.actions[key].execute(this.input, this.tracker);
    }
    /**
     * Метод выполняющий вывод меню на консоль.
     */
    public void show() {
        for (UserAction action : this.actions) {
            System.out.println(action.info());
        }
    }
    /**
     * внутренний внешний нестатический класс добавления заявки.
     */
    class AddItem extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        AddItem(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 0;
        }
        /**
         * Метод выполнения добавления заявки.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            String name = input.ask("Please, enter the name of new Item: ");
            String desc = input.ask("Please enter the description of new Item: ");
            tracker.add(new Item(name, desc));
            System.out.println("Successfully added.");
        }
    }
    /**
     * Внутренний статический класс отображения всех заявок.
     */
    private static class ShowItems extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        ShowItems(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 1;
        }
        /**
         * Метод выполнения отображения всех заявок.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            Item[] items = tracker.findAll();
            if (items.length == 0) {
                System.out.println("The Tracker is empty.");
            } else {
                for (Item item : items) {
                    System.out.printf("Name: %s; Description: %s; Id: %s.%n", item.getName(), item.getDesc(), item.getId());
                    //System.out.println("Name: " + item.getName() + "; Description: " + item.getDesc() + "; Id: " + item.getId() + ".");
                }
            }
        }
    }
    /**
     * Внутренний нестатический класс редактирования заявки.
     */
    private class EditItem extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        EditItem(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 2;
        }
        /**
         * Метод выполнения отображения всех заявок.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            String id = input.ask("Enter the id of edited item: ");
            Item item = tracker.findById(id);
            if (item != null) {
                String name = input.ask("Enter the new name of edited item: ");
                item.setName(name);
                String desc = input.ask("Enter the description of edited item: ");
                item.setDesc(desc);
                System.out.println("Successfully edited.");
            } else {
                System.out.println("Unknown Item's Id! Please, enter correct Id.");
            }
        }
    }
    /**
     * Внутренний нестатический класс удаления заявки.
     */
    private class DeleteItem extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        DeleteItem(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 3;
        }
        /**
         * Метод выполнения удаления заявки.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            String id = input.ask("Enter the id of deleted item: ");
            Item item = tracker.findById(id);
            if (item != null) {
                tracker.delete(item);
                System.out.println("Item successfully deleted.");
            } else {
                System.out.println("Unknown Item's Id! Please, enter correct Id.");
            }
        }
    }
    /**
     * Внутренний нестатический класс поиска заявки по Id.
     */
    private class FindById extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        FindById(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 4;
        }
        /**
         * Метод выполнения поиска заявки по Id.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            String id = input.ask("Enter the id of item: ");
            Item item = tracker.findById(id);
            if (item != null) {
                System.out.printf("For this Id: %3$s %nName: %1$s; Description: %2$s;%n", item.getName(), item.getDesc(), item.getId());
            } else {
                System.out.println("Unknown Item's Id! Please, enter correct Id.");
            }
        }
    }
    /**
     * Внутренний нестатический класс поиска заявки по имени.
     */
    private class FindByName extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        FindByName(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 5;
        }
        /**
         * Метод выполнения поиска заявки(-ок) по имени.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
            String name = input.ask("Enter the name of item: ");
            Item[] items = tracker.findByName(name);
            if (items.length != 0) {
                System.out.printf("Item(s) by \"%s\" name:%n", name);
                for (Item item : items) {
                    System.out.printf("Id: %s; Description: %s;%n", item.getId(), item.getDesc());
                }
            } else {
                System.out.println("No item(s) with such name.");
            }
        }
    }
    /**
     * Внутренний нестатический класс выхода из программы.
     */
    private class Exit extends BaseAction {
        /**
         * Конструктор.
         * @param key - ключ действия.
         * @param name - имя действия.
         */
        Exit(int key, String name) {
            super(key, name);
        }
        /**
         * Метод возврата значения ключа.
         * @return - возврат.
         */
        public int key() {
            return 6;
        }
        /**
         * Метод ничего не выполняющий. Просто реалзиация интерфейса.
         * @param input - ввод.
         * @param tracker - трекер.
         */
        public void execute(Input input, Tracker tracker) {
        }
    }
}


text:
export var X;

//// [decoratorOnImportEquals2_1.ts]
@dec
import lib = require('./decoratorOnImportEquals2_0');

declare function dec<T>(target: T): T;

//// [decoratorOnImportEquals2_0.js]
exports.X;
//// [decoratorOnImportEquals2_1.js]


text:
package org.hellojavaer.ddal.ddr.datasource.manager.rw;

import org.hellojavaer.ddal.ddr.datasource.manager.rw.monitor.ReadOnlyDataSourceMonitorServer;
import org.hellojavaer.ddal.ddr.datasource.manager.DataSourceManager;
import org.hellojavaer.ddal.ddr.datasource.manager.rw.monitor.ReadOnlyDataSourceMonitor;

import java.util.List;

/**
 *
 * @author Kaiming Zou,created on 19/11/2016.
 */
public interface ReadWriteDataSourceManager extends DataSourceManager {

    List<WriteOnlyDataSourceBinding> getWriteOnlyDataSources();

    List<ReadOnlyDataSourceBinding> getReadOnlyDataSources();

    ReadOnlyDataSourceMonitor getReadOnlyDataSourceMonitor();

    ReadOnlyDataSourceMonitorServer getReadOnlyDataSourceMonitorServer();

}


text:
import { NgModule } from '@angular/core';

import { GeocodeService } from './geocode/geocode.service';
import { MapboxGeocodingService } from './geocode/mapbox-geocoding.service';
import { WindowRefService } from './windowRef/window-ref.service';
import { GeolocationService } from './geolocation/geolocation.service';
import { GeosearchingService } from './geosearching/geosearching.service';
import { LeafletMapComponent } from './map/leaflet-map/leaflet-map.component';
import { FirebaseQueryingService } from './firebase-querying/firebase-querying.service';

@NgModule({
  providers: [
    GeocodeService,
    MapboxGeocodingService,
    WindowRefService,
    GeolocationService,
    GeosearchingService,
    FirebaseQueryingService
  ],
  declarations: [
    LeafletMapComponent
  ],
  exports: [
    LeafletMapComponent
  ]
})
export class GeoModule {
}

export {
  GeocodeService,
  GeolocationService,
  GeosearchingService,
  FirebaseQueryingService
}


text:
(function(module) {
    mifosX.controllers = _.extend(module, {
        MemberManageController: function(scope, routeParams , route, location, resourceFactory) {
            scope.group = [];
            scope.managecode = routeParams.managecode;

            resourceFactory.groupResource.get({groupId: routeParams.id,associations:'all'} , function(data) {
                scope.group = data;
            });

            resourceFactory.groupResource.get({groupId: routeParams.id,associations:'clientMembers',template:'true'} , function(data) {
                scope.allClients = data.clientOptions;
                scope.allMembers = data.clientMembers;
            });

            scope.associate = function(){
                resourceFactory.groupResource.save({groupId: routeParams.id,command:'associateClients'} ,this.formData, function(data) {
                    location.path('/viewgroup/' + data.groupId);
                });
            };

            scope.disassociate = function() {
                var disassociateMembers = new Object();
                disassociateMembers.clientMembers = this.formData.clientMembers;
                resourceFactory.groupResource.save({groupId:routeParams.id,command:'disassociateClients'},disassociateMembers, function(data) {
                    location.path('/viewgroup/' + data.groupId);
                });
            };
        }
    });
    mifosX.ng.application.controller('MemberManageController', ['$scope', '$routeParams', '$route', '$location', 'ResourceFactory', mifosX.controllers.MemberManageController]).run(function($log) {
        $log.info("MemberManageController initialized");
    });
}(mifosX.controllers || {}));


text:
namespace CTripOSS.Baiji.Editor
{
    partial class PrunerPanel
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.m_OperationsCheckedListBox = new System.Windows.Forms.CheckedListBox();
            this.SuspendLayout();
            //
            // m_OperationsCheckedListBox
            //
            this.m_OperationsCheckedListBox.CheckOnClick = true;
            this.m_OperationsCheckedListBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_OperationsCheckedListBox.FormattingEnabled = true;
            this.m_OperationsCheckedListBox.Location = new System.Drawing.Point(0, 0);
            this.m_OperationsCheckedListBox.Name = "m_OperationsCheckedListBox";
            this.m_OperationsCheckedListBox.Size = new System.Drawing.Size(442, 162);
            this.m_OperationsCheckedListBox.TabIndex = 1;
            //
            // PrunerPanel
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.m_OperationsCheckedListBox);
            this.Name = "PrunerPanel";
            this.Size = new System.Drawing.Size(442, 162);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.CheckedListBox m_OperationsCheckedListBox;
    }
}


text:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>TitlePane with margin</title>

			Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
			luctus, dolor rutrum fringilla eleifend, augue metus gravida velit, in
			vestibulum magna sapien vitae sapien. Mauris risus lectus, facilisis
			aliquet, mattis quis, elementum porta, lectus. Maecenas dictum iaculis
			ipsum. Duis sit amet neque. Praesent consectetur. Nulla urna risus,
			auctor ut, convallis et, porta eget, lorem. Nunc sagittis, orci eget
			vulputate tincidunt, orci massa fermentum leo, nec sollicitudin eros
			nisi id felis. Duis massa orci, bibendum vel, adipiscing eu, lacinia
			quis, orci. Nulla velit ipsum, venenatis sed, ultricies vitae, suscipit
			at, diam. Aenean lacinia, enim quis adipiscing elementum, lectus risus
			euismod dolor, non pretium justo magna non magna. Nulla commodo arcu in
			lectus. Nam id quam.

			Integer eu arcu. Etiam consectetur mollis elit.
			Suspendisse tincidunt eros a erat. Mauris nisi. In venenatis posuere
			risus. Vivamus egestas condimentum nisi. Phasellus ac ante. Donec sit
			amet nulla. Pellentesque lorem. Fusce orci lectus, mattis id, dignissim
			sit amet, accumsan in, tellus. Etiam turpis. Ut lorem ante, lobortis ac,
			tempor vel, sodales a, tortor. Aliquam commodo, ligula in egestas
			molestie, turpis est placerat metus, vel aliquam urna augue non lectus.
			Praesent nisi augue, facilisis semper, vehicula placerat, congue vel,
			pede. Nam suscipit imperdiet elit. Morbi feugiat, lorem vehicula
			volutpat auctor, felis libero placerat nibh, cursus feugiat enim eros a
			neque. Praesent volutpat. Donec condimentum. Donec metus. Ut et elit
			sollicitudin mi accumsan hendrerit.

TitleContentPane:

	  Is a pane with a title on top, which can have a shadow.

	  An accessible container with a Title Heading. TitledContentPane is an

	  extension to ContentPane. It provides all the ContentPane it's usesful

	  content-control aspects.

features:

Add a title

Use a shadow

Put a margin on the component

example:

	  Here you see an example of a TitledContentPane with a title and fixed margin.

	  If you set the margin, it will set the margin on every side (top, right, bottom and left)

	  The source code:

<head>
<script type="text/javascript">
    dojo.require("dojo.parser");
    dojo.require("ppwcode.dojo.dijit.layout.TitledContentPane");
</script>
</head>
<body>
.
.
.
<div dojoType="ppwcode.dojo.dijit.layout.TitledContentPane"
	title="TitlePane with shadow"
	showShadow="true">
	content
</div>
.
.
.
</body>

package structure:


text: Its a blog by me


text:
package federatedtypes

import (
	apiv1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	pkgruntime "k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/watch"
	kubeclientset "k8s.io/client-go/kubernetes"
	restclient "k8s.io/client-go/rest"
	federationclientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset"
	"k8s.io/kubernetes/federation/pkg/federation-controller/util"
)

const (
	ConfigMapKind           = "configmap"
	ConfigMapControllerName = "configmaps"
)

func init() {
	RegisterFederatedType(ConfigMapKind, ConfigMapControllerName, []schema.GroupVersionResource{apiv1.SchemeGroupVersion.WithResource(ConfigMapControllerName)}, NewConfigMapAdapter)
}

type ConfigMapAdapter struct {
	client federationclientset.Interface
}

func NewConfigMapAdapter(client federationclientset.Interface, config *restclient.Config) FederatedTypeAdapter {
	return &ConfigMapAdapter{client: client}
}

func (a *ConfigMapAdapter) Kind() string {
	return ConfigMapKind
}

func (a *ConfigMapAdapter) ObjectType() pkgruntime.Object {
	return &apiv1.ConfigMap{}
}

func (a *ConfigMapAdapter) IsExpectedType(obj interface{}) bool {
	_, ok := obj.(*apiv1.ConfigMap)
	return ok
}

func (a *ConfigMapAdapter) Copy(obj pkgruntime.Object) pkgruntime.Object {
	configmap := obj.(*apiv1.ConfigMap)
	return &apiv1.ConfigMap{
		ObjectMeta: util.DeepCopyRelevantObjectMeta(configmap.ObjectMeta),
		Data:       configmap.Data,
	}
}

func (a *ConfigMapAdapter) Equivalent(obj1, obj2 pkgruntime.Object) bool {
	configmap1 := obj1.(*apiv1.ConfigMap)
	configmap2 := obj2.(*apiv1.ConfigMap)
	return util.ConfigMapEquivalent(configmap1, configmap2)
}

func (a *ConfigMapAdapter) QualifiedName(obj pkgruntime.Object) QualifiedName {
	configmap := obj.(*apiv1.ConfigMap)
	return QualifiedName{Namespace: configmap.Namespace, Name: configmap.Name}
}

func (a *ConfigMapAdapter) ObjectMeta(obj pkgruntime.Object) *metav1.ObjectMeta {
	return &obj.(*apiv1.ConfigMap).ObjectMeta
}

func (a *ConfigMapAdapter) FedCreate(obj pkgruntime.Object) (pkgruntime.Object, error) {
	configmap := obj.(*apiv1.ConfigMap)
	return a.client.CoreV1().ConfigMaps(configmap.Namespace).Create(configmap)
}

func (a *ConfigMapAdapter) FedDelete(qualifiedName QualifiedName, options *metav1.DeleteOptions) error {
	return a.client.CoreV1().ConfigMaps(qualifiedName.Namespace).Delete(qualifiedName.Name, options)
}

func (a *ConfigMapAdapter) FedGet(qualifiedName QualifiedName) (pkgruntime.Object, error) {
	return a.client.CoreV1().ConfigMaps(qualifiedName.Namespace).Get(qualifiedName.Name, metav1.GetOptions{})
}

func (a *ConfigMapAdapter) FedList(namespace string, options metav1.ListOptions) (pkgruntime.Object, error) {
	return a.client.CoreV1().ConfigMaps(namespace).List(options)
}

func (a *ConfigMapAdapter) FedUpdate(obj pkgruntime.Object) (pkgruntime.Object, error) {
	configmap := obj.(*apiv1.ConfigMap)
	return a.client.CoreV1().ConfigMaps(configmap.Namespace).Update(configmap)
}

func (a *ConfigMapAdapter) FedWatch(namespace string, options metav1.ListOptions) (watch.Interface, error) {
	return a.client.CoreV1().ConfigMaps(namespace).Watch(options)
}

func (a *ConfigMapAdapter) ClusterCreate(client kubeclientset.Interface, obj pkgruntime.Object) (pkgruntime.Object, error) {
	configmap := obj.(*apiv1.ConfigMap)
	return client.CoreV1().ConfigMaps(configmap.Namespace).Create(configmap)
}

func (a *ConfigMapAdapter) ClusterDelete(client kubeclientset.Interface, qualifiedName QualifiedName, options *metav1.DeleteOptions) error {
	return client.CoreV1().ConfigMaps(qualifiedName.Namespace).Delete(qualifiedName.Name, options)
}

func (a *ConfigMapAdapter) ClusterGet(client kubeclientset.Interface, qualifiedName QualifiedName) (pkgruntime.Object, error) {
	return client.CoreV1().ConfigMaps(qualifiedName.Namespace).Get(qualifiedName.Name, metav1.GetOptions{})
}

func (a *ConfigMapAdapter) ClusterList(client kubeclientset.Interface, namespace string, options metav1.ListOptions) (pkgruntime.Object, error) {
	return client.CoreV1().ConfigMaps(namespace).List(options)
}

func (a *ConfigMapAdapter) ClusterUpdate(client kubeclientset.Interface, obj pkgruntime.Object) (pkgruntime.Object, error) {
	configmap := obj.(*apiv1.ConfigMap)
	return client.CoreV1().ConfigMaps(configmap.Namespace).Update(configmap)
}

func (a *ConfigMapAdapter) ClusterWatch(client kubeclientset.Interface, namespace string, options metav1.ListOptions) (watch.Interface, error) {
	return client.CoreV1().ConfigMaps(namespace).Watch(options)
}

func (a *ConfigMapAdapter) IsSchedulingAdapter() bool {
	return false
}

func (a *ConfigMapAdapter) NewTestObject(namespace string) pkgruntime.Object {
	return &apiv1.ConfigMap{
		ObjectMeta: metav1.ObjectMeta{
			GenerateName: "test-configmap-",
			Namespace:    namespace,
		},
		Data: map[string]string{
			"A": "ala ma kota",
		},
	}
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/GoogleSheetsWrapper.iml" filepath="$PROJECT_DIR$/GoogleSheetsWrapper.iml" />
      <module fileurl="file://$PROJECT_DIR$/drivesheetswrapper/drivesheetswrapper.iml" filepath="$PROJECT_DIR$/drivesheetswrapper/drivesheetswrapper.iml" />
    </modules>
  </component>
</project>


text:
package redsync_test

import "github.com/hjr265/redsync.go/redsync"

func ExampleMutex() {
	m, err := redsync.NewMutexWithGenericPool("FlyingSquirrels", pools)
	if err != nil {
		panic(err)
	}

	err = m.Lock()
	if err != nil {
		panic(err)
	}
	defer m.Unlock()

	// Output:
}


text:
import AutocompleteTextBox from "../../Com/Autocomplete/AutocompleteTextBox.js";

// BootstrapAutocompleteTextBox
export default class BootstrapAutocomplete extends AutocompleteTextBox {

    constructor(parentContainer) {

        super(parentContainer);
        this._input.addCssClass("form-control");

    }

}


text:
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter
from django.db.models import F

class IsGeneratedListFilter(SimpleListFilter):
     # Human-readable title which will be displayed in the
     # right admin sidebar just above the filter options.
     title = _('type')

     # Parameter for the filter that will be used in the URL query.
     parameter_name = 'method'

     def lookups(self, request, model_admin):
         """
         Returns a list of tuples. The first element in each
         tuple is the coded value for the option that will
         appear in the URL query. The second element is the
         human-readable name for the option that will appear
         in the right sidebar.
         """
         return (
             ('generated_self', _('Generated in same event')),
             ('generated_ancestor', _('Generated in ancestor event')),
             ('generated', _('Generated anywhere')),
             ('one-off', _('One-off')),
         )

     def queryset(self, request, queryset):
         """
         Returns the filtered queryset based on the value
         provided in the query string and retrievable via
         `self.value()`.
         """
         # Compare the requested value (either '80s' or 'other')
         # to decide how to filter the queryset.

         if self.value() == 'generated_self':
             return queryset.filter(generated_by__event=F('event'))
         if self.value() == 'generated_ancestor':
             return queryset.filter(generated_by__isnull=False).exclude(generated_by__event=F('event'))
         if self.value() == 'generated':
             return queryset.filter(generated_by__isnull=False)
         if self.value() == 'one-off':
             return queryset.filter(generated_by__isnull=True)


text:
static int file_write_fstabrec(FILE * f, bool twrp, const char *blk_device,
			       const char *mount_point, const char *fs_type,
			       const char *fs_options, const char *fs_mgr_flags,
			       const char *unhandled)
{
	if (twrp) {
		if (!strcmp(fs_options, "defaults"))
			fs_options = "";
		if (!strcmp(fs_mgr_flags, "defaults"))
			fs_mgr_flags = "";
		return fprintf(f, "%s %s %s %s %s %s\n", mount_point, fs_type,
			       blk_device, fs_options, fs_mgr_flags, unhandled);
	} else {
		return fprintf(f, "%s %s %s %s %s\n", blk_device, mount_point,
			       fs_type, fs_options, fs_mgr_flags);
	}
}

static int patch_fstab(struct module_data *data, int index)
{
	int i;
	struct fstab_rec *rec;

	struct fstab *fstab_orig = data->target_fstabs[index];
	struct fstab *mbfstab = data->multiboot_fstab;

	// open fstab for writing
	FILE *f = fopen(fstab_orig->fstab_filename, "w");
	if (!f) {
		ERROR("Error opening fstab!\n");
		return -1;
	}
	// write new fstab
	for (i = 0; i < fstab_orig->num_entries; i++) {
		const char *blk_device = fstab_orig->recs[i].blk_device;
		const char *mount_point = fstab_orig->recs[i].mount_point;
		const char *fs_type = fstab_orig->recs[i].fs_type;
		const char *fs_options =
		    fstab_orig->recs[i].fs_options_unparsed ? : "";
		const char *fs_mgr_flags =
		    fstab_orig->recs[i].fs_mgr_flags_unparsed ? : "";
		const char *unhandled_columns =
		    fstab_orig->recs[i].unhandled_columns ? : "";

		// lookup partition in multiboot fstab
		bool use_bind = false;

		// fixup for boot and recovery partitions on 2ndstage devices (busy partition)
		if (data->sndstage_enabled && data->grub_device.blk_device
		    && data->grub_path) {
			struct sys_block_uevent *event =
			    get_blockinfo_for_path(data->block_info,
						   blk_device);
			if (!event) {
				WARNING
				    ("Couldn't find event_info for path %s!\n",
				     blk_device);
			} else if (event->linux_major ==
				   data->grub_blockinfo->linux_major
				   && event->linux_minor ==
				   data->grub_blockinfo->linux_minor) {
				blk_device = PATH_MOUNTPOINT_GRUB;
				fs_options = "bind";
			}
		}

		if (data->multiboot_enabled) {
			rec =
			    fs_mgr_get_entry_for_mount_point(mbfstab,
							     mount_point);
			if (rec && fs_mgr_is_multiboot(rec)) {
				// bind mount
				if (rec->replacement_bind) {
					// set new args
					blk_device = rec->replacement_device;
					fs_options = "bind";

					use_bind = true;
				}
				// fsimage mount
				else {
					blk_device = rec->replacement_device;
				}
			}
		}
		// write new entry
		file_write_fstabrec(f, fstab_orig->twrp, blk_device,
				    mount_point, fs_type, fs_options,
				    fs_mgr_flags, unhandled_columns);

		// we need a remount for bind mounts to set the flags
		if (use_bind && strcmp(mount_point, "/system")) {
			const char *local_fs_options =
			    fstab_orig->recs[i].fs_options_unparsed;
			char *new_fs_options = NULL;
			int size =
			    strlen(FS_OPTION_REMOUNT) +
			    strlen(local_fs_options) + 1;

			// create remount options
			new_fs_options = malloc(size);
			snprintf(new_fs_options, size, "%s%s",
				 FS_OPTION_REMOUNT, local_fs_options);

			// add remount entry
			file_write_fstabrec(f, fstab_orig->twrp, blk_device,
					    mount_point, fs_type,
					    new_fs_options, "wait",
					    unhandled_columns);

			// cleanup
			free(new_fs_options);
		}
	}

	fclose(f);
	return 0;
}

static int fp_fstab_init(struct module_data *data)
{
	unsigned i;

	// recovery is fully ptraced so we don't need any patching
	if (data->bootmode == BOOTMODE_RECOVERY)
		return 0;

	DEBUG("patch fstabs...\n");
	for (i = 0; i < data->target_fstabs_count; i++) {
		if (patch_fstab(data, i))
			return -1;
	}

	return 0;
}

static struct module module_fstab_patcher = {
	.fstab_init = fp_fstab_init,
};

module_add(module_fstab_patcher);


text:
@interface FBRemoteNibDemoAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


text:
extern std::vector<unsigned int> vPrimes;
static const mpz_class mpzOne = 1;
static const mpz_class mpzTwo = 2;
// Generate small prime table
unsigned int PrimeChainTest(uint256& hash, uint64_t nFix, uint64_t nTry, unsigned int nCandidateType);

enum // prime chain type
{
    PRIME_CHAIN_CUNNINGHAM1 = 1u,
    PRIME_CHAIN_CUNNINGHAM2 = 2u,
    PRIME_CHAIN_BI_TWIN     = 3u,
};

inline void mpz_set_uint256(mpz_t r, uint256& u)
{
    mpz_import(r, 32 / sizeof(unsigned long), -1, sizeof(unsigned long), -1, 0, &u);
}
#endif


text:
<?php

namespace Drupal\commerce_order\Form;

use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;

/**
 * Provides a form for selecting the order's customer (uid and mail fields).
 *
 * Used when adding a new order or reassigning an existing one.
 */
trait CustomerFormTrait {

  /**
   * Builds the customer form.
   *
   * @param array $form
   *   The parent form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The current order, if known.
   *
   * @return array
   *   The parent form with the customer form elements added.
   */
  public function buildCustomerForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) {
    $selected_customer_type = $form_state->getValue(['customer_type'], 'existing');
    $wrapper_id = Html::getUniqueId('customer-fieldset-wrapper');

    $form['customer'] = [
      '#type' => 'fieldset',
      '#title' => t('Customer'),
      '#prefix' => '
',
      '#suffix' => '
',
    ];
    $form['customer']['customer_type'] = [
      '#type' => 'radios',
      '#title' => t('Order for'),
      '#title_display' => 'invisible',
      '#attributes' => [
        'class' => ['container-inline'],
      ],
      '#required' => TRUE,
      '#options' => [
        'existing' => t('Existing customer'),
        'new' => t('New customer'),
      ],
      '#default_value' => $selected_customer_type,
      '#ajax' => [
        'callback' => [$this, 'customerFormAjax'],
        'wrapper' => $wrapper_id,
      ],
    ];
    if ($selected_customer_type == 'existing') {
      $form['customer']['uid'] = [
        '#type' => 'entity_autocomplete',
        '#title' => t('Search'),
        '#attributes' => [
          'class' => ['container-inline'],
        ],
        '#placeholder' => t('Search by username or email address'),
        '#target_type' => 'user',
        '#required' => TRUE,
        '#selection_settings' => [
          'match_operator' => 'CONTAINS',
          'include_anonymous' => FALSE,
        ],
      ];
    }
    else {
      // New customer.
      $form['customer']['uid'] = [
        '#type' => 'value',
        '#value' => 0,
      ];
      $form['customer']['mail'] = [
        '#type' => 'email',
        '#title' => t('Email'),
        '#required' => TRUE,
      ];
      $form['customer']['password'] = [
        '#type' => 'container',
      ];
      $form['customer']['password']['generate'] = [
        '#type' => 'checkbox',
        '#title' => t('Generate password'),
        '#default_value' => 1,
      ];
      // The password_confirm element needs to be wrapped in order for #states
      // to work properly. See https://www.drupal.org/node/1427838.
      $form['customer']['password']['password_confirm_wrapper'] = [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            ':input[name="generate"]' => ['checked' => FALSE],
          ],
        ],
      ];
      // We cannot make this required due to HTML5 validation.
      $form['customer']['password']['password_confirm_wrapper']['pass'] = [
        '#type' => 'password_confirm',
        '#size' => 25,
      ];
    }

    return $form;
  }

  /**
   * Ajax callback.
   */
  public function customerFormAjax(array $form, FormStateInterface $form_state) {
    return $form['customer'];
  }

  /**
   * Submit handler for the customer select form.
   *
   * @param array $form
   *   The parent form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitCustomerForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    if ($values['customer_type'] == 'existing') {
      $values['mail'] = User::load($values['uid'])->getEmail();
    }
    else {
      $user = User::create([
        'name' => $values['mail'],
        'mail' => $values['mail'],
        'pass' => ($values['generate']) ? user_password() : $values['pass'],
        'status' => TRUE,
      ]);
      $user->save();
      $values['uid'] = $user->id();
    }

    $form_state->setValues($values);
  }

}