Skip to content

Instantly share code, notes, and snippets.

View ppiotrow's full-sized avatar

Przemek Piotrowski ppiotrow

View GitHub Profile
create temporary table coding_interview_cash_flows(
flow_date DATE NOT NULL,
account VARCHAR (50) NOT NULL,
net_value NUMERIC (10, 2) NOT NULL
);
truncate table coding_interview_cash_flows;
insert into coding_interview_cash_flows VALUES('2021-11-23', 'COMP1_USD', -8000.00);
insert into coding_interview_cash_flows VALUES('2021-11-23', 'COMP2_USD', 5000);
insert into coding_interview_cash_flows VALUES('2021-11-24', 'COMP1_EUR', -13000);
insert into coding_interview_cash_flows VALUES('2021-11-25', 'COMP1_USD', 500000);
@ppiotrow
ppiotrow / NonEmptyRangesCounter.scala
Created December 1, 2020 12:14
NonEmptyRangesCounter (no partitions)
private[evaluator] object NonEmptyRangesCounter {
def count(source: ImmutableRoaringBitmap, ranges: ImmutableRoaringBitmap): Int = {
val rangesIter = ranges.getIntIterator
val sourceIter = source.getIntIterator
var distinct = 0
var continue = rangesIter.hasNext
while (continue) {
@ppiotrow
ppiotrow / NonEmptyRangesCounterSpec.scala
Created December 1, 2020 12:03
Tests for NonEmptyRangesCounter
class NonEmptyRangesCounterSpec extends AnyFlatSpec with Matchers {
it should "count map-distinct in generated test cases" in {
import NonEmptyRangesCounterSpec.generateTest
val totalRanges = 100000
val totalPartitions = 14
val maxPositionsPerRange = 20
val numberOfTests = 10
Random.setSeed(123456)
(0 until numberOfTests).foreach { _ =>
@ppiotrow
ppiotrow / NonEmptyRangesCounter.scala
Last active December 1, 2020 12:03
Counts ranges with at least one value inside. Split results per partitions.
object NonEmptyRangesCounter {
def count(source: ImmutableRoaringBitmap, ranges: ImmutableRoaringBitmap, partitionsEnds: Seq[Long]): Seq[Int] = {
val rangesIter = ranges.getIntIterator
val sourceIter = source.getIntIterator
var partIdx = 0
val results = Array.ofDim[Int](partitionsEnds.length)
var distinct = 0
var continue = rangesIter.hasNext
@ppiotrow
ppiotrow / non-deterministic-builds.sh
Created April 5, 2020 16:30
Docker base image sha on the file creation, modification date. Docker layers are timestamped.
echo "FROM openjdk:14-alpine\nRUN echo \${JAVA_HOME}" > Dockerfile
docker build --tag my-java:latest . # No cache, Successfully built c60c9be06bcb
docker build --tag my-java:latest . # Fully cached, Successfully built c60c9be06bcb
docker rmi my-java:latest
docker build --tag my-java:latest . # Totally new image, Successfully built 514662e21b60
docker build --no-cache --tag my-java:latest . # no-cache flag, Successfully built 364da4d3ef39
image='dckr.myrepo.com/my-org/my-app'
stage0=$image':stage0'
latest=$image':latest'
# Pull to bring docker images to the docker cache.
docker pull $stage0 || true # ||true saves from failure when image has never been published
docker pull $latest || true
# This generates Dockerfile and artifacts
sbt docker:stage
docker build \
@ppiotrow
ppiotrow / sbt-multistage-cache
Created April 5, 2020 14:58
SBT docker native packager - using multistage build cache
dockerUpdateLatest := true,
dockerAutoremoveMultiStageIntermediateImages := false,
dockerBuildOptions := dockerBuildOptions.value ++ Seq(
"--cache-from",
dockerAlias.value.withTag(Some("stage0")).toString,
"--cache-from",
dockerAlias.value.withTag(Some("latest")).toString)
# export DOCKER_BUILDKIT=1
dockerUpdateLatest := true,
dockerBuildOptions := dockerBuildOptions.value ++ Seq(
"--progress=plain",
"--cache-from",
dockerAlias.value.withTag(Some("latest")).toString,
"--build-arg",
"BUILDKIT_INLINE_CACHE=1"),
dockerAutoremoveMultiStageIntermediateImages := false //breaks with Buildkit
dockerLayerGrouping := {
val dockerBaseDirectory = (defaultLinuxInstallLocation in Docker).value
(path: String) =>
{
val pathInWorkdir = path.stripPrefix(dockerBaseDirectory)
if (pathInWorkdir.startsWith(s"/lib/${organization.value}"))
Some(2)
else if (pathInWorkdir.startsWith("/lib/"))
Some(1)
else if (pathInWorkdir.startsWith("/bin/"))
FROM docker.artifactory.mycompany.com/theteam/openjre:11.0.5-0-e58ca54 as stage0
LABEL snp-multi-stage="intermediate"
LABEL snp-multi-stage-id="8c5dc02e-39e8-42ce-9edd-2b2c153f4555"
WORKDIR /opt/my-project
COPY opt /opt
COPY 1/opt /1/opt
COPY 2/opt /2/opt
USER root
RUN ["chmod", "-R", "u=rX,g=rX", "/1/opt/my-project"]