Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1054,13 +1054,11 @@ public List<SqlGroupedWindowFunction> getAuxiliaryFunctions() {
public static final SqlAggFunction SINGLE_VALUE = SqlStdOperatorTable.SINGLE_VALUE;

// ARRAY OPERATORS
public static final SqlOperator ARRAY_VALUE_CONSTRUCTOR =
SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR;
public static final SqlOperator ARRAY_VALUE_CONSTRUCTOR = new SqlArrayConstructor();
public static final SqlOperator ELEMENT = SqlStdOperatorTable.ELEMENT;

// MAP OPERATORS
public static final SqlOperator MAP_VALUE_CONSTRUCTOR =
SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR;
public static final SqlOperator MAP_VALUE_CONSTRUCTOR = new SqlMapConstructor();

// ARRAY MAP SHARED OPERATORS
public static final SqlOperator ITEM = SqlStdOperatorTable.ITEM;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.flink.table.planner.functions.sql;

import org.apache.flink.table.planner.functions.utils.SqlValidatorUtils;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.fun.SqlArrayValueConstructor;
import org.apache.calcite.sql.type.SqlTypeUtil;

/**
* {@link SqlOperator} for <code>ARRAY</code>, which makes explicit casting if the element type not
* equals the derived component type.
*/
public class SqlArrayConstructor extends SqlArrayValueConstructor {

@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
RelDataType type =
getComponentType(opBinding.getTypeFactory(), opBinding.collectOperandTypes());
if (null == type) {
return null;
}

// explicit cast elements to component type if they are not same
SqlValidatorUtils.adjustTypeForArrayConstructor(type, opBinding);

return SqlTypeUtil.createArrayType(opBinding.getTypeFactory(), type, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.flink.table.planner.functions.sql;

import org.apache.flink.table.planner.functions.utils.SqlValidatorUtils;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.fun.SqlMapValueConstructor;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;

import java.util.List;

/**
* {@link SqlOperator} for <code>MAP</code>, which makes explicit casting if the element type not
* equals the derived component type.
*/
public class SqlMapConstructor extends SqlMapValueConstructor {

@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
Pair<RelDataType, RelDataType> type =
getComponentTypes(opBinding.getTypeFactory(), opBinding.collectOperandTypes());
if (null == type) {
return null;
}

// explicit cast elements to component type if they are not same
SqlValidatorUtils.adjustTypeForMapConstructor(type, opBinding);

return SqlTypeUtil.createMapType(opBinding.getTypeFactory(), type.left, type.right, false);
}

private Pair<RelDataType, RelDataType> getComponentTypes(
RelDataTypeFactory typeFactory, List<RelDataType> argTypes) {
return Pair.of(
typeFactory.leastRestrictive(Util.quotientList(argTypes, 2, 0)),
typeFactory.leastRestrictive(Util.quotientList(argTypes, 2, 1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.flink.table.planner.functions.utils;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.util.Pair;

import java.util.List;

/** Utility methods related to SQL validation. */
public class SqlValidatorUtils {

public static void adjustTypeForArrayConstructor(
RelDataType componentType, SqlOperatorBinding opBinding) {
if (opBinding instanceof SqlCallBinding) {
adjustTypeForMultisetConstructor(
componentType, componentType, (SqlCallBinding) opBinding);
}
}

public static void adjustTypeForMapConstructor(
Pair<RelDataType, RelDataType> componentType, SqlOperatorBinding opBinding) {
if (opBinding instanceof SqlCallBinding) {
adjustTypeForMultisetConstructor(
componentType.getKey(), componentType.getValue(), (SqlCallBinding) opBinding);
}
}

/**
* When the element element does not equal with the component type, making explicit casting.
*
* @param evenType derived type for element with even index
* @param oddType derived type for element with odd index
* @param sqlCallBinding description of call
*/
private static void adjustTypeForMultisetConstructor(
RelDataType evenType, RelDataType oddType, SqlCallBinding sqlCallBinding) {
SqlCall call = sqlCallBinding.getCall();
List<RelDataType> operandTypes = sqlCallBinding.collectOperandTypes();
List<SqlNode> operands = call.getOperandList();
RelDataType elementType;
for (int i = 0; i < operands.size(); i++) {
if (i % 2 == 0) {
elementType = evenType;
} else {
elementType = oddType;
}
if (operandTypes.get(i).equalsSansFieldNames(elementType)) {
continue;
}
call.setOperand(i, castTo(operands.get(i), elementType));
}
}

private static SqlNode castTo(SqlNode node, RelDataType type) {
return SqlStdOperatorTable.CAST.createCall(
SqlParserPos.ZERO,
node,
SqlTypeUtil.convertTypeToSpec(type).withNullable(type.isNullable()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ limitations under the License.
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[ARRAY(_UTF-16LE'Hi', _UTF-16LE'Hello', $2)])
LogicalProject(EXPR$0=[ARRAY(_UTF-16LE'Hi':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", _UTF-16LE'Hello':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", $2)])
+- LogicalTableScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[ARRAY(_UTF-16LE'Hi', _UTF-16LE'Hello', c) AS EXPR$0])
Calc(select=[ARRAY(_UTF-16LE'Hi':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", _UTF-16LE'Hello':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", c) AS EXPR$0])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
Expand Down Expand Up @@ -65,6 +65,40 @@ LogicalProject(a=[$0], b=[$1], c=[$2])
<![CDATA[
Calc(select=[a, b, c], where=[((a < 10) AND (b > 20))])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
</TestCase>
<TestCase name="testDecimalArrayWithDifferentPrecision">
<Resource name="sql">
<![CDATA[SELECT ARRAY[0.12, 0.5, 0.99]]]>
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[ARRAY(0.12:DECIMAL(3, 2), 0.5:DECIMAL(3, 2), 0.99:DECIMAL(3, 2))])
+- LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[ARRAY(0.12:DECIMAL(3, 2), 0.5:DECIMAL(3, 2), 0.99:DECIMAL(3, 2)) AS EXPR$0])
+- Values(tuples=[[{ 0 }]], values=[ZERO])
]]>
</Resource>
</TestCase>
<TestCase name="testDecimalMapWithDifferentPrecision">
<Resource name="sql">
<![CDATA[SELECT MAP['a', 0.12, 'b', 0.5]]]>
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[MAP(_UTF-16LE'a', 0.12:DECIMAL(3, 2), _UTF-16LE'b', 0.5:DECIMAL(3, 2))])
+- LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[MAP(_UTF-16LE'a', 0.12:DECIMAL(3, 2), _UTF-16LE'b', 0.5:DECIMAL(3, 2)) AS EXPR$0])
+- Values(tuples=[[{ 0 }]], values=[ZERO])
]]>
</Resource>
</TestCase>
Expand Down Expand Up @@ -342,13 +376,13 @@ LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable4, sourc
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[MAP($1, 30, 10, $0)])
LogicalProject(EXPR$0=[MAP($1, 30:BIGINT, 10, $0)])
+- LogicalTableScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[MAP(b, 30, 10, a) AS EXPR$0])
Calc(select=[MAP(b, 30:BIGINT, 10, a) AS EXPR$0])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ limitations under the License.
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[ARRAY(_UTF-16LE'Hi', _UTF-16LE'Hello', $2)])
LogicalProject(EXPR$0=[ARRAY(_UTF-16LE'Hi':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", _UTF-16LE'Hello':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", $2)])
+- LogicalTableScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[ARRAY(_UTF-16LE'Hi', _UTF-16LE'Hello', c) AS EXPR$0])
Calc(select=[ARRAY(_UTF-16LE'Hi':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", _UTF-16LE'Hello':VARCHAR(2147483647) CHARACTER SET "UTF-16LE", c) AS EXPR$0])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
Expand All @@ -48,6 +48,40 @@ LogicalProject(a=[$0], b=[$1], c=[$2])
<![CDATA[
Calc(select=[a, b, c], where=[((a < 10) AND (b > 20))])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
</TestCase>
<TestCase name="testDecimalArrayWithDifferentPrecision">
<Resource name="sql">
<![CDATA[SELECT ARRAY[0.12, 0.5, 0.99]]]>
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[ARRAY(0.12:DECIMAL(3, 2), 0.5:DECIMAL(3, 2), 0.99:DECIMAL(3, 2))])
+- LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[ARRAY(0.12:DECIMAL(3, 2), 0.5:DECIMAL(3, 2), 0.99:DECIMAL(3, 2)) AS EXPR$0])
+- Values(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
<TestCase name="testDecimalMapWithDifferentPrecision">
<Resource name="sql">
<![CDATA[SELECT MAP['a', 0.12, 'b', 0.5]]]>
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[MAP(_UTF-16LE'a', 0.12:DECIMAL(3, 2), _UTF-16LE'b', 0.5:DECIMAL(3, 2))])
+- LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[MAP(_UTF-16LE'a', 0.12:DECIMAL(3, 2), _UTF-16LE'b', 0.5:DECIMAL(3, 2)) AS EXPR$0])
+- Values(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
Expand Down Expand Up @@ -343,13 +377,13 @@ LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable4, sourc
</Resource>
<Resource name="ast">
<![CDATA[
LogicalProject(EXPR$0=[MAP($1, 30, 10, $0)])
LogicalProject(EXPR$0=[MAP($1, 30:BIGINT, 10, $0)])
+- LogicalTableScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]])
]]>
</Resource>
<Resource name="optimized exec plan">
<![CDATA[
Calc(select=[MAP(b, 30, 10, a) AS EXPR$0])
Calc(select=[MAP(b, 30:BIGINT, 10, a) AS EXPR$0])
+- LegacyTableSourceScan(table=[[default_catalog, default_database, MyTable, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
]]>
</Resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,14 @@ class CalcTest extends TableTestBase {
def testOrWithIsNullInIf(): Unit = {
util.verifyExecPlan("SELECT IF(c = '' OR c IS NULL, 'a', 'b') FROM MyTable")
}

@Test
def testDecimalArrayWithDifferentPrecision(): Unit = {
util.verifyExecPlan("SELECT ARRAY[0.12, 0.5, 0.99]")
}

@Test
def testDecimalMapWithDifferentPrecision(): Unit = {
util.verifyExecPlan("SELECT MAP['a', 0.12, 'b', 0.5]")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,14 @@ class CalcTest extends TableTestBase {
def testOrWithIsNullInIf(): Unit = {
util.verifyExecPlan("SELECT IF(c = '' OR c IS NULL, 'a', 'b') FROM MyTable")
}

@Test
def testDecimalArrayWithDifferentPrecision(): Unit = {
util.verifyExecPlan("SELECT ARRAY[0.12, 0.5, 0.99]")
}

@Test
def testDecimalMapWithDifferentPrecision(): Unit = {
util.verifyExecPlan("SELECT MAP['a', 0.12, 'b', 0.5]")
}
}
Loading