Java Code Examples for org.apache.calcite.sql.validate.SqlConformanceEnum#DEFAULT

The following examples show how to use org.apache.calcite.sql.validate.SqlConformanceEnum#DEFAULT . 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: SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns the {@link SqlConformance} that matches this dialect.
 *
 * <p>The base implementation returns its best guess, based upon
 * {@link #databaseProduct}; sub-classes may override. */
@Nonnull public SqlConformance getConformance() {
  switch (databaseProduct) {
  case UNKNOWN:
  case CALCITE:
    return SqlConformanceEnum.DEFAULT;
  case BIG_QUERY:
    return SqlConformanceEnum.BIG_QUERY;
  case MYSQL:
    return SqlConformanceEnum.MYSQL_5;
  case ORACLE:
    return SqlConformanceEnum.ORACLE_10;
  case MSSQL:
    return SqlConformanceEnum.SQL_SERVER_2008;
  default:
    return SqlConformanceEnum.PRAGMATIC_2003;
  }
}
 
Example 2
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a TesterImpl.
 *
 * @param diffRepos Diff repository
 * @param enableDecorrelate Whether to decorrelate
 * @param enableTrim Whether to trim unused fields
 * @param enableExpand Whether to expand sub-queries
 * @param catalogReaderFactory Function to create catalog reader, or null
 * @param clusterFactory Called after a cluster has been created
 */
protected TesterImpl(DiffRepository diffRepos, boolean enableDecorrelate,
    boolean enableTrim, boolean enableExpand,
    boolean enableLateDecorrelate,
    boolean enableTypeCoercion,
    SqlTestFactory.MockCatalogReaderFactory
        catalogReaderFactory,
    Function<RelOptCluster, RelOptCluster> clusterFactory) {
  this(diffRepos, enableDecorrelate, enableTrim, enableExpand,
      enableLateDecorrelate,
      enableTypeCoercion,
      catalogReaderFactory,
      clusterFactory,
      SqlToRelConverter.Config.DEFAULT,
      SqlConformanceEnum.DEFAULT,
      Contexts.empty());
}
 
Example 3
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2323">[CALCITE-2323]
 * Validator should allow alternative nullCollations for ORDER BY in
 * OVER</a>. */
@Test void testUserDefinedOrderByOver() {
  String sql = "select deptno,\n"
      + "  rank() over(partition by empno order by deptno)\n"
      + "from emp\n"
      + "order by row_number() over(partition by empno order by deptno)";
  Properties properties = new Properties();
  properties.setProperty(
      CalciteConnectionProperty.DEFAULT_NULL_COLLATION.camelName(),
      NullCollation.LOW.name());
  CalciteConnectionConfigImpl connectionConfig =
      new CalciteConnectionConfigImpl(properties);
  TesterImpl tester = new TesterImpl(getDiffRepos(), false, false, true, false, true,
      null, null, SqlToRelConverter.Config.DEFAULT,
      SqlConformanceEnum.DEFAULT, Contexts.of(connectionConfig));
  sql(sql).with(tester).ok();
}
 
Example 4
Source File: AbstractMaterializedViewTest.java    From calcite with 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 5
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static Pair<SqlNode, SqlValidator> testSqlValidator() throws Exception {
    String sql = "select * from my_schema.test where f1=1 or f2=2 order by f3 limit 2";
    sql = "select * from test";
    sql = "select * from my_schema2.test2";
    sql = "select sum(f1),max(f2) from test";

    sql = "select t1.f1,sum(Distinct f1) as sumf1 from test as t1 "
            + "where f2>20 group by f1 having f1>10 order by f1 limit 2";
    // sql = "insert into test(f1,f2,f3) values(1,2,3)";
    // sql = "update test set f1=100 where f2>10";
    // sql = "delete from test where f2>10";
    SqlNode sqlNode = parse(sql);

    SqlOperatorTable opTab = SqlStdOperatorTable.instance();
    RelDataTypeFactory typeFactory = createJavaTypeFactory();
    SqlValidatorCatalogReader catalogReader = createCalciteCatalogReader(typeFactory);
    SqlConformance conformance = SqlConformanceEnum.DEFAULT;

    List<String> names = new ArrayList<>();
    names.add("my_schema");
    names.add("test");
    catalogReader.getTable(names);

    SqlValidator sqlValidator = SqlValidatorUtil.newValidator(opTab, catalogReader, typeFactory, conformance);
    sqlNode = sqlValidator.validate(sqlNode);
    // System.out.println(sqlNode);

    sql = "insert into test(f1,f2,f3) values(1,2,3)";
    // sqlNode = parse(sql);
    // sqlNode = sqlValidator.validate(sqlNode);

    return new Pair<>(sqlNode, sqlValidator);
}
 
Example 6
Source File: SqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an empty context. Use {@link #EMPTY_CONTEXT} to reference the instance. */
private static Context emptyContext() {
  return new ContextImpl(DatabaseProduct.UNKNOWN, null, null, -1, -1,
      "'", "''", null,
      Casing.UNCHANGED, Casing.TO_UPPER, true, SqlConformanceEnum.DEFAULT,
      NullCollation.HIGH, RelDataTypeSystemImpl.DEFAULT,
      JethroDataSqlDialect.JethroInfo.EMPTY);
}
 
Example 7
Source File: RexExecutorImpl.java    From calcite with 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 8
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlTester withConformance(SqlConformance conformance) {
  if (conformance == null) {
    conformance = SqlConformanceEnum.DEFAULT;
  }
  final SqlTester tester = with("conformance", conformance);
  if (conformance instanceof SqlConformanceEnum) {
    return tester
        .withConnectionFactory(
            CalciteAssert.EMPTY_CONNECTION_FACTORY
                .with(CalciteConnectionProperty.CONFORMANCE, conformance));
  } else {
    return tester;
  }
}
 
Example 9
Source File: DrillConformance.java    From Bats with Apache License 2.0 4 votes vote down vote up
public DrillConformance() {
  super(SqlConformanceEnum.DEFAULT);
}
 
Example 10
Source File: DremioSqlConformance.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private DremioSqlConformance() {
  super(SqlConformanceEnum.DEFAULT);
}
 
Example 11
Source File: FlinkCalciteSqlValidator.java    From flink with Apache License 2.0 4 votes vote down vote up
public FlinkCalciteSqlValidator(
		SqlOperatorTable opTab,
		SqlValidatorCatalogReader catalogReader,
		RelDataTypeFactory typeFactory) {
	super(opTab, catalogReader, typeFactory, SqlConformanceEnum.DEFAULT);
}
 
Example 12
Source File: FlinkCalciteSqlValidator.java    From flink with Apache License 2.0 4 votes vote down vote up
public FlinkCalciteSqlValidator(
		SqlOperatorTable opTab,
		SqlValidatorCatalogReader catalogReader,
		RelDataTypeFactory typeFactory) {
	super(opTab, catalogReader, typeFactory, SqlConformanceEnum.DEFAULT);
}
 
Example 13
Source File: SparkRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Result implementSpark(Implementor implementor) {
  final JavaTypeFactory typeFactory = implementor.getTypeFactory();
  final BlockBuilder builder = new BlockBuilder();
  final SparkRel child = (SparkRel) getInput();

  final Result result = implementor.visitInput(this, 0, child);

  final PhysType physType =
      PhysTypeImpl.of(
          typeFactory, getRowType(), JavaRowFormat.CUSTOM);

  // final RDD<Employee> inputRdd = <<child adapter>>;
  // return inputRdd.flatMap(
  //   new FlatMapFunction<Employee, X>() {
  //          public List<X> call(Employee e) {
  //              if (!(e.empno < 10)) {
  //                  return Collections.emptyList();
  //              }
  //              return Collections.singletonList(
  //                  new X(...)));
  //          }
  //      })


  Type outputJavaType = physType.getJavaRowType();
  final Type rddType =
      Types.of(
          JavaRDD.class, outputJavaType);
  Type inputJavaType = result.physType.getJavaRowType();
  final Expression inputRdd_ =
      builder.append(
          "inputRdd",
          result.block);

  BlockBuilder builder2 = new BlockBuilder();

  final ParameterExpression e_ =
      Expressions.parameter(inputJavaType, "e");
  if (program.getCondition() != null) {
    Expression condition =
        RexToLixTranslator.translateCondition(
            program,
            typeFactory,
            builder2,
            new RexToLixTranslator.InputGetterImpl(
                Collections.singletonList(
                    Pair.of((Expression) e_, result.physType))),
            null, implementor.getConformance());
    builder2.add(
        Expressions.ifThen(
            Expressions.not(condition),
            Expressions.return_(null,
                Expressions.call(
                    BuiltInMethod.COLLECTIONS_EMPTY_LIST.method))));
  }

  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  List<Expression> expressions =
      RexToLixTranslator.translateProjects(
          program,
          typeFactory,
          conformance,
          builder2,
          null,
          DataContext.ROOT,
          new RexToLixTranslator.InputGetterImpl(
              Collections.singletonList(
                  Pair.of((Expression) e_, result.physType))),
          null);
  builder2.add(
      Expressions.return_(null,
          Expressions.convert_(
              Expressions.call(
                  BuiltInMethod.COLLECTIONS_SINGLETON_LIST.method,
                  physType.record(expressions)),
              List.class)));

  final BlockStatement callBody = builder2.toBlock();
  builder.add(
      Expressions.return_(
          null,
          Expressions.call(
              inputRdd_,
              SparkMethod.RDD_FLAT_MAP.method,
              Expressions.lambda(
                  SparkRuntime.CalciteFlatMapFunction.class,
                  callBody,
                  e_))));
  return implementor.result(physType, builder.toBlock());
}
 
Example 14
Source File: JaninoRexCompiler.java    From calcite with 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 15
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected Tester createTester() {
  return new TesterImpl(getDiffRepos(), false, false, true, false,
      true, null, null, SqlToRelConverter.Config.DEFAULT,
      SqlConformanceEnum.DEFAULT, Contexts.empty());
}