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

Replace FunctionFieldReferenceOperator with Calcite DOT operator #523

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.apache.calcite.sql2rel.SqlRexConvertlet;
import org.apache.calcite.sql2rel.StandardConvertletTable;

import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;


/**
* ConvertletTable for transformations only relevant to Coral's Intermediate Representation, not specific
Expand All @@ -26,14 +24,6 @@
*/
public class CoralConvertletTable extends ReflectiveConvertletTable {

@SuppressWarnings("unused")
public RexNode convertFunctionFieldReferenceOperator(SqlRexContext cx, FunctionFieldReferenceOperator op,
SqlCall call) {
RexNode funcExpr = cx.convertExpression(call.operand(0));
String fieldName = FunctionFieldReferenceOperator.fieldNameStripQuotes(call.operand(1));
return cx.getRexBuilder().makeFieldAccess(funcExpr, fieldName, false);
}

/**
* Override {@link StandardConvertletTable#convertCast} to avoid cast optimizations that remove the cast.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2017-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand All @@ -9,7 +9,6 @@
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.prepare.CalciteCatalogReader;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlInsert;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperatorTable;
Expand All @@ -19,8 +18,6 @@
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorScope;

import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;


public class HiveSqlValidator extends SqlValidatorImpl {

Expand Down Expand Up @@ -51,15 +48,4 @@ protected void inferUnknownTypes(RelDataType inferredType, SqlValidatorScope sco
super.inferUnknownTypes(inferredType, scope, node);
}

@Override
public SqlNode expand(SqlNode expr, SqlValidatorScope scope) {
if (expr instanceof SqlBasicCall
&& ((SqlBasicCall) expr).getOperator().equals(FunctionFieldReferenceOperator.DOT)) {
SqlBasicCall dotCall = (SqlBasicCall) expr;
if (dotCall.operand(0) instanceof SqlBasicCall) {
return expr;
}
}
return super.expand(expr, scope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.linkedin.coral.com.google.common.collect.Iterables;
import com.linkedin.coral.common.functions.CoralSqlUnnestOperator;
import com.linkedin.coral.common.functions.Function;
import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;
import com.linkedin.coral.hive.hive2rel.functions.HiveFunctionResolver;
import com.linkedin.coral.hive.hive2rel.functions.HiveJsonTupleOperator;
import com.linkedin.coral.hive.hive2rel.functions.HiveRLikeOperator;
Expand Down Expand Up @@ -523,7 +522,7 @@ protected SqlNode visitDotOperator(ASTNode node, ParseContext ctx) {
Iterable<String> names = Iterables.concat(left.names, right.names);
return new SqlIdentifier(ImmutableList.copyOf(names), ZERO);
} else {
return FunctionFieldReferenceOperator.DOT.createCall(ZERO, sqlNodes);
return SqlStdOperatorTable.DOT.createCall(ZERO, sqlNodes);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2017-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand Down Expand Up @@ -49,7 +49,6 @@
import com.linkedin.coral.com.google.common.collect.ImmutableList;
import com.linkedin.coral.com.google.common.collect.ImmutableMap;
import com.linkedin.coral.common.functions.CoralSqlUnnestOperator;
import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;


/**
Expand Down Expand Up @@ -474,7 +473,7 @@ private SqlNode generateRightChildForSqlJoinWithLateralViews(BiRel e, Result rig
* Calcite converts it to a {@link SqlIdentifier} with {@link SqlIdentifier#names} as ["f(x)", "y"] where "f(x)" and "y" are String,
* which is opaque and not aligned with our expectation, since we want to apply transformations on `f(x)` with
* {@link com.linkedin.coral.common.transformers.SqlCallTransformer}. Therefore, we override this
* method to convert `f(x)` to {@link SqlCall} and `.` to {@link com.linkedin.coral.common.functions.FunctionFieldReferenceOperator#DOT}.
* method to convert `f(x)` to {@link SqlCall} and `.` to {@link SqlStdOperatorTable#DOT}.
*
* With this override, the converted CoralSqlNode matches the previous SqlNode handed over to Calcite for validation and conversion
* in `HiveSqlToRelConverter#convertQuery`.
Expand All @@ -500,7 +499,7 @@ public SqlNode toSql(RexProgram program, RexNode rex) {
SqlNode functionCall = toSql(program, referencedExpr);
Collections.reverse(accessNames);
for (String accessName : accessNames) {
functionCall = FunctionFieldReferenceOperator.DOT.createCall(SqlParserPos.ZERO, functionCall,
functionCall = SqlStdOperatorTable.DOT.createCall(SqlParserPos.ZERO, functionCall,
new SqlIdentifier(accessName, POS));
}
return functionCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public void testUnionExtractUDFOnSingleTypeUnions() {

RelNode relNode3 = TestUtils.toRelNode("SELECT extract_union(baz).single.tag_0 from union_table");
String targetSql4 =
"SELECT (coalesce_struct(union_table.baz, 'struct<single:uniontype<array<string>>>').single).tag_0\n"
"SELECT coalesce_struct(union_table.baz, 'struct<single:uniontype<array<string>>>').single.tag_0\n"
+ "FROM default.union_table union_table";
assertEquals(createCoralSpark(relNode3).getSparkSql(), targetSql4);
}
Expand Down Expand Up @@ -876,9 +876,17 @@ public void testSelectStar() {

@Test
public void testConvertFieldAccessOnFunctionCall() {
RelNode relNode = TestUtils.toRelNode("SELECT named_struct('a', 1).a");

String targetSql = "SELECT named_struct('a', 1).a\n" + "FROM (VALUES (0)) t (ZERO)";
assertEquals(createCoralSpark(relNode).getSparkSql(), targetSql);
}

@Test
public void testConvertNestedFieldAccessOnFunctionCall() {
RelNode relNode = TestUtils.toRelNode("SELECT named_struct('a', named_struct('b', 1)).a.b");

String targetSql = "SELECT (named_struct('a', named_struct('b', 1)).a).b\n" + "FROM (VALUES (0)) t (ZERO)";
String targetSql = "SELECT named_struct('a', named_struct('b', 1)).a.b\n" + "FROM (VALUES (0)) t (ZERO)";
assertEquals(createCoralSpark(relNode).getSparkSql(), targetSql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.linkedin.coral.com.google.common.collect.ImmutableList;
import com.linkedin.coral.common.HiveMetastoreClient;
import com.linkedin.coral.common.functions.CoralSqlUnnestOperator;
import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;
import com.linkedin.coral.transformers.CoralRelToSqlNodeConverter;

import static com.google.common.base.Preconditions.*;
Expand Down Expand Up @@ -441,7 +440,7 @@ public SqlNode toSql(RexProgram program, RexNode rex) {
SqlNode functionCall = toSql(program, referencedExpr);
Collections.reverse(accessNames);
for (String accessName : accessNames) {
functionCall = FunctionFieldReferenceOperator.DOT.createCall(SqlParserPos.ZERO, functionCall,
functionCall = SqlStdOperatorTable.DOT.createCall(SqlParserPos.ZERO, functionCall,
new SqlIdentifier(accessName, POS));
}
return functionCall;
Expand Down
Loading