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

com.hazelcast.org.apache.calcite.rex.RexAnalyzer Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in com.hazelcast.com.liance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.com.hazelcast.org.licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.org.apache.calcite.rex;

import com.hazelcast.org.apache.calcite.linq4j.Linq4j;
import com.hazelcast.org.apache.calcite.plan.RelOptPredicateList;
import com.hazelcast.org.apache.calcite.rel.metadata.NullSentinel;
import com.hazelcast.org.apache.calcite.util.NlsString;
import com.hazelcast.org.apache.calcite.util.Pair;
import com.hazelcast.org.apache.calcite.util.Util;

import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableList;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableMap;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.Iterables;

import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** Analyzes an expression, figures out what are the unbound variables,
 * assigns a variety of values to each unbound variable, and evaluates
 * the expression. */
public class RexAnalyzer {
  public final RexNode e;
  public final List variables;
  public final int unsupportedCount;

  /** Creates a RexAnalyzer. */
  public RexAnalyzer(RexNode e, RelOptPredicateList predicates) {
    this.e = e;
    final VariableCollector variableCollector = new VariableCollector();
    e.accept(variableCollector);
    predicates.pulledUpPredicates.forEach(p -> p.accept(variableCollector));
    variables = ImmutableList.copyOf(variableCollector.builder);
    unsupportedCount = variableCollector.unsupportedCount;
  }

  /** Generates a map of variables and lists of values that could be assigned
   * to them. */
  public Iterable> assignments() {
    final List> generators =
        variables.stream().map(RexAnalyzer::getComparables)
            .collect(Util.toImmutableList());
    final Iterable> product = Linq4j.product(generators);
    //noinspection StaticPseudoFunctionalStyleMethod
    return Iterables.transform(product,
        values -> ImmutableMap.copyOf(Pair.zip(variables, values)));
  }

  private static List getComparables(RexNode variable) {
    final ImmutableList.Builder values = ImmutableList.builder();
    switch (variable.getType().getSqlTypeName()) {
    case BOOLEAN:
      values.add(true);
      values.add(false);
      break;
    case INTEGER:
      values.add(BigDecimal.valueOf(-1L));
      values.add(BigDecimal.valueOf(0L));
      values.add(BigDecimal.valueOf(1L));
      values.add(BigDecimal.valueOf(1_000_000L));
      break;
    case DECIMAL:
      values.add(BigDecimal.valueOf(-100L));
      values.add(BigDecimal.valueOf(100L));
      break;
    case VARCHAR:
      values.add(new NlsString("", null, null));
      values.add(new NlsString("hello", null, null));
      break;
    case TIMESTAMP:
      values.add(0L); // 1970-01-01 00:00:00
      break;
    case DATE:
      values.add(0); // 1970-01-01
      values.add(365); // 1971-01-01
      values.add(-365); // 1969-01-01
      break;
    case TIME:
      values.add(0); // 00:00:00.000
      values.add(86_399_000); // 23:59:59.000
      break;
    default:
      throw new AssertionError("don't know values for " + variable
          + " of type " + variable.getType());
    }
    if (variable.getType().isNullable()) {
      values.add(NullSentinel.INSTANCE);
    }
    return values.build();
  }

  /** Collects the variables (or other bindable sites) in an expression, and
   * counts features (such as CAST) that {@link RexInterpreter} cannot
   * handle. */
  private static class VariableCollector extends RexVisitorImpl {
    private final Set builder = new LinkedHashSet<>();
    private int unsupportedCount = 0;

    VariableCollector() {
      super(true);
    }

    @Override public Void visitInputRef(RexInputRef inputRef) {
      builder.add(inputRef);
      return super.visitInputRef(inputRef);
    }

    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      if (fieldAccess.getReferenceExpr() instanceof RexDynamicParam) {
        builder.add(fieldAccess);
        return null;
      } else {
        return super.visitFieldAccess(fieldAccess);
      }
    }

    @Override public Void visitCall(RexCall call) {
      if (!RexInterpreter.SUPPORTED_SQL_KIND.contains(call.getKind())) {
        ++unsupportedCount;
        return null;
      }
      return super.visitCall(call);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy