Skip to content

Commit 8447abf

Browse files
authored
feat: add type casting infrastructure for schema evolution (#79)
1 parent bee3f8d commit 8447abf

12 files changed

Lines changed: 1692 additions & 0 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <memory>
23+
24+
#include "arrow/api.h"
25+
#include "arrow/array/array_base.h"
26+
#include "paimon/memory/memory_pool.h"
27+
#include "paimon/predicate/literal.h"
28+
#include "paimon/result.h"
29+
30+
namespace arrow {
31+
class DataType;
32+
class MemoryPool;
33+
} // namespace arrow
34+
35+
namespace paimon {
36+
class CastExecutor {
37+
public:
38+
virtual ~CastExecutor() = default;
39+
virtual Result<Literal> Cast(const Literal& literal,
40+
const std::shared_ptr<arrow::DataType>& target_type) const = 0;
41+
virtual Result<std::shared_ptr<arrow::Array>> Cast(
42+
const std::shared_ptr<arrow::Array>& array,
43+
const std::shared_ptr<arrow::DataType>& target_type, arrow::MemoryPool* pool) const = 0;
44+
};
45+
} // namespace paimon
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "paimon/core/casting/cast_executor_factory.h"
21+
22+
#include <utility>
23+
24+
#include "paimon/core/casting/binary_to_string_cast_executor.h"
25+
#include "paimon/core/casting/boolean_to_decimal_cast_executor.h"
26+
#include "paimon/core/casting/boolean_to_numeric_cast_executor.h"
27+
#include "paimon/core/casting/boolean_to_string_cast_executor.h"
28+
#include "paimon/core/casting/date_to_string_cast_executor.h"
29+
#include "paimon/core/casting/date_to_timestamp_cast_executor.h"
30+
#include "paimon/core/casting/decimal_to_decimal_cast_executor.h"
31+
#include "paimon/core/casting/decimal_to_numeric_primitive_cast_executor.h"
32+
#include "paimon/core/casting/numeric_primitive_cast_executor.h"
33+
#include "paimon/core/casting/numeric_primitive_to_decimal_cast_executor.h"
34+
#include "paimon/core/casting/numeric_primitive_to_timestamp_cast_executor.h"
35+
#include "paimon/core/casting/numeric_to_boolean_cast_executor.h"
36+
#include "paimon/core/casting/numeric_to_string_cast_executor.h"
37+
#include "paimon/core/casting/string_to_binary_cast_executor.h"
38+
#include "paimon/core/casting/string_to_boolean_cast_executor.h"
39+
#include "paimon/core/casting/string_to_date_cast_executor.h"
40+
#include "paimon/core/casting/string_to_decimal_cast_executor.h"
41+
#include "paimon/core/casting/string_to_numeric_primitive_cast_executor.h"
42+
#include "paimon/core/casting/string_to_timestamp_cast_executor.h"
43+
#include "paimon/core/casting/timestamp_to_date_cast_executor.h"
44+
#include "paimon/core/casting/timestamp_to_numeric_primitive_cast_executor.h"
45+
#include "paimon/core/casting/timestamp_to_string_cast_executor.h"
46+
#include "paimon/core/casting/timestamp_to_timestamp_cast_executor.h"
47+
#include "paimon/defs.h"
48+
49+
namespace paimon {
50+
#define REGISTER_CAST_EXECUTOR(TARGET, SRC, EXECUTOR) \
51+
executor_map_[FieldType::TARGET][FieldType::SRC] = std::make_shared<EXECUTOR>();
52+
53+
CastExecutorFactory* CastExecutorFactory::GetCastExecutorFactory() {
54+
static std::unique_ptr<CastExecutorFactory> executor_factory =
55+
std::unique_ptr<CastExecutorFactory>(new CastExecutorFactory());
56+
return executor_factory.get();
57+
}
58+
59+
std::shared_ptr<CastExecutor> CastExecutorFactory::GetCastExecutor(const FieldType& src,
60+
const FieldType& target) const {
61+
auto target_iter = executor_map_.find(target);
62+
if (target_iter == executor_map_.end()) {
63+
return nullptr;
64+
}
65+
auto src_iter = target_iter->second.find(src);
66+
if (src_iter == target_iter->second.end()) {
67+
return nullptr;
68+
}
69+
return src_iter->second;
70+
}
71+
72+
CastExecutorFactory::CastExecutorFactory() {
73+
REGISTER_CAST_EXECUTOR(TINYINT, BOOLEAN, BooleanToNumericCastExecutor);
74+
REGISTER_CAST_EXECUTOR(SMALLINT, BOOLEAN, BooleanToNumericCastExecutor);
75+
REGISTER_CAST_EXECUTOR(INT, BOOLEAN, BooleanToNumericCastExecutor);
76+
REGISTER_CAST_EXECUTOR(BIGINT, BOOLEAN, BooleanToNumericCastExecutor);
77+
REGISTER_CAST_EXECUTOR(FLOAT, BOOLEAN, BooleanToNumericCastExecutor);
78+
REGISTER_CAST_EXECUTOR(DOUBLE, BOOLEAN, BooleanToNumericCastExecutor);
79+
80+
REGISTER_CAST_EXECUTOR(DECIMAL, BOOLEAN, BooleanToDecimalCastExecutor);
81+
82+
REGISTER_CAST_EXECUTOR(STRING, BOOLEAN, BooleanToStringCastExecutor);
83+
84+
REGISTER_CAST_EXECUTOR(TINYINT, TINYINT, NumericPrimitiveCastExecutor);
85+
REGISTER_CAST_EXECUTOR(TINYINT, SMALLINT, NumericPrimitiveCastExecutor);
86+
REGISTER_CAST_EXECUTOR(TINYINT, INT, NumericPrimitiveCastExecutor);
87+
REGISTER_CAST_EXECUTOR(TINYINT, BIGINT, NumericPrimitiveCastExecutor);
88+
REGISTER_CAST_EXECUTOR(TINYINT, FLOAT, NumericPrimitiveCastExecutor);
89+
REGISTER_CAST_EXECUTOR(TINYINT, DOUBLE, NumericPrimitiveCastExecutor);
90+
91+
REGISTER_CAST_EXECUTOR(SMALLINT, TINYINT, NumericPrimitiveCastExecutor);
92+
REGISTER_CAST_EXECUTOR(SMALLINT, SMALLINT, NumericPrimitiveCastExecutor);
93+
REGISTER_CAST_EXECUTOR(SMALLINT, INT, NumericPrimitiveCastExecutor);
94+
REGISTER_CAST_EXECUTOR(SMALLINT, BIGINT, NumericPrimitiveCastExecutor);
95+
REGISTER_CAST_EXECUTOR(SMALLINT, FLOAT, NumericPrimitiveCastExecutor);
96+
REGISTER_CAST_EXECUTOR(SMALLINT, DOUBLE, NumericPrimitiveCastExecutor);
97+
98+
REGISTER_CAST_EXECUTOR(INT, TINYINT, NumericPrimitiveCastExecutor);
99+
REGISTER_CAST_EXECUTOR(INT, SMALLINT, NumericPrimitiveCastExecutor);
100+
REGISTER_CAST_EXECUTOR(INT, INT, NumericPrimitiveCastExecutor);
101+
REGISTER_CAST_EXECUTOR(INT, BIGINT, NumericPrimitiveCastExecutor);
102+
REGISTER_CAST_EXECUTOR(INT, FLOAT, NumericPrimitiveCastExecutor);
103+
REGISTER_CAST_EXECUTOR(INT, DOUBLE, NumericPrimitiveCastExecutor);
104+
105+
REGISTER_CAST_EXECUTOR(BIGINT, TINYINT, NumericPrimitiveCastExecutor);
106+
REGISTER_CAST_EXECUTOR(BIGINT, SMALLINT, NumericPrimitiveCastExecutor);
107+
REGISTER_CAST_EXECUTOR(BIGINT, INT, NumericPrimitiveCastExecutor);
108+
REGISTER_CAST_EXECUTOR(BIGINT, BIGINT, NumericPrimitiveCastExecutor);
109+
REGISTER_CAST_EXECUTOR(BIGINT, FLOAT, NumericPrimitiveCastExecutor);
110+
REGISTER_CAST_EXECUTOR(BIGINT, DOUBLE, NumericPrimitiveCastExecutor);
111+
112+
REGISTER_CAST_EXECUTOR(FLOAT, TINYINT, NumericPrimitiveCastExecutor);
113+
REGISTER_CAST_EXECUTOR(FLOAT, SMALLINT, NumericPrimitiveCastExecutor);
114+
REGISTER_CAST_EXECUTOR(FLOAT, INT, NumericPrimitiveCastExecutor);
115+
REGISTER_CAST_EXECUTOR(FLOAT, BIGINT, NumericPrimitiveCastExecutor);
116+
REGISTER_CAST_EXECUTOR(FLOAT, FLOAT, NumericPrimitiveCastExecutor);
117+
REGISTER_CAST_EXECUTOR(FLOAT, DOUBLE, NumericPrimitiveCastExecutor);
118+
119+
REGISTER_CAST_EXECUTOR(DOUBLE, TINYINT, NumericPrimitiveCastExecutor);
120+
REGISTER_CAST_EXECUTOR(DOUBLE, SMALLINT, NumericPrimitiveCastExecutor);
121+
REGISTER_CAST_EXECUTOR(DOUBLE, INT, NumericPrimitiveCastExecutor);
122+
REGISTER_CAST_EXECUTOR(DOUBLE, BIGINT, NumericPrimitiveCastExecutor);
123+
REGISTER_CAST_EXECUTOR(DOUBLE, FLOAT, NumericPrimitiveCastExecutor);
124+
REGISTER_CAST_EXECUTOR(DOUBLE, DOUBLE, NumericPrimitiveCastExecutor);
125+
126+
REGISTER_CAST_EXECUTOR(BOOLEAN, TINYINT, NumericToBooleanCastExecutor);
127+
REGISTER_CAST_EXECUTOR(BOOLEAN, SMALLINT, NumericToBooleanCastExecutor);
128+
REGISTER_CAST_EXECUTOR(BOOLEAN, INT, NumericToBooleanCastExecutor);
129+
REGISTER_CAST_EXECUTOR(BOOLEAN, BIGINT, NumericToBooleanCastExecutor);
130+
131+
REGISTER_CAST_EXECUTOR(BOOLEAN, STRING, StringToBooleanCastExecutor);
132+
133+
REGISTER_CAST_EXECUTOR(TINYINT, STRING, StringToNumericPrimitiveCastExecutor);
134+
REGISTER_CAST_EXECUTOR(SMALLINT, STRING, StringToNumericPrimitiveCastExecutor);
135+
REGISTER_CAST_EXECUTOR(INT, STRING, StringToNumericPrimitiveCastExecutor);
136+
REGISTER_CAST_EXECUTOR(BIGINT, STRING, StringToNumericPrimitiveCastExecutor);
137+
REGISTER_CAST_EXECUTOR(FLOAT, STRING, StringToNumericPrimitiveCastExecutor);
138+
REGISTER_CAST_EXECUTOR(DOUBLE, STRING, StringToNumericPrimitiveCastExecutor);
139+
140+
REGISTER_CAST_EXECUTOR(STRING, TINYINT, NumericToStringCastExecutor);
141+
REGISTER_CAST_EXECUTOR(STRING, SMALLINT, NumericToStringCastExecutor);
142+
REGISTER_CAST_EXECUTOR(STRING, INT, NumericToStringCastExecutor);
143+
REGISTER_CAST_EXECUTOR(STRING, BIGINT, NumericToStringCastExecutor);
144+
REGISTER_CAST_EXECUTOR(STRING, FLOAT, NumericToStringCastExecutor);
145+
REGISTER_CAST_EXECUTOR(STRING, DOUBLE, NumericToStringCastExecutor);
146+
REGISTER_CAST_EXECUTOR(STRING, DECIMAL, NumericToStringCastExecutor);
147+
148+
REGISTER_CAST_EXECUTOR(BINARY, STRING, StringToBinaryCastExecutor);
149+
150+
REGISTER_CAST_EXECUTOR(STRING, BINARY, BinaryToStringCastExecutor);
151+
152+
REGISTER_CAST_EXECUTOR(STRING, DATE, DateToStringCastExecutor);
153+
154+
REGISTER_CAST_EXECUTOR(TIMESTAMP, DATE, DateToTimestampCastExecutor);
155+
156+
REGISTER_CAST_EXECUTOR(TIMESTAMP, INT, NumericPrimitiveToTimestampCastExecutor);
157+
REGISTER_CAST_EXECUTOR(TIMESTAMP, BIGINT, NumericPrimitiveToTimestampCastExecutor);
158+
159+
REGISTER_CAST_EXECUTOR(DATE, STRING, StringToDateCastExecutor);
160+
161+
REGISTER_CAST_EXECUTOR(STRING, TIMESTAMP, TimestampToStringCastExecutor);
162+
163+
REGISTER_CAST_EXECUTOR(DATE, TIMESTAMP, TimestampToDateCastExecutor);
164+
165+
REGISTER_CAST_EXECUTOR(INT, TIMESTAMP, TimestampToNumericPrimitiveCastExecutor);
166+
REGISTER_CAST_EXECUTOR(BIGINT, TIMESTAMP, TimestampToNumericPrimitiveCastExecutor);
167+
168+
REGISTER_CAST_EXECUTOR(TIMESTAMP, STRING, StringToTimestampCastExecutor);
169+
170+
REGISTER_CAST_EXECUTOR(TINYINT, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
171+
REGISTER_CAST_EXECUTOR(SMALLINT, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
172+
REGISTER_CAST_EXECUTOR(INT, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
173+
REGISTER_CAST_EXECUTOR(BIGINT, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
174+
REGISTER_CAST_EXECUTOR(FLOAT, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
175+
REGISTER_CAST_EXECUTOR(DOUBLE, DECIMAL, DecimalToNumericPrimitiveCastExecutor);
176+
177+
REGISTER_CAST_EXECUTOR(DECIMAL, TINYINT, NumericPrimitiveToDecimalCastExecutor);
178+
REGISTER_CAST_EXECUTOR(DECIMAL, SMALLINT, NumericPrimitiveToDecimalCastExecutor);
179+
REGISTER_CAST_EXECUTOR(DECIMAL, INT, NumericPrimitiveToDecimalCastExecutor);
180+
REGISTER_CAST_EXECUTOR(DECIMAL, BIGINT, NumericPrimitiveToDecimalCastExecutor);
181+
REGISTER_CAST_EXECUTOR(DECIMAL, FLOAT, NumericPrimitiveToDecimalCastExecutor);
182+
REGISTER_CAST_EXECUTOR(DECIMAL, DOUBLE, NumericPrimitiveToDecimalCastExecutor);
183+
184+
REGISTER_CAST_EXECUTOR(DECIMAL, STRING, StringToDecimalCastExecutor);
185+
186+
REGISTER_CAST_EXECUTOR(DECIMAL, DECIMAL, DecimalToDecimalCastExecutor);
187+
188+
REGISTER_CAST_EXECUTOR(TIMESTAMP, TIMESTAMP, TimestampToTimestampCastExecutor);
189+
}
190+
191+
} // namespace paimon
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
#include <map>
22+
#include <memory>
23+
24+
#include "paimon/core/casting/cast_executor.h"
25+
#include "paimon/defs.h"
26+
27+
namespace paimon {
28+
enum class FieldType;
29+
30+
class CastExecutorFactory {
31+
public:
32+
static CastExecutorFactory* GetCastExecutorFactory();
33+
34+
std::shared_ptr<CastExecutor> GetCastExecutor(const FieldType& src,
35+
const FieldType& target) const;
36+
37+
private:
38+
CastExecutorFactory();
39+
40+
private:
41+
// {target type: {src type: cast executor}}
42+
std::map<FieldType, std::map<FieldType, std::shared_ptr<CastExecutor>>> executor_map_;
43+
};
44+
45+
} // namespace paimon

0 commit comments

Comments
 (0)