@Override public String apply(String input) { return quoteIdentifier(input); }}; }
@Override public String visit(ExpColumnReference col) throws Exception { String tableAlias = col.getTable(); if (tableAlias == null && isKeyword(col.getName())) { tableAlias = tableName; } if (tableAlias == null) { return quoteIdentifier(col.getName()); } else { return format("%s.%s", quoteIdentifier(tableAlias), quoteIdentifier(col.getName())); } }
@Override public void setValue(UserSession session, String value) { session.defaultSchemaPath = Strings.isNullOrEmpty(value) ? null : new NamespaceKey(SqlUtils.parseSchemaPath(value)); } },
/** * quote the identifier if it is a: * - doesn't start with a character, * - contains non-alphanumeric characters or * - is a reserved keyword * @param id * @return */ public static String quoteIdentifier(final String id) { if (id.isEmpty()) { return id; } if (isKeyword(id)) { return quoteString(id); } if (Character.isAlphabetic(id.charAt(0)) && ALPHANUM_MATCHER.matchesAllOf(id)) { return id; } // Special case if (NEWLINE_MATCHER.matchesAnyOf(id)) { return quoteUnicodeString(id); } return quoteString(id); }
private String quoteLiteral(String value, DataType type) { if (type == DataType.TEXT) { return stringLiteral(value); } else if (type == DataType.DATE) { return "DATE " + stringLiteral(value); } else if (type == DataType.TIME) { return "TIME " + stringLiteral(value); } else if (type == DataType.DATETIME) { return "TIMESTAMP " + stringLiteral(value); } return value; } }
@Override public String apply(String input) { RelDataTypeField field = type.getField(input, false, false); if(field == null){ throw UserException.validationError() .message("Unable to find field %s in table %s. Available fields were: %s.", input, SqlUtils.quotedCompound(path), FluentIterable.from(type.getFieldNames()).transform(SqlUtils.QUOTER).join(Joiner.on(", ")) ).build(logger); } return field.getName(); }
private String quoted(String value, DataType type) { if (type == null || type == TEXT) { return stringLiteral(value); } else if (type == DATE) { if (value == null) { return "CAST (null as DATE)"; } return "DATE " + stringLiteral(value); } else if (type == TIME) { if (value == null) { return "CAST (null as TIME)"; } return "TIME " + stringLiteral(value); } else if (type == DATETIME) { if (value == null) { return "CAST (null as TIMESTAMP)"; } return "TIMESTAMP " + stringLiteral(value); } else { return value; } }
@Override public ReflectionGoal apply(Ord<ReflectionDetails> reflectionDetails) { return new ReflectionGoal() .setName(String.format("AUTO_%s_RAW_%d", SqlUtils.quotedCompound(datasetConfig.getFullPathList()), reflectionDetails.i)) .setDetails(reflectionDetails.e) .setType(ReflectionType.RAW); } });
/** * puts back ticks around components if they look like reserved keywords and joins them with . * @param pathComponents can not contain nulls * @return a dot delimited path * Convert a list of path components to fully qualified dotted schema path * [a,b,c] -> a.b.c * [a,b,c-1] -> a.b.`c-1` * [a,b,c.json] -> a.b.`c.json` */ public static String constructFullPath(Collection<String> pathComponents) { final List<String> quotedPathComponents = Lists.newArrayList(); for (final String component : pathComponents) { checkNotNull(component); quotedPathComponents.add(SqlUtils.quoteIdentifier(component)); } return KEY_JOINER.join(quotedPathComponents); }
private String prepareValue(final String value, DataType type) throws Exception { if (value == null) { return "NULL"; } switch (type) { case BOOLEAN: case DECIMAL: case FLOAT: case INTEGER: return value; case DATE: case DATETIME: case TEXT: case TIME: return stringLiteral(value); default: throw new UnsupportedOperationException("prepareValue can not be aplied to " + type.name()); } }
@Test public void testParseSchemaPath() { assertEquals(asList("a", "b", "c"), SqlUtils.parseSchemaPath("a.b.c")); assertEquals(asList("a"), SqlUtils.parseSchemaPath("a")); assertEquals(asList("a", "b.c", "d"), SqlUtils.parseSchemaPath("a.\"b.c\".d")); assertEquals(asList("a", "c"), SqlUtils.parseSchemaPath("a..c")); } }
@Override public ReflectionGoal apply(Ord<ReflectionDetails> reflectionDetails) { return new ReflectionGoal() .setName(String.format("AUTO_%s_AGG_%d", SqlUtils.quotedCompound(datasetConfig.getFullPathList()), reflectionDetails.i)) .setDetails(reflectionDetails.e) .setType(ReflectionType.AGGREGATION); } });
private String formatSQLWithSubQuery(String sql, String alias) { if (isStar && orders.isEmpty() && evaledFilters.isEmpty() && groupBys.isEmpty() && joins.isEmpty()) { return sql; } else { if (alias == null) { throw new UnsupportedOperationException("the subquery should be assigned an alias: " + sql); } return formatSQL(evaledCols, orders, "(\n" + indent(sql) + "\n) " + quoteIdentifier(alias), joins, evaledFilters, groupBys); } }
@Override public String getFunctionExpr(String expr, Object... args) { checkArgument(args.length == 2 && args[0] != null && args[1] != null, "Expected the split position type and index as arguments"); SplitPositionType splitPositionType = (SplitPositionType) args[0]; Integer index = (Integer) args[1]; return String.format("%s(%s, %s, %s, %d)", SplitPattern.REGEXP_SPLIT, expr, getDelimiterRegexLiteral(false), stringLiteral(splitPositionType.toString()), index ); }
private void assertFoldersExist(List<String> expFolders) throws Exception { for (String folderPath : expFolders) { NamespaceKey folderKey = new NamespaceKey(SqlUtils.parseSchemaPath(folderPath)); namespaceService.getFolder(folderKey); // if the folder doesn't exit we get an exception } }
private static List<NameAndGranularity> qualifyColumnsWithGranularity(DremioTable table, List<NameAndGranularity> strings){ final RelDataType type = table.getRowType(JavaTypeFactoryImpl.INSTANCE); return strings.stream().map(input -> { RelDataTypeField field = type.getField(input.getName(), false, false); if(field == null){ throw UserException.validationError() .message("Unable to find field %s in table %s. Available fields were: %s.", input.getName(), SqlUtils.quotedCompound(table.getPath().getPathComponents()), FluentIterable.from(type.getFieldNames()).transform(SqlUtils.QUOTER).join(Joiner.on(", ")) ).build(logger); } return new NameAndGranularity(field.getName(), input.getGranularity()); }).collect(Collectors.toList()); }
<T> String generateCardGenQuery(String inputColName, String datasetPreviewTable, List<TransformRuleWrapper<T>> evaluators) { StringBuilder queryBuilder = new StringBuilder(); String inputExpr = String.format("%s.%s", quoteIdentifier("dremio_preview_data"), quoteIdentifier(inputColName)); List<String> exprs = Lists.newArrayList(); for(int i=0; i<evaluators.size(); i++) { if (evaluators.get(i).canGenerateExamples()) { final String expr = evaluators.get(i).getExampleFunctionExpr(inputExpr); final String outputColAlias = "example_" + i; exprs.add(String.format("%s AS %s", expr, outputColAlias)); } } exprs.add(String.format("%s AS inputCol", inputExpr)); queryBuilder.append("SELECT\n"); queryBuilder.append(Joiner.on(",\n").join(exprs)); queryBuilder.append(format("\nFROM %s as dremio_preview_data", datasetPreviewTable)); queryBuilder.append(format("\nWHERE %s IS NOT NULL", quoteIdentifier(inputColName))); queryBuilder.append(format("\nLIMIT %d", Card.EXAMPLES_TO_SHOW)); return queryBuilder.toString(); }
@Override public String visit(FieldConvertDateToText dateToText) throws Exception { return format( "TO_CHAR(%s, %s)", eval(fieldTransformation.getOperand()), stringLiteral(dateToText.getFormat())); }
private void assertFoldersDoNotExist(List<String> expFolders) throws Exception { for (String folderPath : expFolders) { NamespaceKey folderKey = new NamespaceKey(SqlUtils.parseSchemaPath(folderPath)); try { namespaceService.getFolder(folderKey); // if the folder doesn't exit we get an exception fail(); } catch (NamespaceNotFoundException ex) { /* no-op */ } } }