Skip to content

Commit dc27b87

Browse files
authored
feat: add JSON output format (#20)
1 parent 23f8214 commit dc27b87

7 files changed

Lines changed: 30 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cmake_minimum_required(VERSION 3.9)
22
set(CMAKE_CXX_STANDARD 17)
3+
34
project(duck-query-lambda LANGUAGES CXX)
45

56
find_package(aws-lambda-runtime)

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ARG TARGETPLATFORM
22
ARG TARGETARCH
3-
FROM public.ecr.aws/sam/build-provided.al2023:latest
3+
# Ensuring GLIBCXX_3.4.29 as available in Lambda provided.al2023
4+
FROM public.ecr.aws/sam/build-provided.al2023:1.132
45

56
RUN mkdir src
67
WORKDIR /src

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROJECT_NAME = duck-query-lambda
22
OUTPUT_DIR = output
33
EXTENSIONS = httpfs
4-
DUCKDB_VERSION = 1.1.3
4+
DUCKDB_VERSION = 1.2.2
55

66
.PHONY: docker_build binary build-bootstrap build-DuckFunction build-DuckFunctionArm
77

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,14 @@ Here is an example of how to use the Lambda Layer in an AWS Step Function:
130130

131131
### Invoking the Lambda Function and getting the query results back synchronously
132132

133-
By default, the Lambda function will not return the query results. This is because it's not trivial to convert all results back to JSON in a way that every user expects. If you do want to get the results back synchronously, you can write them to a temporary file in the Lambda and the function will then return the contents of that file, base64 encoded.
133+
By default, the Lambda function will not return the query results. This is because it's not trivial to convert all results back to JSON in a way that every user expects. If you do want to get the results back synchronously, you can write them to a temporary file in the Lambda and the function will then return the contents of that file. This data is base64 encoded by default, but you can also return the raw JSON from the output file using the `outputFormat` parameter.
134134
135135
Here is an example of how to do this:
136136
137137
```json
138138
{
139139
"query": "COPY (SELECT * FROM 'https://github.com/Teradata/kylo/raw/refs/heads/master/samples/sample-data/parquet/userdata1.parquet' LIMIT 10) TO '/tmp/output.json'",
140-
"outputFile": "/tmp/output.json"
140+
"outputFile": "/tmp/output.json",
141+
"outputFormat": "json"
141142
}
142143
```

bootstrap.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,28 @@ static invocation_response query_handler(invocation_request const &req, Connecti
7575
// property, it will be base64 encoded and returned as the result. This temporary file is also deleted before
7676
// the function completes.
7777
auto outputFile = view.GetString("outputFile");
78+
if (view.KeyExists("outputFormat"))
79+
{
80+
auto outputFormat = view.GetString("outputFormat");
81+
if (outputFormat == "json")
82+
{
83+
// Read file as JSON string
84+
ifstream fileStream(outputFile);
85+
if (!fileStream)
86+
{
87+
throw std::runtime_error("Failed to open file");
88+
}
89+
std::string jsonString((std::istreambuf_iterator<char>(fileStream)), std::istreambuf_iterator<char>());
90+
fileStream.close();
91+
remove(outputFile.c_str());
92+
return invocation_response::success(jsonString, "application/json");
93+
}
94+
}
95+
// Vanilla base64 encoded response
7896
auto encodedOutput = b64encode(outputFile);
7997
// Delete the file after encoding
8098
remove(outputFile.c_str());
81-
82-
return invocation_response::success(encodedOutput,
83-
"application/base64");
99+
return invocation_response::success(encodedOutput, "application/base64");
84100
}
85101

86102
return invocation_response::success(result->ToString(), "text/plain");

examples/sam/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build-DuckQueryFunction:
2+
echo "This file is intentionally empty. Everything required is provided by the Lambda layer" > $(ARTIFACTS_DIR)/empty
3+

template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Resources:
2727
CompatibleArchitectures:
2828
- arm64
2929
CompatibleRuntimes:
30-
- provided
30+
- provided.al2023
3131
LicenseInfo: Apache-2.0
3232
RetentionPolicy: Retain
3333

0 commit comments

Comments
 (0)