Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright © 2021 DataSQRL (contact@datasqrl.com)
*
* Licensed 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 com.datasqrl.function.translation.postgres.builtinflink;

import static com.datasqrl.function.CalciteFunctionUtil.lightweightOp;

import com.datasqrl.function.translation.PostgresSqlTranslation;
import com.datasqrl.function.translation.SqlTranslation;
import com.google.auto.service.AutoService;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;

@AutoService(SqlTranslation.class)
public class MapConstructorSqlTranslation extends PostgresSqlTranslation {

public MapConstructorSqlTranslation() {
super(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR);
}

@Override
public void unparse(SqlCall call, SqlWriter writer, int leftPrec, int rightPrec) {
// Translate MAP['k1', v1, 'k2', v2] to jsonb_build_object('k1', v1, 'k2', v2)
lightweightOp("jsonb_build_object")
.createCall(SqlParserPos.ZERO, call.getOperandList())
.unparse(writer, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © 2021 DataSQRL (contact@datasqrl.com)
*
* Licensed 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 com.datasqrl.function.translation.postgres.builtinflink;

import com.datasqrl.function.translation.PostgresSqlTranslation;
import com.datasqrl.function.translation.SqlTranslation;
import com.google.auto.service.AutoService;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.SqlTypeName;

@AutoService(SqlTranslation.class)
public class MapElementSqlTranslation extends PostgresSqlTranslation {

public MapElementSqlTranslation() {
super(SqlStdOperatorTable.ITEM);
}

@Override
public void unparse(SqlCall call, SqlWriter writer, int leftPrec, int rightPrec) {
if (call.operandCount() == 2) {
var baseOperand = call.operand(0);
var indexOrKey = call.operand(1);

var isMapBase =
baseOperand instanceof SqlIdentifier
|| baseOperand.getKind() == SqlKind.MAP_VALUE_CONSTRUCTOR;

// Heuristic: If the index/key is a string literal, treat as map access
// Maps use string keys: map['name'], arrays use numeric indices: array[1]
var isLikelyMapAccess =
indexOrKey instanceof SqlLiteral literal && literal.getTypeName() == SqlTypeName.CHAR;

if (isMapBase && isLikelyMapAccess) {
// Translate map['key'] to map->>'key' for PostgreSQL JSONB access
call.operand(0).unparse(writer, 0, 0);
writer.print("->>");
call.operand(1).unparse(writer, 0, 0);
return;
}
}

call.getOperator().unparse(writer, call, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class FullUseCaseIT extends AbstractFullUseCaseTest {
@Test
@Disabled("Intended for manual usage")
void specificUseCase() {
var pkg = USE_CASES.resolve("duckdb").resolve("package.json");
var pkg = USE_CASES.resolve("function-translation").resolve("postgres").resolve("package.json");

var param = new UseCaseParam(pkg);
fullUseCaseTest(param);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ FunctionCalls := SELECT

ELEMENT(ARRAY[3]) AS element_example,
-- CARDINALITY(MAP['a', 1, 'b', 2]) AS cardinality_example,
-- MAP['a', 1, 'b', 2]['a'] AS map_index_example,
MAP['a', 1, 'b', 2]['a'] AS map_elem_access_example,
ARRAY_CONTAINS(ARRAY[1, 2, 3], 2) AS array_contains_example,
-- ARRAY_DISTINCT(ARRAY['a', 'b', 'b', 'c']) AS array_distinct_example,
ARRAY_PREPEND(ARRAY['b', 'c'], 'a') AS array_prepend_example,
Expand Down Expand Up @@ -141,3 +141,11 @@ ArrayFunctionsTest :=
ARRAY_POSITION(arrayagg_example, 'y') IS NOT NULL AS array_has_y,
ARRAY_POSITION(arrayagg_example, 'z') IS NOT NULL AS array_has_z
FROM ArrayFunctions;

/*+engine(postgres)*/
_MapData := SELECT MAP['name', name,'city', city] AS map_elems
FROM (VALUES ('Alice', 'Berlin'), ('Bob', 'Paris')) AS t(name, city);

/*+test */
MapTest :=
SELECT map_elems['name'] AS elem_name FROM _MapData;
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"is_decimal_example" : true,
"is_digit_example" : true,
"element_example" : 3,
"map_elem_access_example" : 1,
"array_contains_example" : true,
"array_prepend_example" : [ "a", "b", "c" ],
"array_concat_example" : [ "a", "b", "c", "d", "e" ],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data" : {
"MapTest" : [ {
"elem_name" : "Alice"
}, {
"elem_name" : "Bob"
} ]
}
}