org.apache.calcite.sql.SqlIdentifier Java Examples
The following examples show how to use
org.apache.calcite.sql.SqlIdentifier.
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: DrillOperatorTable.java From Bats with Apache License 2.0 | 6 votes |
private void populateFromTypeInference(SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List<SqlOperator> operatorList) { final List<SqlOperator> calciteOperatorList = Lists.newArrayList(); inner.lookupOperatorOverloads(opName, category, syntax, calciteOperatorList); if (!calciteOperatorList.isEmpty()) { for (SqlOperator calciteOperator : calciteOperatorList) { if (calciteToWrapper.containsKey(calciteOperator)) { operatorList.add(calciteToWrapper.get(calciteOperator)); } else { operatorList.add(calciteOperator); } } } else { // if no function is found, check in Drill UDFs if (operatorList.isEmpty() && (syntax == SqlSyntax.FUNCTION || syntax == SqlSyntax.FUNCTION_ID) && opName.isSimple()) { List<SqlOperator> drillOps = drillOperatorsWithInferenceMap.get(opName.getSimple().toLowerCase()); if (drillOps != null && !drillOps.isEmpty()) { operatorList.addAll(drillOps); } } } }
Example #2
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) { // Resolve identifier as a table. final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl(); scope.resolveTable(id.names, catalogReader.nameMatcher(), SqlValidatorScope.Path.EMPTY, resolved); if (resolved.count() != 1) { throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString())); } // We've found a table. But is it a sequence? final SqlValidatorNamespace ns = resolved.only().namespace; if (ns instanceof TableNamespace) { final Table table = ns.getTable().unwrap(Table.class); switch (table.getJdbcTableType()) { case SEQUENCE: case TEMPORARY_SEQUENCE: return; } } throw newValidationError(id, RESOURCE.notASequence(id.toString())); }
Example #3
Source File: StandardConvertletTable.java From Bats with Apache License 2.0 | 6 votes |
/** * Converts a ROW. * * <p>Called automatically via reflection. */ public RexNode convertRow( SqlRexContext cx, SqlRowOperator op, SqlCall call) { if (cx.getValidator().getValidatedNodeType(call).getSqlTypeName() != SqlTypeName.COLUMN_LIST) { return convertCall(cx, call); } final RexBuilder rexBuilder = cx.getRexBuilder(); final List<RexNode> columns = new ArrayList<>(); for (SqlNode operand : call.getOperandList()) { columns.add( rexBuilder.makeLiteral( ((SqlIdentifier) operand).getSimple())); } final RelDataType type = rexBuilder.deriveReturnType(SqlStdOperatorTable.COLUMN_LIST, columns); return rexBuilder.makeCall(type, SqlStdOperatorTable.COLUMN_LIST, columns); }
Example #4
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) { // Resolve identifier as a table. final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl(); scope.resolveTable(id.names, catalogReader.nameMatcher(), SqlValidatorScope.Path.EMPTY, resolved); if (resolved.count() != 1) { throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString())); } // We've found a table. But is it a sequence? final SqlValidatorNamespace ns = resolved.only().namespace; if (ns instanceof TableNamespace) { final Table table = ns.getTable().unwrap(Table.class); switch (table.getJdbcTableType()) { case SEQUENCE: case TEMPORARY_SEQUENCE: return; } } throw newValidationError(id, RESOURCE.notASequence(id.toString())); }
Example #5
Source File: CalciteParser.java From kylin with Apache License 2.0 | 6 votes |
public static void ensureNoAliasInExpr(String expr) { SqlNode sqlNode = getExpNode(expr); SqlVisitor sqlVisitor = new SqlBasicVisitor() { @Override public Object visit(SqlIdentifier id) { if (id.names.size() > 1) { throw new IllegalArgumentException( "Column Identifier in the computed column expression should only contain COLUMN"); } return null; } }; sqlNode.accept(sqlVisitor); }
Example #6
Source File: SqlRefreshTable.java From dremio-oss with Apache License 2.0 | 6 votes |
@Override public void setOperand(int i, SqlNode operand) { switch (i) { case 0: table = (SqlIdentifier) operand; break; case 1: deleteUnavail = (SqlLiteral) operand; break; case 2: forceUp = (SqlLiteral) operand; break; case 3: promotion = (SqlLiteral) operand; break; default: throw new AssertionError(i); } }
Example #7
Source File: SqlCreateHiveView.java From flink with Apache License 2.0 | 6 votes |
public SqlCreateHiveView(SqlParserPos pos, SqlIdentifier viewName, SqlNodeList fieldList, SqlNode query, boolean ifNotExists, SqlCharStringLiteral comment, SqlNodeList properties) { super( pos, viewName, fieldList, query, false, false, ifNotExists, HiveDDLUtils.unescapeStringLiteral(comment), properties ); HiveDDLUtils.unescapeProperties(properties); originPropList = new SqlNodeList(properties.getList(), properties.getParserPosition()); // mark it as a hive view properties.add(HiveDDLUtils.toTableOption(CatalogConfig.IS_GENERIC, "false", pos)); }
Example #8
Source File: QuerySemantics.java From dremio-oss with Apache License 2.0 | 6 votes |
private static ASNode extractAS(SqlCall call) { if (call.getOperator().getKind() == SqlKind.AS) { List<SqlNode> operandList = call.getOperandList(); if (operandList.size() == 2) { SqlNode exp = operandList.get(0); SqlNode colID = operandList.get(1); if (isSimpleID(colID)) { return new ASNode((SqlIdentifier)colID, exp); } else { throw new UnsupportedOperationException("Unexpected AS " + colID + "\n" + SqlNodes.toTreeString(call)); } } else { throw new UnsupportedOperationException("Unexpected AS operands in field: \n" + SqlNodes.toTreeString(call)); } } throw new UnsupportedOperationException("AS not understood: " + SqlNodes.toSQLString(call)); }
Example #9
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
/** * Returns the <code>ordinal</code>th item in the select list. */ private SqlNode nthSelectItem(int ordinal, final SqlParserPos pos) { // TODO: Don't expand the list every time. Maybe keep an expanded // version of each expression -- select lists and identifiers -- in // the validator. SqlNodeList expandedSelectList = expandStar( select.getSelectList(), select, false); SqlNode expr = expandedSelectList.get(ordinal); expr = stripAs(expr); if (expr instanceof SqlIdentifier) { expr = getScope().fullyQualify((SqlIdentifier) expr).identifier; } // Create a copy of the expression with the position of the order // item. return expr.clone(pos); }
Example #10
Source File: SqlQueryParser.java From quark with Apache License 2.0 | 6 votes |
/** * Strips namespace from identifiers of sql * * @param node * @param namespace * @param dialect * @return */ private String stripNamespace(final SqlNode node, final String namespace, final SqlDialect dialect) { final SqlNode transformedNode = node.accept( new SqlShuttle() { @Override public SqlNode visit(SqlIdentifier id) { if (id.names.size() > 1 && id.names.get(0).toUpperCase().equals(namespace.toUpperCase())) { return id.getComponent(1, id.names.size()); } else { return id; } } }); String result = transformedNode.toSqlString(dialect).toString(); return result.replace("\n", " "); }
Example #11
Source File: FlinkSqlParser.java From sylph with Apache License 2.0 | 6 votes |
/** * update having */ private static SqlNode updateOnlyOneFilter(SqlNode filterNode, String joinOutTableName) { if (filterNode.getKind() == IDENTIFIER) { SqlIdentifier field = ((SqlIdentifier) filterNode); checkState(!field.isStar(), "filter field must not Star(*)"); if (field.names.size() > 1) { field.setName(0, field.getComponent(0).getSimple()); field.setName(1, joinOutTableName); } return field; } else if (filterNode instanceof SqlBasicCall) { //demo: `user_id` = 'uid_1' SqlBasicCall sqlBasicCall = (SqlBasicCall) filterNode; for (int i = 0; i < sqlBasicCall.getOperandList().size(); i++) { SqlNode sqlNode = sqlBasicCall.getOperandList().get(i); SqlNode upNode = updateOnlyOneFilter(sqlNode, joinOutTableName); sqlBasicCall.getOperands()[i] = upNode; } return sqlBasicCall; } else { return filterNode; } }
Example #12
Source File: SqlCreateView.java From flink with Apache License 2.0 | 6 votes |
public SqlCreateView( SqlParserPos pos, SqlIdentifier viewName, SqlNodeList fieldList, SqlNode query, boolean replace, boolean isTemporary, boolean ifNotExists, SqlCharStringLiteral comment, SqlNodeList properties) { super(OPERATOR, pos, replace, ifNotExists); this.viewName = requireNonNull(viewName, "viewName should not be null"); this.fieldList = requireNonNull(fieldList, "fieldList should not be null"); this.query = requireNonNull(query, "query should not be null"); this.isTemporary = requireNonNull(isTemporary, "isTemporary should not be null"); this.comment = comment; this.properties = properties; }
Example #13
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
@Override public SqlNode visit(SqlCall call) { SqlKind kind = call.getKind(); if (isLogicalNavigation(kind) || isAggregation(kind) || isRunningOrFinal(kind)) { return call; } switch (kind) { case PREV: final List<SqlNode> operands = call.getOperandList(); if (operands.get(0) instanceof SqlIdentifier) { String name = ((SqlIdentifier) operands.get(0)).names.get(0); return name.equals(alpha) ? call : SqlStdOperatorTable.LAST.createCall(SqlParserPos.ZERO, operands); } } return super.visit(call); }
Example #14
Source File: MergeTableLikeUtil.java From flink with Apache License 2.0 | 6 votes |
private void appendDerivedWatermarks( Map<FeatureOption, MergingStrategy> mergingStrategies, List<SqlWatermark> derivedWatermarkSpecs) { for (SqlWatermark derivedWatermarkSpec : derivedWatermarkSpecs) { SqlIdentifier eventTimeColumnName = derivedWatermarkSpec.getEventTimeColumnName(); HashMap<String, RelDataType> nameToTypeMap = new LinkedHashMap<>(physicalFieldNamesToTypes); nameToTypeMap.putAll(computedFieldNamesToTypes); verifyRowtimeAttribute(mergingStrategies, eventTimeColumnName, nameToTypeMap); String rowtimeAttribute = eventTimeColumnName.toString(); SqlNode expression = derivedWatermarkSpec.getWatermarkStrategy(); // this will validate and expand function identifiers. SqlNode validated = sqlValidator.validateParameterizedExpression(expression, nameToTypeMap); RelDataType validatedType = sqlValidator.getValidatedNodeType(validated); DataType exprDataType = fromLogicalToDataType(toLogicalType(validatedType)); watermarkSpecs.put(rowtimeAttribute, new WatermarkSpec( rowtimeAttribute, escapeExpressions.apply(validated), exprDataType)); } }
Example #15
Source File: CalciteCatalogReader.java From Bats with Apache License 2.0 | 6 votes |
public void lookupOperatorOverloads(final SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List<SqlOperator> operatorList) { if (syntax != SqlSyntax.FUNCTION) { return; } final Predicate<Function> predicate; if (category == null) { predicate = function -> true; } else if (category.isTableFunction()) { predicate = function -> function instanceof TableMacro || function instanceof TableFunction; } else { predicate = function -> !(function instanceof TableMacro || function instanceof TableFunction); } getFunctionsFrom(opName.names) .stream() .filter(predicate) .map(function -> toOp(opName, function)) .forEachOrdered(operatorList::add); }
Example #16
Source File: RelToSqlConverter.java From quark with Apache License 2.0 | 5 votes |
public Result visitValues(Values e) { final List<String> fields = e.getRowType().getFieldNames(); final List<Clause> clauses = Collections.singletonList(Clause.SELECT); final Context context = new AliasContext(Collections.<Pair<String, RelDataType>>emptyList(), false); final List<SqlSelect> selects = new ArrayList<>(); for (List<RexLiteral> tuple : e.getTuples()) { final List<SqlNode> selectList = new ArrayList<>(); for (Pair<RexLiteral, String> literal : Pair.zip(tuple, fields)) { selectList.add( SqlStdOperatorTable.AS.createCall( POS, context.toSql(null, literal.left), new SqlIdentifier(literal.right, POS))); } selects.add( new SqlSelect(POS, SqlNodeList.EMPTY, new SqlNodeList(selectList, POS), null, null, null, null, null, null, null, null)); } SqlNode query = null; for (SqlSelect select : selects) { if (query == null) { query = select; } else { query = SqlStdOperatorTable.UNION_ALL.createCall(POS, query, select); } } return result(query, clauses, e); }
Example #17
Source File: DrillCompoundIdentifier.java From Bats with Apache License 2.0 | 5 votes |
public SqlNode getAsCompoundIdentifier() { List<String> names = Lists.newArrayListWithCapacity(ids.size()); List<SqlParserPos> pos = Lists.newArrayListWithCapacity(ids.size()); for (IdentifierHolder holder : ids) { names.add(holder.value); pos.add(holder.parserPos); } return new SqlIdentifier(names, null, pos.get(0), pos); }
Example #18
Source File: DremioRelToSqlConverter.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * @see #dispatch */ public SqlImplementor.Result visit(ScanRelBase scan) { List<String> tableQualifiedName = scan.getTable().getQualifiedName(); int index = tableQualifiedName.size() > 1 ? 1 /* Full path minus plugin name */ : 0; SqlIdentifier tableName = new SqlIdentifier( ImmutableList.copyOf(scan.getTable().getQualifiedName().listIterator(index)), SqlParserPos.ZERO); return result(tableName, ImmutableList.of(Clause.FROM), scan, null); }
Example #19
Source File: SqlInsertTable.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public List<String> getFieldNames() { for (SqlNode fieldNode : insertFields.getList()) { if (!(fieldNode instanceof SqlIdentifier)) { throw SqlExceptionHelper.parseError("Column type specified", this.toSqlString(new SqlDialect(SqlDialect.EMPTY_CONTEXT)).getSql(), fieldNode.getParserPosition()).buildSilently(); } } return insertFields.getList().stream().map(SqlNode::toString).collect(Collectors.toList()); }
Example #20
Source File: CalciteCatalogReader.java From calcite with Apache License 2.0 | 5 votes |
/** Converts a function to a {@link org.apache.calcite.sql.SqlOperator}. * * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082] * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction * constructor. */ private static SqlOperator toOp(RelDataTypeFactory typeFactory, SqlIdentifier name, final Function function) { List<RelDataType> argTypes = new ArrayList<>(); List<SqlTypeFamily> typeFamilies = new ArrayList<>(); for (FunctionParameter o : function.getParameters()) { final RelDataType type = o.getType(typeFactory); argTypes.add(type); typeFamilies.add( Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY)); } final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, i -> function.getParameters().get(i).isOptional()); final List<RelDataType> paramTypes = toSql(typeFactory, argTypes); if (function instanceof ScalarFunction) { return new SqlUserDefinedFunction(name, infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function); } else if (function instanceof AggregateFunction) { return new SqlUserDefinedAggFunction(name, infer((AggregateFunction) function), InferTypes.explicit(argTypes), typeChecker, (AggregateFunction) function, false, false, Optionality.FORBIDDEN, typeFactory); } else if (function instanceof TableMacro) { return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableMacro) function); } else if (function instanceof TableFunction) { return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableFunction) function); } else { throw new AssertionError("unknown function type " + function); } }
Example #21
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 5 votes |
private List<String> getFieldOrigin(SqlNode sqlQuery, int i) { if (sqlQuery instanceof SqlSelect) { SqlSelect sqlSelect = (SqlSelect) sqlQuery; final SelectScope scope = getRawSelectScope(sqlSelect); final List<SqlNode> selectList = scope.getExpandedSelectList(); final SqlNode selectItem = stripAs(selectList.get(i)); if (selectItem instanceof SqlIdentifier) { final SqlQualified qualified = scope.fullyQualify((SqlIdentifier) selectItem); SqlValidatorNamespace namespace = qualified.namespace; final SqlValidatorTable table = namespace.getTable(); if (table == null) { return null; } final List<String> origin = new ArrayList<>(table.getQualifiedName()); for (String name : qualified.suffix()) { namespace = namespace.lookupChild(name); if (namespace == null) { return null; } origin.add(name); } return origin; } return null; } else if (sqlQuery instanceof SqlOrderBy) { return getFieldOrigin(((SqlOrderBy) sqlQuery).query, i); } else { return null; } }
Example #22
Source File: IdentifierNamespace.java From Bats with Apache License 2.0 | 5 votes |
protected static Pair<SqlIdentifier, SqlNodeList> split(SqlNode node) { switch (node.getKind()) { case EXTEND: final SqlCall call = (SqlCall) node; return Pair.of((SqlIdentifier) call.getOperandList().get(0), (SqlNodeList) call.getOperandList().get(1)); default: return Pair.of((SqlIdentifier) node, null); } }
Example #23
Source File: TableApiIdentifierParsingTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testTableApiIdentifierParsing() throws ParseException { FlinkSqlParserImpl parser = createFlinkParser(stringIdentifier); SqlIdentifier sqlIdentifier = parser.TableApiIdentifier(); assertThat(sqlIdentifier.names, equalTo(expectedParsedIdentifier)); }
Example #24
Source File: FlinkSqlDataTypeSpec.java From flink with Apache License 2.0 | 5 votes |
public FlinkSqlDataTypeSpec( SqlIdentifier collectionsTypeName, SqlIdentifier typeName, int precision, int scale, String charSetName, Boolean nullable, Boolean elementNullable, SqlParserPos pos) { super(collectionsTypeName, typeName, precision, scale, charSetName, null, nullable, pos); this.elementNullable = elementNullable; }
Example #25
Source File: SqlAlterHiveDatabaseLocation.java From flink with Apache License 2.0 | 5 votes |
public SqlAlterHiveDatabaseLocation(SqlParserPos pos, SqlIdentifier databaseName, SqlCharStringLiteral location) { super(pos, databaseName, new SqlNodeList(pos)); getPropertyList().add(new SqlTableOption( SqlLiteral.createCharString(DATABASE_LOCATION_URI, location.getParserPosition()), location, location.getParserPosition())); this.location = location; }
Example #26
Source File: SqlCreateHiveTable.java From flink with Apache License 2.0 | 5 votes |
private HiveTableStoredAs(SqlParserPos pos, SqlIdentifier fileFormat, SqlCharStringLiteral intputFormat, SqlCharStringLiteral outputFormat) throws ParseException { this.pos = pos; this.fileFormat = fileFormat; this.intputFormat = intputFormat; this.outputFormat = outputFormat; validate(); }
Example #27
Source File: BridgingUtils.java From flink with Apache License 2.0 | 5 votes |
static @Nullable SqlIdentifier createSqlIdentifier(@Nullable FunctionIdentifier identifier) { if (identifier == null) { return null; } return identifier.getIdentifier() .map(i -> new SqlIdentifier(i.toList(), SqlParserPos.ZERO)) .orElseGet(() -> new SqlIdentifier(identifier.getSimpleName().get(), SqlParserPos.ZERO)); }
Example #28
Source File: SqlAddExternalReflection.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode... operands) { Preconditions.checkArgument(operands.length == 3, "SqlAddExternalReflection.createCall() has to get 3 operands!"); return new SqlAddExternalReflection( pos, (SqlIdentifier) operands[0], (SqlIdentifier) operands[1], (SqlIdentifier) operands[2] ); }
Example #29
Source File: SqlAlterHivePartitionRename.java From flink with Apache License 2.0 | 5 votes |
public SqlAlterHivePartitionRename(SqlParserPos pos, SqlIdentifier tableName, SqlNodeList partSpec, SqlNodeList newPartSpec) throws ParseException { super(pos, tableName, partSpec); if (partSpec == null || newPartSpec == null) { throw new ParseException("Both old and new partition spec have to be specified"); } HiveDDLUtils.unescapePartitionSpec(partSpec); HiveDDLUtils.unescapePartitionSpec(newPartSpec); this.newPartSpec = newPartSpec; }
Example #30
Source File: SqlCreateTable.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode... operands) { Preconditions.checkArgument(operands.length == 9, "SqlCreateTable.createCall() has to get 9 operands!"); return new SqlCreateTable( pos, (SqlIdentifier) operands[0], (SqlNodeList) operands[1], ((SqlLiteral) operands[2]).symbolValue(PartitionDistributionStrategy.class), (SqlNodeList) operands[3], (SqlNodeList) operands[4], (SqlLiteral) operands[5], (SqlNodeList) operands[6], (SqlNodeList) operands[7], operands[8]); }