Java Code Examples for org.apache.calcite.jdbc.JavaTypeFactoryImpl

The following examples show how to use org.apache.calcite.jdbc.JavaTypeFactoryImpl. These examples are extracted from open source projects. 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 Project: kareldb   Source File: Schema.java    License: Apache License 2.0 6 votes vote down vote up
public static RelDef toRowType(LinkedHashMap<String, ColumnDef> columnTypes, List<String> keyFields) {
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl();
    List<RelDataType> types = new ArrayList<>();
    for (ColumnDef columnDef : columnTypes.values()) {
        final RelDataType type;
        if (columnDef == null) {
            type = typeFactory.createSqlType(SqlTypeName.VARCHAR);
        } else {
            type = columnDef.toType(typeFactory);
        }
        types.add(type);
    }
    RelDataType rowType = typeFactory.createStructType(Pair.zip(new ArrayList<>(columnTypes.keySet()), types));
    List<ColumnStrategy> strategies = columnTypes.values().stream()
        .map(ColumnDef::getColumnStrategy)
        .collect(Collectors.toList());
    return new RelDef(rowType, keyFields, strategies);
}
 
Example 2
Source Project: samza   Source File: SamzaSqlJavaTypeFactoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static RelDataType convertToSql(final RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelRecordType) {
    return typeFactory.createStructType(
        Lists.transform(type.getFieldList(), a0 -> convertToSql(typeFactory, a0.getType())),
        type.getFieldNames());
  }
  if (type instanceof JavaType) {
    SqlTypeName typeName = JavaToSqlTypeConversionRules.instance().lookup(((JavaType) type).getJavaClass());
    // For unknown sql type names, return ANY sql type to make Calcite validation not fail.
    if (typeName == null) {
      typeName = SqlTypeName.ANY;
    }
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(typeName),
        type.isNullable());
  } else {
    return JavaTypeFactoryImpl.toSql(typeFactory, type);
  }
}
 
Example 3
Source Project: quark   Source File: QuarkConnectionImpl.java    License: Apache License 2.0 6 votes vote down vote up
protected QuarkConnectionImpl(QuarkDriver driver, AvaticaFactory factory, String url,
                              Properties info, CalciteRootSchema rootSchema,
                              JavaTypeFactory typeFactory) throws SQLException {
  super(driver, factory, url, info);

  CalciteConnectionConfig cfg = new CalciteConnectionConfigImpl(info);

  if (typeFactory != null) {
    this.typeFactory = typeFactory;
  } else {
    final RelDataTypeSystem typeSystem =
        cfg.typeSystem(RelDataTypeSystem.class, RelDataTypeSystem.DEFAULT);
    this.typeFactory = new JavaTypeFactoryImpl(typeSystem);
  }

  this.properties.put(InternalProperty.CASE_SENSITIVE, cfg.caseSensitive());
  this.properties.put(InternalProperty.UNQUOTED_CASING, cfg.unquotedCasing());
  this.properties.put(InternalProperty.QUOTED_CASING, cfg.quotedCasing());
  this.properties.put(InternalProperty.QUOTING, cfg.quoting());
}
 
Example 4
Source Project: calcite   Source File: PermutationTestCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testProjectPermutation() {
  final RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl();
  final RexBuilder builder = new RexBuilder(typeFactory);
  final RelDataType doubleType =
      typeFactory.createSqlType(SqlTypeName.DOUBLE);

  // A project with [1, 1] is not a permutation, so should return null
  final Permutation perm = Project.getPermutation(2,
      ImmutableList.of(builder.makeInputRef(doubleType, 1),
          builder.makeInputRef(doubleType, 1)));
  assertThat(perm, nullValue());

  // A project with [0, 1, 0] is not a permutation, so should return null
  final Permutation perm1 = Project.getPermutation(2,
      ImmutableList.of(builder.makeInputRef(doubleType, 0),
          builder.makeInputRef(doubleType, 1),
          builder.makeInputRef(doubleType, 0)));
  assertThat(perm1, nullValue());

  // A project of [1, 0] is a valid permutation!
  final Permutation perm2 = Project.getPermutation(2,
      ImmutableList.of(builder.makeInputRef(doubleType, 1),
          builder.makeInputRef(doubleType, 0)));
  assertThat(perm2, is(new Permutation(new int[]{1, 0})));
}
 
Example 5
Source Project: calcite   Source File: RexTransformerTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeEach public void setUp() {
  typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  rexBuilder = new RexBuilder(typeFactory);
  boolRelDataType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);

  x = new RexInputRef(
      0,
      typeFactory.createTypeWithNullability(boolRelDataType, true));
  y = new RexInputRef(
      1,
      typeFactory.createTypeWithNullability(boolRelDataType, true));
  z = new RexInputRef(
      2,
      typeFactory.createTypeWithNullability(boolRelDataType, true));
  trueRex = rexBuilder.makeLiteral(true);
  falseRex = rexBuilder.makeLiteral(false);
}
 
Example 6
Source Project: calcite   Source File: AbstractMaterializedViewTest.java    License: Apache License 2.0 6 votes vote down vote up
private RelNode toRel(RelOptCluster cluster, SchemaPlus rootSchema,
    SchemaPlus defaultSchema, String sql) throws SqlParseException {
  final SqlParser parser = SqlParser.create(sql, SqlParser.Config.DEFAULT);
  final SqlNode parsed = parser.parseStmt();

  final CalciteCatalogReader catalogReader = new CalciteCatalogReader(
      CalciteSchema.from(rootSchema),
      CalciteSchema.from(defaultSchema).path(null),
      new JavaTypeFactoryImpl(), new CalciteConnectionConfigImpl(new Properties()));

  final SqlValidator validator = new ValidatorForTest(SqlStdOperatorTable.instance(),
      catalogReader, new JavaTypeFactoryImpl(), SqlConformanceEnum.DEFAULT);
  final SqlNode validated = validator.validate(parsed);
  final SqlToRelConverter.Config config = SqlToRelConverter.configBuilder()
      .withTrimUnusedFields(true)
      .withExpand(true)
      .withDecorrelationEnabled(true)
      .build();
  final SqlToRelConverter converter = new SqlToRelConverter(
      (rowType, queryString, schemaPath, viewPath) -> {
        throw new UnsupportedOperationException("cannot expand view");
      }, validator, catalogReader, cluster, StandardConvertletTable.INSTANCE, config);
  return converter.convertQuery(validated, false, true).rel;
}
 
Example 7
Source Project: marble   Source File: HiveRexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
private String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.HIVE;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 8
Source Project: marble   Source File: HiveRexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link RexExecutable} that allows to apply the
 * generated code during query processing (filter, projection).
 *
 * @param rexBuilder Rex builder
 * @param exps Expressions
 * @param rowType describes the structure of the input row.
 */
public RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> exps,
    RelDataType rowType) {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final RexToLixTranslator.InputGetter getter = new DataContextInputGetter(rowType, typeFactory);
  final String code = compile(rexBuilder, exps, getter, rowType);
  return new RexExecutable(code, "generated Rex code");
}
 
Example 9
Source Project: streamline   Source File: RexNodeToJavaCodeCompiler.java    License: Apache License 2.0 5 votes vote down vote up
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_,
                                      ParameterExpression outputValues_) {
  RelDataType inputRowType = program.getInputRowType();
  final BlockBuilder builder = new BlockBuilder();
  final JavaTypeFactoryImpl javaTypeFactory =
          new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());

  final RexToLixTranslator.InputGetter inputGetter =
          new RexToLixTranslator.InputGetterImpl(
                  ImmutableList.of(
                          Pair.<Expression, PhysType>of(
                                  Expressions.field(context_,
                                          BuiltInMethod.CONTEXT_VALUES.field),
                                  PhysTypeImpl.of(javaTypeFactory, inputRowType,
                                          JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates =
          new Function1<String, RexToLixTranslator.InputGetter>() {
            public RexToLixTranslator.InputGetter apply(String a0) {
              throw new UnsupportedOperationException();
            }
          };
  final Expression root =
          Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
  final List<Expression> list =
          RexToLixTranslator.translateProjects(program, javaTypeFactory, builder,
                  null, root, inputGetter, correlates);
  for (int i = 0; i < list.size(); i++) {
    builder.add(
            Expressions.statement(
                    Expressions.assign(
                            Expressions.arrayIndex(outputValues_,
                                    Expressions.constant(i)),
                            list.get(i))));
  }

  return builder;
}
 
Example 10
Source Project: streamline   Source File: TestCompilerUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static CalciteState sqlOverDummyTable(String sql)
        throws RelConversionException, ValidationException, SqlParseException {
    SchemaPlus schema = Frameworks.createRootSchema(true);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl
            (RelDataTypeSystem.DEFAULT);
    StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory)
            .field("ID", SqlTypeName.INTEGER)
            .field("NAME", typeFactory.createType(String.class))
            .field("ADDR", typeFactory.createType(String.class))
            .build();
    Table table = streamableTable.stream();
    schema.add("FOO", table);
    schema.add("BAR", table);
    schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));

    List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
    sqlOperatorTables.add(SqlStdOperatorTable.instance());
    sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema),
            false,
            Collections.<String>emptyList(), typeFactory));
    SqlOperatorTable chainedSqlOperatorTable = new ChainedSqlOperatorTable(sqlOperatorTables);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(
            schema).operatorTable(chainedSqlOperatorTable).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode tree = planner.convert(validate);
    System.out.println(RelOptUtil.toString(tree, SqlExplainLevel.ALL_ATTRIBUTES));
    return new CalciteState(schema, tree);
}
 
Example 11
Source Project: calcite   Source File: CalciteCatalogReader.java    License: Apache License 2.0 5 votes vote down vote up
private static RelDataType toSql(RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example 12
Source Project: calcite   Source File: PlannerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void ready() {
  switch (state) {
  case STATE_0_CLOSED:
    reset();
  }
  ensure(State.STATE_1_RESET);

  RelDataTypeSystem typeSystem =
      connectionConfig.typeSystem(RelDataTypeSystem.class,
          RelDataTypeSystem.DEFAULT);
  typeFactory = new JavaTypeFactoryImpl(typeSystem);
  planner = new VolcanoPlanner(costFactory, context);
  RelOptUtil.registerDefaultRules(planner,
      connectionConfig.materializationsEnabled(),
      Hook.ENABLE_BINDABLE.get(false));
  planner.setExecutor(executor);

  state = State.STATE_2_READY;

  // If user specify own traitDef, instead of default default trait,
  // register the trait def specified in traitDefs.
  if (this.traitDefs == null) {
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    if (CalciteSystemProperty.ENABLE_COLLATION_TRAIT.value()) {
      planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    }
  } else {
    for (RelTraitDef def : this.traitDefs) {
      planner.addRelTraitDef(def);
    }
  }
}
 
Example 13
Source Project: calcite   Source File: EnumerableAggregateBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void declareParentAccumulator(List<Expression> initExpressions,
    BlockBuilder initBlock, PhysType accPhysType) {
  if (accPhysType.getJavaRowType()
      instanceof JavaTypeFactoryImpl.SyntheticRecordType) {
    // We have to initialize the SyntheticRecordType instance this way, to
    // avoid using a class constructor with too many parameters.
    final JavaTypeFactoryImpl.SyntheticRecordType synType =
        (JavaTypeFactoryImpl.SyntheticRecordType)
            accPhysType.getJavaRowType();
    final ParameterExpression record0_ =
        Expressions.parameter(accPhysType.getJavaRowType(), "record0");
    initBlock.add(Expressions.declare(0, record0_, null));
    initBlock.add(
        Expressions.statement(
            Expressions.assign(record0_,
                Expressions.new_(accPhysType.getJavaRowType()))));
    List<Types.RecordField> fieldList = synType.getRecordFields();
    for (int i = 0; i < initExpressions.size(); i++) {
      Expression right = initExpressions.get(i);
      initBlock.add(
          Expressions.statement(
              Expressions.assign(
                  Expressions.field(record0_, fieldList.get(i)), right)));
    }
    initBlock.add(record0_);
  } else {
    initBlock.add(accPhysType.record(initExpressions));
  }
}
 
Example 14
Source Project: calcite   Source File: EnumerableRelImplementor.java    License: Apache License 2.0 5 votes vote down vote up
private void register(Type type) {
  if (!seen.add(type)) {
    return;
  }
  if (type instanceof JavaTypeFactoryImpl.SyntheticRecordType) {
    memberDeclarations.add(
        classDecl((JavaTypeFactoryImpl.SyntheticRecordType) type));
  }
  if (type instanceof ParameterizedType) {
    for (Type type1 : ((ParameterizedType) type).getActualTypeArguments()) {
      register(type1);
    }
  }
}
 
Example 15
Source Project: calcite   Source File: SqlUserDefinedAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
private RelDataType toSql(RelDataType type) {
  if (type instanceof RelDataTypeFactoryImpl.JavaType
      && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
      == Object.class) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  }
  return JavaTypeFactoryImpl.toSql(typeFactory, type);
}
 
Example 16
Source Project: calcite   Source File: RexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example 17
Source Project: calcite   Source File: RexExecutorImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link RexExecutable} that allows to apply the
 * generated code during query processing (filter, projection).
 *
 * @param rexBuilder Rex builder
 * @param exps Expressions
 * @param rowType describes the structure of the input row.
 */
public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> exps,
    RelDataType rowType) {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final InputGetter getter = new DataContextInputGetter(rowType, typeFactory);
  final String code = compile(rexBuilder, exps, getter, rowType);
  return new RexExecutable(code, "generated Rex code");
}
 
Example 18
Source Project: calcite   Source File: ArrayTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testLoadSorted() {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType rowType =
      typeFactory.builder()
          .add("empid", typeFactory.createType(int.class))
          .add("deptno", typeFactory.createType(int.class))
          .add("name", typeFactory.createType(String.class))
          .build();
  final Enumerable<Object[]> enumerable =
      Linq4j.asEnumerable(
          Arrays.asList(
              new Object[]{100, 10, "Bill"},
              new Object[]{200, 20, "Eric"},
              new Object[]{150, 10, "Sebastian"},
              new Object[]{160, 10, "Theodore"}));
  final ColumnLoader<Object[]> loader =
      new ColumnLoader<Object[]>(typeFactory, enumerable,
          RelDataTypeImpl.proto(rowType), null);
  checkColumn(
      loader.representationValues.get(0),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=0, bitCount=8, primitive=INT, signed=false), value=[100, 150, 160, 200, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(1),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=1, bitCount=5, primitive=INT, signed=false), value=[10, 10, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(2),
      ArrayTable.RepresentationType.OBJECT_ARRAY,
      "Column(representation=ObjectArray(ordinal=2), value=[Bill, Sebastian, Theodore, Eric])");
}
 
Example 19
Source Project: calcite   Source File: ArrayTableTest.java    License: Apache License 2.0 5 votes vote down vote up
/** As {@link #testLoadSorted()} but column #1 is the unique column, not
 * column #0. The algorithm needs to go back and permute the values of
 * column #0 after it discovers that column #1 is unique and sorts by it. */
@Test void testLoadSorted2() {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType rowType =
      typeFactory.builder()
          .add("deptno", typeFactory.createType(int.class))
          .add("empid", typeFactory.createType(int.class))
          .add("name", typeFactory.createType(String.class))
          .build();
  final Enumerable<Object[]> enumerable =
      Linq4j.asEnumerable(
          Arrays.asList(
              new Object[]{10, 100, "Bill"},
              new Object[]{20, 200, "Eric"},
              new Object[]{30, 150, "Sebastian"},
              new Object[]{10, 160, "Theodore"}));
  final ColumnLoader<Object[]> loader =
      new ColumnLoader<Object[]>(typeFactory, enumerable,
          RelDataTypeImpl.proto(rowType), null);
  // Note that values have been sorted with {20, 200, Eric} last because the
  // value 200 is the highest value of empid, the unique column.
  checkColumn(
      loader.representationValues.get(0),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=0, bitCount=5, primitive=INT, signed=false), value=[10, 30, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(1),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=1, bitCount=8, primitive=INT, signed=false), value=[100, 150, 160, 200, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(2),
      ArrayTable.RepresentationType.OBJECT_ARRAY,
      "Column(representation=ObjectArray(ordinal=2), value=[Bill, Sebastian, Theodore, Eric])");
}
 
Example 20
Source Project: calcite   Source File: SqlOperatorBindingTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {
  JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  integerDataType = typeFactory.createSqlType(SqlTypeName.INTEGER);
  integerType = SqlTypeUtil.convertTypeToSpec(integerDataType);
  rexBuilder = new RexBuilder(typeFactory);
}
 
Example 21
Source Project: calcite   Source File: RexProgramBuilderBase.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeEach public void setUp() {
  typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  rexBuilder = new RexBuilder(typeFactory);
  executor =
      new RexExecutorImpl(new DummyTestDataContext());
  simplify =
      new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor)
          .withParanoid(true);
  trueLiteral = rexBuilder.makeLiteral(true);
  falseLiteral = rexBuilder.makeLiteral(false);

  nonNullableInt = typeFactory.createSqlType(SqlTypeName.INTEGER);
  nullableInt = typeFactory.createTypeWithNullability(nonNullableInt, true);
  nullInt = rexBuilder.makeNullLiteral(nullableInt);

  nonNullableSmallInt = typeFactory.createSqlType(SqlTypeName.SMALLINT);
  nullableSmallInt = typeFactory.createTypeWithNullability(nonNullableSmallInt, true);
  nullSmallInt = rexBuilder.makeNullLiteral(nullableSmallInt);

  nonNullableBool = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
  nullableBool = typeFactory.createTypeWithNullability(nonNullableBool, true);
  nullBool = rexBuilder.makeNullLiteral(nullableBool);

  nonNullableVarchar = typeFactory.createSqlType(SqlTypeName.VARCHAR);
  nullableVarchar = typeFactory.createTypeWithNullability(nonNullableVarchar, true);
  nullVarchar = rexBuilder.makeNullLiteral(nullableVarchar);

  nonNullableDecimal = typeFactory.createSqlType(SqlTypeName.DECIMAL);
  nullableDecimal = typeFactory.createTypeWithNullability(nonNullableDecimal, true);
  nullDecimal = rexBuilder.makeNullLiteral(nullableDecimal);
}
 
Example 22
Source Project: marble   Source File: TableEnv.java    License: Apache License 2.0 4 votes vote down vote up
public TableEnv(TableConfig tableConfig) {
  try {
    this.tableConfig = tableConfig;
    SqlParser.Config sqlParserConfig = tableConfig.getSqlParserConfig()
        != null ? tableConfig.getSqlParserConfig() : SqlParser
        .configBuilder().setCaseSensitive(false)
        .build();
    SqlOperatorTable sqlStdOperatorTable = tableConfig
        .getSqlOperatorTable()
        != null
        ? tableConfig.getSqlOperatorTable()
        : ChainedSqlOperatorTable.of(SqlStdOperatorTable.instance());
    CalciteConnectionConfig calciteConnectionConfig = tableConfig
        .getCalciteConnectionConfig()
        != null
        ? tableConfig.getCalciteConnectionConfig()
        : createDefaultConnectionConfig(sqlParserConfig);
    RelDataTypeSystem typeSystem = tableConfig.getRelDataTypeSystem() != null
        ? tableConfig.getRelDataTypeSystem()
        : calciteConnectionConfig.typeSystem(RelDataTypeSystem.class,
            RelDataTypeSystem.DEFAULT);
    SqlRexConvertletTable convertletTable = tableConfig
        .getConvertletTable()
        != null
        ? tableConfig
        .getConvertletTable()
        : StandardConvertletTable.INSTANCE;
    RexExecutor rexExecutor = tableConfig.getRexExecutor() != null
        ? tableConfig.getRexExecutor()
        : RexUtil.EXECUTOR;
    this.calciteCatalogReader = new CalciteCatalogReader(
        CalciteSchema.from(rootSchema),
        CalciteSchema.from(rootSchema).path(null),
        new JavaTypeFactoryImpl(typeSystem),
        calciteConnectionConfig);
    this.frameworkConfig = createFrameworkConfig(sqlParserConfig,
        ChainedSqlOperatorTable.of(sqlStdOperatorTable,
            calciteCatalogReader), convertletTable,
        calciteConnectionConfig, typeSystem, rexExecutor);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 23
Source Project: attic-apex-malhar   Source File: SQLExecEnvironment.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Construct SQL Execution Environment which works on given DAG objec.
 */
private SQLExecEnvironment()
{
  this.typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
}
 
Example 24
Source Project: streamline   Source File: TestCompilerUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static CalciteState sqlOverNestedTable(String sql)
        throws RelConversionException, ValidationException, SqlParseException {
    SchemaPlus schema = Frameworks.createRootSchema(true);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl
            (RelDataTypeSystem.DEFAULT);

    StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory)
            .field("ID", SqlTypeName.INTEGER)
            .field("MAPFIELD",
                    typeFactory.createTypeWithNullability(
                            typeFactory.createMapType(
                                    typeFactory.createTypeWithNullability(
                                            typeFactory.createSqlType(SqlTypeName.VARCHAR), true),
                                    typeFactory.createTypeWithNullability(
                                            typeFactory.createSqlType(SqlTypeName.INTEGER), true))
                            , true))
            .field("NESTEDMAPFIELD",
                    typeFactory.createTypeWithNullability(
                        typeFactory.createMapType(
                                typeFactory.createTypeWithNullability(
                                        typeFactory.createSqlType(SqlTypeName.VARCHAR), true),
                                typeFactory.createTypeWithNullability(
                                        typeFactory.createMapType(
                                                typeFactory.createTypeWithNullability(
                                                        typeFactory.createSqlType(SqlTypeName.VARCHAR), true),
                                                typeFactory.createTypeWithNullability(
                                                        typeFactory.createSqlType(SqlTypeName.INTEGER), true))
                                        , true))
                                    , true))
            .field("ARRAYFIELD", typeFactory.createTypeWithNullability(
                    typeFactory.createArrayType(
                        typeFactory.createTypeWithNullability(
                            typeFactory.createSqlType(SqlTypeName.INTEGER), true), -1L)
                    , true))
            .build();
    Table table = streamableTable.stream();
    schema.add("FOO", table);
    schema.add("BAR", table);
    schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));
    List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
    sqlOperatorTables.add(SqlStdOperatorTable.instance());
    sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema),
                                                   false,
                                                   Collections.<String>emptyList(), typeFactory));
    SqlOperatorTable chainedSqlOperatorTable = new ChainedSqlOperatorTable(sqlOperatorTables);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(
            schema).operatorTable(chainedSqlOperatorTable).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode tree = planner.convert(validate);
    System.out.println(RelOptUtil.toString(tree, SqlExplainLevel.ALL_ATTRIBUTES));
    return new CalciteState(schema, tree);
}
 
Example 25
Source Project: quark   Source File: PlanExecutor.java    License: Apache License 2.0 4 votes vote down vote up
private RelDataTypeFactoryImpl.JavaType getIntegerJavaType() {
  RelDataTypeFactoryImpl relDataTypeFactoryImpl = new JavaTypeFactoryImpl();
  return relDataTypeFactoryImpl.new JavaType(Integer.class);
}
 
Example 26
Source Project: quark   Source File: PlanExecutor.java    License: Apache License 2.0 4 votes vote down vote up
private RelDataTypeFactoryImpl.JavaType getStringJavaType() {
  RelDataTypeFactoryImpl relDataTypeFactoryImpl = new JavaTypeFactoryImpl();
  return relDataTypeFactoryImpl.new JavaType(String.class,
      !(String.class.isPrimitive()), Util.getDefaultCharset(), null);
}
 
Example 27
Source Project: calcite   Source File: CodeGenerationBenchmark.java    License: Apache License 2.0 4 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
  planInfos = new PlanInfo[queries];
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRule(FilterToCalcRule.INSTANCE);
  planner.addRule(ProjectToCalcRule.INSTANCE);
  planner.addRule(EnumerableRules.ENUMERABLE_CALC_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_VALUES_RULE);

  RelDataTypeFactory typeFactory =
      new JavaTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
  RelTraitSet desiredTraits =
      cluster.traitSet().replace(EnumerableConvention.INSTANCE);

  RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
  // Generates queries of the following form depending on the configuration parameters.
  // SELECT `t`.`name`
  // FROM (VALUES  (1, 'Value0')) AS `t` (`id`, `name`)
  // INNER JOIN (VALUES  (1, 'Value1')) AS `t` (`id`, `name`) AS `t0` ON `t`.`id` = `t0`.`id`
  // INNER JOIN (VALUES  (2, 'Value2')) AS `t` (`id`, `name`) AS `t1` ON `t`.`id` = `t1`.`id`
  // INNER JOIN (VALUES  (3, 'Value3')) AS `t` (`id`, `name`) AS `t2` ON `t`.`id` = `t2`.`id`
  // INNER JOIN ...
  // WHERE
  //  `t`.`name` = 'name0' OR
  //  `t`.`name` = 'name1' OR
  //  `t`.`name` = 'name2' OR
  //  ...
  //  OR `t`.`id` = 0
  // The last disjunction (i.e, t.id = $i) is what makes the queries different from one another
  // by assigning a different constant literal.
  for (int i = 0; i < queries; i++) {
    relBuilder.values(new String[]{"id", "name"}, 1, "Value" + 0);
    for (int j = 1; j <= joins; j++) {
      relBuilder
          .values(new String[]{"id", "name"}, j, "Value" + j)
          .join(JoinRelType.INNER, "id");
    }

    List<RexNode> disjunctions = new ArrayList<>();
    for (int j = 0; j < whereClauseDisjunctions; j++) {
      disjunctions.add(
          relBuilder.equals(
              relBuilder.field("name"),
              relBuilder.literal("name" + j)));
    }
    disjunctions.add(
        relBuilder.equals(
            relBuilder.field("id"),
            relBuilder.literal(i)));
    RelNode query =
        relBuilder
            .filter(relBuilder.or(disjunctions))
            .project(relBuilder.field("name"))
            .build();

    RelNode query0 = planner.changeTraits(query, desiredTraits);
    planner.setRoot(query0);

    PlanInfo info = new PlanInfo();
    EnumerableRel plan = (EnumerableRel) planner.findBestExp();

    EnumerableRelImplementor relImplementor =
        new EnumerableRelImplementor(plan.getCluster().getRexBuilder(), new HashMap<>());
    info.classExpr = relImplementor.implementRoot(plan, EnumerableRel.Prefer.ARRAY);
    info.javaCode =
        Expressions.toString(info.classExpr.memberDeclarations, "\n", false);

    ICompilerFactory compilerFactory;
    try {
      compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
      throw new IllegalStateException(
          "Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(info.classExpr.name);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(
        plan.getRowType().getFieldCount() == 1
            ? new Class[]{Bindable.class, Typed.class}
            : new Class[]{ArrayBindable.class});
    cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
    info.cbe = cbe;
    planInfos[i] = info;
  }

}
 
Example 28
Source Project: calcite   Source File: JaninoRexCompiler.java    License: Apache License 2.0 4 votes vote down vote up
public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  for (RexNode node : nodes) {
    programBuilder.addProject(node, null);
  }
  final RexProgram program = programBuilder.getProgram();

  final BlockBuilder builder = new BlockBuilder();
  final ParameterExpression context_ =
      Expressions.parameter(Context.class, "context");
  final ParameterExpression outputValues_ =
      Expressions.parameter(Object[].class, "outputValues");
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());

  // public void execute(Context, Object[] outputValues)
  final RexToLixTranslator.InputGetter inputGetter =
      new RexToLixTranslator.InputGetterImpl(
          ImmutableList.of(
              Pair.of(
                  Expressions.field(context_,
                      BuiltInMethod.CONTEXT_VALUES.field),
                  PhysTypeImpl.of(javaTypeFactory, inputRowType,
                      JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates = a0 -> {
    throw new UnsupportedOperationException();
  };
  final Expression root =
      Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
  final SqlConformance conformance =
      SqlConformanceEnum.DEFAULT; // TODO: get this from implementor
  final List<Expression> list =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, builder, null, root, inputGetter, correlates);
  for (int i = 0; i < list.size(); i++) {
    builder.add(
        Expressions.statement(
            Expressions.assign(
                Expressions.arrayIndex(outputValues_,
                    Expressions.constant(i)),
                list.get(i))));
  }
  return baz(context_, outputValues_, builder.toBlock());
}
 
Example 29
Source Project: calcite   Source File: MockCatalogReader.java    License: Apache License 2.0 4 votes vote down vote up
MockDynamicTable(String catalogName, String schemaName, String name) {
  super(Object.class);
  this.names = Arrays.asList(catalogName, schemaName, name);
  this.rowType = new DynamicRecordTypeImpl(new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT));
}
 
Example 30
Source Project: calcite   Source File: RexImplicationCheckerTest.java    License: Apache License 2.0 4 votes vote down vote up
public Fixture() {
  typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  rexBuilder = new RexBuilder(typeFactory);
  boolRelDataType = typeFactory.createJavaType(Boolean.class);
  intRelDataType = typeFactory.createJavaType(Integer.class);
  decRelDataType = typeFactory.createJavaType(Double.class);
  longRelDataType = typeFactory.createJavaType(Long.class);
  shortDataType = typeFactory.createJavaType(Short.class);
  byteDataType = typeFactory.createJavaType(Byte.class);
  floatDataType = typeFactory.createJavaType(Float.class);
  charDataType = typeFactory.createJavaType(Character.class);
  dateDataType = typeFactory.createJavaType(Date.class);
  timestampDataType = typeFactory.createJavaType(Timestamp.class);
  timeDataType = typeFactory.createJavaType(Time.class);
  stringDataType = typeFactory.createJavaType(String.class);

  bl = ref(0, this.boolRelDataType);
  i = ref(1, intRelDataType);
  dec = ref(2, decRelDataType);
  lg = ref(3, longRelDataType);
  sh = ref(4, shortDataType);
  by = ref(5, byteDataType);
  fl = ref(6, floatDataType);
  ch = ref(7, charDataType);
  d = ref(8, dateDataType);
  ts = ref(9, timestampDataType);
  t = ref(10, timeDataType);
  str = ref(11, stringDataType);

  rowType = typeFactory.builder()
      .add("bool", this.boolRelDataType)
      .add("int", intRelDataType)
      .add("dec", decRelDataType)
      .add("long", longRelDataType)
      .add("short", shortDataType)
      .add("byte", byteDataType)
      .add("float", floatDataType)
      .add("char", charDataType)
      .add("date", dateDataType)
      .add("timestamp", timestampDataType)
      .add("time", timeDataType)
      .add("string", stringDataType)
      .build();

  executor = Frameworks.withPrepare(
      (cluster, relOptSchema, rootSchema, statement) ->
          new RexExecutorImpl(
              Schemas.createDataContext(statement.getConnection(),
                  rootSchema)));
  simplify =
      new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor)
          .withParanoid(true);
  checker = new RexImplicationChecker(rexBuilder, executor, rowType);
}