org.apache.calcite.avatica.util.Casing Java Examples

The following examples show how to use org.apache.calcite.avatica.util.Casing. 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: SqlParserUtil.java    From Quicksql with MIT License 6 votes vote down vote up
/**
 * Unquotes a quoted string, using different quotes for beginning and end.
 */
public static String strip(String s, String startQuote, String endQuote,
    String escape, Casing casing) {
  if (startQuote != null) {
    assert endQuote != null;
    assert startQuote.length() == 1;
    assert endQuote.length() == 1;
    assert escape != null;
    assert s.startsWith(startQuote) && s.endsWith(endQuote) : s;
    s = s.substring(1, s.length() - 1).replace(escape, endQuote);
  }
  switch (casing) {
  case TO_UPPER:
    return s.toUpperCase(Locale.ROOT);
  case TO_LOWER:
    return s.toLowerCase(Locale.ROOT);
  default:
    return s;
  }
}
 
Example #2
Source File: SqlWorker.java    From quark with Apache License 2.0 6 votes vote down vote up
private Planner buildPlanner(QueryContext context) {
  final List<RelTraitDef> traitDefs = new ArrayList<RelTraitDef>();
  traitDefs.add(ConventionTraitDef.INSTANCE);
  traitDefs.add(RelCollationTraitDef.INSTANCE);
  final ChainedSqlOperatorTable opTab =
      new ChainedSqlOperatorTable(
          ImmutableList.of(SqlStdOperatorTable.instance(),
              HiveSqlOperatorTable.instance(), catalogReader));
  FrameworkConfig config = Frameworks.newConfigBuilder() //
      .parserConfig(SqlParser.configBuilder()
          .setQuotedCasing(Casing.UNCHANGED)
          .setUnquotedCasing(Casing.TO_UPPER)
          .setQuoting(Quoting.DOUBLE_QUOTE)
          .build()) //
      .defaultSchema(context.getDefaultSchema()) //
      .operatorTable(opTab) //
      .traitDefs(traitDefs) //
      .convertletTable(StandardConvertletTable.INSTANCE)//
      .programs(getPrograms()) //
      .typeSystem(RelDataTypeSystem.DEFAULT) //
      .build();
  return Frameworks.getPlanner(config);
}
 
Example #3
Source File: ParserFactory.java    From quark with Apache License 2.0 6 votes vote down vote up
public Parser getParser(String sql, Properties info)
    throws SQLException {
  SqlParser parser = SqlParser.create(sql,
      SqlParser.configBuilder()
          .setQuotedCasing(Casing.UNCHANGED)
          .setUnquotedCasing(Casing.UNCHANGED)
          .setQuoting(Quoting.DOUBLE_QUOTE)
          .setParserFactory(QuarkParserImpl.FACTORY)
          .build());
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseStmt();
  } catch (SqlParseException e) {
    throw new RuntimeException(
        "parse failed: " + e.getMessage(), e);
  }
  if (sqlNode.getKind().equals(SqlKind.OTHER_DDL)) {
    return new DDLParser();
  } else  {
    return getSqlQueryParser(info);
  }
}
 
Example #4
Source File: QueryProcedureProducer.java    From Quicksql with MIT License 6 votes vote down vote up
private void initPlannerConfig(String jsonPath) throws IOException {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);

    new ModelHandler(rootSchema, jsonPath);
    final SqlToRelConverter.Config convertConfig = SqlToRelConverter.configBuilder()
        .withTrimUnusedFields(false)
        .withConvertTableAccess(false)
        .withExpand(false)
        .build();

    final SqlParser.Config parserConfig = SqlParser.configBuilder()
        .setConformance(SqlConformanceEnum.MYSQL_5)
        .setQuoting(Quoting.BACK_TICK)
        .setCaseSensitive(false)
        .setUnquotedCasing(Casing.UNCHANGED)
        .build();

    this.config = Frameworks.newConfigBuilder()
        .parserConfig(parserConfig)
        .defaultSchema(rootSchema)
        .traitDefs((List<RelTraitDef>) null)
        .sqlToRelConverterConfig(convertConfig)
        .build();
}
 
Example #5
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Unquotes a quoted string, using different quotes for beginning and end.
 */
public static String strip(String s, String startQuote, String endQuote,
    String escape, Casing casing) {
  if (startQuote != null) {
    assert endQuote != null;
    assert startQuote.length() == 1;
    assert endQuote.length() == 1;
    assert escape != null;
    assert s.startsWith(startQuote) && s.endsWith(endQuote) : s;
    s = s.substring(1, s.length() - 1).replace(escape, endQuote);
  }
  switch (casing) {
  case TO_UPPER:
    return s.toUpperCase(Locale.ROOT);
  case TO_LOWER:
    return s.toLowerCase(Locale.ROOT);
  default:
    return s;
  }
}
 
Example #6
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the contents of an sql quoted string literal into the
 * corresponding Java string representation (removing leading and trailing
 * quotes and unescaping internal doubled quotes).
 */
public static String parseString(String s) {
  int i = s.indexOf("'"); // start of body
  if (i > 0) {
    s = s.substring(i);
  }
  return strip(s, "'", "'", "''", Casing.UNCHANGED);
}
 
Example #7
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private SqlDialect getSqlDialect() {
	return new CalciteSqlDialect(SqlDialect.EMPTY_CONTEXT
		.withQuotedCasing(Casing.UNCHANGED)
		.withConformance(conformance)
		.withUnquotedCasing(Casing.UNCHANGED)
		.withIdentifierQuoteString("`"));
}
 
Example #8
Source File: SqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Nonnull public Context withQuotedCasing(Casing quotedCasing) {
  return new ContextImpl(databaseProduct, databaseProductName,
      databaseVersion, databaseMajorVersion, databaseMinorVersion,
      literalQuoteString, literalEscapedQuoteString,
      identifierQuoteString, quotedCasing, unquotedCasing, caseSensitive,
      conformance, nullCollation, dataTypeSystem, jethroInfo);
}
 
Example #9
Source File: SqlParser.java    From Quicksql with MIT License 5 votes vote down vote up
private ConfigImpl(int identifierMaxLength, Casing quotedCasing,
    Casing unquotedCasing, Quoting quoting, boolean caseSensitive,
    SqlConformance conformance, SqlParserImplFactory parserFactory) {
  this.identifierMaxLength = identifierMaxLength;
  this.caseSensitive = caseSensitive;
  this.conformance = Objects.requireNonNull(conformance);
  this.quotedCasing = Objects.requireNonNull(quotedCasing);
  this.unquotedCasing = Objects.requireNonNull(unquotedCasing);
  this.quoting = Objects.requireNonNull(quoting);
  this.parserFactory = Objects.requireNonNull(parserFactory);
}
 
Example #10
Source File: SqlDialect.java    From Quicksql with MIT License 5 votes vote down vote up
/** Creates an empty context. Use {@link #EMPTY_CONTEXT} if possible. */
protected 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 #11
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 #12
Source File: SqlParser.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ConfigImpl(int identifierMaxLength, Casing quotedCasing,
    Casing unquotedCasing, Quoting quoting, boolean caseSensitive,
    SqlConformance conformance, SqlParserImplFactory parserFactory) {
  this.identifierMaxLength = identifierMaxLength;
  this.caseSensitive = caseSensitive;
  this.conformance = Objects.requireNonNull(conformance);
  this.quotedCasing = Objects.requireNonNull(quotedCasing);
  this.unquotedCasing = Objects.requireNonNull(unquotedCasing);
  this.quoting = Objects.requireNonNull(quoting);
  this.parserFactory = Objects.requireNonNull(parserFactory);
}
 
Example #13
Source File: SqlDialect.java    From Quicksql with MIT License 5 votes vote down vote up
@Nonnull public Context withUnquotedCasing(Casing unquotedCasing) {
  return new ContextImpl(databaseProduct, databaseProductName,
      databaseVersion, databaseMajorVersion, databaseMinorVersion,
      literalQuoteString, literalEscapedQuoteString,
      identifierQuoteString, quotedCasing, unquotedCasing, caseSensitive,
      conformance, nullCollation, dataTypeSystem, jethroInfo);
}
 
Example #14
Source File: QueryConnectionTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetConnection() throws SQLException, IllegalAccessException {
    Connection connection = QueryConnection.getConnection("default");
    assertNotNull(connection);
    Map<InternalProperty, Object> properties = dirtyReadProperties(connection);
    assertEquals(true, properties.get(InternalProperty.CASE_SENSITIVE));
    assertEquals(Casing.TO_UPPER, properties.get(InternalProperty.UNQUOTED_CASING));
    assertEquals(Quoting.DOUBLE_QUOTE, properties.get(InternalProperty.QUOTING));

    ResultSet resultSet = connection.prepareStatement(SQL_WITH_MIXED_CASE).executeQuery();
    String columnName = resultSet.getMetaData().getColumnName(1);
    assertEquals("VALUE_ALIAS", columnName);

    // override connection properties
    KylinConfig conf = KylinConfig.getInstanceFromEnv();
    conf.setProperty("kylin.query.calcite.extras-props.caseSensitive", "false");
    conf.setProperty("kylin.query.calcite.extras-props.unquotedCasing", "UNCHANGED");
    conf.setProperty("kylin.query.calcite.extras-props.quoting", "BRACKET");
    Connection conn2 = QueryConnection.getConnection("default");
    Map<InternalProperty, Object> props = dirtyReadProperties(conn2);
    assertEquals(false, props.get(InternalProperty.CASE_SENSITIVE));
    assertEquals(Casing.UNCHANGED, props.get(InternalProperty.UNQUOTED_CASING));
    assertEquals(Quoting.BRACKET, props.get(InternalProperty.QUOTING));

    ResultSet resultSet1 = conn2.prepareStatement(SQL_WITH_MIXED_CASE).executeQuery();
    assertEquals("value_ALIAS", resultSet1.getMetaData().getColumnName(1));
}
 
Example #15
Source File: SqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Nonnull public Context withUnquotedCasing(Casing unquotedCasing) {
  return new ContextImpl(databaseProduct, databaseProductName,
      databaseVersion, databaseMajorVersion, databaseMinorVersion,
      literalQuoteString, literalEscapedQuoteString,
      identifierQuoteString, quotedCasing, unquotedCasing, caseSensitive,
      conformance, nullCollation, dataTypeSystem, jethroInfo);
}
 
Example #16
Source File: ServerMetaProvider.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static IdentifierCasing getIdentifierCasing(Casing casing, boolean caseSensitive) {
  switch(casing) {
  case TO_LOWER:
    return IdentifierCasing.IC_STORES_LOWER;

  case TO_UPPER:
    return IdentifierCasing.IC_STORES_UPPER;

  case UNCHANGED:
    return caseSensitive ? IdentifierCasing.IC_SUPPORTS_MIXED : IdentifierCasing.IC_STORES_MIXED;

  default:
    throw new AssertionError("Unknown casing:" + casing);
  }
}
 
Example #17
Source File: QueryConnectionTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetConnection() throws SQLException, IllegalAccessException {
    Connection connection = QueryConnection.getConnection("default");
    assertNotNull(connection);
    Map<InternalProperty, Object> properties = dirtyReadProperties(connection);
    assertEquals(true, properties.get(InternalProperty.CASE_SENSITIVE));
    assertEquals(Casing.TO_UPPER, properties.get(InternalProperty.UNQUOTED_CASING));
    assertEquals(Quoting.DOUBLE_QUOTE, properties.get(InternalProperty.QUOTING));

    ResultSet resultSet = connection.prepareStatement(SQL_WITH_MIXED_CASE).executeQuery();
    String columnName = resultSet.getMetaData().getColumnName(1);
    assertEquals("VALUE_ALIAS", columnName);

    // override connection properties
    KylinConfig conf = KylinConfig.getInstanceFromEnv();
    conf.setProperty("kylin.query.calcite.extras-props.caseSensitive", "false");
    conf.setProperty("kylin.query.calcite.extras-props.unquotedCasing", "UNCHANGED");
    conf.setProperty("kylin.query.calcite.extras-props.quoting", "BRACKET");
    Connection conn2 = QueryConnection.getConnection("default");
    Map<InternalProperty, Object> props = dirtyReadProperties(conn2);
    assertEquals(false, props.get(InternalProperty.CASE_SENSITIVE));
    assertEquals(Casing.UNCHANGED, props.get(InternalProperty.UNQUOTED_CASING));
    assertEquals(Quoting.BRACKET, props.get(InternalProperty.QUOTING));

    ResultSet resultSet1 = conn2.prepareStatement(SQL_WITH_MIXED_CASE).executeQuery();
    assertEquals("value_ALIAS", resultSet1.getMetaData().getColumnName(1));
}
 
Example #18
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static SqlParser.Config createParserConfig(Map<String, Object> options) {
	return SqlParser.configBuilder()
		.setQuoting((Quoting) options.get("quoting"))
		.setUnquotedCasing((Casing) options.get("unquotedCasing"))
		.setQuotedCasing((Casing) options.get("quotedCasing"))
		.setConformance((SqlConformance) options.get("conformance"))
		.setCaseSensitive((boolean) options.get("caseSensitive"))
		.setParserFactory((SqlParserImplFactory) options.get("parserFactory"))
		.build();
}
 
Example #19
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> buildDefaultOptions() {
	final Map<String, Object> m = new HashMap<>();
	m.put("quoting", Quoting.BACK_TICK);
	m.put("quotedCasing", Casing.UNCHANGED);
	m.put("unquotedCasing", Casing.UNCHANGED);
	m.put("caseSensitive", true);
	m.put("enableTypeCoercion", false);
	m.put("conformance", SqlConformanceEnum.DEFAULT);
	m.put("operatorTable", SqlStdOperatorTable.instance());
	m.put("parserFactory", FlinkSqlParserImpl.FACTORY);
	return Collections.unmodifiableMap(m);
}
 
Example #20
Source File: SqlTestFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlParser.Config createParserConfig(ImmutableMap<String, Object> options) {
  return SqlParser.configBuilder()
      .setQuoting((Quoting) options.get("quoting"))
      .setUnquotedCasing((Casing) options.get("unquotedCasing"))
      .setQuotedCasing((Casing) options.get("quotedCasing"))
      .setConformance((SqlConformance) options.get("conformance"))
      .setCaseSensitive((boolean) options.get("caseSensitive"))
      .build();
}
 
Example #21
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private SqlParser getSqlParser(String sql) {
	return SqlParser.create(sql,
		SqlParser.configBuilder()
			.setParserFactory(FlinkSqlParserImpl.FACTORY)
			.setQuoting(Quoting.BACK_TICK)
			.setUnquotedCasing(Casing.UNCHANGED)
			.setQuotedCasing(Casing.UNCHANGED)
			.setConformance(conformance)
			.build());
}
 
Example #22
Source File: SqlParser.java    From Quicksql with MIT License 4 votes vote down vote up
public Casing unquotedCasing() {
  return unquotedCasing;
}
 
Example #23
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Gets completion hints for a syntactically correct sql statement with dummy
 * SqlIdentifier
 *
 * @param sql A syntactically correct sql statement for which to retrieve
 *            completion hints
 * @param pos to indicate the line and column position in the query at which
 *            completion hints need to be retrieved. For example, "select
 *            a.ename, b.deptno from sales.emp a join sales.dept b "on
 *            a.deptno=b.deptno where empno=1"; setting pos to 'Line 1, Column
 *            17' returns all the possible column names that can be selected
 *            from sales.dept table setting pos to 'Line 1, Column 31' returns
 *            all the possible table names in 'sales' schema
 * @return an array of hints ({@link SqlMoniker}) that can fill in at the
 * indicated position
 */
public List<SqlMoniker> getCompletionHints(String sql, SqlParserPos pos) {
  // First try the statement they gave us. If this fails, just return
  // the tokens which were expected at the failure point.
  List<SqlMoniker> hintList = new ArrayList<>();
  SqlNode sqlNode = tryParse(sql, hintList);
  if (sqlNode == null) {
    return hintList;
  }

  // Now construct a statement which is bound to fail. (Character 7 BEL
  // is not legal in any SQL statement.)
  final int x = pos.getColumnNum() - 1;
  sql = sql.substring(0, x)
      + " \07"
      + sql.substring(x);
  tryParse(sql, hintList);

  final SqlMoniker star =
      new SqlMonikerImpl(ImmutableList.of("*"), SqlMonikerType.KEYWORD);
  String hintToken =
      parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN;
  if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) {
    hintList.remove(star);
  }

  // Add the identifiers which are expected at the point of interest.
  try {
    validator.validate(sqlNode);
  } catch (Exception e) {
    // mask any exception that is thrown during the validation, i.e.
    // try to continue even if the sql is invalid. we are doing a best
    // effort here to try to come up with the requested completion
    // hints
    Util.swallow(e, LOGGER);
  }
  final List<SqlMoniker> validatorHints =
      validator.lookupHints(sqlNode, pos);
  hintList.addAll(validatorHints);
  return hintList;
}
 
Example #24
Source File: SqlParser.java    From Quicksql with MIT License 4 votes vote down vote up
public Casing quotedCasing() {
  return quotedCasing;
}
 
Example #25
Source File: AvaticaDatabaseMetaData.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private Casing unquotedCasing() {
  return UNQUOTED_CASING.getEnum(getProperties(), Casing.class);
}
 
Example #26
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 4 votes vote down vote up
public String getReplacement(SqlMoniker hint, String word) {
  Casing preferredCasing = getPreferredCasing(word);
  boolean quoted = !word.isEmpty() && word.charAt(0) == quoteStart();
  return getReplacement(hint, quoted, preferredCasing);
}
 
Example #27
Source File: AvaticaDatabaseMetaData.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
public boolean supportsMixedCaseIdentifiers() throws SQLException {
  return caseSensitive() && unquotedCasing() == Casing.UNCHANGED;
}
 
Example #28
Source File: SqlParser.java    From Quicksql with MIT License 4 votes vote down vote up
public ConfigBuilder setUnquotedCasing(Casing unquotedCasing) {
  this.unquotedCasing = Objects.requireNonNull(unquotedCasing);
  return this;
}
 
Example #29
Source File: AvaticaDatabaseMetaData.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
public boolean storesLowerCaseIdentifiers() throws SQLException {
  return unquotedCasing() == Casing.TO_LOWER;
}
 
Example #30
Source File: AvaticaDatabaseMetaData.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private Casing quotedCasing() {
  return QUOTED_CASING.getEnum(getProperties(), Casing.class);
}