org.apache.calcite.DataContext Java Examples

The following examples show how to use org.apache.calcite.DataContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RexExecutable.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static Function1<DataContext, Object[]> compile(String code,
    Object reason) {
  try {
    final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(GENERATED_CLASS_NAME);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class});
    cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
    cbe.cook(new Scanner(null, new StringReader(code)));
    Class c = cbe.getClazz();
    //noinspection unchecked
    final Constructor<Function1<DataContext, Object[]>> constructor =
        c.getConstructor();
    return constructor.newInstance();
  } catch (CompileException | IOException | InstantiationException
      | IllegalAccessException | InvocationTargetException
      | NoSuchMethodException e) {
    throw new RuntimeException("While compiling " + reason, e);
  }
}
 
Example #2
Source File: RexImplicationChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean isSatisfiable(RexNode second, DataContext dataValues) {
  if (dataValues == null) {
    return false;
  }

  ImmutableList<RexNode> constExps = ImmutableList.of(second);
  final RexExecutable exec = RexExecutorImpl.getExecutable(builder, constExps, rowType);

  Object[] result;
  exec.setDataContext(dataValues);
  try {
    result = exec.execute();
  } catch (Exception e) {
    // TODO: CheckSupport should not allow this exception to be thrown
    // Need to monitor it and handle all the cases raising them.
    LOGGER.warn("Exception thrown while checking if => {}: {}", second, e.getMessage());
    return false;
  }
  return result != null
      && result.length == 1
      && result[0] instanceof Boolean
      && (Boolean) result[0];
}
 
Example #3
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
protected MetaResultSet createResultSet(
    Map<String, Object> internalParameters, List<ColumnMetaData> columns,
    CursorFactory cursorFactory, final Frame firstFrame) {
  try {
    final QuarkConnectionImpl connection = getConnection();
    final AvaticaStatement statement = connection.createStatement();
    final CalcitePrepare.CalciteSignature<Object> signature =
        new CalcitePrepare.CalciteSignature<Object>("",
            ImmutableList.<AvaticaParameter>of(), internalParameters, null,
            columns, cursorFactory, null, ImmutableList.<RelCollation>of(), -1,
            null, Meta.StatementType.SELECT) {
          @Override public Enumerable<Object> enumerable(
              DataContext dataContext) {
            return Linq4j.asEnumerable(firstFrame.rows);
          }
        };
    return MetaResultSet.create(connection.id, statement.getId(), true,
        signature, firstFrame);
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #4
Source File: VisitorDataContext.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static DataContext of(RelDataType rowType,
    List<Pair<RexInputRef, RexNode>> usageList) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  for (Pair<RexInputRef, RexNode> elem : usageList) {
    Pair<Integer, ?> value = getValue(elem.getKey(), elem.getValue());
    if (value == null) {
      LOGGER.warn("{} is not handled for {} for checking implication",
          elem.getKey(), elem.getValue());
      return null;
    }
    int index = value.getKey();
    values[index] = value.getValue();
  }
  return new VisitorDataContext(values);
}
 
Example #5
Source File: OLAPContext.java    From kylin with Apache License 2.0 6 votes vote down vote up
private void bindVariable(TupleFilter filter, DataContext dataContext) {
    if (filter == null) {
        return;
    }

    for (TupleFilter childFilter : filter.getChildren()) {
        bindVariable(childFilter, dataContext);
    }

    if (filter instanceof CompareTupleFilter && dataContext != null) {
        CompareTupleFilter compFilter = (CompareTupleFilter) filter;
        for (Map.Entry<String, Object> entry : compFilter.getVariables().entrySet()) {
            String variable = entry.getKey();
            Object value = dataContext.get(variable);
            if (value != null) {
                String str = value.toString();
                str = transferDateTimeColumnToMillis(compFilter, str);
                compFilter.clearPreviousVariableValues(variable);
                compFilter.bindVariable(variable, str);
            }

        }
    }
}
 
Example #6
Source File: RexExecutable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Function1<DataContext, Object[]> compile(String code,
    Object reason) {
  try {
    final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(GENERATED_CLASS_NAME);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class});
    cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
    cbe.cook(new Scanner(null, new StringReader(code)));
    Class c = cbe.getClazz();
    //noinspection unchecked
    final Constructor<Function1<DataContext, Object[]>> constructor =
        c.getConstructor();
    return constructor.newInstance();
  } catch (CompileException | IOException | InstantiationException
      | IllegalAccessException | InvocationTargetException
      | NoSuchMethodException e) {
    throw new RuntimeException("While compiling " + reason, e);
  }
}
 
Example #7
Source File: JdbcCatalogSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static JdbcCatalogSchema create(
    SchemaPlus parentSchema,
    String name,
    DataSource dataSource,
    SqlDialectFactory dialectFactory,
    String catalog) {
  final Expression expression =
      parentSchema != null
          ? Schemas.subSchemaExpression(parentSchema, name,
              JdbcCatalogSchema.class)
          : Expressions.call(DataContext.ROOT,
              BuiltInMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method);
  final SqlDialect dialect =
      JdbcSchema.createDialect(dialectFactory, dataSource);
  final JdbcConvention convention =
      JdbcConvention.of(dialect, expression, name);
  return new JdbcCatalogSchema(dataSource, dialect, convention, catalog);
}
 
Example #8
Source File: RexExecutorTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that for a given context operator,
 * the correct value is retrieved from the {@link DataContext}.
 *
 * @param operator The Operator to check
 * @param variable The DataContext variable this operator should be bound to
 * @param value The expected value to retrieve.
 */
private void testContextLiteral(
    final SqlOperator operator,
    final DataContext.Variable variable,
    final Object value) {
  Frameworks.withPrepare((cluster, relOptSchema, rootSchema, statement) -> {
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RexExecutorImpl executor =
        new RexExecutorImpl(
            new SingleValueDataContext(variable.camelName, value));
    try {
      checkConstant(value, builder -> {
        final List<RexNode> output = new ArrayList<>();
        executor.reduce(rexBuilder,
            ImmutableList.of(rexBuilder.makeCall(operator)), output);
        return output.get(0);
      });
    } catch (Exception e) {
      throw TestUtil.rethrow(e);
    }
    return null;
  });
}
 
Example #9
Source File: TableFunctionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static CallImplementor createImplementor(final Method method) {
  return RexImpTable.createImplementor(
      new ReflectiveCallNotNullImplementor(method) {
        public Expression implement(RexToLixTranslator translator,
            RexCall call, List<Expression> translatedOperands) {
          Expression expr = super.implement(translator, call,
              translatedOperands);
          final Class<?> returnType = method.getReturnType();
          if (QueryableTable.class.isAssignableFrom(returnType)) {
            Expression queryable = Expressions.call(
                Expressions.convert_(expr, QueryableTable.class),
                BuiltInMethod.QUERYABLE_TABLE_AS_QUERYABLE.method,
                Expressions.call(DataContext.ROOT,
                    BuiltInMethod.DATA_CONTEXT_GET_QUERY_PROVIDER.method),
                Expressions.constant(null, SchemaPlus.class),
                Expressions.constant(call.getOperator().getName(), String.class));
            expr = Expressions.call(queryable,
                BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
          } else {
            expr = Expressions.call(expr,
                BuiltInMethod.SCANNABLE_TABLE_SCAN.method, DataContext.ROOT);
          }
          return expr;
        }
      }, NullPolicy.NONE, false);
}
 
Example #10
Source File: QueryEngineFactory.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object> computeSCALA(DataContext dataContext, RelNode relNode, RelDataType resultType)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    try {
        String property = System.getProperty("kylin-query-engine", "org.apache.kylin.query.runtime.SparkEngine");
        QueryEngine o = (QueryEngine) Class.forName(property).newInstance();
        return o.computeSCALA(dataContext, relNode, resultType);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw e;
    }
}
 
Example #11
Source File: VisitorDataContext.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static DataContext of(RelDataType rowType, RexNode rex) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  final List<RexNode> operands = ((RexCall) rex).getOperands();
  final RexNode firstOperand = operands.get(0);
  final RexNode secondOperand = operands.get(1);
  final Pair<Integer, ?> value = getValue(firstOperand, secondOperand);
  if (value != null) {
    int index = value.getKey();
    values[index] = value.getValue();
    return new VisitorDataContext(values);
  } else {
    return null;
  }
}
 
Example #12
Source File: GeodeSimpleScannableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new GeodeSimpleEnumerator<Object[]>(clientCache, regionName) {
        @Override public Object[] convert(Object obj) {
          Object values = convertToRowValues(relDataType.getFieldList(), obj);
          if (values instanceof Object[]) {
            return (Object[]) values;
          }
          return new Object[]{values};
        }
      };
    }
  };
}
 
Example #13
Source File: SparkExec.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> asyncResult(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }
    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    return QueryEngineFactory.computeAsync(dataContext, olapRel, rowType);
}
 
Example #14
Source File: SparkExec.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object> collectToScalarEnumerable(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }

    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    try {
        Enumerable<Object> objects = QueryEngineFactory.computeSCALA(dataContext, olapRel, rowType);
        return objects;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #15
Source File: SparkExec.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> collectToEnumerable(DataContext dataContext) {
    if (BackdoorToggles.getPrepareOnly()) {
        return Linq4j.emptyEnumerable();
    }

    OLAPRel olapRel = (OLAPRel) QueryContextFacade.current().getOlapRel();
    RelDataType rowType = (RelDataType) QueryContextFacade.current().getResultType();
    try {
        Enumerable<Object[]> computer = QueryEngineFactory.compute(dataContext, olapRel, rowType);
        return computer;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #16
Source File: CsvStreamScannableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  final List<CsvFieldType> fieldTypes = getFieldTypes(root.getTypeFactory());
  final int[] fields = CsvEnumerator.identityList(fieldTypes.size());
  final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new CsvEnumerator<>(source, cancelFlag, true, null,
          new CsvEnumerator.ArrayRowConverter(fieldTypes, fields, true));
    }
  };
}
 
Example #17
Source File: VisitorDataContext.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static DataContext of(RelDataType rowType, RexNode rex) {
  final int size = rowType.getFieldList().size();
  final Object[] values = new Object[size];
  final List<RexNode> operands = ((RexCall) rex).getOperands();
  final RexNode firstOperand = operands.get(0);
  final RexNode secondOperand = operands.get(1);
  final Pair<Integer, ?> value = getValue(firstOperand, secondOperand);
  if (value != null) {
    int index = value.getKey();
    values[index] = value.getValue();
    return new VisitorDataContext(values);
  } else {
    return null;
  }
}
 
Example #18
Source File: OLAPQuery.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public OLAPQuery(DataContext optiqContext, EnumeratorTypeEnum type, int ctxId) {
    this.optiqContext = optiqContext;
    this.type = type;
    this.contextId = ctxId;

    QueryContextFacade.current().addContext(ctxId, type.toString(),
            type == EnumeratorTypeEnum.OLAP);
}
 
Example #19
Source File: QueryEngineFactory.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> compute(DataContext dataContext, RelNode relNode, RelDataType resultType)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    try {
        String property = System.getProperty("kylin-query-engine", "org.apache.kylin.query.runtime.SparkEngine");
        QueryEngine o = (QueryEngine) Class.forName(property).newInstance();
        return o.compute(dataContext, relNode, resultType);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw e;
    }
}
 
Example #20
Source File: RelSchema.java    From quark with Apache License 2.0 5 votes vote down vote up
@Override
public Expression getExpression(SchemaPlus parentSchema,
                                String name) {
  return Expressions.call(
      DataContext.ROOT,
      BuiltInMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method);
}
 
Example #21
Source File: InterpretableRel.java    From calcite with Apache License 2.0 5 votes vote down vote up
public InterpreterImplementor(Compiler compiler,
    CalcitePrepare.SparkHandler spark,
    DataContext dataContext) {
  this.compiler = compiler;
  this.spark = spark;
  this.dataContext = dataContext;
}
 
Example #22
Source File: RexExecutorTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTimestampFromContext() throws Exception {
  // CURRENT_TIMESTAMP actually rounds the value to nearest second
  // and that's why we do currentTimeInMillis / 1000 * 1000
  long val = System.currentTimeMillis() / 1000 * 1000;
  testContextLiteral(SqlStdOperatorTable.CURRENT_TIMESTAMP,
      DataContext.Variable.CURRENT_TIMESTAMP, val);
}
 
Example #23
Source File: ScannableTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters,
    final int[] projects) {
  final Pair<Integer, Object> filter = getFilter(cooperative, filters);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return beatles(buf, filter, projects);
    }
  };
}
 
Example #24
Source File: ScannableTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return tens();
    }
  };
}
 
Example #25
Source File: CalciteConnectionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public DataContext createDataContext(Map<String, Object> parameterValues,
    CalciteSchema rootSchema) {
  if (config().spark()) {
    return new SlimDataContext();
  }
  return new DataContextImpl(this, parameterValues, rootSchema);
}
 
Example #26
Source File: CsvFilterableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters) {
  final List<CsvFieldType> fieldTypes = getFieldTypes(root.getTypeFactory());
  final String[] filterValues = new String[fieldTypes.size()];
  filters.removeIf(filter -> addFilter(filter, filterValues));
  final int[] fields = CsvEnumerator.identityList(fieldTypes.size());
  final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new CsvEnumerator<>(source, cancelFlag, false, filterValues,
          new CsvEnumerator.ArrayRowConverter(fieldTypes, fields));
    }
  };
}
 
Example #27
Source File: CalciteUtls.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static AtomicBoolean getCancelFlag(DataContext root) {
    AtomicBoolean tempFlag = DataContext.Variable.CANCEL_FLAG.get(root);
    if (tempFlag == null) {
        return new AtomicBoolean(false);
    } else {
        return tempFlag;
    }
}
 
Example #28
Source File: JsonScannableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new JsonEnumerator(getDataList(root.getTypeFactory()));
    }
  };
}
 
Example #29
Source File: ReflectiveSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  if (elementType == Object[].class) {
    //noinspection unchecked
    return enumerable;
  } else {
    //noinspection unchecked
    return enumerable.select(new FieldSelector((Class) elementType));
  }
}
 
Example #30
Source File: EnumerableBindable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> bind(DataContext dataContext) {
  final ImmutableMap<String, Object> map = ImmutableMap.of();
  final Bindable bindable = EnumerableInterpretable.toBindable(map, null,
      (EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY);
  final ArrayBindable arrayBindable = EnumerableInterpretable.box(bindable);
  return arrayBindable.bind(dataContext);
}