Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split to table fix #460

Merged
merged 11 commits into from
Jun 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,12 @@ public Result visit(Uncollect e) {
if (x.node instanceof SqlSelect) {
operand = ((SqlSelect) x.node).getSelectList().get(0);
}
final SqlNode unnestNode = SqlStdOperatorTable.UNNEST.createCall(POS, operand);
SqlNode unnestNode = null;
if (e.withOrdinality) {
unnestNode = SqlStdOperatorTable.UNNEST_WITH_ORDINALITY.createCall(POS, operand);
} else {
unnestNode = SqlStdOperatorTable.UNNEST.createCall(POS, operand);
}
final List<SqlNode> operands = createAsFullOperands(e.getRowType(), unnestNode,
requireNonNull(x.neededAlias, () -> "x.neededAlias is null, node is " + x.node));
final SqlNode asNode = SqlStdOperatorTable.AS.createCall(POS, operands);
Expand All @@ -1296,9 +1301,6 @@ public Result visit(Uncollect e) {

public Result visit(TableFunctionScan e) {
List<RelDataTypeField> fieldList = e.getRowType().getFieldList();
if (fieldList == null || fieldList.size() > 1) {
throw new RuntimeException("Table function supports only one argument");
}
final List<SqlNode> inputSqlNodes = new ArrayList<>();
final int inputSize = e.getInputs().size();
for (int i = 0; i < inputSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSetOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlUnnestOperator;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWindow;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlCase;
Expand Down Expand Up @@ -755,10 +757,14 @@ && isIntervalHourAndSecond(call)) {
break;
case AS:
SqlNode var = call.operand(0);
List<SqlNode> sqlNodes = call.getOperandList();
if (call.operand(0) instanceof SqlCharStringLiteral
&& (var.toString().contains("\\")
&& !var.toString().substring(1, 3).startsWith("\\\\"))) {
unparseAsOp(writer, call, leftPrec, rightPrec);
} else if (sqlNodes.size() == 4
&& sqlNodes.get(0).getKind().name().equalsIgnoreCase(SqlKind.UNNEST.name())) {
unparseAsOpWithUnnest(writer, call, leftPrec, rightPrec);
} else {
call.getOperator().unparse(writer, call, leftPrec, rightPrec);
}
Expand All @@ -776,6 +782,9 @@ && isIntervalHourAndSecond(call)) {
super.unparseCall(writer, call, leftPrec, rightPrec);
break;
}
case UNNEST:
unparseUnnest(writer, call, leftPrec, rightPrec);
break;
case COLUMN_LIST:
final SqlWriter.Frame columnListFrame = getColumnListFrame(writer, call);
for (SqlNode operand : call.getOperandList()) {
Expand Down Expand Up @@ -910,6 +919,34 @@ private void unparseAsOp(SqlWriter writer, SqlCall call, int leftPrec, int right
writer.endList(frame);
}

private void unparseAsOpWithUnnest(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
assert call.operandCount() == 4;
SqlUnnestOperator unnestOperator =
(SqlUnnestOperator) ((SqlBasicCall) call.operand(0)).getOperator();
final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.AS);
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.sep("AS");
call.operand(2).unparse(writer, leftPrec, rightPrec);
if (unnestOperator.withOrdinality) {
writer.literal("WITH OFFSET");
writer.sep("AS");
call.operand(3).unparse(writer, leftPrec, rightPrec);
}
writer.endList(frame);
}

private void unparseUnnest(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
if (call.operandCount() == 1
&& call.getOperandList().get(0).getKind() == SqlKind.SELECT) {
// avoid double ( ) on unnesting a sub-query
writer.keyword(call.getOperator().getName());
call.operand(0).unparse(writer, 0, 0);
} else {
SqlUtil.unparseFunctionSyntax(call.getOperator(), writer, call, false);
}
}


private void unparseCastAsInteger(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
boolean isFirstOperandFormatCall = (call.operand(0) instanceof SqlBasicCall)
&& ((SqlBasicCall) call.operand(0)).getOperator().getName().equals("FORMAT");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.sql.fun;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.SqlTableFunction;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlReturnTypeInference;
import org.apache.calcite.sql.type.SqlTypeName;

/**
* class for SPLIT_TO_TABLE tableFunction".
*/
public class SplitToTableFunction extends SqlFunction
implements SqlTableFunction {

public SplitToTableFunction() {
super("SPLIT_TO_TABLE",
SqlKind.OTHER_FUNCTION,
ReturnTypes.ARG0_NULLABLE,
null,
OperandTypes.STRING_STRING,
SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
}

@Override public SqlReturnTypeInference getRowTypeInference() {
return this::getRowType;
}

public RelDataType getRowType(SqlOperatorBinding opBinding) {
RelDataType componentType = inferReturnType(opBinding);
return opBinding.getTypeFactory().builder()
.add("SEQ", SqlTypeName.INTEGER)
.add("INDEX", SqlTypeName.INTEGER)
.add("VALUE", componentType)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ private SqlLibraryOperators() {
OperandTypes.STRING_STRING,
SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {SNOWFLAKE})
public static final SqlFunction SPLIT_TO_TABLE =
new SplitToTableFunction();

/** The "TO_VARCHAR(numeric, string)" function; casts string
* Format first_operand to specified in second operand. */
@LibraryOperator(libraries = {SNOWFLAKE})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13231,6 +13231,20 @@ RelNode createLogicalValueRel(RexNode col1, RexNode col2) {
assertThat(toSql(root, DatabaseProduct.BIG_QUERY.getDialect()), isLinux(expectedBiqQuery));
}

@Test public void testSplitToTable() {
final RelBuilder builder = relBuilder();
final RelNode root = builder
.functionScan(SqlLibraryOperators.SPLIT_TO_TABLE, 0,
builder.literal("a,b,c"), builder.literal(","))
.project(builder.field(2))
.build();

final String expectedBiqQuery = "SELECT \"VALUE\"\n"
+ "FROM TABLE(SPLIT_TO_TABLE('a,b,c', ','))";

assertThat(toSql(root, DatabaseProduct.SNOWFLAKE.getDialect()), isLinux(expectedBiqQuery));
}

@Test public void testSimpleStrtokFunction() {
final RelBuilder builder = relBuilder();
final RexNode strtokNode = builder.call(SqlLibraryOperators.STRTOK,
Expand Down
Loading