Java Code Examples for org.apache.calcite.sql.dialect.MysqlSqlDialect

The following examples show how to use org.apache.calcite.sql.dialect.MysqlSqlDialect. 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
@Override
public PlanRunner plan(List<Object> params) {
    RelNode relNode = getRelNode(params);
    RowBaseIterator run = CalciteRunners.run(MycatCalciteSupport.INSTANCE.convertToHBTText(schema), mycatCalciteDataContext, relNode);
    return new PlanRunner() {
        @Override
        public RowBaseIterator run() {
            return run;
        }

        @Override
        public List<String> explain() {
            return Explains.explain(MycatCalciteSupport.INSTANCE.convertToSql(relNode, MysqlSqlDialect.DEFAULT, false),
                    null,
                    MycatCalciteSupport.INSTANCE.dumpMetaData(relNode.getRowType()),
                    MycatCalciteSupport.INSTANCE.convertToHBTText(relNode, mycatCalciteDataContext),
                    MycatCalciteSupport.INSTANCE.convertToMycatRelNodeText(relNode, mycatCalciteDataContext));
        }

    };
}
 
Example 2
Source Project: Mycat2   Source File: CalciteUtls.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static String getFilterSQLText(List<SimpleColumnInfo> rawColumns, String schemaName, String tableName, List<RexNode> filters) {
    if (filters == null || filters.isEmpty()) {
        return "";
    }
    RexNode rexNode = RexUtil.composeConjunction(MycatCalciteSupport.INSTANCE.RexBuilder, filters);
    SqlImplementor.Context context = new SqlImplementor.Context(MysqlSqlDialect.DEFAULT, rawColumns.size()) {
        @Override
        public SqlNode field(int ordinal) {
            String fieldName = rawColumns.get(ordinal).getColumnName();
            return new SqlIdentifier(ImmutableList.of(schemaName, tableName, fieldName),
                    SqlImplementor.POS);
        }
    };
    try {
        return " where " + context.toSql(null, rexNode).toSqlString(MysqlSqlDialect.DEFAULT).getSql();
    } catch (Exception e) {
        LOGGER.warn("不能生成对应的sql", e);
    }
    return "";
}
 
Example 3
Source Project: Mycat2   Source File: MycatRelBuilder.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * todo for update
 * @param targetName
 * @param relDataType
 * @param sql
 * @return
 */
public RelNode makeBySql(String targetName,RelDataType relDataType, String sql) {
    MycatConvention convention = MycatConvention.of(targetName, MysqlSqlDialect.DEFAULT);
    MycatSQLTableScan transientTable = new MycatSQLTableScan(convention,relDataType,sql);
    id++;
    RelOptTable relOptTable = RelOptTableImpl.create(
            this.getRelOptSchema(),
            relDataType,
            transientTable,
            ImmutableList.of(id +"$"+targetName, id+sql));//名称唯一
    return new MycatTransientSQLTableScan(this.getCluster(), convention, relOptTable, () -> sql);
}
 
Example 4
Source Project: Mycat2   Source File: MycatSqlUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getCalciteSQL(SQLStatement sqlStatement) {
    SQLSelectQueryBlock queryBlock = ((SQLSelectStatement) sqlStatement).getSelect().getQueryBlock();
    MycatCalciteMySqlNodeVisitor calciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor();
    sqlStatement.accept(calciteMySqlNodeVisitor);
    SqlNode sqlNode = calciteMySqlNodeVisitor.getSqlNode();
    return sqlNode.toSqlString(MysqlSqlDialect.DEFAULT).getSql();
}
 
Example 5
Source Project: calcite   Source File: BabelParserTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Similar to {@link #testHoist()} but using custom parser. */
@Test void testHoistMySql() {
  // SQL contains back-ticks, which require MySQL's quoting,
  // and DATEADD, which requires Babel.
  final String sql = "select 1 as x,\n"
      + "  'ab' || 'c' as y\n"
      + "from `my emp` /* comment with 'quoted string'? */ as e\n"
      + "where deptno < 40\n"
      + "and DATEADD(day, 1, hiredate) > date '2010-05-06'";
  final SqlDialect dialect = MysqlSqlDialect.DEFAULT;
  final Hoist.Hoisted hoisted =
      Hoist.create(Hoist.config()
          .withParserConfig(
              dialect.configureParser(SqlParser.configBuilder())
                  .setParserFactory(SqlBabelParserImpl::new)
                  .build()))
          .hoist(sql);

  // Simple toString converts each variable to '?N'
  final String expected = "select ?0 as x,\n"
      + "  ?1 || ?2 as y\n"
      + "from `my emp` /* comment with 'quoted string'? */ as e\n"
      + "where deptno < ?3\n"
      + "and DATEADD(day, ?4, hiredate) > ?5";
  assertThat(hoisted.toString(), is(expected));

  // Custom string converts variables to '[N:TYPE:VALUE]'
  final String expected2 = "select [0:DECIMAL:1] as x,\n"
      + "  [1:CHAR:ab] || [2:CHAR:c] as y\n"
      + "from `my emp` /* comment with 'quoted string'? */ as e\n"
      + "where deptno < [3:DECIMAL:40]\n"
      + "and DATEADD(day, [4:DECIMAL:1], hiredate) > [5:DATE:2010-05-06]";
  assertThat(hoisted.substitute(SqlParserTest::varToStr), is(expected2));
}
 
Example 6
Source Project: Bats   Source File: SqlDialectFactoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlDialect create(DatabaseMetaData databaseMetaData) {
  String databaseProductName;
  int databaseMajorVersion;
  int databaseMinorVersion;
  String databaseVersion;
  try {
    databaseProductName = databaseMetaData.getDatabaseProductName();
    databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
    databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
    databaseVersion = databaseMetaData.getDatabaseProductVersion();
  } catch (SQLException e) {
    throw new RuntimeException("while detecting database product", e);
  }
  final String upperProductName =
      databaseProductName.toUpperCase(Locale.ROOT).trim();
  final String quoteString = getIdentifierQuoteString(databaseMetaData);
  final NullCollation nullCollation = getNullCollation(databaseMetaData);
  final SqlDialect.Context c = SqlDialect.EMPTY_CONTEXT
      .withDatabaseProductName(databaseProductName)
      .withDatabaseMajorVersion(databaseMajorVersion)
      .withDatabaseMinorVersion(databaseMinorVersion)
      .withDatabaseVersion(databaseVersion)
      .withIdentifierQuoteString(quoteString)
      .withNullCollation(nullCollation);
  switch (upperProductName) {
  case "ACCESS":
    return new AccessSqlDialect(c);
  case "APACHE DERBY":
    return new DerbySqlDialect(c);
  case "DBMS:CLOUDSCAPE":
    return new DerbySqlDialect(c);
  case "HIVE":
    return new HiveSqlDialect(c);
  case "INGRES":
    return new IngresSqlDialect(c);
  case "INTERBASE":
    return new InterbaseSqlDialect(c);
  case "JETHRODATA":
    return new JethroDataSqlDialect(
        c.withJethroInfo(jethroCache.get(databaseMetaData)));
  case "LUCIDDB":
    return new LucidDbSqlDialect(c);
  case "ORACLE":
    return new OracleSqlDialect(c);
  case "PHOENIX":
    return new PhoenixSqlDialect(c);
  case "MYSQL (INFOBRIGHT)":
    return new InfobrightSqlDialect(c);
  case "MYSQL":
    return new MysqlSqlDialect(c);
  case "REDSHIFT":
    return new RedshiftSqlDialect(c);
  case "SPARK":
    return new SparkSqlDialect(c);
  }
  // Now the fuzzy matches.
  if (databaseProductName.startsWith("DB2")) {
    return new Db2SqlDialect(c);
  } else if (upperProductName.contains("FIREBIRD")) {
    return new FirebirdSqlDialect(c);
  } else if (databaseProductName.startsWith("Informix")) {
    return new InformixSqlDialect(c);
  } else if (upperProductName.contains("NETEZZA")) {
    return new NetezzaSqlDialect(c);
  } else if (upperProductName.contains("PARACCEL")) {
    return new ParaccelSqlDialect(c);
  } else if (databaseProductName.startsWith("HP Neoview")) {
    return new NeoviewSqlDialect(c);
  } else if (upperProductName.contains("POSTGRE")) {
    return new PostgresqlSqlDialect(c);
  } else if (upperProductName.contains("SQL SERVER")) {
    return new MssqlSqlDialect(c);
  } else if (upperProductName.contains("SYBASE")) {
    return new SybaseSqlDialect(c);
  } else if (upperProductName.contains("TERADATA")) {
    return new TeradataSqlDialect(c);
  } else if (upperProductName.contains("HSQL")) {
    return new HsqldbSqlDialect(c);
  } else if (upperProductName.contains("H2")) {
    return new H2SqlDialect(c);
  } else if (upperProductName.contains("VERTICA")) {
    return new VerticaSqlDialect(c);
  } else if (upperProductName.contains("SPARK")) {
    return new SparkSqlDialect(c);
  } else {
    return new AnsiSqlDialect(c);
  }
}
 
Example 7
Source Project: Bats   Source File: SqlDialectFactoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
/** Returns a basic dialect for a given product, or null if none is known. */
static SqlDialect simple(SqlDialect.DatabaseProduct databaseProduct) {
  switch (databaseProduct) {
  case ACCESS:
    return AccessSqlDialect.DEFAULT;
  case BIG_QUERY:
    return BigQuerySqlDialect.DEFAULT;
  case CALCITE:
    return CalciteSqlDialect.DEFAULT;
  case DB2:
    return Db2SqlDialect.DEFAULT;
  case DERBY:
    return DerbySqlDialect.DEFAULT;
  case FIREBIRD:
    return FirebirdSqlDialect.DEFAULT;
  case H2:
    return H2SqlDialect.DEFAULT;
  case HIVE:
    return HiveSqlDialect.DEFAULT;
  case HSQLDB:
    return HsqldbSqlDialect.DEFAULT;
  case INFOBRIGHT:
    return InfobrightSqlDialect.DEFAULT;
  case INFORMIX:
    return InformixSqlDialect.DEFAULT;
  case INGRES:
    return IngresSqlDialect.DEFAULT;
  case INTERBASE:
    return InterbaseSqlDialect.DEFAULT;
  case JETHRO:
    throw new RuntimeException("Jethro does not support simple creation");
  case LUCIDDB:
    return LucidDbSqlDialect.DEFAULT;
  case MSSQL:
    return MssqlSqlDialect.DEFAULT;
  case MYSQL:
    return MysqlSqlDialect.DEFAULT;
  case NEOVIEW:
    return NeoviewSqlDialect.DEFAULT;
  case NETEZZA:
    return NetezzaSqlDialect.DEFAULT;
  case ORACLE:
    return OracleSqlDialect.DEFAULT;
  case PARACCEL:
    return ParaccelSqlDialect.DEFAULT;
  case PHOENIX:
    return PhoenixSqlDialect.DEFAULT;
  case POSTGRESQL:
    return PostgresqlSqlDialect.DEFAULT;
  case REDSHIFT:
    return RedshiftSqlDialect.DEFAULT;
  case SYBASE:
    return SybaseSqlDialect.DEFAULT;
  case TERADATA:
    return TeradataSqlDialect.DEFAULT;
  case VERTICA:
    return VerticaSqlDialect.DEFAULT;
  case SPARK:
    return SparkSqlDialect.DEFAULT;
  case SQLSTREAM:
  case UNKNOWN:
  default:
    return null;
  }
}
 
Example 8
Source Project: calcite   Source File: SqlDialectFactoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
/** Returns a basic dialect for a given product, or null if none is known. */
static SqlDialect simple(SqlDialect.DatabaseProduct databaseProduct) {
  switch (databaseProduct) {
  case ACCESS:
    return AccessSqlDialect.DEFAULT;
  case BIG_QUERY:
    return BigQuerySqlDialect.DEFAULT;
  case CALCITE:
    return CalciteSqlDialect.DEFAULT;
  case CLICKHOUSE:
    return ClickHouseSqlDialect.DEFAULT;
  case DB2:
    return Db2SqlDialect.DEFAULT;
  case DERBY:
    return DerbySqlDialect.DEFAULT;
  case FIREBIRD:
    return FirebirdSqlDialect.DEFAULT;
  case H2:
    return H2SqlDialect.DEFAULT;
  case HIVE:
    return HiveSqlDialect.DEFAULT;
  case HSQLDB:
    return HsqldbSqlDialect.DEFAULT;
  case INFOBRIGHT:
    return InfobrightSqlDialect.DEFAULT;
  case INFORMIX:
    return InformixSqlDialect.DEFAULT;
  case INGRES:
    return IngresSqlDialect.DEFAULT;
  case INTERBASE:
    return InterbaseSqlDialect.DEFAULT;
  case JETHRO:
    throw new RuntimeException("Jethro does not support simple creation");
  case LUCIDDB:
    return LucidDbSqlDialect.DEFAULT;
  case MSSQL:
    return MssqlSqlDialect.DEFAULT;
  case MYSQL:
    return MysqlSqlDialect.DEFAULT;
  case NEOVIEW:
    return NeoviewSqlDialect.DEFAULT;
  case NETEZZA:
    return NetezzaSqlDialect.DEFAULT;
  case ORACLE:
    return OracleSqlDialect.DEFAULT;
  case PARACCEL:
    return ParaccelSqlDialect.DEFAULT;
  case PHOENIX:
    return PhoenixSqlDialect.DEFAULT;
  case POSTGRESQL:
    return PostgresqlSqlDialect.DEFAULT;
  case PRESTO:
    return PrestoSqlDialect.DEFAULT;
  case REDSHIFT:
    return RedshiftSqlDialect.DEFAULT;
  case SYBASE:
    return SybaseSqlDialect.DEFAULT;
  case TERADATA:
    return TeradataSqlDialect.DEFAULT;
  case VERTICA:
    return VerticaSqlDialect.DEFAULT;
  case SPARK:
    return SparkSqlDialect.DEFAULT;
  case SQLSTREAM:
  case UNKNOWN:
  default:
    return null;
  }
}